cwstring.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. {$if not defined(linux) and not defined(solaris)} // Linux (and maybe glibc platforms in general), have iconv in glibc.
  19. {$linklib iconv}
  20. {$define useiconv}
  21. {$endif linux}
  22. Uses
  23. BaseUnix,
  24. ctypes,
  25. unix,
  26. unixtype,
  27. initc;
  28. Const
  29. {$ifndef useiconv}
  30. libiconvname='c'; // is in libc under Linux.
  31. {$else}
  32. libiconvname='iconv';
  33. {$endif}
  34. { helper functions from libc }
  35. function towlower(__wc:wint_t):wint_t;cdecl;external libiconvname name 'towlower';
  36. function towupper(__wc:wint_t):wint_t;cdecl;external libiconvname name 'towupper';
  37. function wcscoll (__s1:pwchar_t; __s2:pwchar_t):cint;cdecl;external libiconvname name 'wcscoll';
  38. function strcoll (__s1:pchar; __s2:pchar):cint;cdecl;external libiconvname name 'strcoll';
  39. function setlocale(category: cint; locale: pchar): pchar; cdecl; external clib name 'setlocale';
  40. const
  41. {$ifdef linux}
  42. __LC_CTYPE = 0;
  43. LC_ALL = 6;
  44. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  45. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  46. CODESET = _NL_CTYPE_CODESET_NAME;
  47. {$else linux}
  48. {$ifdef darwin}
  49. CODESET = 0;
  50. LC_ALL = 0;
  51. {$else darwin}
  52. {$ifdef FreeBSD} // actually FreeBSD5. internationalisation is afaik not default on 4.
  53. __LC_CTYPE = 0;
  54. LC_ALL = 0;
  55. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  56. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  57. CODESET = 0; // _NL_CTYPE_CODESET_NAME;
  58. {$else freebsd}
  59. {$ifdef solaris}
  60. CODESET=49;
  61. LC_ALL = 6;
  62. {$else}
  63. {$error lookup the value of CODESET in /usr/include/langinfo.h, and the value of LC_ALL in /usr/include/locale.h for your OS }
  64. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  65. {$endif solaris}
  66. {$endif FreeBSD}
  67. {$endif darwin}
  68. {$endif linux}
  69. { unicode encoding name }
  70. {$ifdef FPC_LITTLE_ENDIAN}
  71. unicode_encoding2 = 'UTF-16LE';
  72. unicode_encoding4 = 'UCS-4LE';
  73. {$else FPC_LITTLE_ENDIAN}
  74. unicode_encoding2 = 'UTF-16BE';
  75. unicode_encoding4 = 'UCS-4BE';
  76. {$endif FPC_LITTLE_ENDIAN}
  77. type
  78. piconv_t = ^iconv_t;
  79. iconv_t = pointer;
  80. nl_item = cint;
  81. function nl_langinfo(__item:nl_item):pchar;cdecl;external libiconvname name 'nl_langinfo';
  82. {$ifndef bsd}
  83. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  84. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  85. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  86. {$else}
  87. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  88. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  89. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  90. {$endif}
  91. threadvar
  92. iconv_ansi2ucs4,
  93. iconv_ucs42ansi,
  94. iconv_ansi2wide,
  95. iconv_wide2ansi : iconv_t;
  96. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  97. var
  98. outlength,
  99. outoffset,
  100. srclen,
  101. outleft : size_t;
  102. srcpos : pwidechar;
  103. destpos: pchar;
  104. mynil : pchar;
  105. my0 : size_t;
  106. begin
  107. mynil:=nil;
  108. my0:=0;
  109. { rought estimation }
  110. setlength(dest,len*3);
  111. outlength:=len*3;
  112. srclen:=len*2;
  113. srcpos:=source;
  114. destpos:=pchar(dest);
  115. outleft:=outlength;
  116. while iconv(iconv_wide2ansi,ppchar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  117. begin
  118. case fpgetCerrno of
  119. ESysEILSEQ:
  120. begin
  121. { skip and set to '?' }
  122. inc(srcpos);
  123. dec(srclen,2);
  124. destpos^:='?';
  125. inc(destpos);
  126. dec(outleft);
  127. { reset }
  128. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  129. end;
  130. ESysE2BIG:
  131. begin
  132. outoffset:=destpos-pchar(dest);
  133. { extend }
  134. setlength(dest,outlength+len*3);
  135. inc(outleft,len*3);
  136. inc(outlength,len*3);
  137. { string could have been moved }
  138. destpos:=pchar(dest)+outoffset;
  139. end;
  140. else
  141. runerror(231);
  142. end;
  143. end;
  144. // truncate string
  145. setlength(dest,length(dest)-outleft);
  146. end;
  147. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  148. var
  149. outlength,
  150. outoffset,
  151. outleft : size_t;
  152. srcpos,
  153. destpos: pchar;
  154. mynil : pchar;
  155. my0 : size_t;
  156. begin
  157. mynil:=nil;
  158. my0:=0;
  159. // extra space
  160. outlength:=len+1;
  161. setlength(dest,outlength);
  162. outlength:=len+1;
  163. srcpos:=source;
  164. destpos:=pchar(dest);
  165. outleft:=outlength*2;
  166. while iconv(iconv_ansi2wide,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  167. begin
  168. case fpgetCerrno of
  169. ESysEILSEQ:
  170. begin
  171. { skip and set to '?' }
  172. inc(srcpos);
  173. dec(len);
  174. pwidechar(destpos)^:='?';
  175. inc(destpos,2);
  176. dec(outleft,2);
  177. { reset }
  178. iconv(iconv_ansi2wide,@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. runerror(231);
  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,psize(@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. runerror(231);
  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 InitThread;
  269. begin
  270. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  271. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  272. iconv_ucs42ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding4);
  273. iconv_ansi2ucs4:=iconv_open(unicode_encoding4,nl_langinfo(CODESET));
  274. end;
  275. procedure FiniThread;
  276. begin
  277. if (iconv_wide2ansi <> iconv_t(-1)) then
  278. iconv_close(iconv_wide2ansi);
  279. if (iconv_ansi2wide <> iconv_t(-1)) then
  280. iconv_close(iconv_ansi2wide);
  281. if (iconv_ucs42ansi <> iconv_t(-1)) then
  282. iconv_close(iconv_ucs42ansi);
  283. if (iconv_ansi2ucs4 <> iconv_t(-1)) then
  284. iconv_close(iconv_ansi2ucs4);
  285. end;
  286. Procedure SetCWideStringManager;
  287. Var
  288. CWideStringManager : TWideStringManager;
  289. begin
  290. CWideStringManager:=widestringmanager;
  291. With CWideStringManager do
  292. begin
  293. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  294. Ansi2WideMoveProc:=@Ansi2WideMove;
  295. UpperWideStringProc:=@UpperWideString;
  296. LowerWideStringProc:=@LowerWideString;
  297. CompareWideStringProc:=@CompareWideString;
  298. CompareTextWideStringProc:=@CompareTextWideString;
  299. {
  300. CharLengthPCharProc
  301. UpperAnsiStringProc
  302. LowerAnsiStringProc
  303. CompareStrAnsiStringProc
  304. CompareTextAnsiStringProc
  305. }
  306. StrCompAnsiStringProc:=@StrCompAnsi;
  307. {
  308. StrICompAnsiStringProc
  309. StrLCompAnsiStringProc
  310. StrLICompAnsiStringProc
  311. StrLowerAnsiStringProc
  312. StrUpperAnsiStringProc
  313. }
  314. ThreadInitProc:=@InitThread;
  315. ThreadFiniProc:=@FiniThread;
  316. end;
  317. SetWideStringManager(CWideStringManager);
  318. end;
  319. initialization
  320. SetCWideStringManager;
  321. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  322. { with the information from the environment variables according to POSIX }
  323. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  324. setlocale(LC_ALL,'');
  325. { init conversion tables for main program }
  326. InitThread;
  327. finalization
  328. { fini conversion tables for main program }
  329. FiniThread;
  330. end.