cwstring.pp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_ansi2wide,
  87. iconv_wide2ansi : iconv_t;
  88. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  89. var
  90. outlength,
  91. outoffset,
  92. srclen,
  93. outleft : size_t;
  94. srcpos : pwidechar;
  95. destpos: pchar;
  96. mynil : pchar;
  97. my0 : size_t;
  98. begin
  99. mynil:=nil;
  100. my0:=0;
  101. { rought estimation }
  102. setlength(dest,len*3);
  103. outlength:=len*3;
  104. srclen:=len*2;
  105. srcpos:=source;
  106. destpos:=pchar(dest);
  107. outleft:=outlength;
  108. while iconv(iconv_wide2ansi,@srcpos,@srclen,@destpos,@outleft)=size_t(-1) do
  109. begin
  110. case fpgetCerrno of
  111. ESysEILSEQ:
  112. begin
  113. { skip and set to '?' }
  114. inc(srcpos);
  115. dec(srclen,2);
  116. destpos^:='?';
  117. inc(destpos);
  118. dec(outleft);
  119. { reset }
  120. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  121. end;
  122. ESysE2BIG:
  123. begin
  124. outoffset:=destpos-pchar(dest);
  125. { extend }
  126. setlength(dest,outlength+len*3);
  127. inc(outleft,len*3);
  128. inc(outlength,len*3);
  129. { string could have been moved }
  130. destpos:=pchar(dest)+outoffset;
  131. end;
  132. else
  133. raise EConvertError.Create('iconv error');
  134. end;
  135. end;
  136. // truncate string
  137. setlength(dest,length(dest)-outleft);
  138. end;
  139. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  140. var
  141. outlength,
  142. outoffset,
  143. outleft : size_t;
  144. srcpos,
  145. destpos: pchar;
  146. mynil : pchar;
  147. my0 : size_t;
  148. begin
  149. mynil:=nil;
  150. my0:=0;
  151. // extra space
  152. outlength:=len+1;
  153. setlength(dest,outlength);
  154. outlength:=len+1;
  155. srcpos:=source;
  156. destpos:=pchar(dest);
  157. outleft:=outlength*2;
  158. while iconv(iconv_ansi2wide,@srcpos,@len,@destpos,@outleft)=size_t(-1) do
  159. begin
  160. case fpgetCerrno of
  161. ESysE2BIG:
  162. begin
  163. outoffset:=destpos-pchar(dest);
  164. { extend }
  165. setlength(dest,outlength+len);
  166. inc(outleft,len*2);
  167. inc(outlength,len);
  168. { string could have been moved }
  169. destpos:=pchar(dest)+outoffset;
  170. end;
  171. else
  172. raise EConvertError.Create('iconv error');
  173. end;
  174. end;
  175. // truncate string
  176. setlength(dest,length(dest)-outleft div 2);
  177. end;
  178. function LowerWideString(const s : WideString) : WideString;
  179. var
  180. i : SizeInt;
  181. begin
  182. SetLength(result,length(s));
  183. for i:=1 to length(s) do
  184. result[i]:=WideChar(towlower(wint_t(s[i])));
  185. end;
  186. function UpperWideString(const s : WideString) : WideString;
  187. var
  188. i : SizeInt;
  189. begin
  190. SetLength(result,length(s));
  191. for i:=1 to length(s) do
  192. result[i]:=WideChar(towupper(wint_t(s[i])));
  193. end;
  194. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  195. begin
  196. end;
  197. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  198. begin
  199. end;
  200. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  201. begin
  202. result:=strcoll(s1,s2);
  203. end;
  204. Procedure SetCWideStringManager;
  205. Var
  206. CWideStringManager : TWideStringManager;
  207. begin
  208. CWideStringManager:=widestringmanager;
  209. With CWideStringManager do
  210. begin
  211. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  212. Ansi2WideMoveProc:=@Ansi2WideMove;
  213. UpperWideStringProc:=@UpperWideString;
  214. LowerWideStringProc:=@LowerWideString;
  215. {
  216. CompareWideStringProc
  217. CompareTextWideStringProc
  218. CharLengthPCharProc
  219. UpperAnsiStringProc
  220. LowerAnsiStringProc
  221. CompareStrAnsiStringProc
  222. CompareTextAnsiStringProc
  223. }
  224. StrCompAnsiStringProc:=@StrCompAnsi;
  225. {
  226. StrICompAnsiStringProc
  227. StrLCompAnsiStringProc
  228. StrLICompAnsiStringProc
  229. StrLowerAnsiStringProc
  230. StrUpperAnsiStringProc
  231. }
  232. end;
  233. SetWideStringManager(CWideStringManager);
  234. end;
  235. initialization
  236. SetCWideStringManager;
  237. { init conversion tables }
  238. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding);
  239. iconv_ansi2wide:=iconv_open(unicode_encoding,nl_langinfo(CODESET));
  240. finalization
  241. iconv_close(iconv_ansi2wide);
  242. end.