widestr.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. tcompilerwidecharptr = ^tcompilerwidechar;
  28. {$ifdef delphi}
  29. { delphi doesn't allow pointer accessing as array }
  30. tcompilerwidechararray = array[0..0] of tcompilerwidechar;
  31. pcompilerwidechar = ^tcompilerwidechararray;
  32. {$else}
  33. pcompilerwidechar = ^tcompilerwidechar;
  34. {$endif}
  35. pcompilerwidestring = ^_tcompilerwidestring;
  36. _tcompilerwidestring = record
  37. data : pcompilerwidechar;
  38. maxlen,len : longint;
  39. end;
  40. procedure initwidestring(var r : pcompilerwidestring);
  41. procedure donewidestring(var r : pcompilerwidestring);
  42. procedure setlengthwidestring(r : pcompilerwidestring;l : longint);
  43. function getlengthwidestring(r : pcompilerwidestring) : longint;
  44. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  45. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  46. function comparewidestrings(s1,s2 : pcompilerwidestring) : longint;
  47. procedure copywidestring(s,d : pcompilerwidestring);
  48. function asciichar2unicode(c : char) : tcompilerwidechar;
  49. function unicode2asciichar(c : tcompilerwidechar) : char;
  50. procedure ascii2unicode(p:pchar; l:longint;r : pcompilerwidestring);
  51. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  52. function getcharwidestring(r : pcompilerwidestring;l : longint) : tcompilerwidechar;
  53. function cpavailable(const s : string) : boolean;
  54. implementation
  55. { uses
  56. i8869_1,cp850,cp437; }
  57. uses
  58. globals;
  59. procedure initwidestring(var r : pcompilerwidestring);
  60. begin
  61. new(r);
  62. r^.data:=nil;
  63. r^.len:=0;
  64. r^.maxlen:=0;
  65. end;
  66. procedure donewidestring(var r : pcompilerwidestring);
  67. begin
  68. if assigned(r^.data) then
  69. freemem(r^.data);
  70. dispose(r);
  71. r:=nil;
  72. end;
  73. function getcharwidestring(r : pcompilerwidestring;l : longint) : tcompilerwidechar;
  74. begin
  75. getcharwidestring:=r^.data[l];
  76. end;
  77. function getlengthwidestring(r : pcompilerwidestring) : longint;
  78. begin
  79. getlengthwidestring:=r^.len;
  80. end;
  81. procedure setlengthwidestring(r : pcompilerwidestring;l : longint);
  82. begin
  83. if r^.maxlen>=l then
  84. exit;
  85. if assigned(r^.data) then
  86. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  87. else
  88. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  89. end;
  90. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  91. begin
  92. if r^.len>=r^.maxlen then
  93. setlengthwidestring(r,r^.len+16);
  94. r^.data[r^.len]:=c;
  95. inc(r^.len);
  96. end;
  97. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  98. begin
  99. setlengthwidestring(s1,s1^.len+s2^.len);
  100. inc(s1^.len,s2^.len);
  101. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  102. end;
  103. function comparewidestringwidestring(s1,s2 : pcompilerwidestring) : longint;
  104. begin
  105. {$ifdef fpc}{$warning todo}{$endif}
  106. comparewidestringwidestring:=0;
  107. end;
  108. procedure copywidestring(s,d : pcompilerwidestring);
  109. begin
  110. setlengthwidestring(d,s^.len);
  111. d^.len:=s^.len;
  112. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  113. end;
  114. function comparewidestrings(s1,s2 : pcompilerwidestring) : longint;
  115. begin
  116. {!!!!!! FIXME }
  117. comparewidestrings:=0;
  118. end;
  119. function asciichar2unicode(c : char) : tcompilerwidechar;
  120. {!!!!!!!!
  121. var
  122. m : punicodemap;
  123. begin
  124. m:=getmap(aktsourcecodepage);
  125. asciichar2unicode:=getunicode(c,m);
  126. end;
  127. }
  128. begin
  129. {$ifdef fpc}{$warning todo}{$endif}
  130. asciichar2unicode:=0;
  131. end;
  132. function unicode2asciichar(c : tcompilerwidechar) : char;
  133. begin
  134. {$ifdef fpc}{$warning todo}{$endif}
  135. unicode2asciichar:=#0;
  136. end;
  137. procedure ascii2unicode(p:pchar; l:longint;r : pcompilerwidestring);
  138. (*
  139. var
  140. m : punicodemap;
  141. i : longint;
  142. begin
  143. m:=getmap(aktsourcecodepage);
  144. { should be a very good estimation :) }
  145. setlengthwidestring(r,length(s));
  146. // !!!! MBCS
  147. for i:=1 to length(s) do
  148. begin
  149. end;
  150. end;
  151. *)
  152. var
  153. source : pchar;
  154. dest : tcompilerwidecharptr;
  155. i : longint;
  156. begin
  157. setlengthwidestring(r,l);
  158. source:=p;
  159. r^.len:=l;
  160. dest:=tcompilerwidecharptr(r^.data);
  161. for i:=1 to l do
  162. begin
  163. if byte(source^)<128 then
  164. dest^:=tcompilerwidechar(byte(source^))
  165. else
  166. dest^:=32;
  167. inc(dest);
  168. inc(source);
  169. end;
  170. end;
  171. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  172. (*
  173. var
  174. m : punicodemap;
  175. i : longint;
  176. begin
  177. m:=getmap(aktsourcecodepage);
  178. { should be a very good estimation :) }
  179. setlengthwidestring(r,length(s));
  180. // !!!! MBCS
  181. for i:=1 to length(s) do
  182. begin
  183. end;
  184. end;
  185. *)
  186. var
  187. source : tcompilerwidecharptr;
  188. dest : pchar;
  189. i : longint;
  190. begin
  191. source:=tcompilerwidecharptr(r^.data);
  192. dest:=p;
  193. for i:=1 to r^.len do
  194. begin
  195. if word(source^)<128 then
  196. dest^:=char(word(source^))
  197. else
  198. dest^:=' ';
  199. inc(dest);
  200. inc(source);
  201. end;
  202. end;
  203. function cpavailable(const s : string) : boolean;
  204. {!!!!!!
  205. begin
  206. cpavailable:=mappingavailable(s);
  207. end;
  208. }
  209. begin
  210. cpavailable:=false;
  211. end;
  212. end.
  213. {
  214. $Log$
  215. Revision 1.7 2001-09-02 21:16:25 peter
  216. * delphi fixes
  217. Revision 1.6 2001/07/08 21:00:16 peter
  218. * various widestring updates, it works now mostly without charset
  219. mapping supported
  220. Revision 1.5 2001/05/27 14:30:55 florian
  221. + some widestring stuff added
  222. Revision 1.4 2001/05/08 21:06:33 florian
  223. * some more support for widechars commited especially
  224. regarding type casting and constants
  225. Revision 1.3 2001/04/13 01:22:17 peter
  226. * symtable change to classes
  227. * range check generation and errors fixed, make cycle DEBUG=1 works
  228. * memory leaks fixed
  229. Revision 1.2 2001/04/02 21:20:35 peter
  230. * resulttype rewrite
  231. Revision 1.1 2000/11/29 00:30:43 florian
  232. * unused units removed from uses clause
  233. * some changes for widestrings
  234. }