]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/StringFuncs.c
3 Copyright (c) 2007 - 2008, Intel Corporation
4 All rights reserved. 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
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.
18 Function prototypes and defines for string routines.
24 #include "StringFuncs.h"
27 // Functions implementations
38 Allocates a new string and copies 'String' to clone it
42 String The string to clone
46 CHAR8* - NULL if there are not enough resources
52 NewString
= malloc (strlen (String
) + 1);
53 if (NewString
!= NULL
) {
54 strcpy (NewString
, String
);
62 StripInfDscStringInPlace (
69 Remove all comments, leading and trailing whitespace from the string.
73 String The string to 'strip'
84 return EFI_INVALID_PARAMETER
;
88 // Remove leading whitespace
90 for (Pos
= String
; isspace (*Pos
); Pos
++) {
93 memmove (String
, Pos
, strlen (Pos
) + 1);
99 // What about strings? Comment characters are okay in strings.
100 // What about multiline comments?
103 Pos
= (CHAR8
*) strstr (String
, "//");
108 Pos
= (CHAR8
*) strchr (String
, '#');
114 // Remove trailing whitespace
116 for (Pos
= String
+ strlen (String
);
117 ((Pos
- 1) >= String
) && (isspace (*(Pos
- 1)));
128 SplitStringByWhitespace (
135 Creates and returns a 'split' STRING_LIST by splitting the string
136 on whitespace boundaries.
140 String The string to 'split'
149 CHAR8
*EndOfSubString
;
154 String
= CloneString (String
);
155 if (String
== NULL
) {
158 EndOfString
= String
+ strlen (String
);
160 Output
= NewStringList ();
162 for (Pos
= String
, Item
= 0; Pos
< EndOfString
; Item
++) {
163 while (isspace (*Pos
)) {
167 for (EndOfSubString
=Pos
;
168 (*EndOfSubString
!= '\0') && !isspace (*EndOfSubString
);
173 if (EndOfSubString
== Pos
) {
177 *EndOfSubString
= '\0';
179 AppendCopyOfStringToList (&Output
, Pos
);
181 Pos
= EndOfSubString
+ 1;
196 Creates a new STRING_LIST with 0 strings.
200 STRING_LIST* - Null if there is not enough resources to create the object.
204 STRING_LIST
*NewList
;
205 NewList
= AllocateStringListStruct (0);
206 if (NewList
!= NULL
) {
214 AppendCopyOfStringToList (
215 IN OUT STRING_LIST
**StringList
,
222 Adds String to StringList. A new copy of String is made before it is
231 STRING_LIST
*OldList
;
232 STRING_LIST
*NewList
;
235 OldList
= *StringList
;
236 NewList
= AllocateStringListStruct (OldList
->Count
+ 1);
237 if (NewList
== NULL
) {
238 return EFI_OUT_OF_RESOURCES
;
241 NewString
= CloneString (String
);
242 if (NewString
== NULL
) {
244 return EFI_OUT_OF_RESOURCES
;
250 sizeof (OldList
->Strings
[0]) * OldList
->Count
252 NewList
->Count
= OldList
->Count
+ 1;
253 NewList
->Strings
[OldList
->Count
] = NewString
;
255 *StringList
= NewList
;
263 RemoveLastStringFromList (
264 IN STRING_LIST
*StringList
270 Removes the last string from StringList and frees the memory associated
275 StringList The string list to remove the string from
283 if (StringList
->Count
== 0) {
284 return EFI_INVALID_PARAMETER
;
287 free (StringList
->Strings
[StringList
->Count
- 1]);
294 AllocateStringListStruct (
301 Allocates a STRING_LIST structure that can store StringCount strings.
305 StringCount The number of strings that need to be stored
313 return malloc (OFFSET_OF(STRING_LIST
, Strings
[StringCount
+ 1]));
319 IN STRING_LIST
*StringList
325 Frees all memory associated with StringList.
329 StringList The string list to free
336 while (StringList
->Count
> 0) {
337 RemoveLastStringFromList (StringList
);
346 IN STRING_LIST
*StringList
352 Generates a string that represents the STRING_LIST
356 StringList The string list to convert to a string
360 CHAR8* - The string list represented with a single string. The returned
361 string must be freed by the caller.
370 for (Count
= 0; Count
< StringList
->Count
; Count
++) {
374 Length
+= strlen (StringList
->Strings
[Count
]) + 2;
377 NewString
= malloc (Length
+ 1);
378 if (NewString
== NULL
) {
383 strcat (NewString
, "[");
384 for (Count
= 0; Count
< StringList
->Count
; Count
++) {
386 strcat (NewString
, ", ");
388 strcat (NewString
, "\"");
389 strcat (NewString
, StringList
->Strings
[Count
]);
390 strcat (NewString
, "\"");
392 strcat (NewString
, "]");
400 IN STRING_LIST
*StringList
406 Prints out the string list
410 StringList The string list to print
419 String
= StringListToString (StringList
);
420 if (String
!= NULL
) {
421 printf ("%s", String
);