]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/StrGather.py
BaseTools: Handle the bytes and str difference
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / StrGather.py
1 ## @file
2 # This file is used to parse a strings file and create or add to a string database
3 # file.
4 #
5 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 ##
15 # Import Modules
16 #
17 from __future__ import absolute_import
18 import re
19 import Common.EdkLogger as EdkLogger
20 from Common.BuildToolError import *
21 from .UniClassObject import *
22 from io import BytesIO
23 from struct import pack, unpack
24 from Common.LongFilePathSupport import OpenLongFilePath as open
25
26 ##
27 # Static definitions
28 #
29 EFI_HII_SIBT_END = '0x00'
30 EFI_HII_SIBT_STRING_SCSU = '0x10'
31 EFI_HII_SIBT_STRING_SCSU_FONT = '0x11'
32 EFI_HII_SIBT_STRINGS_SCSU = '0x12'
33 EFI_HII_SIBT_STRINGS_SCSU_FONT = '0x13'
34 EFI_HII_SIBT_STRING_UCS2 = '0x14'
35 EFI_HII_SIBT_STRING_UCS2_FONT = '0x15'
36 EFI_HII_SIBT_STRINGS_UCS2 = '0x16'
37 EFI_HII_SIBT_STRINGS_UCS2_FONT = '0x17'
38 EFI_HII_SIBT_DUPLICATE = '0x20'
39 EFI_HII_SIBT_SKIP2 = '0x21'
40 EFI_HII_SIBT_SKIP1 = '0x22'
41 EFI_HII_SIBT_EXT1 = '0x30'
42 EFI_HII_SIBT_EXT2 = '0x31'
43 EFI_HII_SIBT_EXT4 = '0x32'
44 EFI_HII_SIBT_FONT = '0x40'
45
46 EFI_HII_PACKAGE_STRINGS = '0x04'
47 EFI_HII_PACKAGE_FORM = '0x02'
48
49 StringPackageType = EFI_HII_PACKAGE_STRINGS
50 StringPackageForm = EFI_HII_PACKAGE_FORM
51 StringBlockType = EFI_HII_SIBT_STRING_UCS2
52 StringSkipType = EFI_HII_SIBT_SKIP2
53
54 HexHeader = '0x'
55
56 COMMENT = '// '
57 DEFINE_STR = '#define'
58 COMMENT_DEFINE_STR = COMMENT + DEFINE_STR
59 NOT_REFERENCED = 'not referenced'
60 COMMENT_NOT_REFERENCED = ' ' + COMMENT + NOT_REFERENCED
61 CHAR_ARRAY_DEFIN = 'unsigned char'
62 COMMON_FILE_NAME = 'Strings'
63 STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE)
64
65 EFI_HII_ARRAY_SIZE_LENGTH = 4
66 EFI_HII_PACKAGE_HEADER_LENGTH = 4
67 EFI_HII_HDR_SIZE_LENGTH = 4
68 EFI_HII_STRING_OFFSET_LENGTH = 4
69 EFI_STRING_ID = 1
70 EFI_STRING_ID_LENGTH = 2
71 EFI_HII_LANGUAGE_WINDOW = 0
72 EFI_HII_LANGUAGE_WINDOW_LENGTH = 2
73 EFI_HII_LANGUAGE_WINDOW_NUMBER = 16
74 EFI_HII_STRING_PACKAGE_HDR_LENGTH = EFI_HII_PACKAGE_HEADER_LENGTH + EFI_HII_HDR_SIZE_LENGTH + EFI_HII_STRING_OFFSET_LENGTH + EFI_HII_LANGUAGE_WINDOW_LENGTH * EFI_HII_LANGUAGE_WINDOW_NUMBER + EFI_STRING_ID_LENGTH
75
76 H_C_FILE_HEADER = ['//', \
77 '// DO NOT EDIT -- auto-generated file', \
78 '//', \
79 '// This file is generated by the StrGather utility', \
80 '//']
81 LANGUAGE_NAME_STRING_NAME = '$LANGUAGE_NAME'
82 PRINTABLE_LANGUAGE_NAME_STRING_NAME = '$PRINTABLE_LANGUAGE_NAME'
83
84 ## Convert a dec number to a hex string
85 #
86 # Convert a dec number to a formatted hex string in length digit
87 # The digit is set to default 8
88 # The hex string starts with "0x"
89 # DecToHexStr(1000) is '0x000003E8'
90 # DecToHexStr(1000, 6) is '0x0003E8'
91 #
92 # @param Dec: The number in dec format
93 # @param Digit: The needed digit of hex string
94 #
95 # @retval: The formatted hex string
96 #
97 def DecToHexStr(Dec, Digit = 8):
98 return '0x{0:0{1}X}'.format(Dec, Digit)
99
100 ## Convert a dec number to a hex list
101 #
102 # Convert a dec number to a formatted hex list in size digit
103 # The digit is set to default 8
104 # DecToHexList(1000) is ['0xE8', '0x03', '0x00', '0x00']
105 # DecToHexList(1000, 6) is ['0xE8', '0x03', '0x00']
106 #
107 # @param Dec: The number in dec format
108 # @param Digit: The needed digit of hex list
109 #
110 # @retval: A list for formatted hex string
111 #
112 def DecToHexList(Dec, Digit = 8):
113 Hex = '{0:0{1}X}'.format(Dec, Digit)
114 return ["0x" + Hex[Bit:Bit + 2] for Bit in range(Digit - 2, -1, -2)]
115
116 ## Convert a acsii string to a hex list
117 #
118 # Convert a acsii string to a formatted hex list
119 # AscToHexList('en-US') is ['0x65', '0x6E', '0x2D', '0x55', '0x53']
120 #
121 # @param Ascii: The acsii string
122 #
123 # @retval: A list for formatted hex string
124 #
125 def AscToHexList(Ascii):
126 try:
127 return ['0x{0:02X}'.format(Item) for Item in Ascii]
128 except:
129 return ['0x{0:02X}'.format(ord(Item)) for Item in Ascii]
130
131 ## Create content of .h file
132 #
133 # Create content of .h file
134 #
135 # @param BaseName: The basename of strings
136 # @param UniObjectClass A UniObjectClass instance
137 # @param IsCompatibleMode Compatible mode
138 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
139 #
140 # @retval Str: A string of .h file content
141 #
142 def CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
143 Str = []
144 ValueStartPtr = 60
145 Line = COMMENT_DEFINE_STR + ' ' + LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED
146 Str = WriteLine(Str, Line)
147 Line = COMMENT_DEFINE_STR + ' ' + PRINTABLE_LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + PRINTABLE_LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(1, 4) + COMMENT_NOT_REFERENCED
148 Str = WriteLine(Str, Line)
149 UnusedStr = ''
150
151 #Group the referred/Unused STRING token together.
152 for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):
153 StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]
154 Name = StringItem.StringName
155 Token = StringItem.Token
156 Referenced = StringItem.Referenced
157 if Name is not None:
158 Line = ''
159 if Referenced == True:
160 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
161 Line = DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4)
162 else:
163 Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)
164 Str = WriteLine(Str, Line)
165 else:
166 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
167 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
168 else:
169 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
170 UnusedStr = WriteLine(UnusedStr, Line)
171
172 Str.extend( UnusedStr)
173
174 Str = WriteLine(Str, '')
175 if IsCompatibleMode or UniGenCFlag:
176 Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')
177 return "".join(Str)
178
179 ## Create a complete .h file
180 #
181 # Create a complet .h file with file header and file content
182 #
183 # @param BaseName: The basename of strings
184 # @param UniObjectClass A UniObjectClass instance
185 # @param IsCompatibleMode Compatible mode
186 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
187 #
188 # @retval Str: A string of complete .h file
189 #
190 def CreateHFile(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
191 HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag))
192
193 return "".join(HFile)
194
195 ## Create a buffer to store all items in an array
196 #
197 # @param BinBuffer Buffer to contain Binary data.
198 # @param Array: The array need to be formatted
199 #
200 def CreateBinBuffer(BinBuffer, Array):
201 for Item in Array:
202 BinBuffer.write(pack("B", int(Item, 16)))
203
204 ## Create a formatted string all items in an array
205 #
206 # Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)
207 #
208 # @param Array: The array need to be formatted
209 # @param Width: The line length, the default value is set to 16
210 #
211 # @retval ArrayItem: A string for all formatted array items
212 #
213 def CreateArrayItem(Array, Width = 16):
214 MaxLength = Width
215 Index = 0
216 Line = ' '
217 ArrayItem = []
218
219 for Item in Array:
220 if Index < MaxLength:
221 Line = Line + Item + ', '
222 Index = Index + 1
223 else:
224 ArrayItem = WriteLine(ArrayItem, Line)
225 Line = ' ' + Item + ', '
226 Index = 1
227 ArrayItem = Write(ArrayItem, Line.rstrip())
228
229 return "".join(ArrayItem)
230
231 ## CreateCFileStringValue
232 #
233 # Create a line with string value
234 #
235 # @param Value: Value of the string
236 #
237 # @retval Str: A formatted string with string value
238 #
239
240 def CreateCFileStringValue(Value):
241 Value = [StringBlockType] + Value
242 Str = WriteLine('', CreateArrayItem(Value))
243
244 return "".join(Str)
245
246 ## GetFilteredLanguage
247 #
248 # apply get best language rules to the UNI language code list
249 #
250 # @param UniLanguageList: language code definition list in *.UNI file
251 # @param LanguageFilterList: language code filter list of RFC4646 format in DSC file
252 #
253 # @retval UniLanguageListFiltered: the filtered language code
254 #
255 def GetFilteredLanguage(UniLanguageList, LanguageFilterList):
256 UniLanguageListFiltered = []
257 # if filter list is empty, then consider there is no filter
258 if LanguageFilterList == []:
259 UniLanguageListFiltered = UniLanguageList
260 return UniLanguageListFiltered
261 for Language in LanguageFilterList:
262 # first check for exact match
263 if Language in UniLanguageList:
264 if Language not in UniLanguageListFiltered:
265 UniLanguageListFiltered.append(Language)
266 # find the first one with the same/equivalent primary tag
267 else:
268 if Language.find('-') != -1:
269 PrimaryTag = Language[0:Language.find('-')].lower()
270 else:
271 PrimaryTag = Language
272
273 if len(PrimaryTag) == 3:
274 PrimaryTag = LangConvTable.get(PrimaryTag)
275
276 for UniLanguage in UniLanguageList:
277 if UniLanguage.find('-') != -1:
278 UniLanguagePrimaryTag = UniLanguage[0:UniLanguage.find('-')].lower()
279 else:
280 UniLanguagePrimaryTag = UniLanguage
281
282 if len(UniLanguagePrimaryTag) == 3:
283 UniLanguagePrimaryTag = LangConvTable.get(UniLanguagePrimaryTag)
284
285 if PrimaryTag == UniLanguagePrimaryTag:
286 if UniLanguage not in UniLanguageListFiltered:
287 UniLanguageListFiltered.append(UniLanguage)
288 break
289 else:
290 # Here is rule 3 for "get best language"
291 # If tag is not listed in the Unicode file, the default ("en") tag should be used for that language
292 # for better processing, find the one that best suit for it.
293 DefaultTag = 'en'
294 if DefaultTag not in UniLanguageListFiltered:
295 # check whether language code with primary code equivalent with DefaultTag already in the list, if so, use that
296 for UniLanguage in UniLanguageList:
297 if UniLanguage.startswith('en-') or UniLanguage.startswith('eng-'):
298 if UniLanguage not in UniLanguageListFiltered:
299 UniLanguageListFiltered.append(UniLanguage)
300 break
301 else:
302 UniLanguageListFiltered.append(DefaultTag)
303 return UniLanguageListFiltered
304
305
306 ## Create content of .c file
307 #
308 # Create content of .c file
309 #
310 # @param BaseName: The basename of strings
311 # @param UniObjectClass A UniObjectClass instance
312 # @param IsCompatibleMode Compatible mode
313 # @param UniBinBuffer UniBinBuffer to contain UniBinary data.
314 # @param FilterInfo Platform language filter information
315 #
316 # @retval Str: A string of .c file content
317 #
318 def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniBinBuffer, FilterInfo):
319 #
320 # Init array length
321 #
322 TotalLength = EFI_HII_ARRAY_SIZE_LENGTH
323 Str = ''
324 Offset = 0
325
326 EDK2Module = FilterInfo[0]
327 if EDK2Module:
328 LanguageFilterList = FilterInfo[1]
329 else:
330 # EDK module is using ISO639-2 format filter, convert to the RFC4646 format
331 LanguageFilterList = [LangConvTable.get(F.lower()) for F in FilterInfo[1]]
332
333 UniLanguageList = []
334 for IndexI in range(len(UniObjectClass.LanguageDef)):
335 UniLanguageList += [UniObjectClass.LanguageDef[IndexI][0]]
336
337 UniLanguageListFiltered = GetFilteredLanguage(UniLanguageList, LanguageFilterList)
338
339
340 #
341 # Create lines for each language's strings
342 #
343 for IndexI in range(len(UniObjectClass.LanguageDef)):
344 Language = UniObjectClass.LanguageDef[IndexI][0]
345 if Language not in UniLanguageListFiltered:
346 continue
347
348 StringBuffer = BytesIO()
349 StrStringValue = ''
350 ArrayLength = 0
351 NumberOfUseOtherLangDef = 0
352 Index = 0
353 for IndexJ in range(1, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[IndexI][0]])):
354 Item = UniObjectClass.OrderedStringListByToken[Language][IndexJ]
355
356 Name = Item.StringName
357 Value = Item.StringValueByteList
358 Referenced = Item.Referenced
359 Token = Item.Token
360 UseOtherLangDef = Item.UseOtherLangDef
361
362 if UseOtherLangDef != '' and Referenced:
363 NumberOfUseOtherLangDef = NumberOfUseOtherLangDef + 1
364 Index = Index + 1
365 else:
366 if NumberOfUseOtherLangDef > 0:
367 StrStringValue = WriteLine(StrStringValue, CreateArrayItem([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
368 CreateBinBuffer (StringBuffer, ([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
369 NumberOfUseOtherLangDef = 0
370 ArrayLength = ArrayLength + 3
371 if Referenced and Item.Token > 0:
372 Index = Index + 1
373 StrStringValue = WriteLine(StrStringValue, "// %s: %s:%s" % (DecToHexStr(Index, 4), Name, DecToHexStr(Token, 4)))
374 StrStringValue = Write(StrStringValue, CreateCFileStringValue(Value))
375 CreateBinBuffer (StringBuffer, [StringBlockType] + Value)
376 ArrayLength = ArrayLength + Item.Length + 1 # 1 is for the length of string type
377
378 #
379 # EFI_HII_PACKAGE_HEADER
380 #
381 Offset = EFI_HII_STRING_PACKAGE_HDR_LENGTH + len(Language) + 1
382 ArrayLength = Offset + ArrayLength + 1
383
384 #
385 # Create PACKAGE HEADER
386 #
387 Str = WriteLine(Str, '// PACKAGE HEADER\n')
388 TotalLength = TotalLength + ArrayLength
389
390 List = DecToHexList(ArrayLength, 6) + \
391 [StringPackageType] + \
392 DecToHexList(Offset) + \
393 DecToHexList(Offset) + \
394 DecToHexList(EFI_HII_LANGUAGE_WINDOW, EFI_HII_LANGUAGE_WINDOW_LENGTH * 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER + \
395 DecToHexList(EFI_STRING_ID, 4) + \
396 AscToHexList(Language) + \
397 DecToHexList(0, 2)
398 Str = WriteLine(Str, CreateArrayItem(List, 16) + '\n')
399
400 #
401 # Create PACKAGE DATA
402 #
403 Str = WriteLine(Str, '// PACKAGE DATA\n')
404 Str = Write(Str, StrStringValue)
405
406 #
407 # Add an EFI_HII_SIBT_END at last
408 #
409 Str = WriteLine(Str, ' ' + EFI_HII_SIBT_END + ",")
410
411 #
412 # Create binary UNI string
413 #
414 if UniBinBuffer:
415 CreateBinBuffer (UniBinBuffer, List)
416 UniBinBuffer.write (StringBuffer.getvalue())
417 UniBinBuffer.write (pack("B", int(EFI_HII_SIBT_END, 16)))
418 StringBuffer.close()
419
420 #
421 # Create line for string variable name
422 # "unsigned char $(BaseName)Strings[] = {"
423 #
424 AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n')
425
426 if IsCompatibleMode:
427 #
428 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode
429 #
430 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Length')
431 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength + 2)) + '\n')
432 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Type')
433 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(2, 4)) + '\n')
434 else:
435 #
436 # Create whole array length in UEFI mode
437 #
438 AllStr = WriteLine(AllStr, '// STRGATHER_OUTPUT_HEADER')
439 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength)) + '\n')
440
441 #
442 # Join package data
443 #
444 AllStr = Write(AllStr, Str)
445
446 return "".join(AllStr)
447
448 ## Create end of .c file
449 #
450 # Create end of .c file
451 #
452 # @retval Str: A string of .h file end
453 #
454 def CreateCFileEnd():
455 Str = Write('', '};')
456 return Str
457
458 ## Create a .c file
459 #
460 # Create a complete .c file
461 #
462 # @param BaseName: The basename of strings
463 # @param UniObjectClass A UniObjectClass instance
464 # @param IsCompatibleMode Compatible Mode
465 # @param FilterInfo Platform language filter information
466 #
467 # @retval CFile: A string of complete .c file
468 #
469 def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode, FilterInfo):
470 CFile = ''
471 CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, None, FilterInfo))
472 CFile = WriteLine(CFile, CreateCFileEnd())
473 return "".join(CFile)
474
475 ## GetFileList
476 #
477 # Get a list for all files
478 #
479 # @param IncludeList: A list of all path to be searched
480 # @param SkipList: A list of all types of file could be skipped
481 #
482 # @retval FileList: A list of all files found
483 #
484 def GetFileList(SourceFileList, IncludeList, SkipList):
485 if IncludeList is None:
486 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, "Include path for unicode file is not defined")
487
488 FileList = []
489 if SkipList is None:
490 SkipList = []
491
492 for File in SourceFileList:
493 for Dir in IncludeList:
494 if not os.path.exists(Dir):
495 continue
496 File = os.path.join(Dir, File.Path)
497 #
498 # Ignore Dir
499 #
500 if os.path.isfile(File) != True:
501 continue
502 #
503 # Ignore file listed in skip list
504 #
505 IsSkip = False
506 for Skip in SkipList:
507 if os.path.splitext(File)[1].upper() == Skip.upper():
508 EdkLogger.verbose("Skipped %s for string token uses search" % File)
509 IsSkip = True
510 break
511
512 if not IsSkip:
513 FileList.append(File)
514
515 break
516
517 return FileList
518
519 ## SearchString
520 #
521 # Search whether all string defined in UniObjectClass are referenced
522 # All string used should be set to Referenced
523 #
524 # @param UniObjectClass: Input UniObjectClass
525 # @param FileList: Search path list
526 # @param IsCompatibleMode Compatible Mode
527 #
528 # @retval UniObjectClass: UniObjectClass after searched
529 #
530 def SearchString(UniObjectClass, FileList, IsCompatibleMode):
531 if FileList == []:
532 return UniObjectClass
533
534 for File in FileList:
535 if os.path.isfile(File):
536 Lines = open(File, 'r')
537 for Line in Lines:
538 for StrName in STRING_TOKEN.findall(Line):
539 EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName)
540 UniObjectClass.SetStringReferenced(StrName)
541
542 UniObjectClass.ReToken()
543
544 return UniObjectClass
545
546 ## GetStringFiles
547 #
548 # This function is used for UEFI2.1 spec
549 #
550 #
551 def GetStringFiles(UniFilList, SourceFileList, IncludeList, IncludePathList, SkipList, BaseName, IsCompatibleMode = False, ShellMode = False, UniGenCFlag = True, UniGenBinBuffer = None, FilterInfo = [True, []]):
552 if len(UniFilList) > 0:
553 if ShellMode:
554 #
555 # support ISO 639-2 codes in .UNI files of EDK Shell
556 #
557 Uni = UniFileClassObject(sorted(UniFilList, key=lambda x: x.File), True, IncludePathList)
558 else:
559 Uni = UniFileClassObject(sorted(UniFilList, key=lambda x: x.File), IsCompatibleMode, IncludePathList)
560 else:
561 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, 'No unicode files given')
562
563 FileList = GetFileList(SourceFileList, IncludeList, SkipList)
564
565 Uni = SearchString(Uni, sorted (FileList), IsCompatibleMode)
566
567 HFile = CreateHFile(BaseName, Uni, IsCompatibleMode, UniGenCFlag)
568 CFile = None
569 if IsCompatibleMode or UniGenCFlag:
570 CFile = CreateCFile(BaseName, Uni, IsCompatibleMode, FilterInfo)
571 if UniGenBinBuffer:
572 CreateCFileContent(BaseName, Uni, IsCompatibleMode, UniGenBinBuffer, FilterInfo)
573
574 return HFile, CFile
575
576 #
577 # Write an item
578 #
579 def Write(Target, Item):
580 if isinstance(Target,str):
581 Target = [Target]
582 if not Target:
583 Target = []
584 if isinstance(Item,list):
585 Target.extend(Item)
586 else:
587 Target.append(Item)
588 return Target
589
590 #
591 # Write an item with a break line
592 #
593 def WriteLine(Target, Item):
594 if isinstance(Target,str):
595 Target = [Target]
596 if not Target:
597 Target = []
598 if isinstance(Item, list):
599 Target.extend(Item)
600 else:
601 Target.append(Item)
602 Target.append('\n')
603 return Target
604
605 # This acts like the main() function for the script, unless it is 'import'ed into another
606 # script.
607 if __name__ == '__main__':
608 EdkLogger.info('start')
609
610 UniFileList = [
611 r'C:\\Edk\\Strings2.uni',
612 r'C:\\Edk\\Strings.uni'
613 ]
614
615 SrcFileList = []
616 for Root, Dirs, Files in os.walk('C:\\Edk'):
617 for File in Files:
618 SrcFileList.append(File)
619
620 IncludeList = [
621 r'C:\\Edk'
622 ]
623
624 SkipList = ['.inf', '.uni']
625 BaseName = 'DriverSample'
626 (h, c) = GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipList, BaseName, True)
627 hfile = open('unistring.h', 'w')
628 cfile = open('unistring.c', 'w')
629 hfile.write(h)
630 cfile.write(c)
631
632 EdkLogger.info('end')