1 /* $Id: atom.c,v 1.15 2002/09/08 10:22:43 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
13 #include <ddk/ntddk.h>
15 #include <kernel32/error.h>
18 #include <kernel32/kernel32.h>
21 /* GLOBALS *******************************************************************/
23 static PRTL_ATOM_TABLE LocalAtomTable
= NULL
;
25 static PRTL_ATOM_TABLE
GetLocalAtomTable(VOID
);
28 /* FUNCTIONS *****************************************************************/
31 GlobalAddAtomA(LPCSTR lpString
)
33 UNICODE_STRING AtomName
;
37 if (HIWORD((ULONG
)lpString
) == 0)
39 if ((ULONG
)lpString
>= 0xC000)
41 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
44 return (ATOM
)LOWORD((ULONG
)lpString
);
47 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
50 Status
= NtAddAtom(AtomName
.Buffer
,
52 RtlFreeUnicodeString(&AtomName
);
53 if (!NT_SUCCESS(Status
))
55 SetLastErrorByStatus(Status
);
64 GlobalAddAtomW(LPCWSTR lpString
)
69 if (HIWORD((ULONG
)lpString
) == 0)
71 if ((ULONG
)lpString
>= 0xC000)
73 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
76 return (ATOM
)LOWORD((ULONG
)lpString
);
79 Status
= NtAddAtom((LPWSTR
)lpString
,
81 if (!NT_SUCCESS(Status
))
83 SetLastErrorByStatus(Status
);
92 GlobalDeleteAtom(ATOM nAtom
)
101 Status
= NtDeleteAtom(nAtom
);
102 if (!NT_SUCCESS(Status
))
104 SetLastErrorByStatus(Status
);
113 GlobalFindAtomA(LPCSTR lpString
)
115 UNICODE_STRING AtomName
;
119 if (HIWORD((ULONG
)lpString
) == 0)
121 if ((ULONG
)lpString
>= 0xC000)
123 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
126 return (ATOM
)LOWORD((ULONG
)lpString
);
129 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
131 Status
= NtFindAtom(AtomName
.Buffer
,
133 RtlFreeUnicodeString(&AtomName
);
134 if (!NT_SUCCESS(Status
))
136 SetLastErrorByStatus(Status
);
145 GlobalFindAtomW(LPCWSTR lpString
)
150 if (HIWORD((ULONG
)lpString
) == 0)
152 if ((ULONG
)lpString
>= 0xC000)
154 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
157 return (ATOM
)LOWORD((ULONG
)lpString
);
160 Status
= NtFindAtom((LPWSTR
)lpString
,
162 if (!NT_SUCCESS(Status
))
164 SetLastErrorByStatus(Status
);
173 GlobalGetAtomNameA(ATOM nAtom
,
177 PATOM_BASIC_INFORMATION Buffer
;
178 UNICODE_STRING AtomNameU
;
179 ANSI_STRING AtomName
;
184 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
185 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
189 Status
= NtQueryInformationAtom(nAtom
,
190 AtomBasicInformation
,
194 if (!NT_SUCCESS(Status
))
196 RtlFreeHeap(RtlGetProcessHeap(),
202 RtlInitUnicodeString(&AtomNameU
,
204 AtomName
.Buffer
= lpBuffer
;
206 AtomName
.MaximumLength
= nSize
;
207 RtlUnicodeStringToAnsiString(&AtomName
,
211 ReturnLength
= AtomName
.Length
;
212 RtlFreeHeap(RtlGetProcessHeap(),
221 GlobalGetAtomNameW(ATOM nAtom
,
225 PATOM_BASIC_INFORMATION Buffer
;
230 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
231 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
235 Status
= NtQueryInformationAtom(nAtom
,
236 AtomBasicInformation
,
240 if (!NT_SUCCESS(Status
))
242 RtlFreeHeap(RtlGetProcessHeap(),
248 wcscpy(lpBuffer
, Buffer
->Name
);
249 ReturnLength
= Buffer
->NameLength
/ sizeof(WCHAR
);
250 RtlFreeHeap(RtlGetProcessHeap(),
258 static PRTL_ATOM_TABLE
259 GetLocalAtomTable(VOID
)
261 if (LocalAtomTable
!= NULL
)
263 return LocalAtomTable
;
265 RtlCreateAtomTable(37,
267 return LocalAtomTable
;
272 InitAtomTable(DWORD nSize
)
276 /* nSize should be a prime number */
278 if ( nSize
< 4 || nSize
>= 512 )
283 if (LocalAtomTable
== NULL
)
285 Status
= RtlCreateAtomTable(nSize
,
287 if (!NT_SUCCESS(Status
))
289 SetLastErrorByStatus(Status
);
299 AddAtomA(LPCSTR lpString
)
301 PRTL_ATOM_TABLE AtomTable
;
302 UNICODE_STRING AtomName
;
306 if (HIWORD((ULONG
)lpString
) == 0)
308 if ((ULONG
)lpString
>= 0xC000)
310 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
313 return (ATOM
)LOWORD((ULONG
)lpString
);
316 AtomTable
= GetLocalAtomTable();
318 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
321 Status
= RtlAddAtomToAtomTable(AtomTable
,
324 RtlFreeUnicodeString(&AtomName
);
325 if (!NT_SUCCESS(Status
))
327 SetLastErrorByStatus(Status
);
336 AddAtomW(LPCWSTR lpString
)
338 PRTL_ATOM_TABLE AtomTable
;
342 if (HIWORD((ULONG
)lpString
) == 0)
344 if ((ULONG
)lpString
>= 0xC000)
346 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
349 return (ATOM
)LOWORD((ULONG
)lpString
);
352 AtomTable
= GetLocalAtomTable();
354 Status
= RtlAddAtomToAtomTable(AtomTable
,
357 if (!NT_SUCCESS(Status
))
359 SetLastErrorByStatus(Status
);
368 DeleteAtom(ATOM nAtom
)
370 PRTL_ATOM_TABLE AtomTable
;
378 AtomTable
= GetLocalAtomTable();
380 Status
= RtlDeleteAtomFromAtomTable(AtomTable
,
382 if (!NT_SUCCESS(Status
))
384 SetLastErrorByStatus(Status
);
393 FindAtomA(LPCSTR lpString
)
395 PRTL_ATOM_TABLE AtomTable
;
396 UNICODE_STRING AtomName
;
400 if (HIWORD((ULONG
)lpString
) == 0)
402 if ((ULONG
)lpString
>= 0xC000)
404 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
407 return (ATOM
)LOWORD((ULONG
)lpString
);
410 AtomTable
= GetLocalAtomTable();
411 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
413 Status
= RtlLookupAtomInAtomTable(AtomTable
,
416 RtlFreeUnicodeString(&AtomName
);
417 if (!NT_SUCCESS(Status
))
419 SetLastErrorByStatus(Status
);
428 FindAtomW(LPCWSTR lpString
)
430 PRTL_ATOM_TABLE AtomTable
;
434 if (HIWORD((ULONG
)lpString
) == 0)
436 if ((ULONG
)lpString
>= 0xC000)
438 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
441 return (ATOM
)LOWORD((ULONG
)lpString
);
444 AtomTable
= GetLocalAtomTable();
446 Status
= RtlLookupAtomInAtomTable(AtomTable
,
449 if (!NT_SUCCESS(Status
))
451 SetLastErrorByStatus(Status
);
460 GetAtomNameA(ATOM nAtom
,
464 PRTL_ATOM_TABLE AtomTable
;
466 UNICODE_STRING AtomNameU
;
467 ANSI_STRING AtomName
;
471 AtomTable
= GetLocalAtomTable();
473 NameLength
= nSize
* sizeof(WCHAR
);
474 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
478 Status
= RtlQueryAtomInAtomTable(AtomTable
,
484 if (!NT_SUCCESS(Status
))
486 RtlFreeHeap(RtlGetProcessHeap(),
492 RtlInitUnicodeString(&AtomNameU
,
494 AtomName
.Buffer
= lpBuffer
;
496 AtomName
.MaximumLength
= nSize
;
497 RtlUnicodeStringToAnsiString(&AtomName
,
501 NameLength
= AtomName
.Length
;
502 RtlFreeHeap(RtlGetProcessHeap(),
511 GetAtomNameW(ATOM nAtom
,
515 PRTL_ATOM_TABLE AtomTable
;
519 AtomTable
= GetLocalAtomTable();
521 NameLength
= nSize
* sizeof(WCHAR
);
522 Status
= RtlQueryAtomInAtomTable(AtomTable
,
528 if (!NT_SUCCESS(Status
))
533 return(NameLength
/ sizeof(WCHAR
));