cldrparser.lpr 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. { Unicode CLDR's collation parser.
  2. Copyright (c) 2013 by Inoussa OUEDRAOGO
  3. It creates units from CLDR's collation files.
  4. The source code is distributed under the Library GNU
  5. General Public License with the following modification:
  6. - object files and libraries linked into an application may be
  7. distributed without source code.
  8. If you didn't receive a copy of the file COPYING, contact:
  9. Free Software Foundation
  10. 675 Mass Ave
  11. Cambridge, MA 02139
  12. USA
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. }
  17. program cldrparser;
  18. {$mode objfpc}{$H+}
  19. { $define test_suite} // Define this to execute the parser test suite.
  20. {$define actual_parsing}
  21. uses
  22. SysUtils, classes, getopts,
  23. cldrhelper, helper, cldrtest, cldrxml, unicodeset;
  24. const
  25. SUsageText =
  26. 'This program creates pascal units from CLDR''s collation files for usage ' + sLineBreak +
  27. 'with the FreePascal Native Unicode Manager.' + sLineBreak + sLineBreak +
  28. 'Usage : cldrparser <collationFileName> [<typeName>] [-d<dataDir>] [-o<outputDir>]' + sLineBreak + sLineBreak +
  29. ' where :' + sLineBreak +
  30. ' ' + sLineBreak +
  31. ' - collationFileName : specify the target file.' + sLineBreak +
  32. ' - typeName : optional, specify the collation'' type-name to be parse;' + sLineBreak +
  33. ' If this argument is not supplied, a default type-name' + sLineBreak +
  34. ' is chosen from the type-name list in the following order : ' + sLineBreak +
  35. ' * the "default" specified type-name indicated by the collation' + sLineBreak +
  36. ' * the type named "standard" ' + sLineBreak +
  37. ' * the type named "search" ' + sLineBreak +
  38. ' * the first type.' + sLineBreak +
  39. ' - dataDir : specify the directory that contains the collation files.' + sLineBreak +
  40. ' The default value is the program''s directory.' + sLineBreak +
  41. ' - outputDir : specify the directory where the generated files will be stored.' + sLineBreak +
  42. ' The default value is the program''s directory.' + sLineBreak +
  43. ' ' + sLineBreak +
  44. ' The program expects some files to be present in the <dataDir> folder : ' + sLineBreak +
  45. ' - UCA_Rules_SHORT.xml found in the CollationAuxiliary.zip available on unicode.org' + sLineBreak +
  46. ' - allkeys.txt this is the file allkeys_CLDR.txt contained in CollationAuxiliary.zip renamed to allkeys.txt' + sLineBreak +
  47. ' The CollationAuxiliary.zip archive is provided by unicode in the "unicode collation algorithm data files" section.';
  48. function ParseOptions(
  49. var ADataDir, AOuputDir, ACollationFileName, ACollationTypeName : string
  50. ) : Boolean;
  51. var
  52. c : Char;
  53. idx, k : Integer;
  54. s : string;
  55. begin
  56. if (ParamCount() = 0) then
  57. exit(False);
  58. Result := True;
  59. repeat
  60. c := GetOpt('d:o:h');
  61. case c of
  62. 'd' : ADataDir := ExpandFileName(Trim(OptArg));
  63. 'o' : AOuputDir := ExpandFileName(Trim(OptArg));
  64. 'h', '?' :
  65. begin
  66. WriteLn(SUsageText);
  67. Result := False;
  68. end;
  69. end;
  70. until (c = EndOfOptions);
  71. idx := 0;
  72. for k := 1 to ParamCount() do begin
  73. s := Trim(ParamStr(k));
  74. if (s <> '') and (s[1] <> '-') then begin
  75. if (idx = 0) then
  76. ACollationFileName := s
  77. else if (idx = 1) then
  78. ACollationTypeName := s;
  79. Inc(idx);
  80. if (idx >= 2) then
  81. Break;
  82. end;
  83. end;
  84. end;
  85. var
  86. orderedChars : TOrderedCharacters;
  87. ucaBook : TUCA_DataBook;
  88. stream, endianStream : TMemoryStream;
  89. s, collationFileName, collationTypeName : string;
  90. i , c: Integer;
  91. collation : TCldrCollation;
  92. dataPath, outputPath : string;
  93. collationItem : TCldrCollationItem;
  94. begin
  95. {$ifdef test_suite}
  96. exec_tests();
  97. {$endif test_suite}
  98. {$ifdef actual_parsing}
  99. dataPath := '';
  100. outputPath := '';
  101. collationFileName := '';
  102. collationTypeName := '';
  103. if not ParseOptions(dataPath,outputPath,collationFileName,collationTypeName) then
  104. Halt(1);
  105. if (dataPath <> '') and not(DirectoryExists(dataPath)) then begin
  106. WriteLn('This directory does not exist : ',dataPath);
  107. Halt(1);
  108. end;
  109. if (dataPath = '') then
  110. dataPath := ExtractFilePath(ParamStr(0))
  111. else
  112. dataPath := IncludeTrailingPathDelimiter(dataPath);
  113. if (outputPath = '') then
  114. outputPath := dataPath
  115. else
  116. outputPath := IncludeTrailingPathDelimiter(outputPath);
  117. if (ParamCount() = 0) then begin
  118. WriteLn(SUsageText);
  119. Halt(1);
  120. end;
  121. if not(
  122. FileExists(dataPath+'UCA_Rules_SHORT.xml') and
  123. FileExists(dataPath+'allkeys.txt')
  124. )
  125. then begin
  126. WriteLn(Format('File not found : %s or %s.',[dataPath+'UCA_Rules_SHORT.xml',dataPath+'allkeys.txt']));
  127. Halt(1);
  128. end;
  129. collationFileName := dataPath + collationFileName;
  130. if not FileExists(collationFileName) then begin
  131. WriteLn('File not found : "',collationFileName,'"');
  132. Halt(1);
  133. end;
  134. WriteLn(sLineBreak,'Collation Parsing ',QuotedStr(collationFileName),' ...');
  135. stream := nil;
  136. endianStream := nil;
  137. collation := TCldrCollation.Create();
  138. try
  139. ParseCollationDocument(collationFileName,collation,TCldrParserMode.HeaderParsing);
  140. WriteLn(Format(' Collation Count = %d',[collation.ItemCount]));
  141. if (collation.ItemCount = 0) then begin
  142. WriteLn('No collation in this file.');
  143. end else begin
  144. for i := 0 to collation.ItemCount - 1 do
  145. WriteLn(Format(' Item[%d] = (Type = %s)',[i, collation.Items[i].TypeName]));
  146. collationItem := collation.Find(collationTypeName);
  147. if (collationItem = nil) then begin
  148. collationTypeName := FindCollationDefaultItemName(collation);
  149. collationItem := collation.Find(collationTypeName);
  150. end;
  151. WriteLn(Format('Parsing Collation Item "%s" ...',[collationTypeName]));
  152. ParseCollationDocument(collationFileName,collationItem,collationTypeName);
  153. s := dataPath + 'UCA_Rules_SHORT.xml';
  154. WriteLn;
  155. WriteLn('Parsing ',QuotedStr(s),' ...');
  156. FillByte(orderedChars,SizeOf(orderedChars),0);
  157. orderedChars.Clear();
  158. ParseInitialDocument(@orderedChars,s);
  159. WriteLn('File parsed, ',orderedChars.ActualLength,' characters.');
  160. WriteLn('Loading CLDR root''s key table ...');
  161. stream := TMemoryStream.Create();
  162. s := dataPath + 'allkeys.txt';
  163. stream.LoadFromFile(s);
  164. ParseUCAFile(stream,ucaBook);
  165. c := FillInitialPositions(@orderedChars.Data[0],orderedChars.ActualLength,ucaBook.Lines);
  166. if (c > 0) then
  167. WriteLn(' Missed Initial Positions = ',c);
  168. WriteLn(' Loaded.');
  169. WriteLn('Start generation ...');
  170. stream.Clear();
  171. endianStream := TMemoryStream.Create();
  172. s := COLLATION_FILE_PREFIX + ChangeFileExt(LowerCase(ExtractFileName(collationFileName)),'.pas');
  173. GenerateCdlrCollation(
  174. collation,collationTypeName,s,stream,endianStream,
  175. orderedChars,ucaBook.Lines
  176. );
  177. stream.SaveToFile(ExtractFilePath(collationFileName)+s);
  178. if (endianStream.Size > 0) then
  179. endianStream.SaveToFile(ExtractFilePath(collationFileName)+GenerateEndianIncludeFileName(s));
  180. end;
  181. finally
  182. endianStream.Free();
  183. stream.Free();
  184. collation.Free();
  185. end;
  186. {$endif actual_parsing}
  187. WriteLn(sLineBreak,'Finished.');
  188. end.