cwstring.pp 8.8 KB

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