widestr.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 : tcompilerwidestring);
  40. procedure donewidestring(var r : tcompilerwidestring);
  41. procedure setlengthwidestring(var r : tcompilerwidestring;l : longint);
  42. function getlengthwidestring(const r : tcompilerwidestring) : longint;
  43. procedure concatwidestringchar(var r : tcompilerwidestring;c : tcompilerwidechar);
  44. procedure concatwidestrings(const s1,s2 : tcompilerwidestring;
  45. var r : tcompilerwidestring);
  46. function comparewidestrings(const s1,s2 : tcompilerwidestring) : shortint;
  47. procedure copywidestring(const s : tcompilerwidestring;var d : tcompilerwidestring);
  48. function asciichar2unicode(c : char) : tcompilerwidechar;
  49. function unicode2asciichar(c : tcompilerwidechar) : char;
  50. procedure ascii2unicode(const s : string;var r : tcompilerwidestring);
  51. function getcharwidestring(const r : tcompilerwidestring;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 : tcompilerwidestring);
  59. begin
  60. r.data:=nil;
  61. r.len:=0;
  62. r.maxlen:=0;
  63. end;
  64. procedure donewidestring(var r : tcompilerwidestring);
  65. begin
  66. if assigned(r.data) then
  67. freemem(r.data);
  68. r.data:=nil;
  69. r.maxlen:=0;
  70. r.len:=0;
  71. end;
  72. function getcharwidestring(const r : tcompilerwidestring;l : longint) : tcompilerwidechar;
  73. begin
  74. getcharwidestring:=r.data[l];
  75. end;
  76. function getlengthwidestring(const r : tcompilerwidestring) : longint;
  77. begin
  78. getlengthwidestring:=r.len;
  79. end;
  80. procedure setlengthwidestring(var r : tcompilerwidestring;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(var r : tcompilerwidestring;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(const s1,s2 : tcompilerwidestring;
  97. var r : tcompilerwidestring);
  98. begin
  99. setlengthwidestring(r,s1.len+s2.len);
  100. r.len:=s1.len+s2.len;
  101. move(s1.data^,r.data^,s1.len*2);
  102. move(s2.data^,r.data[s1.len],s2.len*2);
  103. end;
  104. function comparewidestringwidestring(const s1,s2 : tcompilerwidestring) : longint;
  105. begin
  106. {$ifdef fpc}{$warning todo}{$endif}
  107. comparewidestringwidestring:=0;
  108. end;
  109. procedure copywidestring(const s : tcompilerwidestring;var d : tcompilerwidestring);
  110. begin
  111. setlengthwidestring(d,s.len);
  112. d.len:=s.len;
  113. move(s.data^,d.data^,s.len);
  114. end;
  115. function comparewidestrings(const s1,s2 : tcompilerwidestring) : shortint;
  116. begin
  117. {!!!!!! FIXME }
  118. comparewidestrings:=0;
  119. end;
  120. function asciichar2unicode(c : char) : tcompilerwidechar;
  121. {!!!!!!!!
  122. var
  123. m : punicodemap;
  124. begin
  125. m:=getmap(aktsourcecodepage);
  126. asciichar2unicode:=getunicode(c,m);
  127. end;
  128. }
  129. begin
  130. {$ifdef fpc}{$warning todo}{$endif}
  131. asciichar2unicode:=0;
  132. end;
  133. function unicode2asciichar(c : tcompilerwidechar) : char;
  134. begin
  135. end;
  136. procedure ascii2unicode(const s : string;var r : tcompilerwidestring);
  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. begin
  152. end;
  153. function cpavailable(const s : string) : boolean;
  154. {!!!!!!
  155. begin
  156. cpavailable:=mappingavailable(s);
  157. end;
  158. }
  159. begin
  160. cpavailable:=false;
  161. end;
  162. end.
  163. {
  164. $Log$
  165. Revision 1.5 2001-05-27 14:30:55 florian
  166. + some widestring stuff added
  167. Revision 1.4 2001/05/08 21:06:33 florian
  168. * some more support for widechars commited especially
  169. regarding type casting and constants
  170. Revision 1.3 2001/04/13 01:22:17 peter
  171. * symtable change to classes
  172. * range check generation and errors fixed, make cycle DEBUG=1 works
  173. * memory leaks fixed
  174. Revision 1.2 2001/04/02 21:20:35 peter
  175. * resulttype rewrite
  176. Revision 1.1 2000/11/29 00:30:43 florian
  177. * unused units removed from uses clause
  178. * some changes for widestrings
  179. }