cldrparser.lpr 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. uses
  20. SysUtils, classes, getopts,
  21. cldrhelper, helper, cldrtest, cldrxml, unicodeset;
  22. const
  23. SUsageText =
  24. 'This program creates pascal units from CLDR''s collation files for usage ' + sLineBreak +
  25. 'with the FreePascal Native Unicode Manager.' + sLineBreak + sLineBreak +
  26. 'Usage : cldrparser <collationFileName> [<typeName>] [-d<dataDir>] [-o<outputDir>] [-t]' + sLineBreak + sLineBreak +
  27. ' where :' + sLineBreak +
  28. ' ' + sLineBreak +
  29. ' - collationFileName : specify the target file.' + sLineBreak +
  30. ' - typeName : optional, specify the collation'' type-name to be parse;' + sLineBreak +
  31. ' If this argument is not supplied, a default type-name' + sLineBreak +
  32. ' is chosen from the type-name list in the following order : ' + sLineBreak +
  33. ' * the "default" specified type-name indicated by the collation' + sLineBreak +
  34. ' * the type named "standard" ' + sLineBreak +
  35. ' * the type named "search" ' + sLineBreak +
  36. ' * the first type.' + sLineBreak +
  37. ' - dataDir : specify the directory that contains the collation files.' + sLineBreak +
  38. ' The default value is the program''s directory.' + sLineBreak +
  39. ' - outputDir : specify the directory where the generated files will be stored.' + sLineBreak +
  40. ' The default value is the program''s directory.' + sLineBreak +
  41. ' - t : to execute parser the test suite. The program will execute only the test suite and exit.' + sLineBreak +
  42. ' ' + sLineBreak +
  43. ' The program expects some files to be present in the <dataDir> folder : ' + sLineBreak +
  44. ' - UCA_Rules_SHORT.xml found in the CollationAuxiliary.zip available on unicode.org' + sLineBreak +
  45. ' - allkeys.txt this is the file allkeys_CLDR.txt contained in CollationAuxiliary.zip renamed to allkeys.txt' + sLineBreak +
  46. ' The CollationAuxiliary.zip archive is provided by unicode in the "unicode collation algorithm data files" section.';
  47. function ParseOptions(
  48. var ADataDir,
  49. AOuputDir,
  50. ACollationFileName,
  51. ACollationTypeName : string;
  52. var AExecTestSuite : Boolean
  53. ) : Boolean;
  54. var
  55. c : Char;
  56. idx, k : Integer;
  57. s : string;
  58. begin
  59. if (ParamCount() = 0) then
  60. exit(False);
  61. Result := True;
  62. AExecTestSuite := False;
  63. repeat
  64. c := GetOpt('d:o:ht');
  65. case c of
  66. 'd' : ADataDir := ExpandFileName(Trim(OptArg));
  67. 'o' : AOuputDir := ExpandFileName(Trim(OptArg));
  68. 'h', '?' :
  69. begin
  70. WriteLn(SUsageText);
  71. Result := False;
  72. end;
  73. 't' : AExecTestSuite := True;
  74. end;
  75. until (c = EndOfOptions);
  76. idx := 0;
  77. for k := 1 to ParamCount() do begin
  78. s := Trim(ParamStr(k));
  79. if (s <> '') and (s[1] <> '-') then begin
  80. if (idx = 0) then
  81. ACollationFileName := s
  82. else if (idx = 1) then
  83. ACollationTypeName := s;
  84. Inc(idx);
  85. if (idx >= 2) then
  86. Break;
  87. end;
  88. end;
  89. end;
  90. var
  91. orderedChars : TOrderedCharacters;
  92. ucaBook : TUCA_DataBook;
  93. stream, streamNE, streamOE : TMemoryStream;
  94. s, collationFileName, collationTypeName : string;
  95. i , c: Integer;
  96. collation : TCldrCollation;
  97. dataPath, outputPath : string;
  98. collationItem : TCldrCollationItem;
  99. testSuiteFlag : Boolean;
  100. begin
  101. dataPath := '';
  102. outputPath := '';
  103. collationFileName := '';
  104. collationTypeName := '';
  105. testSuiteFlag := False;
  106. if not ParseOptions(dataPath,outputPath,collationFileName,collationTypeName,testSuiteFlag) then
  107. Halt(1);
  108. if testSuiteFlag then begin
  109. exec_tests();
  110. Halt;
  111. end;
  112. if (dataPath <> '') and not(DirectoryExists(dataPath)) then begin
  113. WriteLn('This directory does not exist : ',dataPath);
  114. Halt(1);
  115. end;
  116. if (dataPath = '') then
  117. dataPath := ExtractFilePath(ParamStr(0))
  118. else
  119. dataPath := IncludeTrailingPathDelimiter(dataPath);
  120. if (outputPath = '') then
  121. outputPath := dataPath
  122. else
  123. outputPath := IncludeTrailingPathDelimiter(outputPath);
  124. if (ParamCount() = 0) then begin
  125. WriteLn(SUsageText);
  126. Halt(1);
  127. end;
  128. if not(
  129. FileExists(dataPath+'UCA_Rules_SHORT.xml') and
  130. FileExists(dataPath+'allkeys.txt')
  131. )
  132. then begin
  133. WriteLn(Format('File not found : %s or %s.',[dataPath+'UCA_Rules_SHORT.xml',dataPath+'allkeys.txt']));
  134. Halt(1);
  135. end;
  136. collationFileName := dataPath + collationFileName;
  137. if not FileExists(collationFileName) then begin
  138. WriteLn('File not found : "',collationFileName,'"');
  139. Halt(1);
  140. end;
  141. WriteLn(sLineBreak,'Collation Parsing ',QuotedStr(collationFileName),' ...');
  142. stream := nil;
  143. streamNE := nil;
  144. streamOE := nil;
  145. collation := TCldrCollation.Create();
  146. try
  147. ParseCollationDocument(collationFileName,collation,TCldrParserMode.HeaderParsing);
  148. WriteLn(Format(' Collation Count = %d',[collation.ItemCount]));
  149. if (collation.ItemCount = 0) then begin
  150. WriteLn('No collation in this file.');
  151. end else begin
  152. for i := 0 to collation.ItemCount - 1 do
  153. WriteLn(Format(' Item[%d] = (Type = %s)',[i, collation.Items[i].TypeName]));
  154. collationItem := collation.Find(collationTypeName);
  155. if (collationItem = nil) then begin
  156. collationTypeName := FindCollationDefaultItemName(collation);
  157. collationItem := collation.Find(collationTypeName);
  158. end;
  159. WriteLn(Format('Parsing Collation Item "%s" ...',[collationTypeName]));
  160. ParseCollationDocument(collationFileName,collationItem,collationTypeName);
  161. s := dataPath + 'UCA_Rules_SHORT.xml';
  162. WriteLn;
  163. WriteLn('Parsing ',QuotedStr(s),' ...');
  164. FillByte(orderedChars,SizeOf(orderedChars),0);
  165. orderedChars.Clear();
  166. ParseInitialDocument(@orderedChars,s);
  167. WriteLn('File parsed, ',orderedChars.ActualLength,' characters.');
  168. WriteLn('Loading CLDR root''s key table ...');
  169. stream := TMemoryStream.Create();
  170. s := dataPath + 'allkeys.txt';
  171. stream.LoadFromFile(s);
  172. ParseUCAFile(stream,ucaBook);
  173. c := FillInitialPositions(@orderedChars.Data[0],orderedChars.ActualLength,ucaBook.Lines);
  174. if (c > 0) then
  175. WriteLn(' Missed Initial Positions = ',c);
  176. WriteLn(' Loaded.');
  177. WriteLn('Start generation ...');
  178. stream.Clear();
  179. streamNE := TMemoryStream.Create();
  180. streamOE := TMemoryStream.Create();
  181. s := COLLATION_FILE_PREFIX + ChangeFileExt(LowerCase(ExtractFileName(collationFileName)),'.pas');
  182. GenerateCdlrCollation(
  183. collation,collationTypeName,s,stream,streamNE,streamOE,
  184. orderedChars,ucaBook.Lines
  185. );
  186. stream.SaveToFile(ExtractFilePath(collationFileName)+s);
  187. if (streamNE.Size > 0) then begin
  188. streamNE.SaveToFile(ExtractFilePath(collationFileName)+GenerateEndianIncludeFileName(s,ENDIAN_NATIVE));
  189. streamOE.SaveToFile(ExtractFilePath(collationFileName)+GenerateEndianIncludeFileName(s,ENDIAN_NON_NATIVE));
  190. end;
  191. end;
  192. finally
  193. streamOE.Free();
  194. streamNE.Free();
  195. stream.Free();
  196. collation.Free();
  197. end;
  198. WriteLn(sLineBreak,'Finished.');
  199. end.