widestr.pas 10 KB

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