widestr.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 (current_settings.sourcecodepage <> CP_UTF8) and
  177. codepagetranslation then
  178. begin
  179. for i:=1 to l do
  180. begin
  181. dest^:=getunicode(source^,m);
  182. inc(dest);
  183. inc(source);
  184. end;
  185. end
  186. else
  187. begin
  188. for i:=1 to l do
  189. begin
  190. dest^:=tcompilerwidechar(source^);
  191. inc(dest);
  192. inc(source);
  193. end;
  194. end;
  195. end;
  196. procedure unicode2ascii(r : pcompilerwidestring;p:pchar;cp : tstringencoding);
  197. var
  198. m : punicodemap;
  199. source : tcompilerwidecharptr;
  200. dest : pchar;
  201. i : longint;
  202. begin
  203. if (cp = 0) or (cp=CP_NONE) then
  204. m:=getmap(current_settings.sourcecodepage)
  205. else
  206. m:=getmap(cp);
  207. // !!!! MBCS
  208. source:=tcompilerwidecharptr(r^.data);
  209. dest:=p;
  210. for i:=1 to r^.len do
  211. begin
  212. dest^ := getascii(source^,m)[1];
  213. inc(dest);
  214. inc(source);
  215. end;
  216. end;
  217. (*
  218. var
  219. source : tcompilerwidecharptr;
  220. dest : pchar;
  221. i : longint;
  222. begin
  223. { This routine must work the same as the
  224. the routine in the RTL to have the same compile time (for constant strings)
  225. and runtime conversion (for variables) }
  226. source:=tcompilerwidecharptr(r^.data);
  227. dest:=p;
  228. for i:=1 to r^.len do
  229. begin
  230. if word(source^)<128 then
  231. dest^:=char(word(source^))
  232. else
  233. dest^:='?';
  234. inc(dest);
  235. inc(source);
  236. end;
  237. end;
  238. *)
  239. function hasnonasciichars(const p: pcompilerwidestring): boolean;
  240. var
  241. source : tcompilerwidecharptr;
  242. i : longint;
  243. begin
  244. source:=tcompilerwidecharptr(p^.data);
  245. result:=true;
  246. for i:=1 to p^.len do
  247. begin
  248. if word(source^)>=128 then
  249. exit;
  250. inc(source);
  251. end;
  252. result:=false;
  253. end;
  254. function cpavailable(const s: string): boolean;
  255. begin
  256. result:=mappingavailable(lower(s));
  257. {$if FPC_FULLVERSION>20700}
  258. if not result then
  259. result:=(unicodepath<>'')and(registerbinarymapping(unicodepath+'charset',lower(s)));
  260. {$ifend}
  261. end;
  262. function cpavailable(cp: word): boolean;
  263. begin
  264. result:=mappingavailable(cp);
  265. {$if FPC_FULLVERSION>20700}
  266. if not result then
  267. result:=(unicodepath<>'')and(registerbinarymapping(unicodepath+'charset','cp'+tostr(cp)));
  268. {$ifend}
  269. end;
  270. procedure changecodepage(
  271. s : pchar; l : SizeInt; scp : tstringencoding;
  272. d : pchar; dcp : tstringencoding
  273. );
  274. var
  275. ms, md : punicodemap;
  276. source : pchar;
  277. dest : pchar;
  278. i : longint;
  279. begin
  280. ms:=getmap(scp);
  281. md:=getmap(dcp);
  282. source:=s;
  283. dest:=d;
  284. for i:=1 to l do
  285. begin
  286. dest^ := getascii(getunicode(source^,ms),md)[1];
  287. inc(dest);
  288. inc(source);
  289. end;
  290. end;
  291. function codepagebyname(const s : string) : tstringencoding;
  292. var
  293. p : punicodemap;
  294. begin
  295. Result:=0;
  296. p:=getmap(s);
  297. if (p<>nil) then
  298. Result:=p^.cp;
  299. end;
  300. end.