cwstring.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. Result := '';
  111. end;
  112. {$endif}
  113. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  114. var
  115. outlength,
  116. outoffset,
  117. srclen,
  118. outleft : size_t;
  119. srcpos : pwidechar;
  120. destpos: pchar;
  121. mynil : pchar;
  122. my0 : size_t;
  123. err: cint;
  124. begin
  125. mynil:=nil;
  126. my0:=0;
  127. { rought estimation }
  128. setlength(dest,len*3);
  129. outlength:=len*3;
  130. srclen:=len*2;
  131. srcpos:=source;
  132. destpos:=pchar(dest);
  133. outleft:=outlength;
  134. while iconv(iconv_wide2ansi,ppchar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  135. begin
  136. err:=fpgetCerrno;
  137. case err of
  138. { last character is incomplete sequence }
  139. ESysEINVAL,
  140. { incomplete sequence in the middle }
  141. ESysEILSEQ:
  142. begin
  143. { skip and set to '?' }
  144. inc(srcpos);
  145. dec(srclen,2);
  146. destpos^:='?';
  147. inc(destpos);
  148. dec(outleft);
  149. { reset }
  150. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  151. if err=ESysEINVAL then
  152. break;
  153. end;
  154. ESysE2BIG:
  155. begin
  156. outoffset:=destpos-pchar(dest);
  157. { extend }
  158. setlength(dest,outlength+len*3);
  159. inc(outleft,len*3);
  160. inc(outlength,len*3);
  161. { string could have been moved }
  162. destpos:=pchar(dest)+outoffset;
  163. end;
  164. else
  165. runerror(231);
  166. end;
  167. end;
  168. // truncate string
  169. setlength(dest,length(dest)-outleft);
  170. end;
  171. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  172. var
  173. outlength,
  174. outoffset,
  175. outleft : size_t;
  176. srcpos,
  177. destpos: pchar;
  178. mynil : pchar;
  179. my0 : size_t;
  180. err: cint;
  181. begin
  182. mynil:=nil;
  183. my0:=0;
  184. // extra space
  185. outlength:=len+1;
  186. setlength(dest,outlength);
  187. srcpos:=source;
  188. destpos:=pchar(dest);
  189. outleft:=outlength*2;
  190. while iconv(iconv_ansi2wide,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  191. begin
  192. err:=fpgetCerrno;
  193. case err of
  194. ESysEINVAL,
  195. ESysEILSEQ:
  196. begin
  197. { skip and set to '?' }
  198. inc(srcpos);
  199. dec(len);
  200. pwidechar(destpos)^:='?';
  201. inc(destpos,2);
  202. dec(outleft,2);
  203. { reset }
  204. iconv(iconv_ansi2wide,@mynil,@my0,@mynil,@my0);
  205. if err=ESysEINVAL then
  206. break;
  207. end;
  208. ESysE2BIG:
  209. begin
  210. outoffset:=destpos-pchar(dest);
  211. { extend }
  212. setlength(dest,outlength+len);
  213. inc(outleft,len*2);
  214. inc(outlength,len);
  215. { string could have been moved }
  216. destpos:=pchar(dest)+outoffset;
  217. end;
  218. else
  219. runerror(231);
  220. end;
  221. end;
  222. // truncate string
  223. setlength(dest,length(dest)-outleft div 2);
  224. end;
  225. function LowerWideString(const s : WideString) : WideString;
  226. var
  227. i : SizeInt;
  228. begin
  229. SetLength(result,length(s));
  230. for i:=1 to length(s) do
  231. result[i]:=WideChar(towlower(wint_t(s[i])));
  232. end;
  233. function UpperWideString(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(towupper(wint_t(s[i])));
  240. end;
  241. procedure Ansi2UCS4Move(source:pchar;var dest:UCS4String;len:SizeInt);
  242. var
  243. outlength,
  244. outoffset,
  245. outleft : size_t;
  246. srcpos,
  247. destpos: pchar;
  248. mynil : pchar;
  249. my0 : size_t;
  250. begin
  251. mynil:=nil;
  252. my0:=0;
  253. // extra space
  254. outlength:=len+1;
  255. setlength(dest,outlength);
  256. outlength:=len+1;
  257. srcpos:=source;
  258. destpos:=pchar(dest);
  259. outleft:=outlength*4;
  260. while iconv(iconv_ansi2ucs4,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  261. begin
  262. case fpgetCerrno of
  263. ESysE2BIG:
  264. begin
  265. outoffset:=destpos-pchar(dest);
  266. { extend }
  267. setlength(dest,outlength+len);
  268. inc(outleft,len*4);
  269. inc(outlength,len);
  270. { string could have been moved }
  271. destpos:=pchar(dest)+outoffset;
  272. end;
  273. else
  274. runerror(231);
  275. end;
  276. end;
  277. // truncate string
  278. setlength(dest,length(dest)-outleft div 4);
  279. end;
  280. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  281. var
  282. hs1,hs2 : UCS4String;
  283. begin
  284. hs1:=WideStringToUCS4String(s1);
  285. hs2:=WideStringToUCS4String(s2);
  286. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  287. end;
  288. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  289. begin
  290. result:=CompareWideString(UpperWideString(s1),UpperWideString(s2));
  291. end;
  292. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  293. begin
  294. result:=strcoll(s1,s2);
  295. end;
  296. procedure InitThread;
  297. begin
  298. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  299. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  300. iconv_ucs42ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding4);
  301. iconv_ansi2ucs4:=iconv_open(unicode_encoding4,nl_langinfo(CODESET));
  302. end;
  303. procedure FiniThread;
  304. begin
  305. if (iconv_wide2ansi <> iconv_t(-1)) then
  306. iconv_close(iconv_wide2ansi);
  307. if (iconv_ansi2wide <> iconv_t(-1)) then
  308. iconv_close(iconv_ansi2wide);
  309. if (iconv_ucs42ansi <> iconv_t(-1)) then
  310. iconv_close(iconv_ucs42ansi);
  311. if (iconv_ansi2ucs4 <> iconv_t(-1)) then
  312. iconv_close(iconv_ansi2ucs4);
  313. end;
  314. Procedure SetCWideStringManager;
  315. Var
  316. CWideStringManager : TWideStringManager;
  317. begin
  318. CWideStringManager:=widestringmanager;
  319. With CWideStringManager do
  320. begin
  321. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  322. Ansi2WideMoveProc:=@Ansi2WideMove;
  323. UpperWideStringProc:=@UpperWideString;
  324. LowerWideStringProc:=@LowerWideString;
  325. CompareWideStringProc:=@CompareWideString;
  326. CompareTextWideStringProc:=@CompareTextWideString;
  327. {
  328. CharLengthPCharProc
  329. UpperAnsiStringProc
  330. LowerAnsiStringProc
  331. CompareStrAnsiStringProc
  332. CompareTextAnsiStringProc
  333. }
  334. StrCompAnsiStringProc:=@StrCompAnsi;
  335. {
  336. StrICompAnsiStringProc
  337. StrLCompAnsiStringProc
  338. StrLICompAnsiStringProc
  339. StrLowerAnsiStringProc
  340. StrUpperAnsiStringProc
  341. }
  342. ThreadInitProc:=@InitThread;
  343. ThreadFiniProc:=@FiniThread;
  344. end;
  345. SetWideStringManager(CWideStringManager);
  346. end;
  347. initialization
  348. SetCWideStringManager;
  349. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  350. { with the information from the environment variables according to POSIX }
  351. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  352. setlocale(LC_ALL,'');
  353. { init conversion tables for main program }
  354. InitThread;
  355. finalization
  356. { fini conversion tables for main program }
  357. FiniThread;
  358. end.