push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / setupapi / stringtable.c
blob59ad6ce1b8649ed0150208a5a6ad0cfdfb8607ab
1 /*
2 * Setupapi string table functions
4 * Copyright 2005 Eric Kohl
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "setupapi.h"
33 #include "wine/debug.h"
36 #define TABLE_DEFAULT_SIZE 256
38 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
40 DECLARE_HANDLE(HSTRING_TABLE);
42 typedef struct _TABLE_SLOT
44 LPWSTR pString;
45 LPVOID pData;
46 DWORD dwSize;
47 } TABLE_SLOT, *PTABLE_SLOT;
49 typedef struct _STRING_TABLE
51 PTABLE_SLOT pSlots;
52 DWORD dwUsedSlots;
53 DWORD dwMaxSlots;
54 DWORD dwMaxDataSize;
55 } STRING_TABLE, *PSTRING_TABLE;
58 /**************************************************************************
59 * StringTableInitialize [SETUPAPI.@]
61 * Creates a new string table and initializes it.
63 * PARAMS
64 * None
66 * RETURNS
67 * Success: Handle to the string table
68 * Failure: NULL
70 HSTRING_TABLE WINAPI
71 StringTableInitialize(VOID)
73 PSTRING_TABLE pStringTable;
75 TRACE("\n");
77 pStringTable = MyMalloc(sizeof(STRING_TABLE));
78 if (pStringTable == NULL)
80 ERR("Invalid hStringTable!\n");
81 return NULL;
84 memset(pStringTable, 0, sizeof(STRING_TABLE));
86 pStringTable->pSlots = MyMalloc(sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
87 if (pStringTable->pSlots == NULL)
89 MyFree(pStringTable);
90 return NULL;
93 memset(pStringTable->pSlots, 0, sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
95 pStringTable->dwUsedSlots = 0;
96 pStringTable->dwMaxSlots = TABLE_DEFAULT_SIZE;
97 pStringTable->dwMaxDataSize = 0;
99 TRACE("Done\n");
101 return (HSTRING_TABLE)pStringTable;
105 /**************************************************************************
106 * StringTableInitializeEx [SETUPAPI.@]
108 * Creates a new string table and initializes it.
110 * PARAMS
111 * dwMaxExtraDataSize [I] Maximum extra data size
112 * dwReserved [I] Unused
114 * RETURNS
115 * Success: Handle to the string table
116 * Failure: NULL
118 HSTRING_TABLE WINAPI
119 StringTableInitializeEx(DWORD dwMaxExtraDataSize,
120 DWORD dwReserved)
122 PSTRING_TABLE pStringTable;
124 TRACE("\n");
126 pStringTable = MyMalloc(sizeof(STRING_TABLE));
127 if (pStringTable == NULL) return NULL;
129 memset(pStringTable, 0, sizeof(STRING_TABLE));
131 pStringTable->pSlots = MyMalloc(sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
132 if (pStringTable->pSlots == NULL)
134 MyFree(pStringTable);
135 return NULL;
138 memset(pStringTable->pSlots, 0, sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
140 pStringTable->dwUsedSlots = 0;
141 pStringTable->dwMaxSlots = TABLE_DEFAULT_SIZE;
142 pStringTable->dwMaxDataSize = dwMaxExtraDataSize;
144 TRACE("Done\n");
146 return (HSTRING_TABLE)pStringTable;
150 /**************************************************************************
151 * StringTableDestroy [SETUPAPI.@]
153 * Destroys a string table.
155 * PARAMS
156 * hStringTable [I] Handle to the string table to be destroyed
158 * RETURNS
159 * None
161 VOID WINAPI
162 StringTableDestroy(HSTRING_TABLE hStringTable)
164 PSTRING_TABLE pStringTable;
165 DWORD i;
167 TRACE("%p\n", hStringTable);
169 pStringTable = (PSTRING_TABLE)hStringTable;
170 if (pStringTable == NULL)
171 return;
173 if (pStringTable->pSlots != NULL)
175 for (i = 0; i < pStringTable->dwMaxSlots; i++)
177 MyFree(pStringTable->pSlots[i].pString);
178 pStringTable->pSlots[i].pString = NULL;
180 MyFree(pStringTable->pSlots[i].pData);
181 pStringTable->pSlots[i].pData = NULL;
182 pStringTable->pSlots[i].dwSize = 0;
185 MyFree(pStringTable->pSlots);
188 MyFree(pStringTable);
192 /**************************************************************************
193 * StringTableAddString [SETUPAPI.@]
195 * Adds a new string to the string table.
197 * PARAMS
198 * hStringTable [I] Handle to the string table
199 * lpString [I] String to be added to the string table
200 * dwFlags [I] Flags
201 * 1: case sensitive compare
203 * RETURNS
204 * Success: String ID
205 * Failure: -1
207 * NOTES
208 * If the given string already exists in the string table it will not
209 * be added again. The ID of the existing string will be returned in
210 * this case.
212 DWORD WINAPI
213 StringTableAddString(HSTRING_TABLE hStringTable,
214 LPWSTR lpString,
215 DWORD dwFlags)
217 PSTRING_TABLE pStringTable;
218 DWORD i;
220 TRACE("%p %s %x\n", hStringTable, debugstr_w(lpString), dwFlags);
222 pStringTable = (PSTRING_TABLE)hStringTable;
223 if (pStringTable == NULL)
225 ERR("Invalid hStringTable!\n");
226 return (DWORD)-1;
229 /* Search for existing string in the string table */
230 for (i = 0; i < pStringTable->dwMaxSlots; i++)
232 if (pStringTable->pSlots[i].pString != NULL)
234 if (dwFlags & 1)
236 if (!lstrcmpW(pStringTable->pSlots[i].pString, lpString))
238 return i + 1;
241 else
243 if (!lstrcmpiW(pStringTable->pSlots[i].pString, lpString))
245 return i + 1;
251 /* Check for filled slot table */
252 if (pStringTable->dwUsedSlots == pStringTable->dwMaxSlots)
254 FIXME("Resize the string table!\n");
255 return (DWORD)-1;
258 /* Search for an empty slot */
259 for (i = 0; i < pStringTable->dwMaxSlots; i++)
261 if (pStringTable->pSlots[i].pString == NULL)
263 pStringTable->pSlots[i].pString = MyMalloc((lstrlenW(lpString) + 1) * sizeof(WCHAR));
264 if (pStringTable->pSlots[i].pString == NULL)
266 TRACE("Couldn't allocate memory for a new string!\n");
267 return (DWORD)-1;
270 lstrcpyW(pStringTable->pSlots[i].pString, lpString);
272 pStringTable->dwUsedSlots++;
274 return i + 1;
278 TRACE("Couldn't find an empty slot!\n");
280 return (DWORD)-1;
284 /**************************************************************************
285 * StringTableAddStringEx [SETUPAPI.@]
287 * Adds a new string plus extra data to the string table.
289 * PARAMS
290 * hStringTable [I] Handle to the string table
291 * lpString [I] String to be added to the string table
292 * dwFlags [I] Flags
293 * 1: case sensitive compare
294 * lpExtraData [I] Pointer to the extra data
295 * dwExtraDataSize [I] Size of the extra data
297 * RETURNS
298 * Success: String ID
299 * Failure: -1
301 * NOTES
302 * If the given string already exists in the string table it will not
303 * be added again. The ID of the existing string will be returned in
304 * this case.
306 DWORD WINAPI
307 StringTableAddStringEx(HSTRING_TABLE hStringTable,
308 LPWSTR lpString,
309 DWORD dwFlags,
310 LPVOID lpExtraData,
311 DWORD dwExtraDataSize)
313 FIXME("\n");
314 return (DWORD)-1;
318 /**************************************************************************
319 * StringTableDuplicate [SETUPAPI.@]
321 * Duplicates a given string table.
323 * PARAMS
324 * hStringTable [I] Handle to the string table
326 * RETURNS
327 * Success: Handle to the duplicated string table
328 * Failure: NULL
331 HSTRING_TABLE WINAPI
332 StringTableDuplicate(HSTRING_TABLE hStringTable)
334 PSTRING_TABLE pSourceTable;
335 PSTRING_TABLE pDestinationTable;
336 DWORD i;
337 DWORD length;
339 TRACE("%p\n", hStringTable);
341 pSourceTable = (PSTRING_TABLE)hStringTable;
342 if (pSourceTable == NULL)
344 ERR("Invalid hStringTable!\n");
345 return NULL;
348 pDestinationTable = MyMalloc(sizeof(STRING_TABLE));
349 if (pDestinationTable == NULL)
351 ERR("Could not allocate a new string table!\n");
352 return NULL;
355 memset(pDestinationTable, 0, sizeof(STRING_TABLE));
357 pDestinationTable->pSlots = MyMalloc(sizeof(TABLE_SLOT) * pSourceTable->dwMaxSlots);
358 if (pDestinationTable->pSlots == NULL)
360 MyFree(pDestinationTable);
361 return NULL;
364 memset(pDestinationTable->pSlots, 0, sizeof(TABLE_SLOT) * pSourceTable->dwMaxSlots);
366 pDestinationTable->dwUsedSlots = 0;
367 pDestinationTable->dwMaxSlots = pSourceTable->dwMaxSlots;
369 for (i = 0; i < pSourceTable->dwMaxSlots; i++)
371 if (pSourceTable->pSlots[i].pString != NULL)
373 length = (lstrlenW(pSourceTable->pSlots[i].pString) + 1) * sizeof(WCHAR);
374 pDestinationTable->pSlots[i].pString = MyMalloc(length);
375 if (pDestinationTable->pSlots[i].pString != NULL)
377 memcpy(pDestinationTable->pSlots[i].pString,
378 pSourceTable->pSlots[i].pString,
379 length);
380 pDestinationTable->dwUsedSlots++;
383 if (pSourceTable->pSlots[i].pData != NULL)
385 length = pSourceTable->pSlots[i].dwSize;
386 pDestinationTable->pSlots[i].pData = MyMalloc(length);
387 if (pDestinationTable->pSlots[i].pData)
389 memcpy(pDestinationTable->pSlots[i].pData,
390 pSourceTable->pSlots[i].pData,
391 length);
392 pDestinationTable->pSlots[i].dwSize = length;
398 return (HSTRING_TABLE)pDestinationTable;
402 /**************************************************************************
403 * StringTableGetExtraData [SETUPAPI.@]
405 * Retrieves extra data from a given string table entry.
407 * PARAMS
408 * hStringTable [I] Handle to the string table
409 * dwId [I] String ID
410 * lpExtraData [I] Pointer a buffer that receives the extra data
411 * dwExtraDataSize [I] Size of the buffer
413 * RETURNS
414 * Success: TRUE
415 * Failure: FALSE
417 BOOL WINAPI
418 StringTableGetExtraData(HSTRING_TABLE hStringTable,
419 DWORD dwId,
420 LPVOID lpExtraData,
421 DWORD dwExtraDataSize)
423 PSTRING_TABLE pStringTable;
425 TRACE("%p %x %p %u\n",
426 hStringTable, dwId, lpExtraData, dwExtraDataSize);
428 pStringTable = (PSTRING_TABLE)hStringTable;
429 if (pStringTable == NULL)
431 ERR("Invalid hStringTable!\n");
432 return FALSE;
435 if (dwId == 0 || dwId > pStringTable->dwMaxSlots)
437 ERR("Invalid Slot id!\n");
438 return FALSE;
441 if (pStringTable->pSlots[dwId - 1].dwSize < dwExtraDataSize)
443 ERR("Data size is too large!\n");
444 return FALSE;
447 memcpy(lpExtraData,
448 pStringTable->pSlots[dwId - 1].pData,
449 dwExtraDataSize);
451 return TRUE;
455 /**************************************************************************
456 * StringTableLookUpString [SETUPAPI.@]
458 * Searches a string table for a given string.
460 * PARAMS
461 * hStringTable [I] Handle to the string table
462 * lpString [I] String to be searched for
463 * dwFlags [I] Flags
464 * 1: case sensitive compare
466 * RETURNS
467 * Success: String ID
468 * Failure: -1
470 DWORD WINAPI
471 StringTableLookUpString(HSTRING_TABLE hStringTable,
472 LPWSTR lpString,
473 DWORD dwFlags)
475 PSTRING_TABLE pStringTable;
476 DWORD i;
478 TRACE("%p %s %x\n", hStringTable, debugstr_w(lpString), dwFlags);
480 pStringTable = (PSTRING_TABLE)hStringTable;
481 if (pStringTable == NULL)
483 ERR("Invalid hStringTable!\n");
484 return (DWORD)-1;
487 /* Search for existing string in the string table */
488 for (i = 0; i < pStringTable->dwMaxSlots; i++)
490 if (pStringTable->pSlots[i].pString != NULL)
492 if (dwFlags & 1)
494 if (!lstrcmpW(pStringTable->pSlots[i].pString, lpString))
495 return i + 1;
497 else
499 if (!lstrcmpiW(pStringTable->pSlots[i].pString, lpString))
500 return i + 1;
505 return (DWORD)-1;
509 /**************************************************************************
510 * StringTableLookUpStringEx [SETUPAPI.@]
512 * Searches a string table and extra data for a given string.
514 * PARAMS
515 * hStringTable [I] Handle to the string table
516 * lpString [I] String to be searched for
517 * dwFlags [I] Flags
518 * 1: case sensitive compare
519 * lpExtraData [O] Pointer to the buffer that receives the extra data
520 * lpReserved [I/O] Unused
522 * RETURNS
523 * Success: String ID
524 * Failure: -1
526 DWORD WINAPI
527 StringTableLookUpStringEx(HSTRING_TABLE hStringTable,
528 LPWSTR lpString,
529 DWORD dwFlags,
530 LPVOID lpExtraData,
531 LPDWORD lpReserved)
533 FIXME("\n");
534 return (DWORD)-1;
538 /**************************************************************************
539 * StringTableSetExtraData [SETUPAPI.@]
541 * Sets extra data for a given string table entry.
543 * PARAMS
544 * hStringTable [I] Handle to the string table
545 * dwId [I] String ID
546 * lpExtraData [I] Pointer to the extra data
547 * dwExtraDataSize [I] Size of the extra data
549 * RETURNS
550 * Success: TRUE
551 * Failure: FALSE
553 BOOL WINAPI
554 StringTableSetExtraData(HSTRING_TABLE hStringTable,
555 DWORD dwId,
556 LPVOID lpExtraData,
557 DWORD dwExtraDataSize)
559 PSTRING_TABLE pStringTable;
561 TRACE("%p %x %p %u\n",
562 hStringTable, dwId, lpExtraData, dwExtraDataSize);
564 pStringTable = (PSTRING_TABLE)hStringTable;
565 if (pStringTable == NULL)
567 ERR("Invalid hStringTable!\n");
568 return FALSE;
571 if (dwId == 0 || dwId > pStringTable->dwMaxSlots)
573 ERR("Invalid Slot id!\n");
574 return FALSE;
577 if (pStringTable->dwMaxDataSize < dwExtraDataSize)
579 ERR("Data size is too large!\n");
580 return FALSE;
583 pStringTable->pSlots[dwId - 1].pData = MyMalloc(dwExtraDataSize);
584 if (pStringTable->pSlots[dwId - 1].pData == NULL)
586 ERR("\n");
587 return FALSE;
590 memcpy(pStringTable->pSlots[dwId - 1].pData,
591 lpExtraData,
592 dwExtraDataSize);
593 pStringTable->pSlots[dwId - 1].dwSize = dwExtraDataSize;
595 return TRUE;
599 /**************************************************************************
600 * StringTableStringFromId [SETUPAPI.@]
602 * Returns a pointer to a string for the given string ID.
604 * PARAMS
605 * hStringTable [I] Handle to the string table.
606 * dwId [I] String ID
608 * RETURNS
609 * Success: Pointer to the string
610 * Failure: NULL
612 LPWSTR WINAPI
613 StringTableStringFromId(HSTRING_TABLE hStringTable,
614 DWORD dwId)
616 PSTRING_TABLE pStringTable;
617 static WCHAR empty[] = {0};
619 TRACE("%p %x\n", hStringTable, dwId);
621 pStringTable = (PSTRING_TABLE)hStringTable;
622 if (pStringTable == NULL)
624 ERR("Invalid hStringTable!\n");
625 return NULL;
628 if (dwId == 0 || dwId > pStringTable->dwMaxSlots)
629 return empty;
631 return pStringTable->pSlots[dwId - 1].pString;
635 /**************************************************************************
636 * StringTableStringFromIdEx [SETUPAPI.@]
638 * Returns a string for the given string ID.
640 * PARAMS
641 * hStringTable [I] Handle to the string table
642 * dwId [I] String ID
643 * lpBuffer [I] Pointer to string buffer
644 * lpBufferSize [I/O] Pointer to the size of the string buffer
646 * RETURNS
647 * Success: TRUE
648 * Failure: FALSE
650 BOOL WINAPI
651 StringTableStringFromIdEx(HSTRING_TABLE hStringTable,
652 DWORD dwId,
653 LPWSTR lpBuffer,
654 LPDWORD lpBufferLength)
656 PSTRING_TABLE pStringTable;
657 DWORD dwLength;
658 BOOL bResult = FALSE;
660 TRACE("%p %x %p %p\n", hStringTable, dwId, lpBuffer, lpBufferLength);
662 pStringTable = (PSTRING_TABLE)hStringTable;
663 if (pStringTable == NULL)
665 ERR("Invalid hStringTable!\n");
666 *lpBufferLength = 0;
667 return FALSE;
670 if (dwId == 0 || dwId > pStringTable->dwMaxSlots ||
671 pStringTable->pSlots[dwId - 1].pString == NULL)
673 WARN("Invalid string ID!\n");
674 *lpBufferLength = 0;
675 return FALSE;
678 dwLength = (lstrlenW(pStringTable->pSlots[dwId - 1].pString) + 1) * sizeof(WCHAR);
679 if (dwLength <= *lpBufferLength)
681 lstrcpyW(lpBuffer, pStringTable->pSlots[dwId - 1].pString);
682 bResult = TRUE;
685 *lpBufferLength = dwLength;
687 return bResult;
691 /**************************************************************************
692 * StringTableTrim [SETUPAPI.@]
694 * ...
696 * PARAMS
697 * hStringTable [I] Handle to the string table
699 * RETURNS
700 * None
702 VOID WINAPI
703 StringTableTrim(HSTRING_TABLE hStringTable)
705 FIXME("%p\n", hStringTable);