]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/Common/StringFuncs.c
BaseTools: Fix a bug for Size incorrect of Void* Fixatbuild Pcd
[mirror_edk2.git] / BaseTools / Source / C / Common / StringFuncs.c
CommitLineData
30fdf114 1/** @file\r
97fa0ee9 2Function prototypes and defines for string routines.\r
30fdf114 3\r
97fa0ee9 4Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5This program and the accompanying materials \r
30fdf114
LG
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
30fdf114
LG
13**/\r
14\r
15#include <string.h>\r
16#include <ctype.h>\r
17#include "StringFuncs.h"\r
18\r
19//\r
20// Functions implementations\r
21//\r
22\r
23CHAR8*\r
24CloneString (\r
25 IN CHAR8 *String\r
26 )\r
27/*++\r
28\r
29Routine Description:\r
30\r
31 Allocates a new string and copies 'String' to clone it\r
32\r
33Arguments:\r
34\r
35 String The string to clone\r
36\r
37Returns:\r
38\r
39 CHAR8* - NULL if there are not enough resources\r
40\r
41--*/\r
42{\r
43 CHAR8* NewString;\r
44\r
45 NewString = malloc (strlen (String) + 1);\r
46 if (NewString != NULL) {\r
47 strcpy (NewString, String);\r
48 }\r
49\r
50 return NewString;\r
51}\r
52\r
53\r
54EFI_STATUS\r
55StripInfDscStringInPlace (\r
56 IN CHAR8 *String\r
57 )\r
58/*++\r
59\r
60Routine Description:\r
61\r
62 Remove all comments, leading and trailing whitespace from the string.\r
63\r
64Arguments:\r
65\r
66 String The string to 'strip'\r
67\r
68Returns:\r
69\r
70 EFI_STATUS\r
71\r
72--*/\r
73{\r
74 CHAR8 *Pos;\r
75\r
76 if (String == NULL) {\r
77 return EFI_INVALID_PARAMETER;\r
78 }\r
79\r
80 //\r
81 // Remove leading whitespace\r
82 //\r
a709adfa 83 for (Pos = String; isspace ((int)*Pos); Pos++) {\r
30fdf114
LG
84 }\r
85 if (Pos != String) {\r
86 memmove (String, Pos, strlen (Pos) + 1);\r
87 }\r
88\r
89 //\r
90 // Comment BUGBUGs!\r
91 //\r
92 // What about strings? Comment characters are okay in strings.\r
93 // What about multiline comments?\r
94 //\r
95\r
96 Pos = (CHAR8 *) strstr (String, "//");\r
97 if (Pos != NULL) {\r
98 *Pos = '\0';\r
99 }\r
100\r
101 Pos = (CHAR8 *) strchr (String, '#');\r
102 if (Pos != NULL) {\r
103 *Pos = '\0';\r
104 }\r
105\r
106 //\r
107 // Remove trailing whitespace\r
108 //\r
109 for (Pos = String + strlen (String);\r
a709adfa 110 ((Pos - 1) >= String) && (isspace ((int)*(Pos - 1)));\r
30fdf114
LG
111 Pos--\r
112 ) {\r
113 }\r
114 *Pos = '\0';\r
115\r
116 return EFI_SUCCESS;\r
117}\r
118\r
119\r
120STRING_LIST*\r
121SplitStringByWhitespace (\r
122 IN CHAR8 *String\r
123 )\r
124/*++\r
125\r
126Routine Description:\r
127\r
128 Creates and returns a 'split' STRING_LIST by splitting the string\r
129 on whitespace boundaries.\r
130\r
131Arguments:\r
132\r
133 String The string to 'split'\r
134\r
135Returns:\r
136\r
137 EFI_STATUS\r
138\r
139--*/\r
140{\r
141 CHAR8 *Pos;\r
142 CHAR8 *EndOfSubString;\r
143 CHAR8 *EndOfString;\r
144 STRING_LIST *Output;\r
145 UINTN Item;\r
146\r
147 String = CloneString (String);\r
148 if (String == NULL) {\r
149 return NULL;\r
150 }\r
151 EndOfString = String + strlen (String);\r
152\r
153 Output = NewStringList ();\r
154\r
155 for (Pos = String, Item = 0; Pos < EndOfString; Item++) {\r
a709adfa 156 while (isspace ((int)*Pos)) {\r
30fdf114
LG
157 Pos++;\r
158 }\r
159\r
160 for (EndOfSubString=Pos;\r
a709adfa 161 (*EndOfSubString != '\0') && !isspace ((int)*EndOfSubString);\r
30fdf114
LG
162 EndOfSubString++\r
163 ) {\r
164 }\r
165\r
166 if (EndOfSubString == Pos) {\r
167 break;\r
168 }\r
169\r
170 *EndOfSubString = '\0';\r
171\r
172 AppendCopyOfStringToList (&Output, Pos);\r
173\r
174 Pos = EndOfSubString + 1;\r
175 }\r
176\r
177 free (String);\r
178 return Output;\r
179}\r
180\r
181\r
182STRING_LIST*\r
183NewStringList (\r
184 )\r
185/*++\r
186\r
187Routine Description:\r
188\r
189 Creates a new STRING_LIST with 0 strings.\r
190\r
191Returns:\r
192\r
193 STRING_LIST* - Null if there is not enough resources to create the object.\r
194\r
195--*/\r
196{\r
197 STRING_LIST *NewList;\r
198 NewList = AllocateStringListStruct (0);\r
199 if (NewList != NULL) {\r
200 NewList->Count = 0;\r
201 }\r
202 return NewList;\r
203}\r
204\r
205\r
206EFI_STATUS\r
207AppendCopyOfStringToList (\r
208 IN OUT STRING_LIST **StringList,\r
209 IN CHAR8 *String\r
210 )\r
211/*++\r
212\r
213Routine Description:\r
214\r
215 Adds String to StringList. A new copy of String is made before it is\r
216 added to StringList.\r
217\r
218Returns:\r
219\r
220 EFI_STATUS\r
221\r
222--*/\r
223{\r
224 STRING_LIST *OldList;\r
225 STRING_LIST *NewList;\r
226 CHAR8 *NewString;\r
227\r
228 OldList = *StringList;\r
229 NewList = AllocateStringListStruct (OldList->Count + 1);\r
230 if (NewList == NULL) {\r
231 return EFI_OUT_OF_RESOURCES;\r
232 }\r
233\r
234 NewString = CloneString (String);\r
235 if (NewString == NULL) {\r
236 free (NewList);\r
237 return EFI_OUT_OF_RESOURCES;\r
238 }\r
239\r
240 memcpy (\r
241 NewList->Strings,\r
242 OldList->Strings,\r
243 sizeof (OldList->Strings[0]) * OldList->Count\r
244 );\r
245 NewList->Count = OldList->Count + 1;\r
246 NewList->Strings[OldList->Count] = NewString;\r
247\r
248 *StringList = NewList;\r
249 free (OldList);\r
250\r
251 return EFI_SUCCESS;\r
252}\r
253\r
254\r
255EFI_STATUS\r
256RemoveLastStringFromList (\r
257 IN STRING_LIST *StringList\r
258 )\r
259/*++\r
260\r
261Routine Description:\r
262\r
263 Removes the last string from StringList and frees the memory associated\r
264 with it.\r
265\r
266Arguments:\r
267\r
268 StringList The string list to remove the string from\r
269\r
270Returns:\r
271\r
272 EFI_STATUS\r
273\r
274--*/\r
275{\r
276 if (StringList->Count == 0) {\r
277 return EFI_INVALID_PARAMETER;\r
278 }\r
279\r
280 free (StringList->Strings[StringList->Count - 1]);\r
281 StringList->Count--;\r
282 return EFI_SUCCESS;\r
283}\r
284\r
285\r
286STRING_LIST*\r
287AllocateStringListStruct (\r
288 IN UINTN StringCount\r
289 )\r
290/*++\r
291\r
292Routine Description:\r
293\r
294 Allocates a STRING_LIST structure that can store StringCount strings.\r
295\r
296Arguments:\r
297\r
298 StringCount The number of strings that need to be stored\r
299\r
300Returns:\r
301\r
302 EFI_STATUS\r
303\r
304--*/\r
305{\r
306 return malloc (OFFSET_OF(STRING_LIST, Strings[StringCount + 1]));\r
307}\r
308\r
309\r
310VOID\r
311FreeStringList (\r
312 IN STRING_LIST *StringList\r
313 )\r
314/*++\r
315\r
316Routine Description:\r
317\r
318 Frees all memory associated with StringList.\r
319\r
320Arguments:\r
321\r
322 StringList The string list to free\r
323\r
324Returns:\r
325\r
326 VOID\r
327--*/\r
328{\r
329 while (StringList->Count > 0) {\r
330 RemoveLastStringFromList (StringList);\r
331 }\r
332\r
333 free (StringList);\r
334}\r
335\r
336\r
337CHAR8*\r
338StringListToString (\r
339 IN STRING_LIST *StringList\r
340 )\r
341/*++\r
342\r
343Routine Description:\r
344\r
345 Generates a string that represents the STRING_LIST\r
346\r
347Arguments:\r
348\r
349 StringList The string list to convert to a string\r
350\r
351Returns:\r
352\r
353 CHAR8* - The string list represented with a single string. The returned\r
354 string must be freed by the caller.\r
355\r
356--*/\r
357{\r
358 UINTN Count;\r
359 UINTN Length;\r
360 CHAR8 *NewString;\r
361\r
362 Length = 2;\r
363 for (Count = 0; Count < StringList->Count; Count++) {\r
364 if (Count > 0) {\r
365 Length += 2;\r
366 }\r
367 Length += strlen (StringList->Strings[Count]) + 2;\r
368 }\r
369\r
370 NewString = malloc (Length + 1);\r
371 if (NewString == NULL) {\r
372 return NewString;\r
373 }\r
374 NewString[0] = '\0';\r
375\r
376 strcat (NewString, "[");\r
377 for (Count = 0; Count < StringList->Count; Count++) {\r
378 if (Count > 0) {\r
379 strcat (NewString, ", ");\r
380 }\r
381 strcat (NewString, "\"");\r
382 strcat (NewString, StringList->Strings[Count]);\r
383 strcat (NewString, "\"");\r
384 }\r
385 strcat (NewString, "]");\r
386 \r
387 return NewString;\r
388}\r
389\r
390\r
391VOID\r
392PrintStringList (\r
393 IN STRING_LIST *StringList\r
394 )\r
395/*++\r
396\r
397Routine Description:\r
398\r
399 Prints out the string list\r
400\r
401Arguments:\r
402\r
403 StringList The string list to print\r
404\r
405Returns:\r
406\r
407 EFI_STATUS\r
408\r
409--*/\r
410{\r
411 CHAR8* String;\r
412 String = StringListToString (StringList);\r
413 if (String != NULL) {\r
414 printf ("%s", String);\r
415 free (String);\r
416 }\r
417}\r
418\r
419\r