widestr.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. {$if FPC_FULLVERSION<20700}ccharset{$else}charset{$endif},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;cp : tstringencoding;r : pcompilerwidestring);
  44. procedure unicode2ascii(r : pcompilerwidestring;p : pchar;cp : tstringencoding);
  45. function hasnonasciichars(const p: pcompilerwidestring): boolean;
  46. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  47. function cpavailable(const s : string) : boolean;
  48. function cpavailable(cp : word) : boolean;
  49. procedure changecodepage(
  50. s : pchar; l : SizeInt; scp : tstringencoding;
  51. d : pchar; dcp : tstringencoding
  52. );
  53. function codepagebyname(const s : string) : tstringencoding;
  54. implementation
  55. uses
  56. cp8859_1,cp850,cp437,cp1252,
  57. { cyrillic code pages }
  58. cp1251,cp866,cp8859_5,
  59. globals,cutils;
  60. procedure initwidestring(out r : pcompilerwidestring);
  61. begin
  62. new(r);
  63. r^.data:=nil;
  64. r^.len:=0;
  65. r^.maxlen:=0;
  66. end;
  67. procedure donewidestring(var r : pcompilerwidestring);
  68. begin
  69. if assigned(r^.data) then
  70. freemem(r^.data);
  71. dispose(r);
  72. r:=nil;
  73. end;
  74. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  75. begin
  76. getcharwidestring:=r^.data[l];
  77. end;
  78. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  79. begin
  80. getlengthwidestring:=r^.len;
  81. end;
  82. procedure growwidestring(r : pcompilerwidestring;l : SizeInt);
  83. begin
  84. if r^.maxlen>=l then
  85. exit;
  86. if assigned(r^.data) then
  87. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  88. else
  89. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  90. r^.maxlen:=l;
  91. end;
  92. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  93. begin
  94. r^.len:=l;
  95. if l>r^.maxlen then
  96. growwidestring(r,l);
  97. end;
  98. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  99. begin
  100. if r^.len>=r^.maxlen then
  101. growwidestring(r,r^.len+16);
  102. r^.data[r^.len]:=c;
  103. inc(r^.len);
  104. end;
  105. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  106. begin
  107. growwidestring(s1,s1^.len+s2^.len);
  108. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  109. inc(s1^.len,s2^.len);
  110. end;
  111. procedure copywidestring(s,d : pcompilerwidestring);
  112. begin
  113. setlengthwidestring(d,s^.len);
  114. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  115. end;
  116. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  117. var
  118. maxi,temp : SizeInt;
  119. begin
  120. if pointer(s1)=pointer(s2) then
  121. begin
  122. comparewidestrings:=0;
  123. exit;
  124. end;
  125. maxi:=s1^.len;
  126. temp:=s2^.len;
  127. if maxi>temp then
  128. maxi:=Temp;
  129. temp:=compareword(s1^.data^,s2^.data^,maxi);
  130. if temp=0 then
  131. temp:=s1^.len-s2^.len;
  132. comparewidestrings:=temp;
  133. end;
  134. function asciichar2unicode(c : char) : tcompilerwidechar;
  135. var
  136. m : punicodemap;
  137. begin
  138. if (current_settings.sourcecodepage <> CP_UTF8) then
  139. begin
  140. m:=getmap(current_settings.sourcecodepage);
  141. asciichar2unicode:=getunicode(c,m);
  142. end
  143. else
  144. result:=tcompilerwidechar(c);
  145. end;
  146. function unicode2asciichar(c : tcompilerwidechar) : char;
  147. {begin
  148. if word(c)<128 then
  149. unicode2asciichar:=char(word(c))
  150. else
  151. unicode2asciichar:='?';
  152. end;}
  153. begin
  154. Result := getascii(c,getmap(current_settings.sourcecodepage))[1];
  155. end;
  156. procedure ascii2unicode(p : pchar;l : SizeInt;cp : tstringencoding;r : pcompilerwidestring);
  157. var
  158. source : pchar;
  159. dest : tcompilerwidecharptr;
  160. i : SizeInt;
  161. m : punicodemap;
  162. begin
  163. m:=getmap(cp);
  164. setlengthwidestring(r,l);
  165. source:=p;
  166. dest:=tcompilerwidecharptr(r^.data);
  167. if (current_settings.sourcecodepage <> CP_UTF8) then
  168. begin
  169. for i:=1 to l do
  170. begin
  171. dest^:=getunicode(source^,m);
  172. inc(dest);
  173. inc(source);
  174. end;
  175. end
  176. else
  177. begin
  178. for i:=1 to l do
  179. begin
  180. dest^:=tcompilerwidechar(source^);
  181. inc(dest);
  182. inc(source);
  183. end;
  184. end;
  185. end;
  186. procedure unicode2ascii(r : pcompilerwidestring;p:pchar;cp : tstringencoding);
  187. var
  188. m : punicodemap;
  189. source : tcompilerwidecharptr;
  190. dest : pchar;
  191. i : longint;
  192. begin
  193. if (cp = 0) or (cp=CP_NONE) then
  194. m:=getmap(current_settings.sourcecodepage)
  195. else
  196. m:=getmap(cp);
  197. // !!!! MBCS
  198. source:=tcompilerwidecharptr(r^.data);
  199. dest:=p;
  200. for i:=1 to r^.len do
  201. begin
  202. dest^ := getascii(source^,m)[1];
  203. inc(dest);
  204. inc(source);
  205. end;
  206. end;
  207. (*
  208. var
  209. source : tcompilerwidecharptr;
  210. dest : pchar;
  211. i : longint;
  212. begin
  213. { This routine must work the same as the
  214. the routine in the RTL to have the same compile time (for constant strings)
  215. and runtime conversion (for variables) }
  216. source:=tcompilerwidecharptr(r^.data);
  217. dest:=p;
  218. for i:=1 to r^.len do
  219. begin
  220. if word(source^)<128 then
  221. dest^:=char(word(source^))
  222. else
  223. dest^:='?';
  224. inc(dest);
  225. inc(source);
  226. end;
  227. end;
  228. *)
  229. function hasnonasciichars(const p: pcompilerwidestring): boolean;
  230. var
  231. source : tcompilerwidecharptr;
  232. i : longint;
  233. begin
  234. source:=tcompilerwidecharptr(p^.data);
  235. result:=true;
  236. for i:=1 to p^.len do
  237. begin
  238. if word(source^)>=128 then
  239. exit;
  240. inc(source);
  241. end;
  242. result:=false;
  243. end;
  244. function cpavailable(const s : string) : boolean;
  245. begin
  246. cpavailable:=mappingavailable(lower(s));
  247. end;
  248. function cpavailable(cp : word) : boolean;
  249. begin
  250. cpavailable:=mappingavailable(cp);
  251. end;
  252. procedure changecodepage(
  253. s : pchar; l : SizeInt; scp : tstringencoding;
  254. d : pchar; dcp : tstringencoding
  255. );
  256. var
  257. ms, md : punicodemap;
  258. source : pchar;
  259. dest : pchar;
  260. i : longint;
  261. begin
  262. ms:=getmap(scp);
  263. md:=getmap(dcp);
  264. source:=s;
  265. dest:=d;
  266. for i:=1 to l do
  267. begin
  268. dest^ := getascii(getunicode(source^,ms),md)[1];
  269. inc(dest);
  270. inc(source);
  271. end;
  272. end;
  273. function codepagebyname(const s : string) : tstringencoding;
  274. var
  275. p : punicodemap;
  276. begin
  277. Result:=0;
  278. p:=getmap(s);
  279. if (p<>nil) then
  280. Result:=p^.cp;
  281. end;
  282. end.