widestr.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. This unit contains basic functions for unicode support in the
  4. compiler, this unit is mainly necessary to bootstrap widestring
  5. support ...
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit widestr;
  20. interface
  21. uses
  22. charset,globtype
  23. ;
  24. type
  25. tcompilerwidechar = word;
  26. tcompilerwidecharptr = ^tcompilerwidechar;
  27. pcompilerwidechar = ^tcompilerwidechar;
  28. pcompilerwidestring = ^_tcompilerwidestring;
  29. _tcompilerwidestring = record
  30. data : pcompilerwidechar;
  31. maxlen,len : SizeInt;
  32. end;
  33. procedure initwidestring(var r : pcompilerwidestring);
  34. procedure donewidestring(var r : pcompilerwidestring);
  35. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  36. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  37. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  38. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  39. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  40. procedure copywidestring(s,d : pcompilerwidestring);
  41. function asciichar2unicode(c : char) : tcompilerwidechar;
  42. function unicode2asciichar(c : tcompilerwidechar) : char;
  43. procedure ascii2unicode(p : pchar;l : SizeInt;r : pcompilerwidestring);
  44. procedure unicode2ascii(r : pcompilerwidestring;p : pchar);
  45. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  46. function cpavailable(const s : string) : boolean;
  47. implementation
  48. uses
  49. cp8859_1,cp850,cp437,
  50. globals;
  51. procedure initwidestring(var r : pcompilerwidestring);
  52. begin
  53. new(r);
  54. r^.data:=nil;
  55. r^.len:=0;
  56. r^.maxlen:=0;
  57. end;
  58. procedure donewidestring(var r : pcompilerwidestring);
  59. begin
  60. if assigned(r^.data) then
  61. freemem(r^.data);
  62. dispose(r);
  63. r:=nil;
  64. end;
  65. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  66. begin
  67. getcharwidestring:=r^.data[l];
  68. end;
  69. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  70. begin
  71. getlengthwidestring:=r^.len;
  72. end;
  73. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  74. begin
  75. if r^.maxlen>=l then
  76. exit;
  77. if assigned(r^.data) then
  78. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  79. else
  80. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  81. end;
  82. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  83. begin
  84. if r^.len>=r^.maxlen then
  85. setlengthwidestring(r,r^.len+16);
  86. r^.data[r^.len]:=c;
  87. inc(r^.len);
  88. end;
  89. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  90. begin
  91. setlengthwidestring(s1,s1^.len+s2^.len);
  92. inc(s1^.len,s2^.len);
  93. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  94. end;
  95. procedure copywidestring(s,d : pcompilerwidestring);
  96. begin
  97. setlengthwidestring(d,s^.len);
  98. d^.len:=s^.len;
  99. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  100. end;
  101. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  102. var
  103. maxi,temp : SizeInt;
  104. begin
  105. if pointer(s1)=pointer(s2) then
  106. begin
  107. comparewidestrings:=0;
  108. exit;
  109. end;
  110. maxi:=s1^.len;
  111. temp:=s2^.len;
  112. if maxi>temp then
  113. maxi:=Temp;
  114. temp:=compareword(s1^.data^,s2^.data^,maxi);
  115. if temp=0 then
  116. temp:=s1^.len-s2^.len;
  117. comparewidestrings:=temp;
  118. end;
  119. function asciichar2unicode(c : char) : tcompilerwidechar;
  120. var
  121. m : punicodemap;
  122. begin
  123. m:=getmap(aktsourcecodepage);
  124. asciichar2unicode:=getunicode(c,m);
  125. end;
  126. function unicode2asciichar(c : tcompilerwidechar) : char;
  127. begin
  128. {$ifdef fpc}{$warning todo}{$endif}
  129. unicode2asciichar:=#0;
  130. end;
  131. procedure ascii2unicode(p : pchar;l : SizeInt;r : pcompilerwidestring);
  132. var
  133. source : pchar;
  134. dest : tcompilerwidecharptr;
  135. i : SizeInt;
  136. m : punicodemap;
  137. begin
  138. m:=getmap(aktsourcecodepage);
  139. setlengthwidestring(r,l);
  140. source:=p;
  141. r^.len:=l;
  142. dest:=tcompilerwidecharptr(r^.data);
  143. for i:=1 to l do
  144. begin
  145. dest^:=getunicode(source^,m);
  146. inc(dest);
  147. inc(source);
  148. end;
  149. end;
  150. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  151. (*
  152. var
  153. m : punicodemap;
  154. i : longint;
  155. begin
  156. m:=getmap(aktsourcecodepage);
  157. { should be a very good estimation :) }
  158. setlengthwidestring(r,length(s));
  159. // !!!! MBCS
  160. for i:=1 to length(s) do
  161. begin
  162. end;
  163. end;
  164. *)
  165. var
  166. source : tcompilerwidecharptr;
  167. dest : pchar;
  168. i : longint;
  169. begin
  170. source:=tcompilerwidecharptr(r^.data);
  171. dest:=p;
  172. for i:=1 to r^.len do
  173. begin
  174. if word(source^)<128 then
  175. dest^:=char(word(source^))
  176. else
  177. dest^:=' ';
  178. inc(dest);
  179. inc(source);
  180. end;
  181. end;
  182. function cpavailable(const s : string) : boolean;
  183. begin
  184. cpavailable:=mappingavailable(s);
  185. end;
  186. end.