creumap.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Florian Klaempfl
  4. It creates pascal units from unicode mapping files
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program creumap;
  12. uses
  13. charset;
  14. procedure doerror;
  15. begin
  16. writeln('Usage: creumap <cpname> <cpnumber>');
  17. writeln('cpname : A mapping file called <cpname>.txt must be present');
  18. writeln('cpnumber : the code page number');
  19. halt(1);
  20. end;
  21. type
  22. TEndianKind = (Little, Big);
  23. const
  24. ENDIAN_SUFFIX : array[TEndianKind] of string[2] = ('le','be');
  25. {$IFDEF ENDIAN_LITTLE}
  26. ENDIAN_NATIVE = TEndianKind.Little;
  27. ENDIAN_NON_NATIVE = TEndianKind.Big;
  28. {$ENDIF ENDIAN_LITTLE}
  29. {$IFDEF ENDIAN_BIG}
  30. ENDIAN_NATIVE = TEndianKind.Big;
  31. ENDIAN_NON_NATIVE = TEndianKind.Little;
  32. {$ENDIF ENDIAN_BIG}
  33. procedure CreateBinaryFile(AMap : punicodemap; const ABaseFile : string);
  34. var
  35. nef, oef : File of Byte;
  36. h, th : TSerializedMapHeader;
  37. k : Longint;
  38. um : tunicodecharmapping;
  39. pum : punicodecharmapping;
  40. rm : treversecharmapping;
  41. prm : preversecharmapping;
  42. begin
  43. FillChar(h,SizeOf(h),0);
  44. h.cpName := AMap^.cpname;
  45. h.cp := AMap^.cp;
  46. h.lastChar := AMap^.lastchar;
  47. h.mapLength := (AMap^.lastchar+1)*SizeOf(tunicodecharmapping);
  48. h.reverseMapLength := AMap^.reversemaplength*SizeOf(treversecharmapping);
  49. Assign(nef,(ABaseFile+'_'+ENDIAN_SUFFIX[ENDIAN_NATIVE]+'.bcm'));
  50. Rewrite(nef);
  51. BlockWrite(nef,h,SizeOf(h));
  52. BlockWrite(nef,AMap^.map^,h.mapLength);
  53. BlockWrite(nef,AMap^.reversemap^,h.reverseMapLength);
  54. Close(nef);
  55. th.cpName := h.cpName;
  56. th.cp := SwapEndian(h.cp);
  57. th.mapLength := SwapEndian(h.mapLength);
  58. th.lastChar := SwapEndian(h.lastChar);
  59. th.reverseMapLength := SwapEndian(h.reverseMapLength);
  60. Assign(oef,(ABaseFile+'_'+ENDIAN_SUFFIX[ENDIAN_NON_NATIVE]+'.bcm'));
  61. Rewrite(oef);
  62. BlockWrite(oef,th,SizeOf(th));
  63. pum := AMap^.map;
  64. for k := 0 to AMap^.lastchar do begin
  65. um.flag := pum^.flag;
  66. um.reserved := pum^.reserved;
  67. um.unicode := SwapEndian(pum^.unicode);
  68. BlockWrite(oef,um,SizeOf(um));
  69. Inc(pum);
  70. end;
  71. prm := AMap^.reversemap;
  72. for k := 0 to AMap^.reversemaplength - 1 do begin
  73. rm.unicode := SwapEndian(prm^.unicode);
  74. rm.char1 := prm^.char1;
  75. rm.char2 := prm^.char2;
  76. BlockWrite(oef,rm,SizeOf(rm));
  77. Inc(prm);
  78. end;
  79. Close(oef);
  80. end;
  81. var
  82. p : punicodemap;
  83. i : longint;
  84. t : text;
  85. e : word;
  86. c : longint;
  87. begin
  88. if paramcount<>2 then
  89. doerror;
  90. Val(paramstr(2),i,e);
  91. if e<>0 then
  92. doerror;
  93. p:=loadunicodemapping(paramstr(1),paramstr(1)+'.txt',i);
  94. if p=nil then
  95. doerror;
  96. assign(t,paramstr(1)+'.pp');
  97. rewrite(t);
  98. writeln(t,'{ This is an automatically created file, so don''t edit it }');
  99. writeln(t,'unit ',p^.cpname,';');
  100. writeln(t);
  101. writeln(t,' interface');
  102. writeln(t);
  103. writeln(t,' implementation');
  104. writeln(t);
  105. writeln(t,' uses');
  106. writeln(t,' charset;');
  107. writeln(t);
  108. writeln(t,' const');
  109. writeln(t,' map : array[0..',p^.lastchar,'] of tunicodecharmapping = (');
  110. for i:=0 to p^.lastchar do
  111. begin
  112. write(t,' (unicode : ',p^.map[i].unicode,'; flag : ');
  113. case p^.map[i].flag of
  114. umf_noinfo : write(t,'umf_noinfo');
  115. umf_leadbyte : write(t,'umf_leadbyte');
  116. umf_undefined : write(t,'umf_undefined');
  117. umf_unused : write(t,'umf_unused');
  118. end;
  119. write(t,'; reserved: 0)');
  120. if i<>p^.lastchar then
  121. writeln(t,',')
  122. else
  123. writeln(t);
  124. end;
  125. writeln(t,' );');
  126. writeln(t);
  127. c:=p^.reversemaplength-1;
  128. writeln(t,' reversemap : array[0..',c,'] of treversecharmapping = (');
  129. for i:=0 to c do
  130. begin
  131. write(t,' (',
  132. 'unicode : ',p^.reversemap[i].unicode,
  133. '; char1 : ',p^.reversemap[i].char1,
  134. '; char2 : ',p^.reversemap[i].char2,
  135. ')'
  136. );
  137. if i<>c then
  138. writeln(t,',')
  139. else
  140. writeln(t);
  141. end;
  142. writeln(t,' );');
  143. writeln(t);
  144. writeln(t,' unicodemap : tunicodemap = (');
  145. writeln(t,' cpname : ''',p^.cpname,''';');
  146. writeln(t,' cp : ',p^.cp,';');
  147. writeln(t,' map : @map;');
  148. writeln(t,' lastchar : ',p^.lastchar,';');
  149. writeln(t,' reversemap : @reversemap;');
  150. writeln(t,' reversemaplength : ',p^.reversemaplength,';');
  151. writeln(t,' next : nil;');
  152. writeln(t,' internalmap : true');
  153. writeln(t,' );');
  154. writeln(t);
  155. writeln(t,' begin');
  156. writeln(t,' registermapping(@unicodemap)');
  157. writeln(t,' end.');
  158. close(t);
  159. CreateBinaryFile(p,paramstr(1));
  160. end.