cldrparser.lpr 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 WINCE_TEST}
  20. uses
  21. SysUtils, classes, getopts,{$ifdef WINCE}StreamIO,{$endif}
  22. cldrhelper, helper, cldrtest, cldrxml, unicodeset;
  23. const
  24. SUsageText =
  25. 'This program creates pascal units from CLDR''s collation files for usage ' + sLineBreak +
  26. 'with the FreePascal Native Unicode Manager.' + sLineBreak + sLineBreak +
  27. 'Usage : cldrparser <collationFileName> [<typeName>] [-d<dataDir>] [-o<outputDir>] [-t]' + sLineBreak + sLineBreak +
  28. ' where :' + sLineBreak +
  29. ' ' + sLineBreak +
  30. ' - collationFileName : specify the target file.' + sLineBreak +
  31. ' - typeName : optional, specify the collation'' type-name to be parse;' + sLineBreak +
  32. ' If this argument is not supplied, a default type-name' + sLineBreak +
  33. ' is chosen from the type-name list in the following order : ' + sLineBreak +
  34. ' * the "default" specified type-name indicated by the collation' + sLineBreak +
  35. ' * the type named "standard" ' + sLineBreak +
  36. ' * the type named "search" ' + sLineBreak +
  37. ' * the first type.' + sLineBreak +
  38. ' - dataDir : specify the directory that contains the collation files.' + sLineBreak +
  39. ' The default value is the program''s directory.' + sLineBreak +
  40. ' - outputDir : specify the directory where the generated files will be stored.' + sLineBreak +
  41. ' The default value is the program''s directory.' + sLineBreak +
  42. ' - t : to execute parser the test suite. The program will execute only the test suite and exit.' + 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,
  50. AOuputDir,
  51. ACollationFileName,
  52. ACollationTypeName : string;
  53. var AExecTestSuite : Boolean
  54. ) : Boolean;
  55. var
  56. c : Char;
  57. idx, k : Integer;
  58. s : string;
  59. begin
  60. {$ifdef WINCE_TEST}
  61. ADataDir := ExtractFilePath(ParamStr(0))+'data';
  62. AOuputDir := ADataDir;
  63. ACollationFileName := 'sv.xml';
  64. exit(True);
  65. {$endif WINCE_TEST}
  66. if (ParamCount() = 0) then
  67. exit(False);
  68. Result := True;
  69. AExecTestSuite := False;
  70. repeat
  71. c := GetOpt('d:o:ht');
  72. case c of
  73. 'd' : ADataDir := ExpandFileName(Trim(OptArg));
  74. 'o' : AOuputDir := ExpandFileName(Trim(OptArg));
  75. 'h', '?' :
  76. begin
  77. WriteLn(SUsageText);
  78. Result := False;
  79. end;
  80. 't' : AExecTestSuite := True;
  81. end;
  82. until (c = EndOfOptions);
  83. idx := 0;
  84. for k := 1 to ParamCount() do begin
  85. s := Trim(ParamStr(k));
  86. if (s <> '') and (s[1] <> '-') then begin
  87. if (idx = 0) then
  88. ACollationFileName := s
  89. else if (idx = 1) then
  90. ACollationTypeName := s;
  91. Inc(idx);
  92. if (idx >= 2) then
  93. Break;
  94. end;
  95. end;
  96. end;
  97. var
  98. orderedChars : TOrderedCharacters;
  99. ucaBook : TUCA_DataBook;
  100. stream, streamNE, streamOE, binaryStreamNE, binaryStreamOE : TMemoryStream;
  101. s, collationFileName, collationTypeName : string;
  102. i , c: Integer;
  103. collation : TCldrCollation;
  104. dataPath, outputPath : string;
  105. collationItem : TCldrCollationItem;
  106. testSuiteFlag : Boolean;
  107. {$ifdef WINCE}
  108. fs : TFileStream;
  109. {$endif WINCE}
  110. begin
  111. {$ifdef WINCE}
  112. s := ExtractFilePath(ParamStr(0))+'cldr-log.txt';
  113. DeleteFile(s);
  114. fs := TFileStream.Create(s,fmCreate);
  115. AssignStream(Output,fs);
  116. Rewrite(Output);
  117. s := ExtractFilePath(ParamStr(0))+'cldr-err.txt';
  118. DeleteFile(s);
  119. fs := TFileStream.Create(s,fmCreate);
  120. AssignStream(ErrOutput,fs);
  121. Rewrite(ErrOutput);
  122. {$endif WINCE}
  123. {$ifdef WINCE_TEST}
  124. testSuiteFlag := True;
  125. try
  126. exec_tests();
  127. except
  128. on e : Exception do begin
  129. WriteLn('Exception : '+e.Message);
  130. raise;
  131. end;
  132. end;
  133. exit;
  134. {$endif WINCE_TEST}
  135. dataPath := '';
  136. outputPath := '';
  137. collationFileName := '';
  138. collationTypeName := '';
  139. testSuiteFlag := False;
  140. if not ParseOptions(dataPath,outputPath,collationFileName,collationTypeName,testSuiteFlag) then
  141. Halt(1);
  142. if testSuiteFlag then begin
  143. exec_tests();
  144. Halt;
  145. end;
  146. if (dataPath <> '') and not(DirectoryExists(dataPath)) then begin
  147. WriteLn('This directory does not exist : ',dataPath);
  148. Halt(1);
  149. end;
  150. if (dataPath = '') then
  151. dataPath := ExtractFilePath(ParamStr(0))
  152. else
  153. dataPath := IncludeTrailingPathDelimiter(dataPath);
  154. if (outputPath = '') then
  155. outputPath := dataPath
  156. else
  157. outputPath := IncludeTrailingPathDelimiter(outputPath);
  158. {$ifndef WINCE_TEST}
  159. if (ParamCount() = 0) then begin
  160. WriteLn(SUsageText);
  161. Halt(1);
  162. end;
  163. {$endif WINCE_TEST}
  164. if not(
  165. FileExists(dataPath+'UCA_Rules_SHORT.xml') and
  166. FileExists(dataPath+'allkeys.txt')
  167. )
  168. then begin
  169. WriteLn(Format('File not found : %s or %s.',[dataPath+'UCA_Rules_SHORT.xml',dataPath+'allkeys.txt']));
  170. Halt(1);
  171. end;
  172. collationFileName := dataPath + collationFileName;
  173. if not FileExists(collationFileName) then begin
  174. WriteLn('File not found : "',collationFileName,'"');
  175. Halt(1);
  176. end;
  177. WriteLn(sLineBreak,'Collation Parsing ',QuotedStr(collationFileName),' ...');
  178. stream := nil;
  179. streamNE := nil;
  180. streamOE := nil;
  181. binaryStreamNE := nil;
  182. binaryStreamOE := nil;
  183. collation := TCldrCollation.Create();
  184. try
  185. ParseCollationDocument(collationFileName,collation,TCldrParserMode.HeaderParsing);
  186. WriteLn(Format(' Collation Count = %d',[collation.ItemCount]));
  187. if (collation.ItemCount = 0) then begin
  188. WriteLn('No collation in this file.');
  189. end else begin
  190. for i := 0 to collation.ItemCount - 1 do
  191. WriteLn(Format(' Item[%d] = (Type = %s)',[i, collation.Items[i].TypeName]));
  192. collationItem := collation.Find(collationTypeName);
  193. if (collationItem = nil) then begin
  194. collationTypeName := FindCollationDefaultItemName(collation);
  195. collationItem := collation.Find(collationTypeName);
  196. end;
  197. WriteLn(Format('Parsing Collation Item "%s" ...',[collationTypeName]));
  198. ParseCollationDocument(collationFileName,collationItem,collationTypeName);
  199. s := dataPath + 'UCA_Rules_SHORT.xml';
  200. WriteLn;
  201. WriteLn('Parsing ',QuotedStr(s),' ...');
  202. FillByte(orderedChars,SizeOf(orderedChars),0);
  203. orderedChars.Clear();
  204. ParseInitialDocument(@orderedChars,s);
  205. WriteLn('File parsed, ',orderedChars.ActualLength,' characters.');
  206. WriteLn('Loading CLDR root''s key table ...');
  207. stream := TMemoryStream.Create();
  208. s := dataPath + 'allkeys.txt';
  209. stream.LoadFromFile(s);
  210. ParseUCAFile(stream,ucaBook);
  211. c := FillInitialPositions(@orderedChars.Data[0],orderedChars.ActualLength,ucaBook.Lines);
  212. if (c > 0) then
  213. WriteLn(' Missed Initial Positions = ',c);
  214. WriteLn(' Loaded.');
  215. WriteLn('Start generation ...');
  216. stream.Clear();
  217. streamNE := TMemoryStream.Create();
  218. streamOE := TMemoryStream.Create();
  219. binaryStreamNE := TMemoryStream.Create();
  220. binaryStreamOE := TMemoryStream.Create();
  221. s := COLLATION_FILE_PREFIX + ChangeFileExt(LowerCase(ExtractFileName(collationFileName)),'.pas');
  222. GenerateCdlrCollation(
  223. collation,collationTypeName,s,stream,streamNE,streamOE,
  224. binaryStreamNE,binaryStreamOE,
  225. orderedChars,ucaBook.Lines
  226. );
  227. stream.SaveToFile(ExtractFilePath(collationFileName)+s);
  228. if (streamNE.Size > 0) then begin
  229. streamNE.SaveToFile(ExtractFilePath(collationFileName)+GenerateEndianIncludeFileName(s,ENDIAN_NATIVE));
  230. streamOE.SaveToFile(ExtractFilePath(collationFileName)+GenerateEndianIncludeFileName(s,ENDIAN_NON_NATIVE));
  231. end;
  232. if (binaryStreamNE.Size > 0) then begin
  233. binaryStreamNE.SaveToFile(
  234. ExtractFilePath(collationFileName) +
  235. ChangeFileExt(s,Format('_%s.bco',[ENDIAN_SUFFIX[ENDIAN_NATIVE]]))
  236. );
  237. binaryStreamOE.SaveToFile(
  238. ExtractFilePath(collationFileName) +
  239. ChangeFileExt(s,Format('_%s.bco',[ENDIAN_SUFFIX[ENDIAN_NON_NATIVE]]))
  240. );
  241. end;
  242. end;
  243. finally
  244. binaryStreamOE.Free();
  245. binaryStreamNE.Free();
  246. streamOE.Free();
  247. streamNE.Free();
  248. stream.Free();
  249. collation.Free();
  250. end;
  251. WriteLn(sLineBreak,'Finished.');
  252. end.