1 /* $Id: atom.c,v 1.14 2002/09/07 15:12:27 chorns Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/atom.c
6 * PURPOSE: Atom functions
7 * PROGRAMMER: Eric Kohl ( ariadne@xs4all.nl)
10 * Full rewrite 27/05/2001
14 #define NTOS_USER_MODE
16 #include <kernel32/error.h>
19 #include <kernel32/kernel32.h>
22 /* GLOBALS *******************************************************************/
24 static PRTL_ATOM_TABLE LocalAtomTable
= NULL
;
26 static PRTL_ATOM_TABLE
GetLocalAtomTable(VOID
);
29 /* FUNCTIONS *****************************************************************/
32 GlobalAddAtomA(LPCSTR lpString
)
34 UNICODE_STRING AtomName
;
38 if (HIWORD((ULONG
)lpString
) == 0)
40 if ((ULONG
)lpString
>= 0xC000)
42 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
45 return (ATOM
)LOWORD((ULONG
)lpString
);
48 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
51 Status
= NtAddAtom(AtomName
.Buffer
,
54 RtlFreeUnicodeString(&AtomName
);
55 if (!NT_SUCCESS(Status
))
57 SetLastErrorByStatus(Status
);
66 GlobalAddAtomW(LPCWSTR lpString
)
71 if (HIWORD((ULONG
)lpString
) == 0)
73 if ((ULONG
)lpString
>= 0xC000)
75 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
78 return (ATOM
)LOWORD((ULONG
)lpString
);
81 Status
= NtAddAtom((LPWSTR
)lpString
,
84 if (!NT_SUCCESS(Status
))
86 SetLastErrorByStatus(Status
);
95 GlobalDeleteAtom(ATOM nAtom
)
104 Status
= NtDeleteAtom(nAtom
);
105 if (!NT_SUCCESS(Status
))
107 SetLastErrorByStatus(Status
);
116 GlobalFindAtomA(LPCSTR lpString
)
118 UNICODE_STRING AtomName
;
122 if (HIWORD((ULONG
)lpString
) == 0)
124 if ((ULONG
)lpString
>= 0xC000)
126 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
129 return (ATOM
)LOWORD((ULONG
)lpString
);
132 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
134 Status
= NtFindAtom(AtomName
.Buffer
,
137 RtlFreeUnicodeString(&AtomName
);
138 if (!NT_SUCCESS(Status
))
140 SetLastErrorByStatus(Status
);
149 GlobalFindAtomW(LPCWSTR lpString
)
154 if (HIWORD((ULONG
)lpString
) == 0)
156 if ((ULONG
)lpString
>= 0xC000)
158 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
161 return (ATOM
)LOWORD((ULONG
)lpString
);
164 Status
= NtFindAtom((LPWSTR
)lpString
,
167 if (!NT_SUCCESS(Status
))
169 SetLastErrorByStatus(Status
);
178 GlobalGetAtomNameA(ATOM nAtom
,
182 PATOM_BASIC_INFORMATION Buffer
;
183 UNICODE_STRING AtomNameU
;
184 ANSI_STRING AtomName
;
189 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
190 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
194 Status
= NtQueryInformationAtom(nAtom
,
195 AtomBasicInformation
,
199 if (!NT_SUCCESS(Status
))
201 RtlFreeHeap(RtlGetProcessHeap(),
207 RtlInitUnicodeString(&AtomNameU
,
209 AtomName
.Buffer
= lpBuffer
;
211 AtomName
.MaximumLength
= nSize
;
212 RtlUnicodeStringToAnsiString(&AtomName
,
216 ReturnLength
= AtomName
.Length
;
217 RtlFreeHeap(RtlGetProcessHeap(),
226 GlobalGetAtomNameW(ATOM nAtom
,
230 PATOM_BASIC_INFORMATION Buffer
;
235 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
236 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
240 Status
= NtQueryInformationAtom(nAtom
,
241 AtomBasicInformation
,
245 if (!NT_SUCCESS(Status
))
247 RtlFreeHeap(RtlGetProcessHeap(),
253 wcscpy(lpBuffer
, Buffer
->Name
);
254 ReturnLength
= Buffer
->NameLength
/ sizeof(WCHAR
);
255 RtlFreeHeap(RtlGetProcessHeap(),
263 static PRTL_ATOM_TABLE
264 GetLocalAtomTable(VOID
)
266 if (LocalAtomTable
!= NULL
)
268 return LocalAtomTable
;
270 RtlCreateAtomTable(37,
272 return LocalAtomTable
;
277 InitAtomTable(DWORD nSize
)
281 /* nSize should be a prime number */
283 if ( nSize
< 4 || nSize
>= 512 )
288 if (LocalAtomTable
== NULL
)
290 Status
= RtlCreateAtomTable(nSize
,
292 if (!NT_SUCCESS(Status
))
294 SetLastErrorByStatus(Status
);
304 AddAtomA(LPCSTR lpString
)
306 PRTL_ATOM_TABLE AtomTable
;
307 UNICODE_STRING AtomName
;
311 if (HIWORD((ULONG
)lpString
) == 0)
313 if ((ULONG
)lpString
>= 0xC000)
315 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
318 return (ATOM
)LOWORD((ULONG
)lpString
);
321 AtomTable
= GetLocalAtomTable();
323 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
326 Status
= RtlAddAtomToAtomTable(AtomTable
,
329 RtlFreeUnicodeString(&AtomName
);
330 if (!NT_SUCCESS(Status
))
332 SetLastErrorByStatus(Status
);
341 AddAtomW(LPCWSTR lpString
)
343 PRTL_ATOM_TABLE AtomTable
;
347 if (HIWORD((ULONG
)lpString
) == 0)
349 if ((ULONG
)lpString
>= 0xC000)
351 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
354 return (ATOM
)LOWORD((ULONG
)lpString
);
357 AtomTable
= GetLocalAtomTable();
359 Status
= RtlAddAtomToAtomTable(AtomTable
,
362 if (!NT_SUCCESS(Status
))
364 SetLastErrorByStatus(Status
);
373 DeleteAtom(ATOM nAtom
)
375 PRTL_ATOM_TABLE AtomTable
;
383 AtomTable
= GetLocalAtomTable();
385 Status
= RtlDeleteAtomFromAtomTable(AtomTable
,
387 if (!NT_SUCCESS(Status
))
389 SetLastErrorByStatus(Status
);
398 FindAtomA(LPCSTR lpString
)
400 PRTL_ATOM_TABLE AtomTable
;
401 UNICODE_STRING AtomName
;
405 if (HIWORD((ULONG
)lpString
) == 0)
407 if ((ULONG
)lpString
>= 0xC000)
409 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
412 return (ATOM
)LOWORD((ULONG
)lpString
);
415 AtomTable
= GetLocalAtomTable();
416 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
418 Status
= RtlLookupAtomInAtomTable(AtomTable
,
421 RtlFreeUnicodeString(&AtomName
);
422 if (!NT_SUCCESS(Status
))
424 SetLastErrorByStatus(Status
);
433 FindAtomW(LPCWSTR lpString
)
435 PRTL_ATOM_TABLE AtomTable
;
439 if (HIWORD((ULONG
)lpString
) == 0)
441 if ((ULONG
)lpString
>= 0xC000)
443 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
446 return (ATOM
)LOWORD((ULONG
)lpString
);
449 AtomTable
= GetLocalAtomTable();
451 Status
= RtlLookupAtomInAtomTable(AtomTable
,
454 if (!NT_SUCCESS(Status
))
456 SetLastErrorByStatus(Status
);
465 GetAtomNameA(ATOM nAtom
,
469 PRTL_ATOM_TABLE AtomTable
;
471 UNICODE_STRING AtomNameU
;
472 ANSI_STRING AtomName
;
476 AtomTable
= GetLocalAtomTable();
478 NameLength
= nSize
* sizeof(WCHAR
);
479 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
483 Status
= RtlQueryAtomInAtomTable(AtomTable
,
489 if (!NT_SUCCESS(Status
))
491 RtlFreeHeap(RtlGetProcessHeap(),
497 RtlInitUnicodeString(&AtomNameU
,
499 AtomName
.Buffer
= lpBuffer
;
501 AtomName
.MaximumLength
= nSize
;
502 RtlUnicodeStringToAnsiString(&AtomName
,
506 NameLength
= AtomName
.Length
;
507 RtlFreeHeap(RtlGetProcessHeap(),
516 GetAtomNameW(ATOM nAtom
,
520 PRTL_ATOM_TABLE AtomTable
;
524 AtomTable
= GetLocalAtomTable();
526 NameLength
= nSize
* sizeof(WCHAR
);
527 Status
= RtlQueryAtomInAtomTable(AtomTable
,
533 if (!NT_SUCCESS(Status
))
538 return(NameLength
/ sizeof(WCHAR
));