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