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