widestr.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 concatwidestringwidestring(const s1,s2 : tcompilerwidestring;
  45. var r : tcompilerwidestring);
  46. procedure copywidestring(const s : tcompilerwidestring;var d : tcompilerwidestring);
  47. function asciichar2unicode(c : char) : tcompilerwidechar;
  48. function unicode2asciichar(c : tcompilerwidechar) : char;
  49. procedure ascii2unicode(const s : string;var r : tcompilerwidestring);
  50. function getcharwidestring(const r : tcompilerwidestring;l : longint) : tcompilerwidechar;
  51. function cpavailable(const s : string) : boolean;
  52. implementation
  53. { uses
  54. i8869_1,cp850,cp437; }
  55. uses
  56. globals;
  57. procedure initwidestring(var r : tcompilerwidestring);
  58. begin
  59. r.data:=nil;
  60. r.len:=0;
  61. r.maxlen:=0;
  62. end;
  63. procedure donewidestring(var r : tcompilerwidestring);
  64. begin
  65. if assigned(r.data) then
  66. freemem(r.data);
  67. r.data:=nil;
  68. r.maxlen:=0;
  69. r.len:=0;
  70. end;
  71. function getcharwidestring(const r : tcompilerwidestring;l : longint) : tcompilerwidechar;
  72. begin
  73. getcharwidestring:=r.data[l];
  74. end;
  75. function getlengthwidestring(const r : tcompilerwidestring) : longint;
  76. begin
  77. getlengthwidestring:=r.len;
  78. end;
  79. procedure setlengthwidestring(var r : tcompilerwidestring;l : longint);
  80. begin
  81. if r.maxlen>=l then
  82. exit;
  83. if assigned(r.data) then
  84. reallocmem(r.data,sizeof(tcompilerwidechar)*l)
  85. else
  86. getmem(r.data,sizeof(tcompilerwidechar)*l);
  87. end;
  88. procedure concatwidestringchar(var r : tcompilerwidestring;c : tcompilerwidechar);
  89. begin
  90. if r.len>=r.maxlen then
  91. setlengthwidestring(r,r.len+16);
  92. r.data[r.len]:=c;
  93. inc(r.len);
  94. end;
  95. procedure concatwidestringwidestring(const s1,s2 : tcompilerwidestring;
  96. var r : tcompilerwidestring);
  97. begin
  98. setlengthwidestring(r,s1.len+s2.len);
  99. r.len:=s1.len+s2.len;
  100. move(s1.data^,r.data^,s1.len);
  101. move(s2.data^,r.data[s1.len],s2.len);
  102. end;
  103. function comparewidestringwidestring(const s1,s2 : tcompilerwidestring) : longint;
  104. begin
  105. {$ifdef fpc}{$warning todo}{$endif}
  106. comparewidestringwidestring:=0;
  107. end;
  108. procedure copywidestring(const s : tcompilerwidestring;var d : tcompilerwidestring);
  109. begin
  110. setlengthwidestring(d,s.len);
  111. d.len:=s.len;
  112. move(s.data^,d.data^,s.len);
  113. end;
  114. function asciichar2unicode(c : char) : tcompilerwidechar;
  115. {!!!!!!!!
  116. var
  117. m : punicodemap;
  118. begin
  119. m:=getmap(aktsourcecodepage);
  120. asciichar2unicode:=getunicode(c,m);
  121. end;
  122. }
  123. begin
  124. {$ifdef fpc}{$warning todo}{$endif}
  125. asciichar2unicode:=0;
  126. end;
  127. function unicode2asciichar(c : tcompilerwidechar) : char;
  128. begin
  129. end;
  130. procedure ascii2unicode(const s : string;var r : tcompilerwidestring);
  131. (*
  132. var
  133. m : punicodemap;
  134. i : longint;
  135. begin
  136. m:=getmap(aktsourcecodepage);
  137. { should be a very good estimation :) }
  138. setlengthwidestring(r,length(s));
  139. // !!!! MBCS
  140. for i:=1 to length(s) do
  141. begin
  142. end;
  143. end;
  144. *)
  145. begin
  146. end;
  147. function cpavailable(const s : string) : boolean;
  148. {!!!!!!
  149. begin
  150. cpavailable:=mappingavailable(s);
  151. end;
  152. }
  153. begin
  154. cpavailable:=false;
  155. end;
  156. end.
  157. {
  158. $Log$
  159. Revision 1.4 2001-05-08 21:06:33 florian
  160. * some more support for widechars commited especially
  161. regarding type casting and constants
  162. Revision 1.3 2001/04/13 01:22:17 peter
  163. * symtable change to classes
  164. * range check generation and errors fixed, make cycle DEBUG=1 works
  165. * memory leaks fixed
  166. Revision 1.2 2001/04/02 21:20:35 peter
  167. * resulttype rewrite
  168. Revision 1.1 2000/11/29 00:30:43 florian
  169. * unused units removed from uses clause
  170. * some changes for widestrings
  171. }