widestr.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Florian Klaempfl
  4. This unit contains basic functions for unicode support in the
  5. compiler, this unit is mainly necessary to bootstrap widestring
  6. support ...
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit widestr;
  21. interface
  22. { uses
  23. charset;
  24. }
  25. type
  26. tcompilerwidechar = word;
  27. {$ifdef delphi}
  28. { delphi doesn't allow pointer accessing as array }
  29. tcompilerwidechararray = array[0..0] of tcompilerwidechar;
  30. pcompilerwidechar = ^tcompilerwidechararray;
  31. {$else}
  32. pcompilerwidechar = ^tcompilerwidechar;
  33. {$endif}
  34. pcompilerwidestring = ^_tcompilerwidestring;
  35. _tcompilerwidestring = record
  36. data : pcompilerwidechar;
  37. maxlen,len : longint;
  38. end;
  39. procedure initwidestring(var r : pcompilerwidestring);
  40. procedure donewidestring(var r : pcompilerwidestring);
  41. procedure setlengthwidestring(r : pcompilerwidestring;l : longint);
  42. function getlengthwidestring(r : pcompilerwidestring) : longint;
  43. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  44. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  45. function comparewidestrings(s1,s2 : pcompilerwidestring) : longint;
  46. procedure copywidestring(s,d : pcompilerwidestring);
  47. function asciichar2unicode(c : char) : tcompilerwidechar;
  48. function unicode2asciichar(c : tcompilerwidechar) : char;
  49. procedure ascii2unicode(p:pchar; l:longint;r : pcompilerwidestring);
  50. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  51. function getcharwidestring(r : pcompilerwidestring;l : longint) : tcompilerwidechar;
  52. function cpavailable(const s : string) : boolean;
  53. implementation
  54. { uses
  55. i8869_1,cp850,cp437; }
  56. uses
  57. globals;
  58. procedure initwidestring(var r : pcompilerwidestring);
  59. begin
  60. new(r);
  61. r^.data:=nil;
  62. r^.len:=0;
  63. r^.maxlen:=0;
  64. end;
  65. procedure donewidestring(var r : pcompilerwidestring);
  66. begin
  67. if assigned(r^.data) then
  68. freemem(r^.data);
  69. dispose(r);
  70. r:=nil;
  71. end;
  72. function getcharwidestring(r : pcompilerwidestring;l : longint) : tcompilerwidechar;
  73. begin
  74. getcharwidestring:=r^.data[l];
  75. end;
  76. function getlengthwidestring(r : pcompilerwidestring) : longint;
  77. begin
  78. getlengthwidestring:=r^.len;
  79. end;
  80. procedure setlengthwidestring(r : pcompilerwidestring;l : longint);
  81. begin
  82. if r^.maxlen>=l then
  83. exit;
  84. if assigned(r^.data) then
  85. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  86. else
  87. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  88. end;
  89. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  90. begin
  91. if r^.len>=r^.maxlen then
  92. setlengthwidestring(r,r^.len+16);
  93. r^.data[r^.len]:=c;
  94. inc(r^.len);
  95. end;
  96. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  97. begin
  98. setlengthwidestring(s1,s1^.len+s2^.len);
  99. inc(s1^.len,s2^.len);
  100. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  101. end;
  102. function comparewidestringwidestring(s1,s2 : pcompilerwidestring) : longint;
  103. begin
  104. {$ifdef fpc}{$warning todo}{$endif}
  105. comparewidestringwidestring:=0;
  106. end;
  107. procedure copywidestring(s,d : pcompilerwidestring);
  108. begin
  109. setlengthwidestring(d,s^.len);
  110. d^.len:=s^.len;
  111. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  112. end;
  113. function comparewidestrings(s1,s2 : pcompilerwidestring) : longint;
  114. begin
  115. {!!!!!! FIXME }
  116. comparewidestrings:=0;
  117. end;
  118. function asciichar2unicode(c : char) : tcompilerwidechar;
  119. {!!!!!!!!
  120. var
  121. m : punicodemap;
  122. begin
  123. m:=getmap(aktsourcecodepage);
  124. asciichar2unicode:=getunicode(c,m);
  125. end;
  126. }
  127. begin
  128. {$ifdef fpc}{$warning todo}{$endif}
  129. asciichar2unicode:=0;
  130. end;
  131. function unicode2asciichar(c : tcompilerwidechar) : char;
  132. begin
  133. {$ifdef fpc}{$warning todo}{$endif}
  134. unicode2asciichar:=#0;
  135. end;
  136. procedure ascii2unicode(p:pchar; l:longint;r : pcompilerwidestring);
  137. (*
  138. var
  139. m : punicodemap;
  140. i : longint;
  141. begin
  142. m:=getmap(aktsourcecodepage);
  143. { should be a very good estimation :) }
  144. setlengthwidestring(r,length(s));
  145. // !!!! MBCS
  146. for i:=1 to length(s) do
  147. begin
  148. end;
  149. end;
  150. *)
  151. var
  152. source : pchar;
  153. dest : pcompilerwidechar;
  154. i : longint;
  155. begin
  156. setlengthwidestring(r,l);
  157. source:=p;
  158. r^.len:=l;
  159. dest:=r^.data;
  160. for i:=1 to l do
  161. begin
  162. if byte(source^)<128 then
  163. dest^:=tcompilerwidechar(byte(source^))
  164. else
  165. dest^:=32;
  166. inc(dest);
  167. inc(source);
  168. end;
  169. end;
  170. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  171. (*
  172. var
  173. m : punicodemap;
  174. i : longint;
  175. begin
  176. m:=getmap(aktsourcecodepage);
  177. { should be a very good estimation :) }
  178. setlengthwidestring(r,length(s));
  179. // !!!! MBCS
  180. for i:=1 to length(s) do
  181. begin
  182. end;
  183. end;
  184. *)
  185. var
  186. source : pcompilerwidechar;
  187. dest : pchar;
  188. i : longint;
  189. begin
  190. source:=r^.data;
  191. dest:=p;
  192. for i:=1 to r^.len do
  193. begin
  194. if word(source^)<128 then
  195. dest^:=char(word(source^))
  196. else
  197. dest^:=' ';
  198. inc(dest);
  199. inc(source);
  200. end;
  201. end;
  202. function cpavailable(const s : string) : boolean;
  203. {!!!!!!
  204. begin
  205. cpavailable:=mappingavailable(s);
  206. end;
  207. }
  208. begin
  209. cpavailable:=false;
  210. end;
  211. end.
  212. {
  213. $Log$
  214. Revision 1.6 2001-07-08 21:00:16 peter
  215. * various widestring updates, it works now mostly without charset
  216. mapping supported
  217. Revision 1.5 2001/05/27 14:30:55 florian
  218. + some widestring stuff added
  219. Revision 1.4 2001/05/08 21:06:33 florian
  220. * some more support for widechars commited especially
  221. regarding type casting and constants
  222. Revision 1.3 2001/04/13 01:22:17 peter
  223. * symtable change to classes
  224. * range check generation and errors fixed, make cycle DEBUG=1 works
  225. * memory leaks fixed
  226. Revision 1.2 2001/04/02 21:20:35 peter
  227. * resulttype rewrite
  228. Revision 1.1 2000/11/29 00:30:43 florian
  229. * unused units removed from uses clause
  230. * some changes for widestrings
  231. }