source: trunk/src/kernel32/atom.cpp@ 6199

Last change on this file since 6199 was 6199, checked in by sandervl, 24 years ago

Rewrote Atom functions + GetAtomNameW fix

File size: 10.8 KB
Line 
1/* $Id: atom.cpp,v 1.9 2001-07-07 13:58:37 sandervl Exp $ */
2
3/*
4 * Win32 ATOM api functions
5 *
6 * Copyright 1998-2001 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * TODO: DeleteAtom doesn't appear to work properly. FindAtom still works
10 * after deleting it.
11 *
12 * Project Odin Software License can be found in LICENSE.TXT
13 *
14 */
15#define INCL_WIN
16#include <os2wrap.h>
17#include <win32api.h>
18#include <winconst.h>
19#include <unicode.h>
20#include <heapstring.h>
21#include <misc.h>
22
23#define DBG_LOCALLOG DBG_atom
24#include "dbglocal.h"
25
26// action codes for LookupAtom
27#define LOOKUP_FIND 0
28#define LOOKUP_ADD 1
29#define LOOKUP_DELETE 2
30#define LOOKUP_NOCASE 0x80000000
31
32ATOM APIENTRY LookupAtom(HATOMTBL hAtomTbl, PSZ psz, ULONG actionMask);
33
34HATOMTBL privateAtomTable = NULL;
35HATOMTBL systemAtomTable = NULL;
36//******************************************************************************
37//******************************************************************************
38HATOMTBL inline getPrivateAtomTable()
39{
40 if(privateAtomTable == NULL) {
41 privateAtomTable = WinCreateAtomTable(0, 37);
42 }
43 return privateAtomTable;
44}
45//******************************************************************************
46//******************************************************************************
47HATOMTBL inline getSystemAtomTable()
48{
49 if(systemAtomTable == NULL) {
50 systemAtomTable = WinQuerySystemAtomTable();
51 }
52 return systemAtomTable;
53}
54//******************************************************************************
55//******************************************************************************
56BOOL WIN32API InitAtomTable(DWORD numEntries)
57{
58 dprintf(("KERNEL32: InitAtomTable %d", numEntries));
59
60 if(privateAtomTable == NULL) {
61 privateAtomTable = WinCreateAtomTable(0, numEntries);
62 }
63 return (privateAtomTable != NULL);
64}
65//******************************************************************************
66//******************************************************************************
67ATOM WIN32API FindAtomA( LPCSTR atomName)
68{
69 HATOMTBL atomTable = getPrivateAtomTable();
70 ATOM atom = 0;
71
72 if(HIWORD(atomName)) {
73 dprintf(("FindAtomA %s", atomName));
74 }
75 else dprintf(("FindAtomA %x", atomName));
76
77 if(atomTable != NULL) {
78 atom = LookupAtom(atomTable, HIWORD(atomName) ?
79 (PSZ) atomName : (PSZ) LOWORD(atomName),
80 LOOKUP_ADD | LOOKUP_NOCASE);
81 }
82 dprintf(("FindAtomA returned %x", atom));
83
84 if(!atom) {
85 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
86 }
87 else SetLastError(ERROR_SUCCESS_W);
88 return atom;
89}
90//******************************************************************************
91//******************************************************************************
92ATOM WIN32API FindAtomW(LPCWSTR atomName)
93{
94 ATOM rc;
95 char *astring;
96
97 dprintf(("KERNEL32: FindAtomW"));
98 if(HIWORD(atomName))
99 {
100 astring = UnicodeToAsciiString((LPWSTR)atomName);
101 rc = FindAtomA(astring);
102 FreeAsciiString(astring);
103 }
104 else rc = FindAtomA((char*)atomName);
105
106 return(rc);
107}
108//******************************************************************************
109//******************************************************************************
110ATOM WIN32API AddAtomA(LPCSTR atomName)
111{
112 ATOM atom = 0;
113 HATOMTBL atomTable = getPrivateAtomTable();
114
115 if(atomTable != NULL)
116 {
117 atom = LookupAtom(atomTable, HIWORD(atomName) ?
118 (PSZ) atomName : (PSZ) LOWORD(atomName),
119 LOOKUP_ADD | LOOKUP_NOCASE);
120 }
121
122 if(HIWORD(atomName)) {
123 dprintf(("KERNEL32: AddAtomA %s returned %x", atomName, atom));
124 }
125 else dprintf(("KERNEL32: AddAtomA %x returned %x", atomName, atom));
126
127 if(!atom) {
128 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
129 }
130 else SetLastError(ERROR_SUCCESS_W);
131 return atom;
132}
133//******************************************************************************
134//******************************************************************************
135ATOM WIN32API AddAtomW(LPCWSTR atomName)
136{
137 ATOM rc;
138 char *astring;
139
140 if(HIWORD(atomName) == 0) {
141 rc = AddAtomA((char*)atomName);
142 }
143 else
144 {
145 astring = UnicodeToAsciiString((LPWSTR)atomName);
146 rc = AddAtomA(astring);
147 FreeAsciiString(astring);
148 }
149 return(rc);
150}
151//******************************************************************************
152//******************************************************************************
153UINT WIN32API GetAtomNameA( ATOM atom, LPSTR atomName, int nameLen)
154{
155 UINT result = 0;
156 HATOMTBL atomTable = getPrivateAtomTable();
157
158 dprintf(("KERNEL32: GetAtomNameA %x %x %d", LOWORD(atom), atomName, nameLen));
159 if(atomTable != NULL)
160 result = (UINT)WinQueryAtomName( atomTable, LOWORD(atom), atomName, nameLen);
161
162 dprintf(("KERNEL32: GetAtomNameA returned %s", (result) ? atomName : NULL));
163
164 if(!result) {
165 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
166 }
167 else SetLastError(ERROR_SUCCESS_W);
168 return (result);
169}
170//******************************************************************************
171//******************************************************************************
172UINT WIN32API GetAtomNameW(ATOM atom, LPWSTR lpszBuffer, int cchBuffer)
173{
174 char *astring;
175 UINT rc;
176
177 dprintf(("KERNEL32: GetAtomNameW %x %x %d", atom, lpszBuffer, cchBuffer));
178 astring = (char *)alloca(cchBuffer);
179 if(astring == NULL) {
180 dprintf(("GlobalGetAtomNameW: alloca failed!!"));
181 DebugInt3();
182 return 0;
183 }
184 rc = GetAtomNameA(atom, astring, cchBuffer);
185 if(rc) {
186 lstrcpyAtoW(lpszBuffer, astring);
187 }
188 else lpszBuffer[0] = 0; //necessary?
189 return rc;
190}
191//******************************************************************************
192//******************************************************************************
193ATOM WIN32API DeleteAtom(ATOM atom)
194{
195 HATOMTBL atomTable = getPrivateAtomTable();
196
197 dprintf(("DeleteAtom %x", atom));
198 if (atomTable != NULL) {
199 return (ATOM) LookupAtom(atomTable, (PSZ) MAKEULONG(atom, 0xFFFF),
200 LOOKUP_DELETE | LOOKUP_NOCASE);
201 }
202 return 0;
203}
204//******************************************************************************
205//******************************************************************************
206ATOM WIN32API GlobalDeleteAtom(ATOM atom)
207{
208 HATOMTBL atomTable = getSystemAtomTable();
209
210 dprintf(("KERNEL32: GlobalDeleteAtom %x", atom));
211 return (ATOM) LookupAtom(atomTable, (PSZ) MAKEULONG(atom, 0xFFFF),
212 LOOKUP_DELETE | LOOKUP_NOCASE);
213}
214//******************************************************************************
215//******************************************************************************
216ATOM WIN32API GlobalAddAtomA(LPCSTR atomName)
217{
218 ATOM atom = 0;
219 HATOMTBL atomTable = getSystemAtomTable();
220
221 if(atomTable != NULL)
222 {
223 atom = LookupAtom(atomTable, HIWORD(atomName) ?
224 (PSZ) atomName : (PSZ) LOWORD(atomName),
225 LOOKUP_ADD | LOOKUP_NOCASE);
226 }
227
228 if(HIWORD(atomName)) {
229 dprintf(("KERNEL32: GlobalAddAtomA %s returned %x", atomName, atom));
230 }
231 else dprintf(("KERNEL32: GlobalAddAtomA %x returned %x", atomName, atom));
232
233 if(!atom) {
234 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
235 }
236 else SetLastError(ERROR_SUCCESS_W);
237 return atom;
238}
239//******************************************************************************
240//******************************************************************************
241ATOM WIN32API GlobalAddAtomW(LPCWSTR atomName)
242{
243 char *astring;
244 ATOM rc;
245
246 if(HIWORD(atomName) == 0)
247 {
248 rc = GlobalAddAtomA((char*)atomName);
249 }
250 else
251 {
252 astring = UnicodeToAsciiString((LPWSTR)atomName);
253 rc = GlobalAddAtomA(astring);
254 FreeAsciiString(astring);
255 }
256 return(rc);
257}
258//******************************************************************************
259//******************************************************************************
260ATOM WIN32API GlobalFindAtomA( LPCSTR atomName)
261{
262 HATOMTBL atomTable = getSystemAtomTable();
263 ATOM atom = 0;
264
265 if(HIWORD(atomName)) {
266 dprintf(("GlobalFindAtomA %s", atomName));
267 }
268 else dprintf(("GlobalFindAtomA %x", atomName));
269
270 atom = LookupAtom(atomTable, HIWORD(atomName) ?
271 (PSZ) atomName : (PSZ) LOWORD(atomName),
272 LOOKUP_ADD | LOOKUP_NOCASE);
273 dprintf(("GlobalFindAtomA returned %x", atom));
274
275 if(!atom) {
276 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
277 }
278 else SetLastError(ERROR_SUCCESS_W);
279
280 return atom;
281}
282//******************************************************************************
283//******************************************************************************
284ATOM WIN32API GlobalFindAtomW(LPCWSTR atomName)
285{
286 ATOM rc;
287 char *astring;
288
289 dprintf(("KERNEL32: GlobalFindAtomW"));
290 if(HIWORD(atomName))
291 {
292 astring = UnicodeToAsciiString((LPWSTR)atomName);
293 rc = GlobalFindAtomA(astring);
294 FreeAsciiString(astring);
295 }
296 else rc = GlobalFindAtomA((char*)atomName);
297
298 return(rc);
299}
300//******************************************************************************
301//******************************************************************************
302UINT WIN32API GlobalGetAtomNameA(ATOM atom, LPSTR lpszBuffer, int cchBuffer)
303{
304 UINT result = 0;
305 HATOMTBL atomTable = getSystemAtomTable();
306
307 dprintf(("KERNEL32: GlobalGetAtomNameA %x %x %d", LOWORD(atom), lpszBuffer, cchBuffer));
308 if(atomTable != NULL)
309 result = (UINT)WinQueryAtomName( atomTable, LOWORD(atom), lpszBuffer, cchBuffer);
310
311 if(!result) {
312 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
313 }
314 else SetLastError(ERROR_SUCCESS_W);
315
316 dprintf(("KERNEL32: GlobalGetAtomNameA returned %s", (result) ? lpszBuffer : NULL));
317 return (result);
318}
319//******************************************************************************
320//******************************************************************************
321UINT WIN32API GlobalGetAtomNameW(ATOM atom, LPWSTR lpszBuffer, int cchBuffer)
322{
323 char *astring;
324 UINT rc;
325
326 dprintf(("KERNEL32: GlobalGetAtomNameW %x %x %d", atom, lpszBuffer, cchBuffer));
327 astring = (char *)alloca(cchBuffer);
328 if(astring == NULL) {
329 dprintf(("GlobalGetAtomNameW: alloca failed!!"));
330 DebugInt3();
331 return 0;
332 }
333 rc = GlobalGetAtomNameA(atom, astring, cchBuffer);
334 if(rc) {
335 lstrcpyAtoW(lpszBuffer, astring);
336 }
337 else lpszBuffer[0] = 0; //necessary?
338 return rc;
339}
340//******************************************************************************
341//******************************************************************************
Note: See TracBrowser for help on using the repository browser.