]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/StringUtils.py
2 # This file is used to define common string related functions used in parsing process
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
11 from __future__
import absolute_import
13 from . import DataType
14 import Common
.LongFilePathOs
as os
16 from . import EdkLogger
as EdkLogger
18 from . import GlobalData
19 from .BuildToolError
import *
20 from CommonDataClass
.Exceptions
import *
21 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
22 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
24 gHexVerPatt
= re
.compile('0x[a-f0-9]{4}[a-f0-9]{4}$', re
.IGNORECASE
)
25 gHumanReadableVerPatt
= re
.compile(r
'([1-9][0-9]*|0)\.[0-9]{1,2}$')
29 # Get a value list from a string with multiple values split with SplitTag
30 # The default SplitTag is DataType.TAB_VALUE_SPLIT
31 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
33 # @param String: The input string to be splitted
34 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
35 # @param MaxSplit: The max number of split values, default is -1
37 # @retval list() A list for splitted string
39 def GetSplitValueList(String
, SplitTag
=DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
43 InSingleQuoteString
= False
44 InDoubleQuoteString
= False
46 for Index
in range(0, len(String
)):
50 # Found a splitter not in a string, split it
51 if (not InSingleQuoteString
or not InDoubleQuoteString
) and InParenthesis
== 0 and Char
== SplitTag
:
52 ValueList
.append(String
[Last
:Index
].strip())
54 if MaxSplit
> 0 and len(ValueList
) >= MaxSplit
:
57 if Char
== '\\' and (InSingleQuoteString
or InDoubleQuoteString
):
59 elif Char
== '"' and not InSingleQuoteString
:
60 if not InDoubleQuoteString
:
61 InDoubleQuoteString
= True
63 InDoubleQuoteString
= False
64 elif Char
== "'" and not InDoubleQuoteString
:
65 if not InSingleQuoteString
:
66 InSingleQuoteString
= True
68 InSingleQuoteString
= False
70 InParenthesis
= InParenthesis
+ 1
72 InParenthesis
= InParenthesis
- 1
76 if Last
< len(String
):
77 ValueList
.append(String
[Last
:].strip())
78 elif Last
== len(String
):
85 # Get a value list from a string with multiple values split with SplitString
86 # The default SplitTag is DataType.TAB_VALUE_SPLIT
87 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
89 # @param String: The input string to be splitted
90 # @param SplitStr: The split key, default is DataType.TAB_VALUE_SPLIT
91 # @param MaxSplit: The max number of split values, default is -1
93 # @retval list() A list for splitted string
95 def GetSplitList(String
, SplitStr
=DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
96 return list(map(lambda l
: l
.strip(), String
.split(SplitStr
, MaxSplit
)))
100 # Find a key's all arches in dict, add the new arch to the list
101 # If not exist any arch, set the arch directly
103 # @param Dict: The input value for Dict
104 # @param Key: The input value for Key
105 # @param Arch: The Arch to be added or merged
107 def MergeArches(Dict
, Key
, Arch
):
109 Dict
[Key
].append(Arch
)
111 Dict
[Key
] = Arch
.split()
115 # Parse a string with format "DEFINE <VarName> = <PATH>"
116 # Generate a map Defines[VarName] = PATH
117 # Return False if invalid format
119 # @param String: String with DEFINE statement
120 # @param Arch: Supported Arch
121 # @param Defines: DEFINE statement to be parsed
123 # @retval 0 DEFINE statement found, and valid
124 # @retval 1 DEFINE statement found, but not valid
125 # @retval -1 DEFINE statement not found
127 def GenDefines(String
, Arch
, Defines
):
128 if String
.find(DataType
.TAB_DEFINE
+ ' ') > -1:
129 List
= String
.replace(DataType
.TAB_DEFINE
+ ' ', '').split(DataType
.TAB_EQUAL_SPLIT
)
131 Defines
[(CleanString(List
[0]), Arch
)] = CleanString(List
[1])
140 # Parse a string with format "!include <Filename>"
141 # Return the file path
142 # Return False if invalid format or NOT FOUND
144 # @param String: String with INCLUDE statement
145 # @param IncludeFiles: INCLUDE statement to be parsed
146 # @param Arch: Supported Arch
151 def GenInclude(String
, IncludeFiles
, Arch
):
152 if String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') > -1:
153 IncludeFile
= CleanString(String
[String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') + len(DataType
.TAB_INCLUDE
+ ' ') : ])
154 MergeArches(IncludeFiles
, IncludeFile
, Arch
)
159 ## GetLibraryClassesWithModuleType
161 # Get Library Class definition when no module type defined
163 # @param Lines: The content to be parsed
164 # @param Key: Reserved
165 # @param KeyValues: To store data after parsing
166 # @param CommentCharacter: Comment char, used to ignore comment content
168 # @retval True Get library classes successfully
170 def GetLibraryClassesWithModuleType(Lines
, Key
, KeyValues
, CommentCharacter
):
171 newKey
= SplitModuleType(Key
)
172 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
173 LineList
= Lines
.splitlines()
174 for Line
in LineList
:
175 Line
= CleanString(Line
, CommentCharacter
)
176 if Line
!= '' and Line
[0] != CommentCharacter
:
177 KeyValues
.append([CleanString(Line
, CommentCharacter
), newKey
[1]])
185 # @param Lines: The content to be parsed
186 # @param Key: Reserved
187 # @param KeyValues: To store data after parsing
188 # @param CommentCharacter: Comment char, used to ignore comment content
190 # @retval True Get Dynamic Pcds successfully
192 def GetDynamics(Lines
, Key
, KeyValues
, CommentCharacter
):
194 # Get SkuId Name List
196 SkuIdNameList
= SplitModuleType(Key
)
198 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
199 LineList
= Lines
.splitlines()
200 for Line
in LineList
:
201 Line
= CleanString(Line
, CommentCharacter
)
202 if Line
!= '' and Line
[0] != CommentCharacter
:
203 KeyValues
.append([CleanString(Line
, CommentCharacter
), SkuIdNameList
[1]])
209 # Split ModuleType out of section defien to get key
210 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
212 # @param Key: String to be parsed
214 # @retval ReturnValue A list for module types
216 def SplitModuleType(Key
):
217 KeyList
= Key
.split(DataType
.TAB_SPLIT
)
223 # Fill in for moduletype
227 KeyValue
= KeyList
[0]
229 KeyValue
= KeyValue
+ DataType
.TAB_SPLIT
+ KeyList
[1]
230 ReturnValue
.append(KeyValue
)
231 ReturnValue
.append(GetSplitValueList(KeyList
[2]))
235 ## Replace macro in strings list
237 # This method replace macros used in a given string list. The macros are
238 # given in a dictionary.
240 # @param StringList StringList to be processed
241 # @param MacroDefinitions The macro definitions in the form of dictionary
242 # @param SelfReplacement To decide whether replace un-defined macro to ''
244 # @retval NewList A new string list whose macros are replaced
246 def ReplaceMacros(StringList
, MacroDefinitions
=None, SelfReplacement
=False):
248 if MacroDefinitions
is None:
249 MacroDefinitions
= {}
250 for String
in StringList
:
251 if isinstance(String
, type('')):
252 NewList
.append(ReplaceMacro(String
, MacroDefinitions
, SelfReplacement
))
254 NewList
.append(String
)
258 ## Replace macro in string
260 # This method replace macros used in given string. The macros are given in a
263 # @param String String to be processed
264 # @param MacroDefinitions The macro definitions in the form of dictionary
265 # @param SelfReplacement To decide whether replace un-defined macro to ''
267 # @retval string The string whose macros are replaced
269 def ReplaceMacro(String
, MacroDefinitions
=None, SelfReplacement
=False, RaiseError
=False):
271 if MacroDefinitions
is None:
272 MacroDefinitions
= {}
273 while String
and MacroDefinitions
:
274 MacroUsed
= GlobalData
.gMacroRefPattern
.findall(String
)
275 # no macro found in String, stop replacing
276 if len(MacroUsed
) == 0:
279 for Macro
in MacroUsed
:
280 if Macro
not in MacroDefinitions
:
282 raise SymbolNotFound("%s not defined" % Macro
)
284 String
= String
.replace("$(%s)" % Macro
, '')
286 if "$(%s)" % Macro
not in MacroDefinitions
[Macro
]:
287 String
= String
.replace("$(%s)" % Macro
, MacroDefinitions
[Macro
])
288 # in case there's macro not defined
289 if String
== LastString
:
297 # Create a normal path
298 # And replace DEFINE in the path
300 # @param Path: The input value for Path to be converted
301 # @param Defines: A set for DEFINE statement
303 # @retval Path Formatted path
305 def NormPath(Path
, Defines
=None):
306 IsRelativePath
= False
309 IsRelativePath
= True
311 # Replace with Define
314 Path
= ReplaceMacro(Path
, Defines
)
316 # To local path format
318 Path
= os
.path
.normpath(Path
)
319 if Path
.startswith(GlobalData
.gWorkspace
) and not Path
.startswith(GlobalData
.gBuildDirectory
) and not os
.path
.exists(Path
):
320 Path
= Path
[len (GlobalData
.gWorkspace
):]
321 if Path
[0] == os
.path
.sep
:
323 Path
= mws
.join(GlobalData
.gWorkspace
, Path
)
325 if IsRelativePath
and Path
[0] != '.':
326 Path
= os
.path
.join('.', Path
)
332 # Remove comments in a string
335 # @param Line: The string to be cleaned
336 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
338 # @retval Path Formatted path
340 def CleanString(Line
, CommentCharacter
=DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False, BuildOption
=False):
346 # Replace Edk's comment character
348 if AllowCppStyleComment
:
349 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
351 # remove comments, but we should escape comment character in string
353 InDoubleQuoteString
= False
354 InSingleQuoteString
= False
355 CommentInString
= False
356 for Index
in range(0, len(Line
)):
357 if Line
[Index
] == '"' and not InSingleQuoteString
:
358 InDoubleQuoteString
= not InDoubleQuoteString
359 elif Line
[Index
] == "'" and not InDoubleQuoteString
:
360 InSingleQuoteString
= not InSingleQuoteString
361 elif Line
[Index
] == CommentCharacter
and (InSingleQuoteString
or InDoubleQuoteString
):
362 CommentInString
= True
363 elif Line
[Index
] == CommentCharacter
and not (InSingleQuoteString
or InDoubleQuoteString
):
364 Line
= Line
[0: Index
]
367 if CommentInString
and BuildOption
:
368 Line
= Line
.replace('"', '')
369 ChIndex
= Line
.find('#')
371 if GlobalData
.gIsWindows
:
372 if ChIndex
== 0 or Line
[ChIndex
- 1] != '^':
373 Line
= Line
[0:ChIndex
] + '^' + Line
[ChIndex
:]
374 ChIndex
= Line
.find('#', ChIndex
+ 2)
376 ChIndex
= Line
.find('#', ChIndex
+ 1)
378 if ChIndex
== 0 or Line
[ChIndex
- 1] != '\\':
379 Line
= Line
[0:ChIndex
] + '\\' + Line
[ChIndex
:]
380 ChIndex
= Line
.find('#', ChIndex
+ 2)
382 ChIndex
= Line
.find('#', ChIndex
+ 1)
384 # remove whitespace again
392 # Split statement with comments in a string
395 # @param Line: The string to be cleaned
396 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
398 # @retval Path Formatted path
400 def CleanString2(Line
, CommentCharacter
=DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
406 # Replace Edk's comment character
408 if AllowCppStyleComment
:
409 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
411 # separate comments and statements, but we should escape comment character in string
413 InDoubleQuoteString
= False
414 InSingleQuoteString
= False
415 CommentInString
= False
417 for Index
in range(0, len(Line
)):
418 if Line
[Index
] == '"' and not InSingleQuoteString
:
419 InDoubleQuoteString
= not InDoubleQuoteString
420 elif Line
[Index
] == "'" and not InDoubleQuoteString
:
421 InSingleQuoteString
= not InSingleQuoteString
422 elif Line
[Index
] == CommentCharacter
and (InDoubleQuoteString
or InSingleQuoteString
):
423 CommentInString
= True
424 elif Line
[Index
] == CommentCharacter
and not (InDoubleQuoteString
or InSingleQuoteString
):
425 Comment
= Line
[Index
:].strip()
426 Line
= Line
[0:Index
].strip()
431 ## GetMultipleValuesOfKeyFromLines
433 # Parse multiple strings to clean comment and spaces
434 # The result is saved to KeyValues
436 # @param Lines: The content to be parsed
437 # @param Key: Reserved
438 # @param KeyValues: To store data after parsing
439 # @param CommentCharacter: Comment char, used to ignore comment content
441 # @retval True Successfully executed
443 def GetMultipleValuesOfKeyFromLines(Lines
, Key
, KeyValues
, CommentCharacter
):
444 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
445 LineList
= Lines
.split('\n')
446 for Line
in LineList
:
447 Line
= CleanString(Line
, CommentCharacter
)
448 if Line
!= '' and Line
[0] != CommentCharacter
:
449 KeyValues
.append(Line
)
455 # Parse a DEFINE statement to get defined value
458 # @param String: The content to be parsed
459 # @param Key: The key of DEFINE statement
460 # @param CommentCharacter: Comment char, used to ignore comment content
462 # @retval string The defined value
464 def GetDefineValue(String
, Key
, CommentCharacter
):
465 String
= CleanString(String
)
466 return String
[String
.find(Key
+ ' ') + len(Key
+ ' ') : ]
470 # Get a Hex Version Value
472 # @param VerString: The version string to be parsed
475 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
476 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
477 # where mmmm is the major number and nnnn is the adjusted minor number.
479 def GetHexVerValue(VerString
):
480 VerString
= CleanString(VerString
)
482 if gHumanReadableVerPatt
.match(VerString
):
483 ValueList
= VerString
.split('.')
488 DeciValue
= (int(Major
) << 16) + int(Minor
);
489 return "0x%08x" % DeciValue
490 elif gHexVerPatt
.match(VerString
):
496 ## GetSingleValueOfKeyFromLines
498 # Parse multiple strings as below to get value of each definition line
501 # The result is saved to Dictionary
503 # @param Lines: The content to be parsed
504 # @param Dictionary: To store data after parsing
505 # @param CommentCharacter: Comment char, be used to ignore comment content
506 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
507 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
508 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
510 # @retval True Successfully executed
512 def GetSingleValueOfKeyFromLines(Lines
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
513 Lines
= Lines
.split('\n')
521 # Handle DEFINE and SPEC
523 if Line
.find(DataType
.TAB_INF_DEFINES_DEFINE
+ ' ') > -1:
524 if '' in DefineValues
:
525 DefineValues
.remove('')
526 DefineValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_DEFINE
, CommentCharacter
))
528 if Line
.find(DataType
.TAB_INF_DEFINES_SPEC
+ ' ') > -1:
530 SpecValues
.remove('')
531 SpecValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_SPEC
, CommentCharacter
))
537 LineList
= Line
.split(KeySplitCharacter
, 1)
538 if len(LineList
) >= 2:
539 Key
= LineList
[0].split()
540 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
:
542 # Remove comments and white spaces
544 LineList
[1] = CleanString(LineList
[1], CommentCharacter
)
546 Value
= list(map(string
.strip
, LineList
[1].split(ValueSplitCharacter
)))
548 Value
= CleanString(LineList
[1], CommentCharacter
).splitlines()
550 if Key
[0] in Dictionary
:
551 if Key
[0] not in Keys
:
552 Dictionary
[Key
[0]] = Value
555 Dictionary
[Key
[0]].extend(Value
)
557 Dictionary
[DataType
.TAB_INF_DEFINES_MACRO
][Key
[0]] = Value
[0]
559 if DefineValues
== []:
563 Dictionary
[DataType
.TAB_INF_DEFINES_DEFINE
] = DefineValues
564 Dictionary
[DataType
.TAB_INF_DEFINES_SPEC
] = SpecValues
568 ## The content to be parsed
570 # Do pre-check for a file before it is parsed
574 # @param FileName: Used for error report
575 # @param FileContent: File content to be parsed
576 # @param SupSectionTag: Used for error report
578 def PreCheck(FileName
, FileContent
, SupSectionTag
):
582 for Line
in FileContent
.splitlines():
587 Line
= CleanString(Line
)
590 # Remove commented line
592 if Line
.find(DataType
.TAB_COMMA_SPLIT
) == 0:
597 if Line
.find('$') > -1:
598 if Line
.find('$(') < 0 or Line
.find(')') < 0:
599 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
604 if Line
.find('[') > -1 or Line
.find(']') > -1:
606 # Only get one '[' or one ']'
608 if not (Line
.find('[') > -1 and Line
.find(']') > -1):
609 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
612 # Regenerate FileContent
614 NewFileContent
= NewFileContent
+ Line
+ '\r\n'
617 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
619 return NewFileContent
623 # Check if the Filename is including ExtName
624 # Return True if it exists
625 # Raise a error message if it not exists
627 # @param CheckFilename: Name of the file to be checked
628 # @param ExtName: Ext name of the file to be checked
629 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
630 # @param SectionName: Used for error report
631 # @param Line: The line in container file which defines the file to be checked
633 # @retval True The file type is correct
635 def CheckFileType(CheckFilename
, ExtName
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
636 if CheckFilename
!= '' and CheckFilename
is not None:
637 (Root
, Ext
) = os
.path
.splitext(CheckFilename
)
638 if Ext
.upper() != ExtName
.upper():
639 ContainerFile
= open(ContainerFilename
, 'r').read()
641 LineNo
= GetLineNo(ContainerFile
, Line
)
642 ErrorMsg
= "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName
, CheckFilename
, ExtName
)
643 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, Line
=LineNo
,
644 File
=ContainerFilename
, RaiseError
=EdkLogger
.IsRaiseError
)
650 # Check if the file exists
651 # Return True if it exists
652 # Raise a error message if it not exists
654 # @param CheckFilename: Name of the file to be checked
655 # @param WorkspaceDir: Current workspace dir
656 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
657 # @param SectionName: Used for error report
658 # @param Line: The line in container file which defines the file to be checked
660 # @retval The file full path if the file exists
662 def CheckFileExist(WorkspaceDir
, CheckFilename
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
664 if CheckFilename
!= '' and CheckFilename
is not None:
665 CheckFile
= WorkspaceFile(WorkspaceDir
, CheckFilename
)
666 if not os
.path
.isfile(CheckFile
):
667 ContainerFile
= open(ContainerFilename
, 'r').read()
669 LineNo
= GetLineNo(ContainerFile
, Line
)
670 ErrorMsg
= "Can't find file '%s' defined in section '%s'" % (CheckFile
, SectionName
)
671 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
,
672 File
=ContainerFilename
, Line
=LineNo
, RaiseError
=EdkLogger
.IsRaiseError
)
678 # Find the index of a line in a file
680 # @param FileContent: Search scope
681 # @param Line: Search key
683 # @retval int Index of the line
684 # @retval -1 The line is not found
686 def GetLineNo(FileContent
, Line
, IsIgnoreComment
=True):
687 LineList
= FileContent
.splitlines()
688 for Index
in range(len(LineList
)):
689 if LineList
[Index
].find(Line
) > -1:
691 # Ignore statement in comment
694 if LineList
[Index
].strip()[0] == DataType
.TAB_COMMENT_SPLIT
:
702 # Raise a parser error
704 # @param Line: String which has error
705 # @param Section: Used for error report
706 # @param File: File which has the string
707 # @param Format: Correct format
709 def RaiseParserError(Line
, Section
, File
, Format
='', LineNo
= -1):
711 LineNo
= GetLineNo(open(os
.path
.normpath(File
), 'r').read(), Line
)
712 ErrorMsg
= "Invalid statement '%s' is found in section '%s'" % (Line
, Section
)
714 Format
= "Correct format is " + Format
715 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, File
=File
, Line
=LineNo
, ExtraData
=Format
, RaiseError
=EdkLogger
.IsRaiseError
)
719 # Return a full path with workspace dir
721 # @param WorkspaceDir: Workspace dir
722 # @param Filename: Relative file name
724 # @retval string A full path
726 def WorkspaceFile(WorkspaceDir
, Filename
):
727 return mws
.join(NormPath(WorkspaceDir
), NormPath(Filename
))
731 # Remove '"' which startswith and endswith string
733 # @param String: The string need to be split
735 # @retval String: The string after removed '""'
737 def SplitString(String
):
738 if String
.startswith('\"'):
740 if String
.endswith('\"'):
745 ## Convert To Sql String
747 # 1. Replace "'" with "''" in each item of StringList
749 # @param StringList: A list for strings to be converted
751 def ConvertToSqlString(StringList
):
752 return list(map(lambda s
: s
.replace("'", "''"), StringList
))
754 ## Convert To Sql String
756 # 1. Replace "'" with "''" in the String
758 # @param String: A String to be converted
760 def ConvertToSqlString2(String
):
761 return String
.replace("'", "''")
764 # Remove comment block
766 def RemoveBlockComment(Lines
):
767 IsFindBlockComment
= False
768 IsFindBlockCode
= False
775 # Remove comment block
777 if Line
.find(DataType
.TAB_COMMENT_EDK_START
) > -1:
778 ReservedLine
= GetSplitList(Line
, DataType
.TAB_COMMENT_EDK_START
, 1)[0]
779 IsFindBlockComment
= True
780 if Line
.find(DataType
.TAB_COMMENT_EDK_END
) > -1:
781 Line
= ReservedLine
+ GetSplitList(Line
, DataType
.TAB_COMMENT_EDK_END
, 1)[1]
783 IsFindBlockComment
= False
784 if IsFindBlockComment
:
788 NewLines
.append(Line
)
792 # Get String of a List
794 def GetStringOfList(List
, Split
=' '):
795 if not isinstance(List
, type([])):
799 Str
= Str
+ Item
+ Split
804 # Get HelpTextList from HelpTextClassList
806 def GetHelpTextList(HelpTextClassList
):
808 if HelpTextClassList
:
809 for HelpText
in HelpTextClassList
:
810 if HelpText
.String
.endswith('\n'):
811 HelpText
.String
= HelpText
.String
[0: len(HelpText
.String
) - len('\n')]
812 List
.extend(HelpText
.String
.split('\n'))
816 def StringToArray(String
):
817 if String
.startswith('L"'):
818 if String
== "L\"\"":
821 return "{%s,0x00,0x00}" % ",".join("0x%02x,0x00" % ord(C
) for C
in String
[2:-1])
822 elif String
.startswith('"'):
826 StringLen
= len(String
[1:-1])
828 return "{%s,0x00}" % ",".join("0x%02x" % ord(C
) for C
in String
[1:-1])
830 return "{%s,0x00,0x00}" % ",".join("0x%02x" % ord(C
) for C
in String
[1:-1])
831 elif String
.startswith('{'):
832 return "{%s}" % ",".join(C
.strip() for C
in String
[1:-1].split(','))
834 if len(String
.split()) % 2:
835 return '{%s,0}' % ','.join(String
.split())
837 return '{%s,0,0}' % ','.join(String
.split())
839 def StringArrayLength(String
):
840 if String
.startswith('L"'):
841 return (len(String
) - 3 + 1) * 2
842 elif String
.startswith('"'):
843 return (len(String
) - 2 + 1)
845 return len(String
.split()) + 1
847 def RemoveDupOption(OptionString
, Which
="/I", Against
=None):
848 OptionList
= OptionString
.split()
852 for Index
in range(len(OptionList
)):
853 Opt
= OptionList
[Index
]
854 if not Opt
.startswith(Which
):
856 if len(Opt
) > len(Which
):
857 Val
= Opt
[len(Which
):]
861 OptionList
[Index
] = ""
863 ValueList
.append(Val
)
864 return " ".join(OptionList
)
868 # This acts like the main() function for the script, unless it is 'import'ed into another
871 if __name__
== '__main__':