]>
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 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "StringFuncs.h"
20 // Functions implementations
31 Allocates a new string and copies 'String' to clone it
35 String The string to clone
39 CHAR8* - NULL if there are not enough resources
45 NewString
= malloc (strlen (String
) + 1);
46 if (NewString
!= NULL
) {
47 strcpy (NewString
, String
);
55 StripInfDscStringInPlace (
62 Remove all comments, leading and trailing whitespace from the string.
66 String The string to 'strip'
77 return EFI_INVALID_PARAMETER
;
81 // Remove leading whitespace
83 for (Pos
= String
; isspace ((int)*Pos
); Pos
++) {
86 memmove (String
, Pos
, strlen (Pos
) + 1);
92 // What about strings? Comment characters are okay in strings.
93 // What about multiline comments?
96 Pos
= (CHAR8
*) strstr (String
, "//");
101 Pos
= (CHAR8
*) strchr (String
, '#');
107 // Remove trailing whitespace
109 for (Pos
= String
+ strlen (String
);
110 ((Pos
- 1) >= String
) && (isspace ((int)*(Pos
- 1)));
121 SplitStringByWhitespace (
128 Creates and returns a 'split' STRING_LIST by splitting the string
129 on whitespace boundaries.
133 String The string to 'split'
142 CHAR8
*EndOfSubString
;
147 String
= CloneString (String
);
148 if (String
== NULL
) {
151 EndOfString
= String
+ strlen (String
);
153 Output
= NewStringList ();
155 for (Pos
= String
, Item
= 0; Pos
< EndOfString
; Item
++) {
156 while (isspace ((int)*Pos
)) {
160 for (EndOfSubString
=Pos
;
161 (*EndOfSubString
!= '\0') && !isspace ((int)*EndOfSubString
);
166 if (EndOfSubString
== Pos
) {
170 *EndOfSubString
= '\0';
172 AppendCopyOfStringToList (&Output
, Pos
);
174 Pos
= EndOfSubString
+ 1;
189 Creates a new STRING_LIST with 0 strings.
193 STRING_LIST* - Null if there is not enough resources to create the object.
197 STRING_LIST
*NewList
;
198 NewList
= AllocateStringListStruct (0);
199 if (NewList
!= NULL
) {
207 AppendCopyOfStringToList (
208 IN OUT STRING_LIST
**StringList
,
215 Adds String to StringList. A new copy of String is made before it is
224 STRING_LIST
*OldList
;
225 STRING_LIST
*NewList
;
228 OldList
= *StringList
;
229 NewList
= AllocateStringListStruct (OldList
->Count
+ 1);
230 if (NewList
== NULL
) {
231 return EFI_OUT_OF_RESOURCES
;
234 NewString
= CloneString (String
);
235 if (NewString
== NULL
) {
237 return EFI_OUT_OF_RESOURCES
;
243 sizeof (OldList
->Strings
[0]) * OldList
->Count
245 NewList
->Count
= OldList
->Count
+ 1;
246 NewList
->Strings
[OldList
->Count
] = NewString
;
248 *StringList
= NewList
;
256 RemoveLastStringFromList (
257 IN STRING_LIST
*StringList
263 Removes the last string from StringList and frees the memory associated
268 StringList The string list to remove the string from
276 if (StringList
->Count
== 0) {
277 return EFI_INVALID_PARAMETER
;
280 free (StringList
->Strings
[StringList
->Count
- 1]);
287 AllocateStringListStruct (
294 Allocates a STRING_LIST structure that can store StringCount strings.
298 StringCount The number of strings that need to be stored
306 return malloc (OFFSET_OF(STRING_LIST
, Strings
[StringCount
+ 1]));
312 IN STRING_LIST
*StringList
318 Frees all memory associated with StringList.
322 StringList The string list to free
329 while (StringList
->Count
> 0) {
330 RemoveLastStringFromList (StringList
);
339 IN STRING_LIST
*StringList
345 Generates a string that represents the STRING_LIST
349 StringList The string list to convert to a string
353 CHAR8* - The string list represented with a single string. The returned
354 string must be freed by the caller.
363 for (Count
= 0; Count
< StringList
->Count
; Count
++) {
367 Length
+= strlen (StringList
->Strings
[Count
]) + 2;
370 NewString
= malloc (Length
+ 1);
371 if (NewString
== NULL
) {
376 strcat (NewString
, "[");
377 for (Count
= 0; Count
< StringList
->Count
; Count
++) {
379 strcat (NewString
, ", ");
381 strcat (NewString
, "\"");
382 strcat (NewString
, StringList
->Strings
[Count
]);
383 strcat (NewString
, "\"");
385 strcat (NewString
, "]");
393 IN STRING_LIST
*StringList
399 Prints out the string list
403 StringList The string list to print
412 String
= StringListToString (StringList
);
413 if (String
!= NULL
) {
414 printf ("%s", String
);