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