widestr.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. {$ifdef VER2_2}ccharset{$else VER2_2}charset{$endif VER2_2},globtype;
  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(out 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 hasnonasciichars(const p: pcompilerwidestring): boolean;
  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. { cyrillic code pages }
  52. cp1251,cp866,cp8859_5,
  53. globals,cutils;
  54. procedure initwidestring(out r : pcompilerwidestring);
  55. begin
  56. new(r);
  57. r^.data:=nil;
  58. r^.len:=0;
  59. r^.maxlen:=0;
  60. end;
  61. procedure donewidestring(var r : pcompilerwidestring);
  62. begin
  63. if assigned(r^.data) then
  64. freemem(r^.data);
  65. dispose(r);
  66. r:=nil;
  67. end;
  68. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  69. begin
  70. getcharwidestring:=r^.data[l];
  71. end;
  72. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  73. begin
  74. getlengthwidestring:=r^.len;
  75. end;
  76. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  77. begin
  78. if r^.maxlen>=l then
  79. exit;
  80. if assigned(r^.data) then
  81. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  82. else
  83. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  84. r^.maxlen:=l;
  85. end;
  86. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  87. begin
  88. if r^.len>=r^.maxlen then
  89. setlengthwidestring(r,r^.len+16);
  90. r^.data[r^.len]:=c;
  91. inc(r^.len);
  92. end;
  93. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  94. begin
  95. setlengthwidestring(s1,s1^.len+s2^.len);
  96. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  97. inc(s1^.len,s2^.len);
  98. end;
  99. procedure copywidestring(s,d : pcompilerwidestring);
  100. begin
  101. setlengthwidestring(d,s^.len);
  102. d^.len:=s^.len;
  103. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  104. end;
  105. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  106. var
  107. maxi,temp : SizeInt;
  108. begin
  109. if pointer(s1)=pointer(s2) then
  110. begin
  111. comparewidestrings:=0;
  112. exit;
  113. end;
  114. maxi:=s1^.len;
  115. temp:=s2^.len;
  116. if maxi>temp then
  117. maxi:=Temp;
  118. temp:=compareword(s1^.data^,s2^.data^,maxi);
  119. if temp=0 then
  120. temp:=s1^.len-s2^.len;
  121. comparewidestrings:=temp;
  122. end;
  123. function asciichar2unicode(c : char) : tcompilerwidechar;
  124. var
  125. m : punicodemap;
  126. begin
  127. if (current_settings.sourcecodepage <> 'utf8') then
  128. begin
  129. m:=getmap(current_settings.sourcecodepage);
  130. asciichar2unicode:=getunicode(c,m);
  131. end
  132. else
  133. result:=tcompilerwidechar(c);
  134. end;
  135. function unicode2asciichar(c : tcompilerwidechar) : char;
  136. begin
  137. if word(c)<128 then
  138. unicode2asciichar:=char(word(c))
  139. else
  140. unicode2asciichar:='?';
  141. end;
  142. procedure ascii2unicode(p : pchar;l : SizeInt;r : pcompilerwidestring);
  143. var
  144. source : pchar;
  145. dest : tcompilerwidecharptr;
  146. i : SizeInt;
  147. m : punicodemap;
  148. begin
  149. m:=getmap(current_settings.sourcecodepage);
  150. setlengthwidestring(r,l);
  151. source:=p;
  152. r^.len:=l;
  153. dest:=tcompilerwidecharptr(r^.data);
  154. if (current_settings.sourcecodepage <> 'utf8') then
  155. begin
  156. for i:=1 to l do
  157. begin
  158. dest^:=getunicode(source^,m);
  159. inc(dest);
  160. inc(source);
  161. end;
  162. end
  163. else
  164. begin
  165. for i:=1 to l do
  166. begin
  167. dest^:=tcompilerwidechar(source^);
  168. inc(dest);
  169. inc(source);
  170. end;
  171. end;
  172. end;
  173. procedure unicode2ascii(r : pcompilerwidestring;p:pchar);
  174. (*
  175. var
  176. m : punicodemap;
  177. i : longint;
  178. begin
  179. m:=getmap(current_settings.sourcecodepage);
  180. { should be a very good estimation :) }
  181. setlengthwidestring(r,length(s));
  182. // !!!! MBCS
  183. for i:=1 to length(s) do
  184. begin
  185. end;
  186. end;
  187. *)
  188. var
  189. source : tcompilerwidecharptr;
  190. dest : pchar;
  191. i : longint;
  192. begin
  193. { This routine must work the same as the
  194. the routine in the RTL to have the same compile time (for constant strings)
  195. and runtime conversion (for variables) }
  196. source:=tcompilerwidecharptr(r^.data);
  197. dest:=p;
  198. for i:=1 to r^.len do
  199. begin
  200. if word(source^)<128 then
  201. dest^:=char(word(source^))
  202. else
  203. dest^:='?';
  204. inc(dest);
  205. inc(source);
  206. end;
  207. end;
  208. function hasnonasciichars(const p: pcompilerwidestring): boolean;
  209. var
  210. source : tcompilerwidecharptr;
  211. i : longint;
  212. begin
  213. source:=tcompilerwidecharptr(p^.data);
  214. result:=true;
  215. for i:=1 to p^.len do
  216. begin
  217. if word(source^)>=128 then
  218. exit;
  219. inc(source);
  220. end;
  221. result:=false;
  222. end;
  223. function cpavailable(const s : string) : boolean;
  224. begin
  225. cpavailable:=mappingavailable(lower(s));
  226. end;
  227. end.