cldrparser.lpr 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. { Unicode CLDR's collation parser.
  2. Copyright (c) 2013-2017 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 //heaptrc,
  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.txt ' + sLineBreak +
  50. ' - allkeys.txt this is the file allkeys_CLDR.txt renamed to allkeys.txt' + sLineBreak +
  51. ' - PropList.txt ' + sLineBreak +
  52. ' These files are in the core.zip file of the CLDR release files, except the PropList.txt which' + sLineBreak +
  53. ' is from the UCA. The CLDR''version used should be synchronized the' + sLineBreak +
  54. ' version of the Unicode version used, for example for Uniocde 14.0.0 it will be CLDR 40.' + sLineBreak +
  55. ' The CLDR files are provided by the Unicode Consortium at http://cldr.unicode.org/index/downloads';
  56. function ParseOptions(
  57. var ADataDir,
  58. AOuputDir,
  59. ACollationFileName,
  60. ACollationTypeName,
  61. ACollationTypeAlt : string;
  62. var AExecTestSuite,
  63. ATestHaltOnFail : Boolean
  64. ) : Boolean;
  65. var
  66. c : Char;
  67. idx, k : Integer;
  68. s : string;
  69. begin
  70. {$ifdef WINCE_TEST}
  71. ADataDir := ExtractFilePath(ParamStr(0))+'data';
  72. AOuputDir := ADataDir;
  73. ACollationFileName := 'sv.xml';
  74. exit(True);
  75. {$endif WINCE_TEST}
  76. if (ParamCount() = 0) then
  77. exit(False);
  78. Result := True;
  79. AExecTestSuite := False;
  80. repeat
  81. c := GetOpt('a:d:o:ht:');
  82. case c of
  83. 'a' : ACollationTypeAlt := Trim(OptArg);
  84. 'd' : ADataDir := ExpandFileName(Trim(OptArg));
  85. 'o' : AOuputDir := ExpandFileName(Trim(OptArg));
  86. 'h', '?' :
  87. begin
  88. WriteLn(SUsageText);
  89. Result := False;
  90. end;
  91. 't' :
  92. begin
  93. AExecTestSuite := True;
  94. s := Trim(OptArg);
  95. ATestHaltOnFail := (s <> '') and CharInSet(s[1],['y','Y','t','T','1']);
  96. end;
  97. end;
  98. until (c = EndOfOptions);
  99. idx := 0;
  100. for k := 1 to ParamCount() do begin
  101. s := Trim(ParamStr(k));
  102. if (s <> '') and (s[1] <> '-') then begin
  103. if (idx = 0) then
  104. ACollationFileName := s
  105. else if (idx = 1) then
  106. ACollationTypeName := s;
  107. Inc(idx);
  108. if (idx >= 2) then
  109. Break;
  110. end;
  111. end;
  112. end;
  113. procedure Main;
  114. var
  115. propList : TPropListLineRecArray;
  116. unifiedIdeographCodePoints : TCodePointRecArray;
  117. orderedChars : TOrderedCharacters;
  118. settings : TSettingRecArray;
  119. ucaBook : TUCA_DataBook;
  120. stream, streamNE, streamOE, binaryStreamNE, binaryStreamOE : TMemoryStream;
  121. s, collationFileName, collationTypeName, collationTypeAlt : string;
  122. i , c: Integer;
  123. repository : TCldrCollationRepository;
  124. collation : TCldrCollation;
  125. dataPath, outputPath : string;
  126. collationItem : TCldrCollationItem;
  127. testSuiteFlag, testSuiteHaltOnFailFlag : Boolean;
  128. {$ifdef WINCE}
  129. fs : TFileStream;
  130. {$endif WINCE}
  131. begin
  132. {$ifdef WINCE}
  133. s := ExtractFilePath(ParamStr(0))+'cldr-log.txt';
  134. DeleteFile(s);
  135. fs := TFileStream.Create(s,fmCreate);
  136. AssignStream(Output,fs);
  137. Rewrite(Output);
  138. s := ExtractFilePath(ParamStr(0))+'cldr-err.txt';
  139. DeleteFile(s);
  140. fs := TFileStream.Create(s,fmCreate);
  141. AssignStream(ErrOutput,fs);
  142. Rewrite(ErrOutput);
  143. {$endif WINCE}
  144. {$ifdef WINCE_TEST}
  145. testSuiteFlag := True;
  146. try
  147. exec_tests();
  148. except
  149. on e : Exception do begin
  150. WriteLn('Exception : '+e.Message);
  151. raise;
  152. end;
  153. end;
  154. exit;
  155. {$endif WINCE_TEST}
  156. dataPath := '';
  157. outputPath := '';
  158. collationFileName := '';
  159. collationTypeName := '';
  160. collationTypeAlt := '';
  161. testSuiteFlag := False;
  162. testSuiteHaltOnFailFlag := True;
  163. if not ParseOptions(
  164. dataPath,outputPath,collationFileName,collationTypeName,
  165. collationTypeAlt,testSuiteFlag,testSuiteHaltOnFailFlag
  166. )
  167. then begin
  168. WriteLn(SUsageText);
  169. Halt(1);
  170. end;
  171. if testSuiteFlag then begin
  172. WriteLn('Executing the test suite ...');
  173. exec_tests(testSuiteHaltOnFailFlag);
  174. Halt;
  175. end;
  176. if (dataPath <> '') and not(DirectoryExists(dataPath)) then begin
  177. WriteLn('This directory does not exist : ',dataPath);
  178. Halt(1);
  179. end;
  180. if (dataPath = '') then
  181. dataPath := ExtractFilePath(ParamStr(0))
  182. else
  183. dataPath := IncludeTrailingPathDelimiter(dataPath);
  184. if (outputPath = '') then
  185. outputPath := dataPath
  186. else
  187. outputPath := IncludeTrailingPathDelimiter(outputPath);
  188. {$ifndef WINCE_TEST}
  189. if (ParamCount() = 0) then begin
  190. WriteLn(SUsageText);
  191. Halt(1);
  192. end;
  193. {$endif WINCE_TEST}
  194. if not(
  195. FileExists(dataPath+SROOT_RULES_FILE) and
  196. FileExists(dataPath+'allkeys.txt') and
  197. FileExists(dataPath+'PropList.txt')
  198. )
  199. then begin
  200. WriteLn(
  201. Format(
  202. 'File not found : %s or %s or %s.',
  203. [dataPath+SROOT_RULES_FILE,dataPath+'allkeys.txt',dataPath+'PropList.txt']
  204. )
  205. );
  206. Halt(1);
  207. end;
  208. {collationFileName := dataPath + collationFileName;
  209. if not FileExists(collationFileName) then begin
  210. WriteLn('File not found : "',collationFileName,'"');
  211. Halt(1);
  212. end;}
  213. WriteLn(sLineBreak,'Collation Parsing ',QuotedStr(collationFileName),' ...');
  214. stream := nil;
  215. streamNE := nil;
  216. streamOE := nil;
  217. binaryStreamNE := nil;
  218. binaryStreamOE := nil;
  219. repository := TCldrCollationRepository.Create(
  220. TCldrCollationFileLoader.Create(dataPath) as ICldrCollationLoader
  221. );
  222. try
  223. collation := repository.Load(collationFileName,TCldrParserMode.HeaderParsing);
  224. WriteLn(Format(' Collation Count = %d',[collation.FindPublicItemCount()]));
  225. if (collation.FindPublicItemCount() = 0) then begin
  226. WriteLn('No collation in this file.');
  227. end else begin
  228. for i := 0 to collation.ItemCount - 1 do begin
  229. if not collation.Items[i].IsPrivate() then begin
  230. s := collation.Items[i].TypeName;
  231. if (collation.Items[i].Alt <> '') then
  232. s := s + ', Alt = ' + collation.Items[i].Alt;
  233. WriteLn(Format(' Item[%d] = (Type = %s)',[i,s]));
  234. end;
  235. end;
  236. if (collationTypeAlt = '') then
  237. collationItem := collation.Find(collationTypeName)
  238. else
  239. collationItem := collation.Find(collationTypeName,collationTypeAlt);
  240. if (collationItem = nil) then begin
  241. collationTypeName := FindCollationDefaultItemName(collation);
  242. collationItem := collation.Find(collationTypeName);
  243. collationTypeAlt := collationItem.Alt;
  244. end;
  245. s := collationTypeName;
  246. if (collationTypeAlt <> '') then
  247. s := Format('%s (%s)',[s,collationTypeAlt]);
  248. WriteLn(Format('Parsing Collation Item "%s" ...',[s]));
  249. collationItem := repository.LoadType(collationFileName,collationTypeName,collationTypeAlt);
  250. stream := TMemoryStream.Create();
  251. s := dataPath + 'PropList.txt';
  252. WriteLn('Load file PropList.txt ...', DateTimeToStr(Now));
  253. stream.Clear();
  254. stream.LoadFromFile(s);
  255. stream.Position := 0;
  256. propList := nil;
  257. ParseProps(stream,propList);
  258. stream.Clear();
  259. unifiedIdeographCodePoints := FindCodePointsByProperty('Unified_Ideograph',propList);
  260. s := dataPath + SROOT_RULES_FILE;
  261. WriteLn;
  262. WriteLn('Parsing ',QuotedStr(s),' ...');
  263. FillByte(orderedChars,SizeOf(orderedChars),0);
  264. orderedChars.Clear();
  265. SetLength(settings,0);
  266. ParseInitialDocument(@orderedChars,s,settings);
  267. WriteLn('File parsed, ',orderedChars.ActualLength,' characters.');
  268. WriteLn('Loading CLDR root''s key table ...');
  269. stream.Clear();
  270. s := dataPath + 'allkeys.txt';
  271. stream.LoadFromFile(s);
  272. FillChar(ucaBook,SizeOf(ucaBook),#0);
  273. ParseUCAFile(stream,ucaBook);
  274. //WriteLn(' LEVEL-2''s items Value = ',CalcMaxLevel2Value(ucaBook.Lines));
  275. //RewriteLevel2Values(@ucaBook.Lines[0],Length(ucaBook.Lines));
  276. //WriteLn(' LEVEL-2''s items Value (after rewrite) = ',CalcMaxLevel2Value(ucaBook.Lines));
  277. c := FillInitialPositions(@orderedChars.Data[0],orderedChars.ActualLength,ucaBook.Lines);
  278. if (c > 0) then
  279. WriteLn(' Missed Initial Positions = ',c);
  280. WriteLn(' Loaded.');
  281. WriteLn('Start generation ...');
  282. stream.Clear();
  283. streamNE := TMemoryStream.Create();
  284. streamOE := TMemoryStream.Create();
  285. binaryStreamNE := TMemoryStream.Create();
  286. binaryStreamOE := TMemoryStream.Create();
  287. s := COLLATION_FILE_PREFIX + ChangeFileExt(LowerCase(ExtractFileName(collationFileName)),'.pas');
  288. GenerateCdlrCollation(
  289. collation,collationTypeName,s,stream,streamNE,streamOE,
  290. binaryStreamNE,binaryStreamOE,
  291. orderedChars,ucaBook.Lines,unifiedIdeographCodePoints
  292. );
  293. stream.SaveToFile(outputPath+s);
  294. if (streamNE.Size > 0) then begin
  295. streamNE.SaveToFile(outputPath+GenerateEndianIncludeFileName(s,ENDIAN_NATIVE));
  296. streamOE.SaveToFile(outputPath+GenerateEndianIncludeFileName(s,ENDIAN_NON_NATIVE));
  297. end;
  298. if (binaryStreamNE.Size > 0) then begin
  299. binaryStreamNE.SaveToFile(
  300. outputPath +
  301. ChangeFileExt(s,Format('_%s.bco',[ENDIAN_SUFFIX[ENDIAN_NATIVE]]))
  302. );
  303. binaryStreamOE.SaveToFile(
  304. outputPath +
  305. ChangeFileExt(s,Format('_%s.bco',[ENDIAN_SUFFIX[ENDIAN_NON_NATIVE]]))
  306. );
  307. end;
  308. end;
  309. repository.Clear();
  310. finally
  311. binaryStreamOE.Free();
  312. binaryStreamNE.Free();
  313. streamOE.Free();
  314. streamNE.Free();
  315. stream.Free();
  316. repository.Free();
  317. end;
  318. WriteLn(sLineBreak,'Finished.');
  319. end;
  320. begin
  321. Main();
  322. end.