2
0

cldrparser.lpr 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. begin
  94. {$ifdef test_suite}
  95. exec_tests();
  96. {$endif test_suite}
  97. {$ifdef actual_parsing}
  98. dataPath := '';
  99. outputPath := '';
  100. collationFileName := '';
  101. collationTypeName := '';
  102. if not ParseOptions(dataPath,outputPath,collationFileName,collationTypeName) then
  103. Halt(1);
  104. if (dataPath <> '') and not(DirectoryExists(dataPath)) then begin
  105. WriteLn('This directory does not exist : ',dataPath);
  106. Halt(1);
  107. end;
  108. if (dataPath = '') then
  109. dataPath := ExtractFilePath(ParamStr(0))
  110. else
  111. dataPath := IncludeTrailingPathDelimiter(dataPath);
  112. if (outputPath = '') then
  113. outputPath := dataPath
  114. else
  115. outputPath := IncludeTrailingPathDelimiter(outputPath);
  116. if (ParamCount() = 0) then begin
  117. WriteLn(SUsageText);
  118. Halt(1);
  119. end;
  120. if not(
  121. FileExists(dataPath+'UCA_Rules_SHORT.xml') and
  122. FileExists(dataPath+'allkeys.txt')
  123. )
  124. then begin
  125. WriteLn(Format('File not found : %s or %s.',[dataPath+'UCA_Rules_SHORT.xml',dataPath+'allkeys.txt']));
  126. Halt(1);
  127. end;
  128. collationFileName := dataPath + collationFileName;
  129. if not FileExists(collationFileName) then begin
  130. WriteLn('File not found : "',collationFileName,'"');
  131. Halt(1);
  132. end;
  133. WriteLn(sLineBreak,'Collation Parsing ',QuotedStr(collationFileName),' ...');
  134. stream := nil;
  135. endianStream := nil;
  136. collation := TCldrCollation.Create();
  137. try
  138. ParseCollationDocument(collationFileName,collation);
  139. WriteLn(Format(' Collation Count = %d',[collation.ItemCount]));
  140. if (collation.ItemCount = 0) then begin
  141. WriteLn('No collation in this file.');
  142. end else begin
  143. for i := 0 to collation.ItemCount - 1 do
  144. WriteLn(Format(' Item[%d] = %d "resets"; Type = %s',[i, Length(collation.Items[i].Rules),collation.Items[i].TypeName]));
  145. if (collation.Find(collationTypeName) = nil) then
  146. collationTypeName := FindCollationDefaultItemName(collation);
  147. WriteLn('Collation Item Name : ',collationTypeName);
  148. s := dataPath + 'UCA_Rules_SHORT.xml';
  149. WriteLn;
  150. WriteLn('Parsing ',QuotedStr(s),' ...');
  151. FillByte(orderedChars,SizeOf(orderedChars),0);
  152. orderedChars.Clear();
  153. ParseInitialDocument(@orderedChars,s);
  154. WriteLn('File parsed, ',orderedChars.ActualLength,' characters.');
  155. WriteLn('Loading CLDR root''s key table ...');
  156. stream := TMemoryStream.Create();
  157. s := dataPath + 'allkeys.txt';
  158. stream.LoadFromFile(s);
  159. ParseUCAFile(stream,ucaBook);
  160. c := FillInitialPositions(@orderedChars.Data[0],orderedChars.ActualLength,ucaBook.Lines);
  161. if (c > 0) then
  162. WriteLn(' Missed Initial Positions = ',c);
  163. WriteLn(' Loaded.');
  164. WriteLn('Start generation ...');
  165. stream.Clear();
  166. endianStream := TMemoryStream.Create();
  167. s := COLLATION_FILE_PREFIX + ChangeFileExt(LowerCase(ExtractFileName(collationFileName)),'.pas');
  168. GenerateCdlrCollation(
  169. collation,collationTypeName,s,stream,endianStream,
  170. orderedChars,ucaBook.Lines
  171. );
  172. stream.SaveToFile(ExtractFilePath(collationFileName)+s);
  173. if (endianStream.Size > 0) then
  174. endianStream.SaveToFile(ExtractFilePath(collationFileName)+GenerateEndianIncludeFileName(s));
  175. end;
  176. finally
  177. endianStream.Free();
  178. stream.Free();
  179. collation.Free();
  180. end;
  181. {$endif actual_parsing}
  182. WriteLn(sLineBreak,'Finished.');
  183. end.