1 | /* $Id: atom.cpp,v 1.13 2003-09-18 14:21:07 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 |
|
---|
32 | ATOM APIENTRY LookupAtom(HATOMTBL hAtomTbl, PSZ psz, ULONG actionMask);
|
---|
33 |
|
---|
34 | inline ATOM _LookupAtom(HATOMTBL hAtomTbl, PSZ psz, ULONG actionMask)
|
---|
35 | {
|
---|
36 | ATOM yyrc;
|
---|
37 | USHORT sel = RestoreOS2FS();
|
---|
38 |
|
---|
39 | yyrc = LookupAtom(hAtomTbl, psz, actionMask);
|
---|
40 | SetFS(sel);
|
---|
41 |
|
---|
42 | return yyrc;
|
---|
43 | }
|
---|
44 |
|
---|
45 | #undef LookupAtom
|
---|
46 | #define LookupAtom _LookupAtom
|
---|
47 |
|
---|
48 |
|
---|
49 | HATOMTBL privateAtomTable = NULL;
|
---|
50 | HATOMTBL systemAtomTable = NULL;
|
---|
51 | //******************************************************************************
|
---|
52 | //******************************************************************************
|
---|
53 | HATOMTBL inline getPrivateAtomTable()
|
---|
54 | {
|
---|
55 | if(privateAtomTable == NULL) {
|
---|
56 | privateAtomTable = WinCreateAtomTable(0, 37);
|
---|
57 | }
|
---|
58 | return privateAtomTable;
|
---|
59 | }
|
---|
60 | //******************************************************************************
|
---|
61 | //******************************************************************************
|
---|
62 | HATOMTBL inline getSystemAtomTable()
|
---|
63 | {
|
---|
64 | if(systemAtomTable == NULL) {
|
---|
65 | systemAtomTable = WinQuerySystemAtomTable();
|
---|
66 | }
|
---|
67 | return systemAtomTable;
|
---|
68 | }
|
---|
69 | //******************************************************************************
|
---|
70 | //******************************************************************************
|
---|
71 | BOOL WIN32API InitAtomTable(DWORD numEntries)
|
---|
72 | {
|
---|
73 | dprintf(("KERNEL32: InitAtomTable %d", numEntries));
|
---|
74 |
|
---|
75 | if(privateAtomTable == NULL) {
|
---|
76 | privateAtomTable = WinCreateAtomTable(0, numEntries);
|
---|
77 | }
|
---|
78 | return (privateAtomTable != NULL);
|
---|
79 | }
|
---|
80 | //******************************************************************************
|
---|
81 | //******************************************************************************
|
---|
82 | ATOM WIN32API FindAtomA( LPCSTR atomName)
|
---|
83 | {
|
---|
84 | HATOMTBL atomTable = getPrivateAtomTable();
|
---|
85 | ATOM atom = 0;
|
---|
86 |
|
---|
87 | if(HIWORD(atomName)) {
|
---|
88 | dprintf(("FindAtomA %s", atomName));
|
---|
89 | }
|
---|
90 | else dprintf(("FindAtomA %x", atomName));
|
---|
91 |
|
---|
92 | if(atomTable != NULL) {
|
---|
93 | atom = LookupAtom(atomTable, HIWORD(atomName) ?
|
---|
94 | (PSZ) atomName : (PSZ) (LOWORD(atomName) | 0xFFFF0000),
|
---|
95 | LOOKUP_FIND | LOOKUP_NOCASE);
|
---|
96 | }
|
---|
97 | dprintf(("FindAtomA returned %x", atom));
|
---|
98 |
|
---|
99 | if(!atom) {
|
---|
100 | SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
|
---|
101 | }
|
---|
102 | else SetLastError(ERROR_SUCCESS_W);
|
---|
103 | return atom;
|
---|
104 | }
|
---|
105 | //******************************************************************************
|
---|
106 | //******************************************************************************
|
---|
107 | ATOM WIN32API FindAtomW(LPCWSTR atomName)
|
---|
108 | {
|
---|
109 | ATOM rc;
|
---|
110 | char *astring;
|
---|
111 |
|
---|
112 | dprintf(("KERNEL32: FindAtomW"));
|
---|
113 | if(HIWORD(atomName))
|
---|
114 | {
|
---|
115 | astring = UnicodeToAsciiString((LPWSTR)atomName);
|
---|
116 | rc = FindAtomA(astring);
|
---|
117 | FreeAsciiString(astring);
|
---|
118 | }
|
---|
119 | else rc = FindAtomA((char*)atomName);
|
---|
120 |
|
---|
121 | return(rc);
|
---|
122 | }
|
---|
123 | //******************************************************************************
|
---|
124 | //******************************************************************************
|
---|
125 | ATOM WIN32API AddAtomA(LPCSTR atomName)
|
---|
126 | {
|
---|
127 | ATOM atom = 0;
|
---|
128 | HATOMTBL atomTable = getPrivateAtomTable();
|
---|
129 |
|
---|
130 | if(atomTable != NULL)
|
---|
131 | {
|
---|
132 | atom = LookupAtom(atomTable, HIWORD(atomName) ?
|
---|
133 | (PSZ) atomName : (PSZ) (LOWORD(atomName) | 0xFFFF0000),
|
---|
134 | LOOKUP_ADD | LOOKUP_NOCASE);
|
---|
135 | }
|
---|
136 |
|
---|
137 | if(HIWORD(atomName)) {
|
---|
138 | dprintf(("KERNEL32: AddAtomA %s returned %x", atomName, atom));
|
---|
139 | }
|
---|
140 | else dprintf(("KERNEL32: AddAtomA %x returned %x", atomName, atom));
|
---|
141 |
|
---|
142 | if(!atom) {
|
---|
143 | SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
|
---|
144 | }
|
---|
145 | else SetLastError(ERROR_SUCCESS_W);
|
---|
146 | return atom;
|
---|
147 | }
|
---|
148 | //******************************************************************************
|
---|
149 | //******************************************************************************
|
---|
150 | ATOM WIN32API AddAtomW(LPCWSTR atomName)
|
---|
151 | {
|
---|
152 | ATOM rc;
|
---|
153 | char *astring;
|
---|
154 |
|
---|
155 | if(HIWORD(atomName) == 0) {
|
---|
156 | rc = AddAtomA((char*)atomName);
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | astring = UnicodeToAsciiString((LPWSTR)atomName);
|
---|
161 | rc = AddAtomA(astring);
|
---|
162 | FreeAsciiString(astring);
|
---|
163 | }
|
---|
164 | return(rc);
|
---|
165 | }
|
---|
166 | //******************************************************************************
|
---|
167 | //******************************************************************************
|
---|
168 | UINT WIN32API GetAtomNameA( ATOM atom, LPSTR atomName, int nameLen)
|
---|
169 | {
|
---|
170 | UINT result = 0;
|
---|
171 | HATOMTBL atomTable = getPrivateAtomTable();
|
---|
172 |
|
---|
173 | dprintf(("KERNEL32: GetAtomNameA %x %x %d", LOWORD(atom), atomName, nameLen));
|
---|
174 | if(atomTable != NULL)
|
---|
175 | result = (UINT)WinQueryAtomName( atomTable, LOWORD(atom), atomName, nameLen);
|
---|
176 |
|
---|
177 | dprintf(("KERNEL32: GetAtomNameA returned %s", (result) ? atomName : NULL));
|
---|
178 |
|
---|
179 | if(!result) {
|
---|
180 | SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
|
---|
181 | }
|
---|
182 | else SetLastError(ERROR_SUCCESS_W);
|
---|
183 | return (result);
|
---|
184 | }
|
---|
185 | //******************************************************************************
|
---|
186 | //******************************************************************************
|
---|
187 | UINT WIN32API GetAtomNameW(ATOM atom, LPWSTR lpszBuffer, int cchBuffer)
|
---|
188 | {
|
---|
189 | char *astring;
|
---|
190 | UINT rc;
|
---|
191 |
|
---|
192 | dprintf(("KERNEL32: GetAtomNameW %x %x %d", atom, lpszBuffer, cchBuffer));
|
---|
193 | astring = (char *)alloca(cchBuffer * sizeof( WCHAR ));
|
---|
194 | if(astring == NULL) {
|
---|
195 | dprintf(("GlobalGetAtomNameW: alloca failed!!"));
|
---|
196 | DebugInt3();
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 | rc = GetAtomNameA(atom, astring, cchBuffer * sizeof( WCHAR ));
|
---|
200 | if(rc) {
|
---|
201 | lstrcpynAtoW(lpszBuffer, astring, cchBuffer);
|
---|
202 | }
|
---|
203 | else lpszBuffer[0] = 0; //necessary?
|
---|
204 | return lstrlenW( lpszBuffer );
|
---|
205 | }
|
---|
206 | //******************************************************************************
|
---|
207 | //******************************************************************************
|
---|
208 | ATOM WIN32API DeleteAtom(ATOM atom)
|
---|
209 | {
|
---|
210 | HATOMTBL atomTable = getPrivateAtomTable();
|
---|
211 |
|
---|
212 | dprintf(("DeleteAtom %x", atom));
|
---|
213 | if (atomTable != NULL) {
|
---|
214 | return (ATOM) LookupAtom(atomTable, (PSZ) MAKEULONG(atom, 0xFFFF),
|
---|
215 | LOOKUP_DELETE | LOOKUP_NOCASE);
|
---|
216 | }
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 | //******************************************************************************
|
---|
220 | //******************************************************************************
|
---|
221 | ATOM WIN32API GlobalDeleteAtom(ATOM atom)
|
---|
222 | {
|
---|
223 | HATOMTBL atomTable = getSystemAtomTable();
|
---|
224 |
|
---|
225 | dprintf(("KERNEL32: GlobalDeleteAtom %x", atom));
|
---|
226 | return (ATOM) LookupAtom(atomTable, (PSZ) MAKEULONG(atom, 0xFFFF),
|
---|
227 | LOOKUP_DELETE | LOOKUP_NOCASE);
|
---|
228 | }
|
---|
229 | //******************************************************************************
|
---|
230 | //******************************************************************************
|
---|
231 | ATOM WIN32API GlobalAddAtomA(LPCSTR atomName)
|
---|
232 | {
|
---|
233 | ATOM atom = 0;
|
---|
234 | HATOMTBL atomTable = getSystemAtomTable();
|
---|
235 |
|
---|
236 | if(atomTable != NULL)
|
---|
237 | {
|
---|
238 | atom = LookupAtom(atomTable, HIWORD(atomName) ?
|
---|
239 | (PSZ) atomName : (PSZ) (LOWORD(atomName) | 0xFFFF0000),
|
---|
240 | LOOKUP_ADD | LOOKUP_NOCASE);
|
---|
241 | }
|
---|
242 |
|
---|
243 | if(HIWORD(atomName)) {
|
---|
244 | dprintf(("KERNEL32: GlobalAddAtomA %s returned %x", atomName, atom));
|
---|
245 | }
|
---|
246 | else dprintf(("KERNEL32: GlobalAddAtomA %x returned %x", atomName, atom));
|
---|
247 |
|
---|
248 | if(!atom) {
|
---|
249 | SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
|
---|
250 | }
|
---|
251 | else SetLastError(ERROR_SUCCESS_W);
|
---|
252 | return atom;
|
---|
253 | }
|
---|
254 | //******************************************************************************
|
---|
255 | //******************************************************************************
|
---|
256 | ATOM WIN32API GlobalAddAtomW(LPCWSTR atomName)
|
---|
257 | {
|
---|
258 | char *astring;
|
---|
259 | ATOM rc;
|
---|
260 |
|
---|
261 | if(HIWORD(atomName) == 0)
|
---|
262 | {
|
---|
263 | rc = GlobalAddAtomA((char*)atomName);
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | astring = UnicodeToAsciiString((LPWSTR)atomName);
|
---|
268 | rc = GlobalAddAtomA(astring);
|
---|
269 | FreeAsciiString(astring);
|
---|
270 | }
|
---|
271 | return(rc);
|
---|
272 | }
|
---|
273 | //******************************************************************************
|
---|
274 | //******************************************************************************
|
---|
275 | ATOM WIN32API GlobalFindAtomA( LPCSTR atomName)
|
---|
276 | {
|
---|
277 | HATOMTBL atomTable = getSystemAtomTable();
|
---|
278 | ATOM atom = 0;
|
---|
279 |
|
---|
280 | if(HIWORD(atomName)) {
|
---|
281 | dprintf(("KERNEL32: GlobalFindAtomA %s", atomName));
|
---|
282 | }
|
---|
283 | else dprintf(("KERNEL32: GlobalFindAtomA %x", atomName));
|
---|
284 |
|
---|
285 | atom = LookupAtom(atomTable, HIWORD(atomName) ?
|
---|
286 | (PSZ) atomName : (PSZ) (LOWORD(atomName) | 0xFFFF0000),
|
---|
287 | LOOKUP_FIND | LOOKUP_NOCASE);
|
---|
288 | dprintf(("KERNEL32: GlobalFindAtomA returned %x", atom));
|
---|
289 |
|
---|
290 | if(!atom) {
|
---|
291 | SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
|
---|
292 | }
|
---|
293 | else SetLastError(ERROR_SUCCESS_W);
|
---|
294 |
|
---|
295 | return atom;
|
---|
296 | }
|
---|
297 | //******************************************************************************
|
---|
298 | //******************************************************************************
|
---|
299 | ATOM WIN32API GlobalFindAtomW(LPCWSTR atomName)
|
---|
300 | {
|
---|
301 | ATOM rc;
|
---|
302 | char *astring;
|
---|
303 |
|
---|
304 | dprintf(("KERNEL32: GlobalFindAtomW"));
|
---|
305 | if(HIWORD(atomName))
|
---|
306 | {
|
---|
307 | astring = UnicodeToAsciiString((LPWSTR)atomName);
|
---|
308 | rc = GlobalFindAtomA(astring);
|
---|
309 | FreeAsciiString(astring);
|
---|
310 | }
|
---|
311 | else rc = GlobalFindAtomA((char*)atomName);
|
---|
312 |
|
---|
313 | return(rc);
|
---|
314 | }
|
---|
315 | //******************************************************************************
|
---|
316 | //******************************************************************************
|
---|
317 | UINT WIN32API GlobalGetAtomNameA(ATOM atom, LPSTR lpszBuffer, int cchBuffer)
|
---|
318 | {
|
---|
319 | UINT result = 0;
|
---|
320 | HATOMTBL atomTable = getSystemAtomTable();
|
---|
321 |
|
---|
322 | dprintf(("KERNEL32: GlobalGetAtomNameA %x %x %d", LOWORD(atom), lpszBuffer, cchBuffer));
|
---|
323 | if(atomTable != NULL)
|
---|
324 | result = (UINT)WinQueryAtomName( atomTable, LOWORD(atom), lpszBuffer, cchBuffer);
|
---|
325 |
|
---|
326 | if(!result) {
|
---|
327 | SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: find real error
|
---|
328 | }
|
---|
329 | else SetLastError(ERROR_SUCCESS_W);
|
---|
330 |
|
---|
331 | dprintf(("KERNEL32: GlobalGetAtomNameA returned %s", (result) ? lpszBuffer : NULL));
|
---|
332 | return (result);
|
---|
333 | }
|
---|
334 | //******************************************************************************
|
---|
335 | //******************************************************************************
|
---|
336 | UINT WIN32API GlobalGetAtomNameW(ATOM atom, LPWSTR lpszBuffer, int cchBuffer)
|
---|
337 | {
|
---|
338 | char *astring;
|
---|
339 | UINT rc;
|
---|
340 |
|
---|
341 | dprintf(("KERNEL32: GlobalGetAtomNameW %x %x %d", atom, lpszBuffer, cchBuffer));
|
---|
342 | astring = (char *)alloca(cchBuffer * sizeof( WCHAR ));
|
---|
343 | if(astring == NULL) {
|
---|
344 | dprintf(("GlobalGetAtomNameW: alloca failed!!"));
|
---|
345 | DebugInt3();
|
---|
346 | return 0;
|
---|
347 | }
|
---|
348 | rc = GlobalGetAtomNameA(atom, astring, cchBuffer * sizeof( WCHAR ));
|
---|
349 | if(rc) {
|
---|
350 | lstrcpynAtoW(lpszBuffer, astring, cchBuffer);
|
---|
351 | }
|
---|
352 | else lpszBuffer[0] = 0; //necessary?
|
---|
353 | return lstrlenW( lpszBuffer );
|
---|
354 | }
|
---|
355 | //******************************************************************************
|
---|
356 | //******************************************************************************
|
---|