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