creumap.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. FillChar(th,SizeOf(th),0);
  56. th.cpName := h.cpName;
  57. th.cp := SwapEndian(h.cp);
  58. th.mapLength := SwapEndian(h.mapLength);
  59. th.lastChar := SwapEndian(h.lastChar);
  60. th.reverseMapLength := SwapEndian(h.reverseMapLength);
  61. Assign(oef,(ABaseFile+'_'+ENDIAN_SUFFIX[ENDIAN_NON_NATIVE]+'.bcm'));
  62. Rewrite(oef);
  63. BlockWrite(oef,th,SizeOf(th));
  64. pum := AMap^.map;
  65. FillChar(um,SizeOf(um),0);
  66. for k := 0 to AMap^.lastchar do begin
  67. um.flag := pum^.flag;
  68. um.reserved := pum^.reserved;
  69. um.unicode := SwapEndian(pum^.unicode);
  70. BlockWrite(oef,um,SizeOf(um));
  71. Inc(pum);
  72. end;
  73. prm := AMap^.reversemap;
  74. FillChar(rm,SizeOf(rm),0);
  75. for k := 0 to AMap^.reversemaplength - 1 do begin
  76. rm.unicode := SwapEndian(prm^.unicode);
  77. rm.char1 := prm^.char1;
  78. rm.char2 := prm^.char2;
  79. BlockWrite(oef,rm,SizeOf(rm));
  80. Inc(prm);
  81. end;
  82. Close(oef);
  83. end;
  84. var
  85. p : punicodemap;
  86. i : longint;
  87. t : text;
  88. e : word;
  89. c : longint;
  90. begin
  91. if paramcount<>2 then
  92. doerror;
  93. Val(paramstr(2),i,e);
  94. if e<>0 then
  95. doerror;
  96. p:=loadunicodemapping(paramstr(1),paramstr(1)+'.txt',i);
  97. if p=nil then
  98. doerror;
  99. assign(t,paramstr(1)+'.pp');
  100. rewrite(t);
  101. writeln(t,'{ This is an automatically created file, so don''t edit it }');
  102. writeln(t,'unit ',p^.cpname,';');
  103. writeln(t);
  104. writeln(t,' interface');
  105. writeln(t);
  106. writeln(t,' implementation');
  107. writeln(t);
  108. writeln(t,' uses');
  109. writeln(t,' charset;');
  110. writeln(t);
  111. writeln(t,' const');
  112. writeln(t,' map : array[0..',p^.lastchar,'] of tunicodecharmapping = (');
  113. for i:=0 to p^.lastchar do
  114. begin
  115. write(t,' (unicode : ',p^.map[i].unicode,'; flag : ');
  116. case p^.map[i].flag of
  117. umf_noinfo : write(t,'umf_noinfo');
  118. umf_leadbyte : write(t,'umf_leadbyte');
  119. umf_undefined : write(t,'umf_undefined');
  120. umf_unused : write(t,'umf_unused');
  121. end;
  122. write(t,'; reserved: 0)');
  123. if i<>p^.lastchar then
  124. writeln(t,',')
  125. else
  126. writeln(t);
  127. end;
  128. writeln(t,' );');
  129. writeln(t);
  130. c:=p^.reversemaplength-1;
  131. writeln(t,' reversemap : array[0..',c,'] of treversecharmapping = (');
  132. for i:=0 to c do
  133. begin
  134. write(t,' (',
  135. 'unicode : ',p^.reversemap[i].unicode,
  136. '; char1 : ',p^.reversemap[i].char1,
  137. '; char2 : ',p^.reversemap[i].char2,
  138. ')'
  139. );
  140. if i<>c then
  141. writeln(t,',')
  142. else
  143. writeln(t);
  144. end;
  145. writeln(t,' );');
  146. writeln(t);
  147. writeln(t,' unicodemap : tunicodemap = (');
  148. writeln(t,' cpname : ''',p^.cpname,''';');
  149. writeln(t,' cp : ',p^.cp,';');
  150. writeln(t,' map : @map;');
  151. writeln(t,' lastchar : ',p^.lastchar,';');
  152. writeln(t,' reversemap : @reversemap;');
  153. writeln(t,' reversemaplength : ',p^.reversemaplength,';');
  154. writeln(t,' next : nil;');
  155. writeln(t,' internalmap : true');
  156. writeln(t,' );');
  157. writeln(t);
  158. writeln(t,' begin');
  159. writeln(t,' registermapping(@unicodemap)');
  160. writeln(t,' end.');
  161. close(t);
  162. CreateBinaryFile(p,paramstr(1));
  163. end.