]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/StringFuncs.c
2 Function prototypes and defines for string routines.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "StringFuncs.h"
14 // Functions implementations
25 Allocates a new string and copies 'String' to clone it
29 String The string to clone
33 CHAR8* - NULL if there are not enough resources
39 NewString
= malloc (strlen (String
) + 1);
40 if (NewString
!= NULL
) {
41 strcpy (NewString
, String
);
49 StripInfDscStringInPlace (
56 Remove all comments, leading and trailing whitespace from the string.
60 String The string to 'strip'
71 return EFI_INVALID_PARAMETER
;
75 // Remove leading whitespace
77 for (Pos
= String
; isspace ((int)*Pos
); Pos
++) {
80 memmove (String
, Pos
, strlen (Pos
) + 1);
86 // What about strings? Comment characters are okay in strings.
87 // What about multiline comments?
90 Pos
= (CHAR8
*) strstr (String
, "//");
95 Pos
= (CHAR8
*) strchr (String
, '#');
101 // Remove trailing whitespace
103 for (Pos
= String
+ strlen (String
);
104 ((Pos
- 1) >= String
) && (isspace ((int)*(Pos
- 1)));
115 SplitStringByWhitespace (
122 Creates and returns a 'split' STRING_LIST by splitting the string
123 on whitespace boundaries.
127 String The string to 'split'
136 CHAR8
*EndOfSubString
;
141 String
= CloneString (String
);
142 if (String
== NULL
) {
145 EndOfString
= String
+ strlen (String
);
147 Output
= NewStringList ();
149 for (Pos
= String
, Item
= 0; Pos
< EndOfString
; Item
++) {
150 while (isspace ((int)*Pos
)) {
154 for (EndOfSubString
=Pos
;
155 (*EndOfSubString
!= '\0') && !isspace ((int)*EndOfSubString
);
160 if (EndOfSubString
== Pos
) {
164 *EndOfSubString
= '\0';
166 AppendCopyOfStringToList (&Output
, Pos
);
168 Pos
= EndOfSubString
+ 1;
183 Creates a new STRING_LIST with 0 strings.
187 STRING_LIST* - Null if there is not enough resources to create the object.
191 STRING_LIST
*NewList
;
192 NewList
= AllocateStringListStruct (0);
193 if (NewList
!= NULL
) {
201 AppendCopyOfStringToList (
202 IN OUT STRING_LIST
**StringList
,
209 Adds String to StringList. A new copy of String is made before it is
218 STRING_LIST
*OldList
;
219 STRING_LIST
*NewList
;
222 OldList
= *StringList
;
223 NewList
= AllocateStringListStruct (OldList
->Count
+ 1);
224 if (NewList
== NULL
) {
225 return EFI_OUT_OF_RESOURCES
;
228 NewString
= CloneString (String
);
229 if (NewString
== NULL
) {
231 return EFI_OUT_OF_RESOURCES
;
237 sizeof (OldList
->Strings
[0]) * OldList
->Count
239 NewList
->Count
= OldList
->Count
+ 1;
240 NewList
->Strings
[OldList
->Count
] = NewString
;
242 *StringList
= NewList
;
250 RemoveLastStringFromList (
251 IN STRING_LIST
*StringList
257 Removes the last string from StringList and frees the memory associated
262 StringList The string list to remove the string from
270 if (StringList
->Count
== 0) {
271 return EFI_INVALID_PARAMETER
;
274 free (StringList
->Strings
[StringList
->Count
- 1]);
281 AllocateStringListStruct (
288 Allocates a STRING_LIST structure that can store StringCount strings.
292 StringCount The number of strings that need to be stored
300 return malloc (OFFSET_OF(STRING_LIST
, Strings
[StringCount
+ 1]));
306 IN STRING_LIST
*StringList
312 Frees all memory associated with StringList.
316 StringList The string list to free
323 while (StringList
->Count
> 0) {
324 RemoveLastStringFromList (StringList
);
333 IN STRING_LIST
*StringList
339 Generates a string that represents the STRING_LIST
343 StringList The string list to convert to a string
347 CHAR8* - The string list represented with a single string. The returned
348 string must be freed by the caller.
357 for (Count
= 0; Count
< StringList
->Count
; Count
++) {
361 Length
+= strlen (StringList
->Strings
[Count
]) + 2;
364 NewString
= malloc (Length
+ 1);
365 if (NewString
== NULL
) {
370 strcat (NewString
, "[");
371 for (Count
= 0; Count
< StringList
->Count
; Count
++) {
373 strcat (NewString
, ", ");
375 strcat (NewString
, "\"");
376 strcat (NewString
, StringList
->Strings
[Count
]);
377 strcat (NewString
, "\"");
379 strcat (NewString
, "]");
387 IN STRING_LIST
*StringList
393 Prints out the string list
397 StringList The string list to print
406 String
= StringListToString (StringList
);
407 if (String
!= NULL
) {
408 printf ("%s", String
);