cwstring.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 solaris}
  63. {$ifdef beos}
  64. {$warning check correct value for BeOS}
  65. CODESET=49;
  66. LC_ALL = 6; // Checked for BeOS, but 0 under Haiku...
  67. ESysEILSEQ = EILSEQ;
  68. {$else}
  69. {$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 }
  70. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  71. {$endif beos}
  72. {$endif solaris}
  73. {$endif FreeBSD}
  74. {$endif darwin}
  75. {$endif linux}
  76. { unicode encoding name }
  77. {$ifdef FPC_LITTLE_ENDIAN}
  78. unicode_encoding2 = 'UTF-16LE';
  79. unicode_encoding4 = 'UCS-4LE';
  80. {$else FPC_LITTLE_ENDIAN}
  81. unicode_encoding2 = 'UTF-16BE';
  82. unicode_encoding4 = 'UCS-4BE';
  83. {$endif FPC_LITTLE_ENDIAN}
  84. type
  85. piconv_t = ^iconv_t;
  86. iconv_t = pointer;
  87. nl_item = cint;
  88. {$ifndef beos}
  89. function nl_langinfo(__item:nl_item):pchar;cdecl;external libiconvname name 'nl_langinfo';
  90. {$endif}
  91. { $ ifndef bsd}
  92. {$if not defined(bsd) and not defined(beos)}
  93. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  94. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  95. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  96. {$else}
  97. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  98. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  99. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  100. {$endif}
  101. threadvar
  102. iconv_ansi2ucs4,
  103. iconv_ucs42ansi,
  104. iconv_ansi2wide,
  105. iconv_wide2ansi : iconv_t;
  106. {$ifdef beos}
  107. function nl_langinfo(__item:nl_item):pchar;
  108. begin
  109. {$warning TODO BeOS nl_langinfo or more uptodate port of iconv...}
  110. // Now implement the minimum required to correctly initialize WideString support
  111. case __item of
  112. CODESET : Result := 'UTF-8'; // BeOS use UTF-8
  113. else
  114. begin
  115. Assert(False, 'nl_langinfo was called with an unknown nl_item value');
  116. Result := '';
  117. end;
  118. end;
  119. end;
  120. {$endif}
  121. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  122. var
  123. outlength,
  124. outoffset,
  125. srclen,
  126. outleft : size_t;
  127. srcpos : pwidechar;
  128. destpos: pchar;
  129. mynil : pchar;
  130. my0 : size_t;
  131. err: cint;
  132. begin
  133. mynil:=nil;
  134. my0:=0;
  135. { rought estimation }
  136. setlength(dest,len*3);
  137. outlength:=len*3;
  138. srclen:=len*2;
  139. srcpos:=source;
  140. destpos:=pchar(dest);
  141. outleft:=outlength;
  142. while iconv(iconv_wide2ansi,ppchar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  143. begin
  144. err:=fpgetCerrno;
  145. case err of
  146. { last character is incomplete sequence }
  147. ESysEINVAL,
  148. { incomplete sequence in the middle }
  149. ESysEILSEQ:
  150. begin
  151. { skip and set to '?' }
  152. inc(srcpos);
  153. dec(srclen,2);
  154. destpos^:='?';
  155. inc(destpos);
  156. dec(outleft);
  157. { reset }
  158. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  159. if err=ESysEINVAL then
  160. break;
  161. end;
  162. ESysE2BIG:
  163. begin
  164. outoffset:=destpos-pchar(dest);
  165. { extend }
  166. setlength(dest,outlength+len*3);
  167. inc(outleft,len*3);
  168. inc(outlength,len*3);
  169. { string could have been moved }
  170. destpos:=pchar(dest)+outoffset;
  171. end;
  172. else
  173. runerror(231);
  174. end;
  175. end;
  176. // truncate string
  177. setlength(dest,length(dest)-outleft);
  178. end;
  179. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  180. var
  181. outlength,
  182. outoffset,
  183. outleft : size_t;
  184. srcpos,
  185. destpos: pchar;
  186. mynil : pchar;
  187. my0 : size_t;
  188. err: cint;
  189. begin
  190. mynil:=nil;
  191. my0:=0;
  192. // extra space
  193. outlength:=len+1;
  194. setlength(dest,outlength);
  195. srcpos:=source;
  196. destpos:=pchar(dest);
  197. outleft:=outlength*2;
  198. while iconv(iconv_ansi2wide,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  199. begin
  200. err:=fpgetCerrno;
  201. case err of
  202. ESysEINVAL,
  203. ESysEILSEQ:
  204. begin
  205. { skip and set to '?' }
  206. inc(srcpos);
  207. dec(len);
  208. pwidechar(destpos)^:='?';
  209. inc(destpos,2);
  210. dec(outleft,2);
  211. { reset }
  212. iconv(iconv_ansi2wide,@mynil,@my0,@mynil,@my0);
  213. if err=ESysEINVAL then
  214. break;
  215. end;
  216. ESysE2BIG:
  217. begin
  218. outoffset:=destpos-pchar(dest);
  219. { extend }
  220. setlength(dest,outlength+len);
  221. inc(outleft,len*2);
  222. inc(outlength,len);
  223. { string could have been moved }
  224. destpos:=pchar(dest)+outoffset;
  225. end;
  226. else
  227. runerror(231);
  228. end;
  229. end;
  230. // truncate string
  231. setlength(dest,length(dest)-outleft div 2);
  232. end;
  233. function LowerWideString(const s : WideString) : WideString;
  234. var
  235. i : SizeInt;
  236. begin
  237. SetLength(result,length(s));
  238. for i:=1 to length(s) do
  239. result[i]:=WideChar(towlower(wint_t(s[i])));
  240. end;
  241. function UpperWideString(const s : WideString) : WideString;
  242. var
  243. i : SizeInt;
  244. begin
  245. SetLength(result,length(s));
  246. for i:=1 to length(s) do
  247. result[i]:=WideChar(towupper(wint_t(s[i])));
  248. end;
  249. procedure Ansi2UCS4Move(source:pchar;var dest:UCS4String;len:SizeInt);
  250. var
  251. outlength,
  252. outoffset,
  253. outleft : size_t;
  254. srcpos,
  255. destpos: pchar;
  256. mynil : pchar;
  257. my0 : size_t;
  258. begin
  259. mynil:=nil;
  260. my0:=0;
  261. // extra space
  262. outlength:=len+1;
  263. setlength(dest,outlength);
  264. outlength:=len+1;
  265. srcpos:=source;
  266. destpos:=pchar(dest);
  267. outleft:=outlength*4;
  268. while iconv(iconv_ansi2ucs4,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  269. begin
  270. case fpgetCerrno of
  271. ESysE2BIG:
  272. begin
  273. outoffset:=destpos-pchar(dest);
  274. { extend }
  275. setlength(dest,outlength+len);
  276. inc(outleft,len*4);
  277. inc(outlength,len);
  278. { string could have been moved }
  279. destpos:=pchar(dest)+outoffset;
  280. end;
  281. else
  282. runerror(231);
  283. end;
  284. end;
  285. // truncate string
  286. setlength(dest,length(dest)-outleft div 4);
  287. end;
  288. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  289. var
  290. hs1,hs2 : UCS4String;
  291. begin
  292. hs1:=WideStringToUCS4String(s1);
  293. hs2:=WideStringToUCS4String(s2);
  294. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  295. end;
  296. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  297. begin
  298. result:=CompareWideString(UpperWideString(s1),UpperWideString(s2));
  299. end;
  300. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  301. begin
  302. result:=strcoll(s1,s2);
  303. end;
  304. procedure InitThread;
  305. begin
  306. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  307. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  308. iconv_ucs42ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding4);
  309. iconv_ansi2ucs4:=iconv_open(unicode_encoding4,nl_langinfo(CODESET));
  310. end;
  311. procedure FiniThread;
  312. begin
  313. if (iconv_wide2ansi <> iconv_t(-1)) then
  314. iconv_close(iconv_wide2ansi);
  315. if (iconv_ansi2wide <> iconv_t(-1)) then
  316. iconv_close(iconv_ansi2wide);
  317. if (iconv_ucs42ansi <> iconv_t(-1)) then
  318. iconv_close(iconv_ucs42ansi);
  319. if (iconv_ansi2ucs4 <> iconv_t(-1)) then
  320. iconv_close(iconv_ansi2ucs4);
  321. end;
  322. Procedure SetCWideStringManager;
  323. Var
  324. CWideStringManager : TWideStringManager;
  325. begin
  326. CWideStringManager:=widestringmanager;
  327. With CWideStringManager do
  328. begin
  329. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  330. Ansi2WideMoveProc:=@Ansi2WideMove;
  331. UpperWideStringProc:=@UpperWideString;
  332. LowerWideStringProc:=@LowerWideString;
  333. CompareWideStringProc:=@CompareWideString;
  334. CompareTextWideStringProc:=@CompareTextWideString;
  335. {
  336. CharLengthPCharProc
  337. UpperAnsiStringProc
  338. LowerAnsiStringProc
  339. CompareStrAnsiStringProc
  340. CompareTextAnsiStringProc
  341. }
  342. StrCompAnsiStringProc:=@StrCompAnsi;
  343. {
  344. StrICompAnsiStringProc
  345. StrLCompAnsiStringProc
  346. StrLICompAnsiStringProc
  347. StrLowerAnsiStringProc
  348. StrUpperAnsiStringProc
  349. }
  350. ThreadInitProc:=@InitThread;
  351. ThreadFiniProc:=@FiniThread;
  352. end;
  353. SetWideStringManager(CWideStringManager);
  354. end;
  355. initialization
  356. SetCWideStringManager;
  357. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  358. { with the information from the environment variables according to POSIX }
  359. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  360. setlocale(LC_ALL,'');
  361. { init conversion tables for main program }
  362. InitThread;
  363. finalization
  364. { fini conversion tables for main program }
  365. FiniThread;
  366. end.