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