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