widestr.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 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,globtype
  24. ;
  25. type
  26. tcompilerwidechar = word;
  27. tcompilerwidecharptr = ^tcompilerwidechar;
  28. pcompilerwidechar = ^tcompilerwidechar;
  29. pcompilerwidestring = ^_tcompilerwidestring;
  30. _tcompilerwidestring = record
  31. data : pcompilerwidechar;
  32. maxlen,len : SizeInt;
  33. end;
  34. procedure initwidestring(var r : pcompilerwidestring);
  35. procedure donewidestring(var r : pcompilerwidestring);
  36. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  37. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  38. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  39. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  40. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  41. procedure copywidestring(s,d : pcompilerwidestring);
  42. function asciichar2unicode(c : char) : tcompilerwidechar;
  43. function unicode2asciichar(c : tcompilerwidechar) : char;
  44. procedure ascii2unicode(p : pchar;l : SizeInt;r : pcompilerwidestring);
  45. procedure unicode2ascii(r : pcompilerwidestring;p : pchar);
  46. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  47. function cpavailable(const s : string) : boolean;
  48. implementation
  49. uses
  50. cp8859_1,cp850,cp437,
  51. globals;
  52. procedure initwidestring(var r : pcompilerwidestring);
  53. begin
  54. new(r);
  55. r^.data:=nil;
  56. r^.len:=0;
  57. r^.maxlen:=0;
  58. end;
  59. procedure donewidestring(var r : pcompilerwidestring);
  60. begin
  61. if assigned(r^.data) then
  62. freemem(r^.data);
  63. dispose(r);
  64. r:=nil;
  65. end;
  66. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  67. begin
  68. getcharwidestring:=r^.data[l];
  69. end;
  70. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  71. begin
  72. getlengthwidestring:=r^.len;
  73. end;
  74. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  75. begin
  76. if r^.maxlen>=l then
  77. exit;
  78. if assigned(r^.data) then
  79. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  80. else
  81. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  82. end;
  83. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  84. begin
  85. if r^.len>=r^.maxlen then
  86. setlengthwidestring(r,r^.len+16);
  87. r^.data[r^.len]:=c;
  88. inc(r^.len);
  89. end;
  90. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  91. begin
  92. setlengthwidestring(s1,s1^.len+s2^.len);
  93. inc(s1^.len,s2^.len);
  94. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  95. end;
  96. procedure copywidestring(s,d : pcompilerwidestring);
  97. begin
  98. setlengthwidestring(d,s^.len);
  99. d^.len:=s^.len;
  100. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  101. end;
  102. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  103. var
  104. maxi,temp : SizeInt;
  105. begin
  106. if pointer(s1)=pointer(s2) then
  107. begin
  108. comparewidestrings:=0;
  109. exit;
  110. end;
  111. maxi:=s1^.len;
  112. temp:=s2^.len;
  113. if maxi>temp then
  114. maxi:=Temp;
  115. temp:=compareword(s1^.data^,s2^.data^,maxi);
  116. if temp=0 then
  117. temp:=s1^.len-s2^.len;
  118. comparewidestrings:=temp;
  119. end;
  120. function asciichar2unicode(c : char) : tcompilerwidechar;
  121. var
  122. m : punicodemap;
  123. begin
  124. m:=getmap(aktsourcecodepage);
  125. asciichar2unicode:=getunicode(c,m);
  126. end;
  127. function unicode2asciichar(c : tcompilerwidechar) : char;
  128. begin
  129. {$ifdef fpc}{$warning todo}{$endif}
  130. unicode2asciichar:=#0;
  131. end;
  132. procedure ascii2unicode(p : pchar;l : SizeInt;r : pcompilerwidestring);
  133. var
  134. source : pchar;
  135. dest : tcompilerwidecharptr;
  136. i : SizeInt;
  137. m : punicodemap;
  138. begin
  139. m:=getmap(aktsourcecodepage);
  140. setlengthwidestring(r,l);
  141. source:=p;
  142. r^.len:=l;
  143. dest:=tcompilerwidecharptr(r^.data);
  144. for i:=1 to l do
  145. begin
  146. dest^:=getunicode(source^,m);
  147. inc(dest);
  148. inc(source);
  149. end;
  150. end;
  151. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  152. (*
  153. var
  154. m : punicodemap;
  155. i : longint;
  156. begin
  157. m:=getmap(aktsourcecodepage);
  158. { should be a very good estimation :) }
  159. setlengthwidestring(r,length(s));
  160. // !!!! MBCS
  161. for i:=1 to length(s) do
  162. begin
  163. end;
  164. end;
  165. *)
  166. var
  167. source : tcompilerwidecharptr;
  168. dest : pchar;
  169. i : longint;
  170. begin
  171. source:=tcompilerwidecharptr(r^.data);
  172. dest:=p;
  173. for i:=1 to r^.len do
  174. begin
  175. if word(source^)<128 then
  176. dest^:=char(word(source^))
  177. else
  178. dest^:=' ';
  179. inc(dest);
  180. inc(source);
  181. end;
  182. end;
  183. function cpavailable(const s : string) : boolean;
  184. begin
  185. cpavailable:=mappingavailable(s);
  186. end;
  187. end.
  188. {
  189. $Log$
  190. Revision 1.16 2004-10-15 09:14:17 mazen
  191. - remove $IFDEF DELPHI and related code
  192. - remove $IFDEF FPCPROCVAR and related code
  193. Revision 1.15 2004/06/20 08:55:30 florian
  194. * logs truncated
  195. Revision 1.14 2004/06/16 20:07:10 florian
  196. * dwarf branch merged
  197. Revision 1.13 2004/05/02 11:48:46 peter
  198. * strlenint is replaced with sizeint
  199. Revision 1.12.2.2 2004/05/02 00:45:51 peter
  200. * define sizeint for 1.0.x
  201. Revision 1.12.2.1 2004/05/02 00:31:33 peter
  202. * fixedi i386 compile
  203. }