]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/String.py
Sync BaseTools Branch (version r2321) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / Common / String.py
1 ## @file
2 # This file is used to define common string related functions used in parsing process
3 #
4 # Copyright (c) 2007 - 2008, 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
9 #
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.
12 #
13
14 ##
15 # Import Modules
16 #
17 import re
18 import DataType
19 import os.path
20 import string
21 import EdkLogger as EdkLogger
22
23 import GlobalData
24 from BuildToolError import *
25
26 gHexVerPatt = re.compile('0x[a-f0-9]{4}[a-f0-9]{4}$',re.IGNORECASE)
27 gHumanReadableVerPatt = re.compile(r'([1-9][0-9]*|0)\.[0-9]{1,2}$')
28
29 ## GetSplitValueList
30 #
31 # Get a value list from a string with multiple values splited with SplitTag
32 # The default SplitTag is DataType.TAB_VALUE_SPLIT
33 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
34 #
35 # @param String: The input string to be splitted
36 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
37 # @param MaxSplit: The max number of split values, default is -1
38 #
39 # @retval list() A list for splitted string
40 #
41 def GetSplitValueList(String, SplitTag = DataType.TAB_VALUE_SPLIT, MaxSplit = -1):
42 return map(lambda l: l.strip(), String.split(SplitTag, MaxSplit))
43
44 ## MergeArches
45 #
46 # Find a key's all arches in dict, add the new arch to the list
47 # If not exist any arch, set the arch directly
48 #
49 # @param Dict: The input value for Dict
50 # @param Key: The input value for Key
51 # @param Arch: The Arch to be added or merged
52 #
53 def MergeArches(Dict, Key, Arch):
54 if Key in Dict.keys():
55 Dict[Key].append(Arch)
56 else:
57 Dict[Key] = Arch.split()
58
59 ## GenDefines
60 #
61 # Parse a string with format "DEFINE <VarName> = <PATH>"
62 # Generate a map Defines[VarName] = PATH
63 # Return False if invalid format
64 #
65 # @param String: String with DEFINE statement
66 # @param Arch: Supportted Arch
67 # @param Defines: DEFINE statement to be parsed
68 #
69 # @retval 0 DEFINE statement found, and valid
70 # @retval 1 DEFINE statement found, but not valid
71 # @retval -1 DEFINE statement not found
72 #
73 def GenDefines(String, Arch, Defines):
74 if String.find(DataType.TAB_DEFINE + ' ') > -1:
75 List = String.replace(DataType.TAB_DEFINE + ' ', '').split(DataType.TAB_EQUAL_SPLIT)
76 if len(List) == 2:
77 Defines[(CleanString(List[0]), Arch)] = CleanString(List[1])
78 return 0
79 else:
80 return -1
81
82 return 1
83
84 ## GenInclude
85 #
86 # Parse a string with format "!include <Filename>"
87 # Return the file path
88 # Return False if invalid format or NOT FOUND
89 #
90 # @param String: String with INCLUDE statement
91 # @param IncludeFiles: INCLUDE statement to be parsed
92 # @param Arch: Supportted Arch
93 #
94 # @retval True
95 # @retval False
96 #
97 def GenInclude(String, IncludeFiles, Arch):
98 if String.upper().find(DataType.TAB_INCLUDE.upper() + ' ') > -1:
99 IncludeFile = CleanString(String[String.upper().find(DataType.TAB_INCLUDE.upper() + ' ') + len(DataType.TAB_INCLUDE + ' ') : ])
100 MergeArches(IncludeFiles, IncludeFile, Arch)
101 return True
102 else:
103 return False
104
105 ## GetLibraryClassesWithModuleType
106 #
107 # Get Library Class definition when no module type defined
108 #
109 # @param Lines: The content to be parsed
110 # @param Key: Reserved
111 # @param KeyValues: To store data after parsing
112 # @param CommentCharacter: Comment char, used to ignore comment content
113 #
114 # @retval True Get library classes successfully
115 #
116 def GetLibraryClassesWithModuleType(Lines, Key, KeyValues, CommentCharacter):
117 newKey = SplitModuleType(Key)
118 Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]
119 LineList = Lines.splitlines()
120 for Line in LineList:
121 Line = CleanString(Line, CommentCharacter)
122 if Line != '' and Line[0] != CommentCharacter:
123 KeyValues.append([CleanString(Line, CommentCharacter), newKey[1]])
124
125 return True
126
127 ## GetDynamics
128 #
129 # Get Dynamic Pcds
130 #
131 # @param Lines: The content to be parsed
132 # @param Key: Reserved
133 # @param KeyValues: To store data after parsing
134 # @param CommentCharacter: Comment char, used to ignore comment content
135 #
136 # @retval True Get Dynamic Pcds successfully
137 #
138 def GetDynamics(Lines, Key, KeyValues, CommentCharacter):
139 #
140 # Get SkuId Name List
141 #
142 SkuIdNameList = SplitModuleType(Key)
143
144 Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]
145 LineList = Lines.splitlines()
146 for Line in LineList:
147 Line = CleanString(Line, CommentCharacter)
148 if Line != '' and Line[0] != CommentCharacter:
149 KeyValues.append([CleanString(Line, CommentCharacter), SkuIdNameList[1]])
150
151 return True
152
153 ## SplitModuleType
154 #
155 # Split ModuleType out of section defien to get key
156 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
157 #
158 # @param Key: String to be parsed
159 #
160 # @retval ReturnValue A list for module types
161 #
162 def SplitModuleType(Key):
163 KeyList = Key.split(DataType.TAB_SPLIT)
164 #
165 # Fill in for arch
166 #
167 KeyList.append('')
168 #
169 # Fill in for moduletype
170 #
171 KeyList.append('')
172 ReturnValue = []
173 KeyValue = KeyList[0]
174 if KeyList[1] != '':
175 KeyValue = KeyValue + DataType.TAB_SPLIT + KeyList[1]
176 ReturnValue.append(KeyValue)
177 ReturnValue.append(GetSplitValueList(KeyList[2]))
178
179 return ReturnValue
180
181 ## Replace macro in strings list
182 #
183 # This method replace macros used in a given string list. The macros are
184 # given in a dictionary.
185 #
186 # @param StringList StringList to be processed
187 # @param MacroDefinitions The macro definitions in the form of dictionary
188 # @param SelfReplacement To decide whether replace un-defined macro to ''
189 #
190 # @retval NewList A new string list whose macros are replaced
191 #
192 def ReplaceMacros(StringList, MacroDefinitions={}, SelfReplacement = False):
193 NewList = []
194 for String in StringList:
195 if type(String) == type(''):
196 NewList.append(ReplaceMacro(String, MacroDefinitions, SelfReplacement))
197 else:
198 NewList.append(String)
199
200 return NewList
201
202 ## Replace macro in string
203 #
204 # This method replace macros used in given string. The macros are given in a
205 # dictionary.
206 #
207 # @param String String to be processed
208 # @param MacroDefinitions The macro definitions in the form of dictionary
209 # @param SelfReplacement To decide whether replace un-defined macro to ''
210 #
211 # @retval string The string whose macros are replaced
212 #
213 def ReplaceMacro(String, MacroDefinitions={}, SelfReplacement = False):
214 LastString = String
215 while MacroDefinitions:
216 MacroUsed = GlobalData.gMacroPattern.findall(String)
217 # no macro found in String, stop replacing
218 if len(MacroUsed) == 0:
219 break
220
221 for Macro in MacroUsed:
222 if Macro not in MacroDefinitions:
223 if SelfReplacement:
224 String = String.replace("$(%s)" % Macro, '')
225 continue
226 String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro])
227 # in case there's macro not defined
228 if String == LastString:
229 break
230 LastString = String
231
232 return String
233
234 ## NormPath
235 #
236 # Create a normal path
237 # And replace DFEINE in the path
238 #
239 # @param Path: The input value for Path to be converted
240 # @param Defines: A set for DEFINE statement
241 #
242 # @retval Path Formatted path
243 #
244 def NormPath(Path, Defines = {}):
245 IsRelativePath = False
246 if Path:
247 if Path[0] == '.':
248 IsRelativePath = True
249 #
250 # Replace with Define
251 #
252 if Defines:
253 Path = ReplaceMacro(Path, Defines)
254 #
255 # To local path format
256 #
257 Path = os.path.normpath(Path)
258
259 if IsRelativePath and Path[0] != '.':
260 Path = os.path.join('.', Path)
261
262 return Path
263
264 ## CleanString
265 #
266 # Remove comments in a string
267 # Remove spaces
268 #
269 # @param Line: The string to be cleaned
270 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
271 #
272 # @retval Path Formatted path
273 #
274 def CleanString(Line, CommentCharacter = DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):
275 #
276 # remove whitespace
277 #
278 Line = Line.strip();
279 #
280 # Replace Edk's comment character
281 #
282 if AllowCppStyleComment:
283 Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)
284 #
285 # remove comments, but we should escape comment character in string
286 #
287 InString = False
288 CommentInString = False
289 for Index in range(0, len(Line)):
290 if Line[Index] == '"':
291 InString = not InString
292 elif Line[Index] == CommentCharacter and InString :
293 CommentInString = True
294 elif Line[Index] == CommentCharacter and not InString :
295 Line = Line[0: Index]
296 break
297
298 if CommentInString:
299 Line = Line.replace('"', '')
300 ChIndex = Line.find('#')
301 while ChIndex >= 0:
302 if GlobalData.gIsWindows:
303 if ChIndex == 0 or Line[ChIndex-1] != '^':
304 Line = Line[0:ChIndex] + '^' + Line[ChIndex:]
305 ChIndex = Line.find('#', ChIndex + 2)
306 else:
307 ChIndex = Line.find('#', ChIndex + 1)
308 else:
309 if ChIndex == 0 or Line[ChIndex-1] != '\\':
310 Line = Line[0:ChIndex] + '\\' + Line[ChIndex:]
311 ChIndex = Line.find('#', ChIndex + 2)
312 else:
313 ChIndex = Line.find('#', ChIndex + 1)
314 #
315 # remove whitespace again
316 #
317 Line = Line.strip();
318
319 return Line
320
321 ## CleanString2
322 #
323 # Split comments in a string
324 # Remove spaces
325 #
326 # @param Line: The string to be cleaned
327 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
328 #
329 # @retval Path Formatted path
330 #
331 def CleanString2(Line, CommentCharacter = DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):
332 #
333 # remove whitespace
334 #
335 Line = Line.strip();
336 #
337 # Replace Edk's comment character
338 #
339 if AllowCppStyleComment:
340 Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)
341 #
342 # separate comments and statements
343 #
344 LineParts = Line.split(CommentCharacter, 1);
345 #
346 # remove whitespace again
347 #
348 Line = LineParts[0].strip();
349 if len(LineParts) > 1:
350 Comment = LineParts[1].strip()
351 # Remove prefixed and trailing comment characters
352 Start = 0
353 End = len(Comment)
354 while Start < End and Comment.startswith(CommentCharacter, Start, End):
355 Start += 1
356 while End >= 0 and Comment.endswith(CommentCharacter, Start, End):
357 End -= 1
358 Comment = Comment[Start:End]
359 Comment = Comment.strip()
360 else:
361 Comment = ''
362
363 return Line, Comment
364
365 ## GetMultipleValuesOfKeyFromLines
366 #
367 # Parse multiple strings to clean comment and spaces
368 # The result is saved to KeyValues
369 #
370 # @param Lines: The content to be parsed
371 # @param Key: Reserved
372 # @param KeyValues: To store data after parsing
373 # @param CommentCharacter: Comment char, used to ignore comment content
374 #
375 # @retval True Successfully executed
376 #
377 def GetMultipleValuesOfKeyFromLines(Lines, Key, KeyValues, CommentCharacter):
378 Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]
379 LineList = Lines.split('\n')
380 for Line in LineList:
381 Line = CleanString(Line, CommentCharacter)
382 if Line != '' and Line[0] != CommentCharacter:
383 KeyValues += [Line]
384
385 return True
386
387 ## GetDefineValue
388 #
389 # Parse a DEFINE statement to get defined value
390 # DEFINE Key Value
391 #
392 # @param String: The content to be parsed
393 # @param Key: The key of DEFINE statement
394 # @param CommentCharacter: Comment char, used to ignore comment content
395 #
396 # @retval string The defined value
397 #
398 def GetDefineValue(String, Key, CommentCharacter):
399 String = CleanString(String)
400 return String[String.find(Key + ' ') + len(Key + ' ') : ]
401
402 ## GetHexVerValue
403 #
404 # Get a Hex Version Value
405 #
406 # @param VerString: The version string to be parsed
407 #
408 #
409 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
410 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
411 # where mmmm is the major number and nnnn is the adjusted minor number.
412 #
413 def GetHexVerValue(VerString):
414 VerString = CleanString(VerString)
415
416 if gHumanReadableVerPatt.match(VerString):
417 ValueList = VerString.split('.')
418 Major = ValueList[0]
419 Minor = ValueList[1]
420 if len(Minor) == 1:
421 Minor += '0'
422 DeciValue = (int(Major) << 16) + int(Minor);
423 return "0x%08x"%DeciValue
424 elif gHexVerPatt.match(VerString):
425 return VerString
426 else:
427 return None
428
429
430 ## GetSingleValueOfKeyFromLines
431 #
432 # Parse multiple strings as below to get value of each definition line
433 # Key1 = Value1
434 # Key2 = Value2
435 # The result is saved to Dictionary
436 #
437 # @param Lines: The content to be parsed
438 # @param Dictionary: To store data after parsing
439 # @param CommentCharacter: Comment char, be used to ignore comment content
440 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
441 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
442 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
443 #
444 # @retval True Successfully executed
445 #
446 def GetSingleValueOfKeyFromLines(Lines, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
447 Lines = Lines.split('\n')
448 Keys = []
449 Value = ''
450 DefineValues = ['']
451 SpecValues = ['']
452
453 for Line in Lines:
454 #
455 # Handle DEFINE and SPEC
456 #
457 if Line.find(DataType.TAB_INF_DEFINES_DEFINE + ' ') > -1:
458 if '' in DefineValues:
459 DefineValues.remove('')
460 DefineValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_DEFINE, CommentCharacter))
461 continue
462 if Line.find(DataType.TAB_INF_DEFINES_SPEC + ' ') > -1:
463 if '' in SpecValues:
464 SpecValues.remove('')
465 SpecValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_SPEC, CommentCharacter))
466 continue
467
468 #
469 # Handle Others
470 #
471 LineList = Line.split(KeySplitCharacter, 1)
472 if len(LineList) >= 2:
473 Key = LineList[0].split()
474 if len(Key) == 1 and Key[0][0] != CommentCharacter:
475 #
476 # Remove comments and white spaces
477 #
478 LineList[1] = CleanString(LineList[1], CommentCharacter)
479 if ValueSplitFlag:
480 Value = map(string.strip, LineList[1].split(ValueSplitCharacter))
481 else:
482 Value = CleanString(LineList[1], CommentCharacter).splitlines()
483
484 if Key[0] in Dictionary:
485 if Key[0] not in Keys:
486 Dictionary[Key[0]] = Value
487 Keys.append(Key[0])
488 else:
489 Dictionary[Key[0]].extend(Value)
490 else:
491 Dictionary[DataType.TAB_INF_DEFINES_MACRO][Key[0]] = Value[0]
492
493 if DefineValues == []:
494 DefineValues = ['']
495 if SpecValues == []:
496 SpecValues = ['']
497 Dictionary[DataType.TAB_INF_DEFINES_DEFINE] = DefineValues
498 Dictionary[DataType.TAB_INF_DEFINES_SPEC] = SpecValues
499
500 return True
501
502 ## The content to be parsed
503 #
504 # Do pre-check for a file before it is parsed
505 # Check $()
506 # Check []
507 #
508 # @param FileName: Used for error report
509 # @param FileContent: File content to be parsed
510 # @param SupSectionTag: Used for error report
511 #
512 def PreCheck(FileName, FileContent, SupSectionTag):
513 LineNo = 0
514 IsFailed = False
515 NewFileContent = ''
516 for Line in FileContent.splitlines():
517 LineNo = LineNo + 1
518 #
519 # Clean current line
520 #
521 Line = CleanString(Line)
522
523 #
524 # Remove commented line
525 #
526 if Line.find(DataType.TAB_COMMA_SPLIT) == 0:
527 Line = ''
528 #
529 # Check $()
530 #
531 if Line.find('$') > -1:
532 if Line.find('$(') < 0 or Line.find(')') < 0:
533 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError = EdkLogger.IsRaiseError)
534
535 #
536 # Check []
537 #
538 if Line.find('[') > -1 or Line.find(']') > -1:
539 #
540 # Only get one '[' or one ']'
541 #
542 if not (Line.find('[') > -1 and Line.find(']') > -1):
543 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError = EdkLogger.IsRaiseError)
544
545 #
546 # Regenerate FileContent
547 #
548 NewFileContent = NewFileContent + Line + '\r\n'
549
550 if IsFailed:
551 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError = EdkLogger.IsRaiseError)
552
553 return NewFileContent
554
555 ## CheckFileType
556 #
557 # Check if the Filename is including ExtName
558 # Return True if it exists
559 # Raise a error message if it not exists
560 #
561 # @param CheckFilename: Name of the file to be checked
562 # @param ExtName: Ext name of the file to be checked
563 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
564 # @param SectionName: Used for error report
565 # @param Line: The line in container file which defines the file to be checked
566 #
567 # @retval True The file type is correct
568 #
569 def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, LineNo = -1):
570 if CheckFilename != '' and CheckFilename != None:
571 (Root, Ext) = os.path.splitext(CheckFilename)
572 if Ext.upper() != ExtName.upper():
573 ContainerFile = open(ContainerFilename, 'r').read()
574 if LineNo == -1:
575 LineNo = GetLineNo(ContainerFile, Line)
576 ErrorMsg = "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName, CheckFilename, ExtName)
577 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, Line=LineNo,
578 File=ContainerFilename, RaiseError = EdkLogger.IsRaiseError)
579
580 return True
581
582 ## CheckFileExist
583 #
584 # Check if the file exists
585 # Return True if it exists
586 # Raise a error message if it not exists
587 #
588 # @param CheckFilename: Name of the file to be checked
589 # @param WorkspaceDir: Current workspace dir
590 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
591 # @param SectionName: Used for error report
592 # @param Line: The line in container file which defines the file to be checked
593 #
594 # @retval The file full path if the file exists
595 #
596 def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo = -1):
597 CheckFile = ''
598 if CheckFilename != '' and CheckFilename != None:
599 CheckFile = WorkspaceFile(WorkspaceDir, CheckFilename)
600 if not os.path.isfile(CheckFile):
601 ContainerFile = open(ContainerFilename, 'r').read()
602 if LineNo == -1:
603 LineNo = GetLineNo(ContainerFile, Line)
604 ErrorMsg = "Can't find file '%s' defined in section '%s'" % (CheckFile, SectionName)
605 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg,
606 File=ContainerFilename, Line = LineNo, RaiseError = EdkLogger.IsRaiseError)
607
608 return CheckFile
609
610 ## GetLineNo
611 #
612 # Find the index of a line in a file
613 #
614 # @param FileContent: Search scope
615 # @param Line: Search key
616 #
617 # @retval int Index of the line
618 # @retval -1 The line is not found
619 #
620 def GetLineNo(FileContent, Line, IsIgnoreComment = True):
621 LineList = FileContent.splitlines()
622 for Index in range(len(LineList)):
623 if LineList[Index].find(Line) > -1:
624 #
625 # Ignore statement in comment
626 #
627 if IsIgnoreComment:
628 if LineList[Index].strip()[0] == DataType.TAB_COMMENT_SPLIT:
629 continue
630 return Index + 1
631
632 return -1
633
634 ## RaiseParserError
635 #
636 # Raise a parser error
637 #
638 # @param Line: String which has error
639 # @param Section: Used for error report
640 # @param File: File which has the string
641 # @param Format: Correct format
642 #
643 def RaiseParserError(Line, Section, File, Format = '', LineNo = -1):
644 if LineNo == -1:
645 LineNo = GetLineNo(open(os.path.normpath(File), 'r').read(), Line)
646 ErrorMsg = "Invalid statement '%s' is found in section '%s'" % (Line, Section)
647 if Format != '':
648 Format = "Correct format is " + Format
649 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, File=File, Line=LineNo, ExtraData=Format, RaiseError = EdkLogger.IsRaiseError)
650
651 ## WorkspaceFile
652 #
653 # Return a full path with workspace dir
654 #
655 # @param WorkspaceDir: Workspace dir
656 # @param Filename: Relative file name
657 #
658 # @retval string A full path
659 #
660 def WorkspaceFile(WorkspaceDir, Filename):
661 return os.path.join(NormPath(WorkspaceDir), NormPath(Filename))
662
663 ## Split string
664 #
665 # Revmove '"' which startswith and endswith string
666 #
667 # @param String: The string need to be splited
668 #
669 # @retval String: The string after removed '""'
670 #
671 def SplitString(String):
672 if String.startswith('\"'):
673 String = String[1:]
674 if String.endswith('\"'):
675 String = String[:-1]
676
677 return String
678
679 ## Convert To Sql String
680 #
681 # 1. Replace "'" with "''" in each item of StringList
682 #
683 # @param StringList: A list for strings to be converted
684 #
685 def ConvertToSqlString(StringList):
686 return map(lambda s: s.replace("'", "''") , StringList)
687
688 ## Convert To Sql String
689 #
690 # 1. Replace "'" with "''" in the String
691 #
692 # @param String: A String to be converted
693 #
694 def ConvertToSqlString2(String):
695 return String.replace("'", "''")
696
697 #
698 # Remove comment block
699 #
700 def RemoveBlockComment(Lines):
701 IsFindBlockComment = False
702 IsFindBlockCode = False
703 ReservedLine = ''
704 NewLines = []
705
706 for Line in Lines:
707 Line = Line.strip()
708 #
709 # Remove comment block
710 #
711 if Line.find(DataType.TAB_COMMENT_EDK_START) > -1:
712 ReservedLine = GetSplitValueList(Line, DataType.TAB_COMMENT_EDK_START, 1)[0]
713 IsFindBlockComment = True
714 if Line.find(DataType.TAB_COMMENT_EDK_END) > -1:
715 Line = ReservedLine + GetSplitValueList(Line, DataType.TAB_COMMENT_EDK_END, 1)[1]
716 ReservedLine = ''
717 IsFindBlockComment = False
718 if IsFindBlockComment:
719 NewLines.append('')
720 continue
721
722 NewLines.append(Line)
723 return NewLines
724
725 #
726 # Get String of a List
727 #
728 def GetStringOfList(List, Split = ' '):
729 if type(List) != type([]):
730 return List
731 Str = ''
732 for Item in List:
733 Str = Str + Item + Split
734
735 return Str.strip()
736
737 #
738 # Get HelpTextList from HelpTextClassList
739 #
740 def GetHelpTextList(HelpTextClassList):
741 List = []
742 if HelpTextClassList:
743 for HelpText in HelpTextClassList:
744 if HelpText.String.endswith('\n'):
745 HelpText.String = HelpText.String[0: len(HelpText.String) - len('\n')]
746 List.extend(HelpText.String.split('\n'))
747
748 return List
749
750 def StringToArray(String):
751 if isinstance(String, unicode):
752 if len(unicode) ==0:
753 return "{0x00, 0x00}"
754 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String])
755 elif String.startswith('L"'):
756 if String == "L\"\"":
757 return "{0x00, 0x00}"
758 else:
759 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String[2:-1]])
760 elif String.startswith('"'):
761 if String == "\"\"":
762 return "{0x00}";
763 else:
764 return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])
765 else:
766 return '{%s, 0}' % ', '.join(String.split())
767
768 def StringArrayLength(String):
769 if isinstance(String, unicode):
770 return (len(String) + 1) * 2 + 1;
771 elif String.startswith('L"'):
772 return (len(String) - 3 + 1) * 2
773 elif String.startswith('"'):
774 return (len(String) - 2 + 1)
775 else:
776 return len(String.split()) + 1
777
778 def RemoveDupOption(OptionString, Which="/I", Against=None):
779 OptionList = OptionString.split()
780 ValueList = []
781 if Against:
782 ValueList += Against
783 for Index in range(len(OptionList)):
784 Opt = OptionList[Index]
785 if not Opt.startswith(Which):
786 continue
787 if len(Opt) > len(Which):
788 Val = Opt[len(Which):]
789 else:
790 Val = ""
791 if Val in ValueList:
792 OptionList[Index] = ""
793 else:
794 ValueList.append(Val)
795 return " ".join(OptionList)
796
797 ##
798 #
799 # This acts like the main() function for the script, unless it is 'import'ed into another
800 # script.
801 #
802 if __name__ == '__main__':
803 pass
804