1 /*
2  * PROJECT:     ReactOS WHERE command
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     Providing string list
5  * COPYRIGHT:   Copyright 2021 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6  */
7 
8 #pragma once
9 
10 #define str_clone _wcsdup
11 
12 typedef struct strlist_t
13 {
14     LPWSTR *ppsz;
15     unsigned int count;
16 } strlist_t;
17 #define strlist_default { NULL, 0 }
18 
strlist_init(strlist_t * plist)19 static inline void strlist_init(strlist_t *plist)
20 {
21     plist->ppsz = NULL;
22     plist->count = 0;
23 }
24 
strlist_get_at(strlist_t * plist,unsigned int i)25 static inline LPWSTR strlist_get_at(strlist_t *plist, unsigned int i)
26 {
27     return plist->ppsz[i];
28 }
29 
strlist_add(strlist_t * plist,LPCWSTR psz)30 static int strlist_add(strlist_t *plist, LPCWSTR psz)
31 {
32     LPWSTR *ppsz, clone = str_clone(psz);
33     if (!clone)
34         return 0;
35     ppsz = (LPWSTR *)realloc(plist->ppsz, (plist->count + 1) * sizeof(LPWSTR));
36     if (!ppsz)
37     {
38         free(clone);
39         return 0;
40     }
41     plist->ppsz = ppsz;
42     plist->ppsz[plist->count] = clone;
43     ++(plist->count);
44     return 1;
45 }
46 
strlist_destroy(strlist_t * plist)47 static void strlist_destroy(strlist_t *plist)
48 {
49     unsigned int i;
50     for (i = 0; i < plist->count; ++i)
51         free(plist->ppsz[i]);
52     plist->count = 0;
53     free(plist->ppsz);
54     plist->ppsz = NULL;
55 }
56 
strlist_find_i(strlist_t * plist,LPCWSTR psz)57 static inline int strlist_find_i(strlist_t *plist, LPCWSTR psz)
58 {
59     unsigned int i;
60     for (i = 0; i < plist->count; ++i)
61     {
62         if (_wcsicmp(plist->ppsz[i], psz) == 0)
63             return i;
64     }
65     return -1;
66 }
67