cwstring.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. const
  47. {$ifdef linux}
  48. __LC_CTYPE = 0;
  49. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  50. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  51. CODESET = _NL_CTYPE_CODESET_NAME;
  52. {$else linux}
  53. {$ifdef darwin}
  54. CODESET = 0;
  55. {$else darwin}
  56. {$ifdef FreeBSD} // actually FreeBSD5. internationalisation is afaik not default on 4.
  57. CODESET = 0;
  58. {$else freebsd}
  59. {$error lookup the value of CODESET in /usr/include/langinfo.h for your OS }
  60. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  61. {$endif FreeBSD}
  62. {$endif darwin}
  63. {$endif linux}
  64. { unicode encoding name }
  65. {$ifdef FPC_LITTLE_ENDIAN}
  66. unicode_encoding = 'UNICODELITTLE';
  67. {$else FPC_LITTLE_ENDIAN}
  68. unicode_encoding = 'UNICODEBIG';
  69. {$endif FPC_LITTLE_ENDIAN}
  70. type
  71. piconv_t = ^iconv_t;
  72. iconv_t = pointer;
  73. nl_item = cint;
  74. function nl_langinfo(__item:nl_item):pchar;cdecl;external libiconvname name 'nl_langinfo';
  75. {$ifndef Darwin}
  76. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  77. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  78. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  79. {$else}
  80. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  81. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  82. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  83. {$endif}
  84. var
  85. iconv_ansi2wide,
  86. iconv_wide2ansi : iconv_t;
  87. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  88. var
  89. outlength,
  90. outoffset,
  91. srclen,
  92. outleft : size_t;
  93. srcpos : pwidechar;
  94. destpos: pchar;
  95. mynil : pchar;
  96. my0 : size_t;
  97. begin
  98. mynil:=nil;
  99. my0:=0;
  100. { rought estimation }
  101. setlength(dest,len*3);
  102. outlength:=len*3;
  103. srclen:=len*2;
  104. srcpos:=source;
  105. destpos:=pchar(dest);
  106. outleft:=outlength;
  107. while iconv(iconv_wide2ansi,@srcpos,@srclen,@destpos,@outleft)=size_t(-1) do
  108. begin
  109. case fpgetCerrno of
  110. ESysEILSEQ:
  111. begin
  112. { skip and set to '?' }
  113. inc(srcpos);
  114. dec(srclen,2);
  115. destpos^:='?';
  116. inc(destpos);
  117. dec(outleft);
  118. { reset }
  119. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  120. end;
  121. ESysE2BIG:
  122. begin
  123. outoffset:=destpos-pchar(dest);
  124. { extend }
  125. setlength(dest,outlength+len*3);
  126. inc(outleft,len*3);
  127. inc(outlength,len*3);
  128. { string could have been moved }
  129. destpos:=pchar(dest)+outoffset;
  130. end;
  131. else
  132. raise EConvertError.Create('iconv error');
  133. end;
  134. end;
  135. // truncate string
  136. setlength(dest,length(dest)-outleft);
  137. end;
  138. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  139. var
  140. outlength,
  141. outoffset,
  142. outleft : size_t;
  143. srcpos,
  144. destpos: pchar;
  145. mynil : pchar;
  146. my0 : size_t;
  147. begin
  148. mynil:=nil;
  149. my0:=0;
  150. // extra space
  151. outlength:=len+1;
  152. setlength(dest,outlength);
  153. outlength:=len+1;
  154. srcpos:=source;
  155. destpos:=pchar(dest);
  156. outleft:=outlength*2;
  157. while iconv(iconv_ansi2wide,@srcpos,@len,@destpos,@outleft)=size_t(-1) do
  158. begin
  159. case fpgetCerrno of
  160. ESysE2BIG:
  161. begin
  162. outoffset:=destpos-pchar(dest);
  163. { extend }
  164. setlength(dest,outlength+len);
  165. inc(outleft,len*2);
  166. inc(outlength,len);
  167. { string could have been moved }
  168. destpos:=pchar(dest)+outoffset;
  169. end;
  170. else
  171. raise EConvertError.Create('iconv error');
  172. end;
  173. end;
  174. // truncate string
  175. setlength(dest,length(dest)-outleft div 2);
  176. end;
  177. function LowerWideString(const s : WideString) : WideString;
  178. var
  179. i : SizeInt;
  180. begin
  181. SetLength(result,length(s));
  182. for i:=1 to length(s) do
  183. result[i]:=WideChar(towlower(wint_t(s[i])));
  184. end;
  185. function UpperWideString(const s : WideString) : WideString;
  186. var
  187. i : SizeInt;
  188. begin
  189. SetLength(result,length(s));
  190. for i:=1 to length(s) do
  191. result[i]:=WideChar(towupper(wint_t(s[i])));
  192. end;
  193. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  194. begin
  195. end;
  196. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  197. begin
  198. end;
  199. Var
  200. CWideStringManager : TWideStringManager;
  201. Procedure SetCWideStringManager;
  202. begin
  203. CWideStringManager:=widestringmanager;
  204. With CWideStringManager do
  205. begin
  206. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  207. Ansi2WideMoveProc:=@Ansi2WideMove;
  208. UpperWideStringProc:=@UpperWideString;
  209. LowerWideStringProc:=@LowerWideString;
  210. {
  211. CompareWideStringProc
  212. CompareTextWideStringProc
  213. CharLengthPCharProc
  214. UpperAnsiStringProc
  215. LowerAnsiStringProc
  216. CompareStrAnsiStringProc
  217. CompareTextAnsiStringProc
  218. StrCompAnsiStringProc
  219. StrICompAnsiStringProc
  220. StrLCompAnsiStringProc
  221. StrLICompAnsiStringProc
  222. StrLowerAnsiStringProc
  223. StrUpperAnsiStringProc
  224. }
  225. end;
  226. SetWideStringManager(CWideStringManager);
  227. end;
  228. initialization
  229. SetCWideStringManager;
  230. { init conversion tables }
  231. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding);
  232. iconv_ansi2wide:=iconv_open(unicode_encoding,nl_langinfo(CODESET));
  233. finalization
  234. iconv_close(iconv_ansi2wide);
  235. end.