cwstring.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Florian Klaempfl,
  4. member of the Free Pascal development team.
  5. libc based wide string support
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. unit cwstring;
  14. interface
  15. procedure SetCWidestringManager;
  16. implementation
  17. {$linklib c}
  18. {$ifndef linux} // Linux (and maybe glibc platforms in general), have iconv in glibc.
  19. {$ifndef FreeBSD5}
  20. {$linklib iconv}
  21. {$define useiconv}
  22. {$endif}
  23. {$endif linux}
  24. Uses
  25. BaseUnix,
  26. ctypes,
  27. unix,
  28. unixtype,
  29. sysutils,
  30. initc;
  31. Const
  32. {$ifndef useiconv}
  33. libiconvname='c'; // is in libc under Linux.
  34. {$else}
  35. libiconvname='iconv';
  36. {$endif}
  37. { Case-mapping "arrays" }
  38. var
  39. AnsiUpperChars: AnsiString; // 1..255
  40. AnsiLowerChars: AnsiString; // 1..255
  41. WideUpperChars: WideString; // 1..65535
  42. WideLowerChars: WideString; // 1..65535
  43. { the following declarations are from the libc unit for linux so they
  44. might be very linux centric
  45. maybe this needs to be splitted in an os depend way later }
  46. function towlower(__wc:wint_t):wint_t;cdecl;external libiconvname name 'towlower';
  47. function towupper(__wc:wint_t):wint_t;cdecl;external libiconvname name 'towupper';
  48. function wcscoll (__s1:pwchar_t; __s2:pwchar_t):cint;cdecl;external libiconvname name 'wcscoll';
  49. function strcoll (__s1:pchar; __s2:pchar):cint;cdecl;external libiconvname name 'strcoll';
  50. const
  51. {$ifdef linux}
  52. __LC_CTYPE = 0;
  53. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  54. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  55. CODESET = _NL_CTYPE_CODESET_NAME;
  56. {$else linux}
  57. {$ifdef darwin}
  58. CODESET = 0;
  59. {$else darwin}
  60. {$ifdef solaris}
  61. CODESET = 0;
  62. {$else solaris}
  63. {$ifdef FreeBSD} // actually FreeBSD5. internationalisation is afaik not default on 4.
  64. CODESET = 0;
  65. {$else freebsd}
  66. {$error lookup the value of CODESET in /usr/include/langinfo.h for your OS }
  67. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  68. {$endif FreeBSD}
  69. {$endif solaris}
  70. {$endif darwin}
  71. {$endif linux}
  72. { unicode encoding name }
  73. {$ifdef FPC_LITTLE_ENDIAN}
  74. unicode_encoding = 'UNICODELITTLE';
  75. {$else FPC_LITTLE_ENDIAN}
  76. unicode_encoding = 'UNICODEBIG';
  77. {$endif FPC_LITTLE_ENDIAN}
  78. type
  79. piconv_t = ^iconv_t;
  80. iconv_t = pointer;
  81. nl_item = cint;
  82. function nl_langinfo(__item:nl_item):pchar;cdecl;external libiconvname name 'nl_langinfo';
  83. {$ifndef Darwin}
  84. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  85. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  86. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  87. {$else}
  88. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  89. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  90. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  91. {$endif}
  92. var
  93. iconv_ansi2ucs4,
  94. iconv_ucs42ansi,
  95. iconv_ansi2wide,
  96. iconv_wide2ansi : iconv_t;
  97. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  98. var
  99. outlength,
  100. outoffset,
  101. srclen,
  102. outleft : size_t;
  103. srcpos : pwidechar;
  104. destpos: pchar;
  105. mynil : pchar;
  106. my0 : size_t;
  107. begin
  108. mynil:=nil;
  109. my0:=0;
  110. { rought estimation }
  111. setlength(dest,len*3);
  112. outlength:=len*3;
  113. srclen:=len*2;
  114. srcpos:=source;
  115. destpos:=pchar(dest);
  116. outleft:=outlength;
  117. while iconv(iconv_wide2ansi,@srcpos,@srclen,@destpos,@outleft)=size_t(-1) do
  118. begin
  119. case fpgetCerrno of
  120. ESysEILSEQ:
  121. begin
  122. { skip and set to '?' }
  123. inc(srcpos);
  124. dec(srclen,2);
  125. destpos^:='?';
  126. inc(destpos);
  127. dec(outleft);
  128. { reset }
  129. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  130. end;
  131. ESysE2BIG:
  132. begin
  133. outoffset:=destpos-pchar(dest);
  134. { extend }
  135. setlength(dest,outlength+len*3);
  136. inc(outleft,len*3);
  137. inc(outlength,len*3);
  138. { string could have been moved }
  139. destpos:=pchar(dest)+outoffset;
  140. end;
  141. else
  142. raise EConvertError.Create('iconv error '+IntToStr(fpgetCerrno));
  143. end;
  144. end;
  145. // truncate string
  146. setlength(dest,length(dest)-outleft);
  147. end;
  148. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  149. var
  150. outlength,
  151. outoffset,
  152. outleft : size_t;
  153. srcpos,
  154. destpos: pchar;
  155. mynil : pchar;
  156. my0 : size_t;
  157. begin
  158. mynil:=nil;
  159. my0:=0;
  160. // extra space
  161. outlength:=len+1;
  162. setlength(dest,outlength);
  163. outlength:=len+1;
  164. srcpos:=source;
  165. destpos:=pchar(dest);
  166. outleft:=outlength*2;
  167. while iconv(iconv_ansi2wide,@srcpos,@len,@destpos,@outleft)=size_t(-1) do
  168. begin
  169. case fpgetCerrno of
  170. ESysEILSEQ:
  171. begin
  172. { skip and set to '?' }
  173. inc(srcpos);
  174. pwidechar(destpos)^:='?';
  175. inc(destpos,2);
  176. dec(outleft,2);
  177. { reset }
  178. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  179. end;
  180. ESysE2BIG:
  181. begin
  182. outoffset:=destpos-pchar(dest);
  183. { extend }
  184. setlength(dest,outlength+len);
  185. inc(outleft,len*2);
  186. inc(outlength,len);
  187. { string could have been moved }
  188. destpos:=pchar(dest)+outoffset;
  189. end;
  190. else
  191. raise EConvertError.Create('iconv error '+IntToStr(fpgetCerrno));
  192. end;
  193. end;
  194. // truncate string
  195. setlength(dest,length(dest)-outleft div 2);
  196. end;
  197. function LowerWideString(const s : WideString) : WideString;
  198. var
  199. i : SizeInt;
  200. begin
  201. SetLength(result,length(s));
  202. for i:=1 to length(s) do
  203. result[i]:=WideChar(towlower(wint_t(s[i])));
  204. end;
  205. function UpperWideString(const s : WideString) : WideString;
  206. var
  207. i : SizeInt;
  208. begin
  209. SetLength(result,length(s));
  210. for i:=1 to length(s) do
  211. result[i]:=WideChar(towupper(wint_t(s[i])));
  212. end;
  213. procedure Ansi2UCS4Move(source:pchar;var dest:UCS4String;len:SizeInt);
  214. var
  215. outlength,
  216. outoffset,
  217. outleft : size_t;
  218. srcpos,
  219. destpos: pchar;
  220. mynil : pchar;
  221. my0 : size_t;
  222. begin
  223. mynil:=nil;
  224. my0:=0;
  225. // extra space
  226. outlength:=len+1;
  227. setlength(dest,outlength);
  228. outlength:=len+1;
  229. srcpos:=source;
  230. destpos:=pchar(dest);
  231. outleft:=outlength*4;
  232. while iconv(iconv_ansi2ucs4,@srcpos,@len,@destpos,@outleft)=size_t(-1) do
  233. begin
  234. case fpgetCerrno of
  235. ESysE2BIG:
  236. begin
  237. outoffset:=destpos-pchar(dest);
  238. { extend }
  239. setlength(dest,outlength+len);
  240. inc(outleft,len*4);
  241. inc(outlength,len);
  242. { string could have been moved }
  243. destpos:=pchar(dest)+outoffset;
  244. end;
  245. else
  246. raise EConvertError.Create('iconv error '+IntToStr(fpgetCerrno));
  247. end;
  248. end;
  249. // truncate string
  250. setlength(dest,length(dest)-outleft div 4);
  251. end;
  252. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  253. var
  254. hs1,hs2 : UCS4String;
  255. begin
  256. hs1:=WideStringToUCS4String(s1);
  257. hs2:=WideStringToUCS4String(s2);
  258. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  259. end;
  260. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  261. begin
  262. result:=CompareWideString(UpperWideString(s1),UpperWideString(s2));
  263. end;
  264. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  265. begin
  266. result:=strcoll(s1,s2);
  267. end;
  268. Procedure SetCWideStringManager;
  269. Var
  270. CWideStringManager : TWideStringManager;
  271. begin
  272. CWideStringManager:=widestringmanager;
  273. With CWideStringManager do
  274. begin
  275. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  276. Ansi2WideMoveProc:=@Ansi2WideMove;
  277. UpperWideStringProc:=@UpperWideString;
  278. LowerWideStringProc:=@LowerWideString;
  279. CompareWideStringProc:=@CompareWideString;
  280. CompareTextWideStringProc:=@CompareTextWideString;
  281. {
  282. CharLengthPCharProc
  283. UpperAnsiStringProc
  284. LowerAnsiStringProc
  285. CompareStrAnsiStringProc
  286. CompareTextAnsiStringProc
  287. }
  288. StrCompAnsiStringProc:=@StrCompAnsi;
  289. {
  290. StrICompAnsiStringProc
  291. StrLCompAnsiStringProc
  292. StrLICompAnsiStringProc
  293. StrLowerAnsiStringProc
  294. StrUpperAnsiStringProc
  295. }
  296. end;
  297. SetWideStringManager(CWideStringManager);
  298. end;
  299. initialization
  300. SetCWideStringManager;
  301. { init conversion tables }
  302. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding);
  303. iconv_ansi2wide:=iconv_open(unicode_encoding,nl_langinfo(CODESET));
  304. iconv_ucs42ansi:=iconv_open(nl_langinfo(CODESET),'UCS4');
  305. iconv_ansi2ucs4:=iconv_open('UCS4',nl_langinfo(CODESET));
  306. finalization
  307. iconv_close(iconv_ansi2wide);
  308. end.