]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/String.py
2 # This file is used to define common string related functions used in parsing process
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.
19 import Common
.LongFilePathOs
as os
21 import EdkLogger
as EdkLogger
24 from BuildToolError
import *
25 from CommonDataClass
.Exceptions
import *
26 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
28 gHexVerPatt
= re
.compile('0x[a-f0-9]{4}[a-f0-9]{4}$', re
.IGNORECASE
)
29 gHumanReadableVerPatt
= re
.compile(r
'([1-9][0-9]*|0)\.[0-9]{1,2}$')
33 # Get a value list from a string with multiple values splited with SplitTag
34 # The default SplitTag is DataType.TAB_VALUE_SPLIT
35 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
37 # @param String: The input string to be splitted
38 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
39 # @param MaxSplit: The max number of split values, default is -1
41 # @retval list() A list for splitted string
43 def GetSplitValueList(String
, SplitTag
=DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
48 for Index
in range(0, len(String
)):
52 # Found a splitter not in a string, split it
53 if not InString
and Char
== SplitTag
:
54 ValueList
.append(String
[Last
:Index
].strip())
56 if MaxSplit
> 0 and len(ValueList
) >= MaxSplit
:
59 if Char
== '\\' and InString
:
69 if Last
< len(String
):
70 ValueList
.append(String
[Last
:].strip())
71 elif Last
== len(String
):
78 # Get a value list from a string with multiple values splited with SplitString
79 # The default SplitTag is DataType.TAB_VALUE_SPLIT
80 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
82 # @param String: The input string to be splitted
83 # @param SplitStr: The split key, default is DataType.TAB_VALUE_SPLIT
84 # @param MaxSplit: The max number of split values, default is -1
86 # @retval list() A list for splitted string
88 def GetSplitList(String
, SplitStr
=DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
89 return map(lambda l
: l
.strip(), String
.split(SplitStr
, MaxSplit
))
93 # Find a key's all arches in dict, add the new arch to the list
94 # If not exist any arch, set the arch directly
96 # @param Dict: The input value for Dict
97 # @param Key: The input value for Key
98 # @param Arch: The Arch to be added or merged
100 def MergeArches(Dict
, Key
, Arch
):
101 if Key
in Dict
.keys():
102 Dict
[Key
].append(Arch
)
104 Dict
[Key
] = Arch
.split()
108 # Parse a string with format "DEFINE <VarName> = <PATH>"
109 # Generate a map Defines[VarName] = PATH
110 # Return False if invalid format
112 # @param String: String with DEFINE statement
113 # @param Arch: Supportted Arch
114 # @param Defines: DEFINE statement to be parsed
116 # @retval 0 DEFINE statement found, and valid
117 # @retval 1 DEFINE statement found, but not valid
118 # @retval -1 DEFINE statement not found
120 def GenDefines(String
, Arch
, Defines
):
121 if String
.find(DataType
.TAB_DEFINE
+ ' ') > -1:
122 List
= String
.replace(DataType
.TAB_DEFINE
+ ' ', '').split(DataType
.TAB_EQUAL_SPLIT
)
124 Defines
[(CleanString(List
[0]), Arch
)] = CleanString(List
[1])
133 # Parse a string with format "!include <Filename>"
134 # Return the file path
135 # Return False if invalid format or NOT FOUND
137 # @param String: String with INCLUDE statement
138 # @param IncludeFiles: INCLUDE statement to be parsed
139 # @param Arch: Supportted Arch
144 def GenInclude(String
, IncludeFiles
, Arch
):
145 if String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') > -1:
146 IncludeFile
= CleanString(String
[String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') + len(DataType
.TAB_INCLUDE
+ ' ') : ])
147 MergeArches(IncludeFiles
, IncludeFile
, Arch
)
152 ## GetLibraryClassesWithModuleType
154 # Get Library Class definition when no module type defined
156 # @param Lines: The content to be parsed
157 # @param Key: Reserved
158 # @param KeyValues: To store data after parsing
159 # @param CommentCharacter: Comment char, used to ignore comment content
161 # @retval True Get library classes successfully
163 def GetLibraryClassesWithModuleType(Lines
, Key
, KeyValues
, CommentCharacter
):
164 newKey
= SplitModuleType(Key
)
165 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
166 LineList
= Lines
.splitlines()
167 for Line
in LineList
:
168 Line
= CleanString(Line
, CommentCharacter
)
169 if Line
!= '' and Line
[0] != CommentCharacter
:
170 KeyValues
.append([CleanString(Line
, CommentCharacter
), newKey
[1]])
178 # @param Lines: The content to be parsed
179 # @param Key: Reserved
180 # @param KeyValues: To store data after parsing
181 # @param CommentCharacter: Comment char, used to ignore comment content
183 # @retval True Get Dynamic Pcds successfully
185 def GetDynamics(Lines
, Key
, KeyValues
, CommentCharacter
):
187 # Get SkuId Name List
189 SkuIdNameList
= SplitModuleType(Key
)
191 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
192 LineList
= Lines
.splitlines()
193 for Line
in LineList
:
194 Line
= CleanString(Line
, CommentCharacter
)
195 if Line
!= '' and Line
[0] != CommentCharacter
:
196 KeyValues
.append([CleanString(Line
, CommentCharacter
), SkuIdNameList
[1]])
202 # Split ModuleType out of section defien to get key
203 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
205 # @param Key: String to be parsed
207 # @retval ReturnValue A list for module types
209 def SplitModuleType(Key
):
210 KeyList
= Key
.split(DataType
.TAB_SPLIT
)
216 # Fill in for moduletype
220 KeyValue
= KeyList
[0]
222 KeyValue
= KeyValue
+ DataType
.TAB_SPLIT
+ KeyList
[1]
223 ReturnValue
.append(KeyValue
)
224 ReturnValue
.append(GetSplitValueList(KeyList
[2]))
228 ## Replace macro in strings list
230 # This method replace macros used in a given string list. The macros are
231 # given in a dictionary.
233 # @param StringList StringList to be processed
234 # @param MacroDefinitions The macro definitions in the form of dictionary
235 # @param SelfReplacement To decide whether replace un-defined macro to ''
237 # @retval NewList A new string list whose macros are replaced
239 def ReplaceMacros(StringList
, MacroDefinitions
={}, SelfReplacement
=False):
241 for String
in StringList
:
242 if type(String
) == type(''):
243 NewList
.append(ReplaceMacro(String
, MacroDefinitions
, SelfReplacement
))
245 NewList
.append(String
)
249 ## Replace macro in string
251 # This method replace macros used in given string. The macros are given in a
254 # @param String String to be processed
255 # @param MacroDefinitions The macro definitions in the form of dictionary
256 # @param SelfReplacement To decide whether replace un-defined macro to ''
258 # @retval string The string whose macros are replaced
260 def ReplaceMacro(String
, MacroDefinitions
={}, SelfReplacement
=False, RaiseError
=False):
262 while String
and MacroDefinitions
:
263 MacroUsed
= GlobalData
.gMacroRefPattern
.findall(String
)
264 # no macro found in String, stop replacing
265 if len(MacroUsed
) == 0:
268 for Macro
in MacroUsed
:
269 if Macro
not in MacroDefinitions
:
271 raise SymbolNotFound("%s not defined" % Macro
)
273 String
= String
.replace("$(%s)" % Macro
, '')
275 String
= String
.replace("$(%s)" % Macro
, MacroDefinitions
[Macro
])
276 # in case there's macro not defined
277 if String
== LastString
:
285 # Create a normal path
286 # And replace DFEINE in the path
288 # @param Path: The input value for Path to be converted
289 # @param Defines: A set for DEFINE statement
291 # @retval Path Formatted path
293 def NormPath(Path
, Defines
={}):
294 IsRelativePath
= False
297 IsRelativePath
= True
299 # Replace with Define
302 Path
= ReplaceMacro(Path
, Defines
)
304 # To local path format
306 Path
= os
.path
.normpath(Path
)
308 if IsRelativePath
and Path
[0] != '.':
309 Path
= os
.path
.join('.', Path
)
315 # Remove comments in a string
318 # @param Line: The string to be cleaned
319 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
321 # @retval Path Formatted path
323 def CleanString(Line
, CommentCharacter
=DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False, BuildOption
=False):
329 # Replace Edk's comment character
331 if AllowCppStyleComment
:
332 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
334 # remove comments, but we should escape comment character in string
337 CommentInString
= False
338 for Index
in range(0, len(Line
)):
339 if Line
[Index
] == '"':
340 InString
= not InString
341 elif Line
[Index
] == CommentCharacter
and InString
:
342 CommentInString
= True
343 elif Line
[Index
] == CommentCharacter
and not InString
:
344 Line
= Line
[0: Index
]
347 if CommentInString
and BuildOption
:
348 Line
= Line
.replace('"', '')
349 ChIndex
= Line
.find('#')
351 if GlobalData
.gIsWindows
:
352 if ChIndex
== 0 or Line
[ChIndex
- 1] != '^':
353 Line
= Line
[0:ChIndex
] + '^' + Line
[ChIndex
:]
354 ChIndex
= Line
.find('#', ChIndex
+ 2)
356 ChIndex
= Line
.find('#', ChIndex
+ 1)
358 if ChIndex
== 0 or Line
[ChIndex
- 1] != '\\':
359 Line
= Line
[0:ChIndex
] + '\\' + Line
[ChIndex
:]
360 ChIndex
= Line
.find('#', ChIndex
+ 2)
362 ChIndex
= Line
.find('#', ChIndex
+ 1)
364 # remove whitespace again
372 # Split statement with comments in a string
375 # @param Line: The string to be cleaned
376 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
378 # @retval Path Formatted path
380 def CleanString2(Line
, CommentCharacter
=DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
386 # Replace Edk's comment character
388 if AllowCppStyleComment
:
389 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
391 # separate comments and statements, but we should escape comment character in string
394 CommentInString
= False
396 for Index
in range(0, len(Line
)):
397 if Line
[Index
] == '"':
398 InString
= not InString
399 elif Line
[Index
] == CommentCharacter
and InString
:
400 CommentInString
= True
401 elif Line
[Index
] == CommentCharacter
and not InString
:
402 Comment
= Line
[Index
:].strip()
403 Line
= Line
[0:Index
].strip()
408 ## GetMultipleValuesOfKeyFromLines
410 # Parse multiple strings to clean comment and spaces
411 # The result is saved to KeyValues
413 # @param Lines: The content to be parsed
414 # @param Key: Reserved
415 # @param KeyValues: To store data after parsing
416 # @param CommentCharacter: Comment char, used to ignore comment content
418 # @retval True Successfully executed
420 def GetMultipleValuesOfKeyFromLines(Lines
, Key
, KeyValues
, CommentCharacter
):
421 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
422 LineList
= Lines
.split('\n')
423 for Line
in LineList
:
424 Line
= CleanString(Line
, CommentCharacter
)
425 if Line
!= '' and Line
[0] != CommentCharacter
:
432 # Parse a DEFINE statement to get defined value
435 # @param String: The content to be parsed
436 # @param Key: The key of DEFINE statement
437 # @param CommentCharacter: Comment char, used to ignore comment content
439 # @retval string The defined value
441 def GetDefineValue(String
, Key
, CommentCharacter
):
442 String
= CleanString(String
)
443 return String
[String
.find(Key
+ ' ') + len(Key
+ ' ') : ]
447 # Get a Hex Version Value
449 # @param VerString: The version string to be parsed
452 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
453 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
454 # where mmmm is the major number and nnnn is the adjusted minor number.
456 def GetHexVerValue(VerString
):
457 VerString
= CleanString(VerString
)
459 if gHumanReadableVerPatt
.match(VerString
):
460 ValueList
= VerString
.split('.')
465 DeciValue
= (int(Major
) << 16) + int(Minor
);
466 return "0x%08x" % DeciValue
467 elif gHexVerPatt
.match(VerString
):
473 ## GetSingleValueOfKeyFromLines
475 # Parse multiple strings as below to get value of each definition line
478 # The result is saved to Dictionary
480 # @param Lines: The content to be parsed
481 # @param Dictionary: To store data after parsing
482 # @param CommentCharacter: Comment char, be used to ignore comment content
483 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
484 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
485 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
487 # @retval True Successfully executed
489 def GetSingleValueOfKeyFromLines(Lines
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
490 Lines
= Lines
.split('\n')
498 # Handle DEFINE and SPEC
500 if Line
.find(DataType
.TAB_INF_DEFINES_DEFINE
+ ' ') > -1:
501 if '' in DefineValues
:
502 DefineValues
.remove('')
503 DefineValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_DEFINE
, CommentCharacter
))
505 if Line
.find(DataType
.TAB_INF_DEFINES_SPEC
+ ' ') > -1:
507 SpecValues
.remove('')
508 SpecValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_SPEC
, CommentCharacter
))
514 LineList
= Line
.split(KeySplitCharacter
, 1)
515 if len(LineList
) >= 2:
516 Key
= LineList
[0].split()
517 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
:
519 # Remove comments and white spaces
521 LineList
[1] = CleanString(LineList
[1], CommentCharacter
)
523 Value
= map(string
.strip
, LineList
[1].split(ValueSplitCharacter
))
525 Value
= CleanString(LineList
[1], CommentCharacter
).splitlines()
527 if Key
[0] in Dictionary
:
528 if Key
[0] not in Keys
:
529 Dictionary
[Key
[0]] = Value
532 Dictionary
[Key
[0]].extend(Value
)
534 Dictionary
[DataType
.TAB_INF_DEFINES_MACRO
][Key
[0]] = Value
[0]
536 if DefineValues
== []:
540 Dictionary
[DataType
.TAB_INF_DEFINES_DEFINE
] = DefineValues
541 Dictionary
[DataType
.TAB_INF_DEFINES_SPEC
] = SpecValues
545 ## The content to be parsed
547 # Do pre-check for a file before it is parsed
551 # @param FileName: Used for error report
552 # @param FileContent: File content to be parsed
553 # @param SupSectionTag: Used for error report
555 def PreCheck(FileName
, FileContent
, SupSectionTag
):
559 for Line
in FileContent
.splitlines():
564 Line
= CleanString(Line
)
567 # Remove commented line
569 if Line
.find(DataType
.TAB_COMMA_SPLIT
) == 0:
574 if Line
.find('$') > -1:
575 if Line
.find('$(') < 0 or Line
.find(')') < 0:
576 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
581 if Line
.find('[') > -1 or Line
.find(']') > -1:
583 # Only get one '[' or one ']'
585 if not (Line
.find('[') > -1 and Line
.find(']') > -1):
586 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
589 # Regenerate FileContent
591 NewFileContent
= NewFileContent
+ Line
+ '\r\n'
594 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
596 return NewFileContent
600 # Check if the Filename is including ExtName
601 # Return True if it exists
602 # Raise a error message if it not exists
604 # @param CheckFilename: Name of the file to be checked
605 # @param ExtName: Ext name of the file to be checked
606 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
607 # @param SectionName: Used for error report
608 # @param Line: The line in container file which defines the file to be checked
610 # @retval True The file type is correct
612 def CheckFileType(CheckFilename
, ExtName
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
613 if CheckFilename
!= '' and CheckFilename
!= None:
614 (Root
, Ext
) = os
.path
.splitext(CheckFilename
)
615 if Ext
.upper() != ExtName
.upper():
616 ContainerFile
= open(ContainerFilename
, 'r').read()
618 LineNo
= GetLineNo(ContainerFile
, Line
)
619 ErrorMsg
= "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName
, CheckFilename
, ExtName
)
620 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, Line
=LineNo
,
621 File
=ContainerFilename
, RaiseError
=EdkLogger
.IsRaiseError
)
627 # Check if the file exists
628 # Return True if it exists
629 # Raise a error message if it not exists
631 # @param CheckFilename: Name of the file to be checked
632 # @param WorkspaceDir: Current workspace dir
633 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
634 # @param SectionName: Used for error report
635 # @param Line: The line in container file which defines the file to be checked
637 # @retval The file full path if the file exists
639 def CheckFileExist(WorkspaceDir
, CheckFilename
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
641 if CheckFilename
!= '' and CheckFilename
!= None:
642 CheckFile
= WorkspaceFile(WorkspaceDir
, CheckFilename
)
643 if not os
.path
.isfile(CheckFile
):
644 ContainerFile
= open(ContainerFilename
, 'r').read()
646 LineNo
= GetLineNo(ContainerFile
, Line
)
647 ErrorMsg
= "Can't find file '%s' defined in section '%s'" % (CheckFile
, SectionName
)
648 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
,
649 File
=ContainerFilename
, Line
=LineNo
, RaiseError
=EdkLogger
.IsRaiseError
)
655 # Find the index of a line in a file
657 # @param FileContent: Search scope
658 # @param Line: Search key
660 # @retval int Index of the line
661 # @retval -1 The line is not found
663 def GetLineNo(FileContent
, Line
, IsIgnoreComment
=True):
664 LineList
= FileContent
.splitlines()
665 for Index
in range(len(LineList
)):
666 if LineList
[Index
].find(Line
) > -1:
668 # Ignore statement in comment
671 if LineList
[Index
].strip()[0] == DataType
.TAB_COMMENT_SPLIT
:
679 # Raise a parser error
681 # @param Line: String which has error
682 # @param Section: Used for error report
683 # @param File: File which has the string
684 # @param Format: Correct format
686 def RaiseParserError(Line
, Section
, File
, Format
='', LineNo
= -1):
688 LineNo
= GetLineNo(open(os
.path
.normpath(File
), 'r').read(), Line
)
689 ErrorMsg
= "Invalid statement '%s' is found in section '%s'" % (Line
, Section
)
691 Format
= "Correct format is " + Format
692 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, File
=File
, Line
=LineNo
, ExtraData
=Format
, RaiseError
=EdkLogger
.IsRaiseError
)
696 # Return a full path with workspace dir
698 # @param WorkspaceDir: Workspace dir
699 # @param Filename: Relative file name
701 # @retval string A full path
703 def WorkspaceFile(WorkspaceDir
, Filename
):
704 return os
.path
.join(NormPath(WorkspaceDir
), NormPath(Filename
))
708 # Revmove '"' which startswith and endswith string
710 # @param String: The string need to be splited
712 # @retval String: The string after removed '""'
714 def SplitString(String
):
715 if String
.startswith('\"'):
717 if String
.endswith('\"'):
722 ## Convert To Sql String
724 # 1. Replace "'" with "''" in each item of StringList
726 # @param StringList: A list for strings to be converted
728 def ConvertToSqlString(StringList
):
729 return map(lambda s
: s
.replace("'", "''") , StringList
)
731 ## Convert To Sql String
733 # 1. Replace "'" with "''" in the String
735 # @param String: A String to be converted
737 def ConvertToSqlString2(String
):
738 return String
.replace("'", "''")
741 # Remove comment block
743 def RemoveBlockComment(Lines
):
744 IsFindBlockComment
= False
745 IsFindBlockCode
= False
752 # Remove comment block
754 if Line
.find(DataType
.TAB_COMMENT_EDK_START
) > -1:
755 ReservedLine
= GetSplitList(Line
, DataType
.TAB_COMMENT_EDK_START
, 1)[0]
756 IsFindBlockComment
= True
757 if Line
.find(DataType
.TAB_COMMENT_EDK_END
) > -1:
758 Line
= ReservedLine
+ GetSplitList(Line
, DataType
.TAB_COMMENT_EDK_END
, 1)[1]
760 IsFindBlockComment
= False
761 if IsFindBlockComment
:
765 NewLines
.append(Line
)
769 # Get String of a List
771 def GetStringOfList(List
, Split
=' '):
772 if type(List
) != type([]):
776 Str
= Str
+ Item
+ Split
781 # Get HelpTextList from HelpTextClassList
783 def GetHelpTextList(HelpTextClassList
):
785 if HelpTextClassList
:
786 for HelpText
in HelpTextClassList
:
787 if HelpText
.String
.endswith('\n'):
788 HelpText
.String
= HelpText
.String
[0: len(HelpText
.String
) - len('\n')]
789 List
.extend(HelpText
.String
.split('\n'))
793 def StringToArray(String
):
794 if isinstance(String
, unicode):
795 if len(unicode) == 0:
796 return "{0x00, 0x00}"
797 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
])
798 elif String
.startswith('L"'):
799 if String
== "L\"\"":
800 return "{0x00, 0x00}"
802 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
[2:-1]])
803 elif String
.startswith('"'):
807 StringLen
= len(String
[1:-1])
809 return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C
) for C
in String
[1:-1]])
811 return "{%s, 0x00,0x00}" % ", ".join(["0x%02x" % ord(C
) for C
in String
[1:-1]])
812 elif String
.startswith('{'):
813 StringLen
= len(String
.split(","))
815 return "{%s, 0x00}" % ", ".join([ C
for C
in String
[1:-1].split(',')])
817 return "{%s}" % ", ".join([ C
for C
in String
[1:-1].split(',')])
820 if len(String
.split()) % 2:
821 return '{%s, 0}' % ', '.join(String
.split())
823 return '{%s, 0,0}' % ', '.join(String
.split())
825 def StringArrayLength(String
):
826 if isinstance(String
, unicode):
827 return (len(String
) + 1) * 2 + 1;
828 elif String
.startswith('L"'):
829 return (len(String
) - 3 + 1) * 2
830 elif String
.startswith('"'):
831 return (len(String
) - 2 + 1)
833 return len(String
.split()) + 1
835 def RemoveDupOption(OptionString
, Which
="/I", Against
=None):
836 OptionList
= OptionString
.split()
840 for Index
in range(len(OptionList
)):
841 Opt
= OptionList
[Index
]
842 if not Opt
.startswith(Which
):
844 if len(Opt
) > len(Which
):
845 Val
= Opt
[len(Which
):]
849 OptionList
[Index
] = ""
851 ValueList
.append(Val
)
852 return " ".join(OptionList
)
856 # This acts like the main() function for the script, unless it is 'import'ed into another
859 if __name__
== '__main__':