cldrparser.lpr 11 KB

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