cwstring.pp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. {$modeswitch unicodestrings-}
  14. {$h-}
  15. {$inline on}
  16. {$implicitexceptions off}
  17. {$IFNDEF FPC_DOTTEDUNITS}
  18. unit cwstring;
  19. {$ENDIF FPC_DOTTEDUNITS}
  20. interface
  21. procedure SetCWidestringManager;
  22. implementation
  23. {$linklib c}
  24. // Linux (and maybe glibc platforms in general), have iconv in glibc.
  25. {$if defined(linux) or defined(solaris)}
  26. {$define iconv_is_in_libc}
  27. {$endif}
  28. {$ifdef netbsd}
  29. {$ifndef DISABLE_ICONV_LIBC}
  30. {$define iconv_is_in_libc}
  31. {$endif}
  32. {$endif}
  33. {$ifdef dragonfly}
  34. { iconv function are included inside libc for DragonFly version 6.3 }
  35. { See https://man.dragonflybsd.org/?command=iconv&section=3 }
  36. {$ifndef DISABLE_ICONV_LIBC}
  37. {$define iconv_is_in_libc}
  38. {$endif}
  39. {$endif}
  40. { Modern FreeBSD releases also claim to have iconv
  41. function inside libc, at least
  42. this is stated explicitly in iconv(3) man
  43. of FreeBSD from 10.0 release
  44. PM 2023-05-05 }
  45. {$ifdef freebsd}
  46. {$ifndef DISABLE_ICONV_LIBC}
  47. {$define iconv_is_in_libc}
  48. {$endif}
  49. {$endif}
  50. {$ifndef iconv_is_in_libc}
  51. {$if defined(haiku)}
  52. {$linklib textencoding}
  53. {$else}
  54. {$linklib iconv}
  55. {$endif}
  56. {$define useiconv}
  57. {$endif not iconv_is_in_libc}
  58. {$i rtldefs.inc}
  59. {$IFDEF FPC_DOTTEDUNITS}
  60. Uses
  61. UnixApi.Base,
  62. System.CTypes,
  63. UnixApi.Unix,
  64. UnixApi.Types,
  65. System.InitC,
  66. System.DynLibs,
  67. UnixApi.CP;
  68. {$ELSE FPC_DOTTEDUNITS}
  69. Uses
  70. BaseUnix,
  71. ctypes,
  72. unix,
  73. unixtype,
  74. initc,
  75. dynlibs,
  76. unixcp;
  77. {$ENDIF FPC_DOTTEDUNITS}
  78. Const
  79. {$ifndef useiconv}
  80. libiconvname='c'; // is in libc under Linux.
  81. libprefix='lib';
  82. {$else}
  83. {$ifdef haiku}
  84. libiconvname='textencoding'; // is in libtextencoding under Haiku
  85. libprefix='lib';
  86. {$else}
  87. {$if defined(darwin) or defined(aix)}
  88. libiconvname='libiconv';
  89. libprefix='';
  90. {$else}
  91. libiconvname='iconv';
  92. libprefix='lib';
  93. {$endif}
  94. {$endif}
  95. {$endif}
  96. { helper functions from libc }
  97. function towlower(__wc:wint_t):wint_t;cdecl;external clib name 'towlower';
  98. function towupper(__wc:wint_t):wint_t;cdecl;external clib name 'towupper';
  99. function wcscoll (__s1:pwchar_t; __s2:pwchar_t):cint;cdecl;external clib name 'wcscoll';
  100. function strcoll (__s1:PAnsiChar; __s2:PAnsiChar):cint;cdecl;external clib name 'strcoll';
  101. {$ifdef netbsd}
  102. { NetBSD has a new setlocale function defined in /usr/include/locale.h
  103. that should be used }
  104. function setlocale(category: cint; locale: PAnsiChar): PAnsiChar; cdecl; external clib name '__setlocale50';
  105. {$else}
  106. function setlocale(category: cint; locale: PAnsiChar): PAnsiChar; cdecl; external clib name 'setlocale';
  107. {$endif}
  108. {$if not(defined(beos) and not defined(haiku))}
  109. function mbrtowc(pwc: pwchar_t; const s: PAnsiChar; n: size_t; ps: pmbstate_t): size_t; cdecl; external clib name 'mbrtowc';
  110. function wcrtomb(s: PAnsiChar; wc: wchar_t; ps: pmbstate_t): size_t; cdecl; external clib name 'wcrtomb';
  111. function mbrlen(const s: PAnsiChar; n: size_t; ps: pmbstate_t): size_t; cdecl; external clib name 'mbrlen';
  112. {$else beos}
  113. function mbtowc(pwc: pwchar_t; const s: PAnsiChar; n: size_t): size_t; cdecl; external clib name 'mbtowc';
  114. function wctomb(s: PAnsiChar; wc: wchar_t): size_t; cdecl; external clib name 'wctomb';
  115. function mblen(const s: PAnsiChar; n: size_t): size_t; cdecl; external clib name 'mblen';
  116. {$endif beos}
  117. const
  118. {$if defined(linux)}
  119. __LC_CTYPE = 0;
  120. LC_ALL = 6;
  121. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  122. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  123. CODESET = _NL_CTYPE_CODESET_NAME;
  124. {$elseif defined(darwin)}
  125. CODESET = 0;
  126. LC_ALL = 0;
  127. {$elseif defined(FreeBSD)} // actually FreeBSD5. internationalisation is afaik not default on 4.
  128. __LC_CTYPE = 0;
  129. LC_ALL = 0;
  130. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  131. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  132. CODESET = 0; // _NL_CTYPE_CODESET_NAME;
  133. {$elseif defined(solaris)}
  134. {$define ACCEPT_646}
  135. CODESET=49;
  136. LC_ALL = 6;
  137. {$elseif defined(beos)}
  138. {$ifdef haiku}
  139. CODESET= 0; // Checked for Haiku
  140. LC_ALL = 0; // Checked for Haiku
  141. {$else}
  142. {$warning check correct value for BeOS}
  143. CODESET=49;
  144. LC_ALL = 6; // Checked for BeOS
  145. {$endif}
  146. ESysEILSEQ = EILSEQ;
  147. {$elseif defined(OpenBSD)}
  148. {$define ACCEPT_646}
  149. CODESET = 51;
  150. LC_ALL = 0;
  151. {$elseif defined(NetBSD)}
  152. {$define ACCEPT_646}
  153. CODESET = 51;
  154. LC_ALL = 0;
  155. {$elseif defined(aix)}
  156. CODESET = 49;
  157. LC_ALL = -1;
  158. {$elseif defined(dragonfly)}
  159. CODESET = 0;
  160. LC_ALL = 0;
  161. __LC_CTYPE = 0;
  162. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  163. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  164. {$else not aix}
  165. {$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 }
  166. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  167. {$endif}
  168. { unicode encoding name }
  169. {$ifdef FPC_LITTLE_ENDIAN}
  170. unicode_encoding2 = 'UTF-16LE';
  171. unicode_encoding4 = 'UCS-4LE';
  172. {$else FPC_LITTLE_ENDIAN}
  173. {$ifdef AIX}
  174. unicode_encoding2 = 'UTF-16';
  175. {$else AIX}
  176. unicode_encoding2 = 'UTF-16BE';
  177. unicode_encoding4 = 'UCS-4BE';
  178. {$endif AIX}
  179. {$endif FPC_LITTLE_ENDIAN}
  180. { en_US.UTF-8 needs maximally 6 chars, UCS-4/UTF-32 needs 4 }
  181. { -> 10 should be enough? Should actually use MB_CUR_MAX, but }
  182. { that's a libc macro mapped to internal functions/variables }
  183. { and thus not a stable external API on systems where libc }
  184. { breaks backwards compatibility every now and then }
  185. MB_CUR_MAX = 10;
  186. { Requests for iconvctl }
  187. ICONV_TRIVIALP = 0; // int *argument
  188. ICONV_GET_TRANSLITERATE = 1; // int *argument
  189. ICONV_SET_TRANSLITERATE = 2; // const int *argument
  190. ICONV_GET_DISCARD_ILSEQ = 3; // int *argument
  191. ICONV_SET_DISCARD_ILSEQ = 4; // const int *argument
  192. ICONV_SET_HOOKS = 5; // const struct iconv_hooks *argument
  193. ICONV_SET_FALLBACKS = 6; // const struct iconv_fallbacks *argument
  194. type
  195. piconv_t = ^iconv_t;
  196. iconv_t = pointer;
  197. nl_item = cint;
  198. {$ifdef haiku}
  199. function nl_langinfo(__item:nl_item):PAnsiChar;cdecl;external 'root' name 'nl_langinfo';
  200. {$else}
  201. {$ifndef beos}
  202. function nl_langinfo(__item:nl_item):PAnsiChar;cdecl;external libiconvname name 'nl_langinfo';
  203. {$endif}
  204. {$endif}
  205. {$if (not defined(bsd) and not defined(beos)) or defined(iconv_is_in_libc) or (defined(darwin) and not defined(cpupowerpc32))}
  206. function iconv_open(__tocode:PAnsiChar; __fromcode:PAnsiChar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  207. function iconv(__cd:iconv_t; __inbuf:PPAnsiChar; __inbytesleft:psize_t; __outbuf:PPAnsiChar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  208. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  209. const
  210. iconvctlname='iconvctl';
  211. {$else}
  212. function iconv_open(__tocode:PAnsiChar; __fromcode:PAnsiChar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  213. function iconv(__cd:iconv_t; __inbuf:PPAnsiChar; __inbytesleft:psize_t; __outbuf:PPAnsiChar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  214. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  215. const
  216. iconvctlname='libiconvctl';
  217. {$endif}
  218. var
  219. iconvctl:function(__cd:iconv_t; __request:cint; __argument:pointer):cint;cdecl;
  220. procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];
  221. Const
  222. UTF8Name : ShortString = 'UTF-8';
  223. TransLitname : ShortString ='//TRANSLIT';
  224. threadvar
  225. iconv_ansi2wide,
  226. iconv_wide2ansi : iconv_t;
  227. { since we cache the iconv_t converters, we have to do the same
  228. for the DefaultSystemCodePage variable since if it changes, we
  229. have to re-initialize the converters too. We can't do that via
  230. a callback in the widestring manager because DefaultSystemCodePage
  231. is not a threadvar and we can't automatically change this in all
  232. threads }
  233. current_DefaultSystemCodePage: TSystemCodePage;
  234. procedure InitThread;
  235. var
  236. transliterate: cint;
  237. {$if not(defined(darwin) and (defined(cpuarm) or defined(cpuaarch64))) and not defined(iphonesim)}
  238. iconvindex: longint;
  239. {$endif}
  240. iconvname, toencoding: shortstring;
  241. begin
  242. current_DefaultSystemCodePage:=DefaultSystemCodePage;
  243. {$if declared(iconvindex)}
  244. iconvindex:=GetCodepageData(DefaultSystemCodePage);
  245. if iconvindex<>-1 then
  246. iconvname:=UnixCpMap[iconvindex].name
  247. else
  248. { default to UTF-8 on Unix platforms }
  249. iconvname:=UTF8Name;
  250. {$else}
  251. { Unix locale settings are ignored on iPhoneOS/iPhoneSimulator }
  252. iconvname:='UTF-8';
  253. {$endif}
  254. toencoding:=iconvname;
  255. if not assigned(iconvctl) then
  256. toencoding:=toencoding+TransLitName+#0;
  257. iconvname:=iconvname+#0;
  258. iconv_wide2ansi:=iconv_open(PAnsiChar(@toencoding[1]),unicode_encoding2);
  259. iconv_ansi2wide:=iconv_open(unicode_encoding2,PAnsiChar(@iconvname[1]));
  260. if assigned(iconvctl) and
  261. (iconv_wide2ansi<>iconv_t(-1)) then
  262. begin
  263. transliterate:=1;
  264. iconvctl(iconv_wide2ansi,ICONV_SET_TRANSLITERATE,@transliterate);
  265. end;
  266. end;
  267. procedure FiniThread;
  268. begin
  269. if (iconv_wide2ansi <> iconv_t(-1)) then
  270. iconv_close(iconv_wide2ansi);
  271. if (iconv_ansi2wide <> iconv_t(-1)) then
  272. iconv_close(iconv_ansi2wide);
  273. end;
  274. {$if defined(beos) and not defined(haiku)}
  275. function nl_langinfo(__item:nl_item):PAnsiChar;
  276. begin
  277. {$warning TODO BeOS nl_langinfo or more uptodate port of iconv...}
  278. // Now implement the minimum required to correctly initialize WideString support
  279. case __item of
  280. CODESET : Result := 'UTF-8'; // BeOS use UTF-8
  281. else
  282. begin
  283. Assert(False, 'nl_langinfo was called with an unknown nl_item value');
  284. Result := '';
  285. end;
  286. end;
  287. end;
  288. {$endif}
  289. function open_iconv_for_cps(cp: TSystemCodePage; const otherencoding: PAnsiChar; cp_is_from: boolean): iconv_t;
  290. var
  291. iconvindex: longint;
  292. toencoding: rawbytestring;
  293. transliterate: cint;
  294. begin
  295. { TODO: add caching (then we also don't need separate code for
  296. the default system page and other ones)
  297. -- typecasting an ansistring function result to PAnsiChar is
  298. unsafe normally, but these are constant strings -> no
  299. problem }
  300. open_iconv_for_cps:=iconv_t(-1);
  301. iconvindex:=GetCodepageData(cp);
  302. if iconvindex=-1 then
  303. exit;
  304. repeat
  305. if cp_is_from then
  306. open_iconv_for_cps:=iconv_open(otherencoding,PAnsiChar(UnixCpMap[iconvindex].name))
  307. else
  308. begin
  309. toencoding:=UnixCpMap[iconvindex].name;
  310. if not assigned(iconvctl) then
  311. toencoding:=toencoding+'//TRANSLIT';
  312. open_iconv_for_cps:=iconv_open(PAnsiChar(toencoding),otherencoding);
  313. end;
  314. inc(iconvindex);
  315. until (open_iconv_for_cps<>iconv_t(-1)) or
  316. (iconvindex>high(UnixCpMap)) or
  317. (UnixCpMap[iconvindex].cp<>cp);
  318. if not cp_is_from and
  319. (open_iconv_for_cps<>iconv_t(-1)) and
  320. assigned(iconvctl) then
  321. begin
  322. transliterate:=1;
  323. iconvctl(open_iconv_for_cps,ICONV_SET_TRANSLITERATE,@transliterate);
  324. end;
  325. end;
  326. {$ifdef aix}
  327. {$i cwstraix.inc}
  328. {$endif aix}
  329. procedure Wide2AnsiMove(source:pwidechar; var dest:RawByteString; cp:TSystemCodePage; len:SizeInt);
  330. var
  331. outlength,
  332. outoffset,
  333. srclen,
  334. outleft : size_t;
  335. use_iconv: iconv_t;
  336. srcpos : pwidechar;
  337. destpos: PAnsiChar;
  338. mynil : PAnsiChar;
  339. my0 : size_t;
  340. err : longint;
  341. transliterate: cint;
  342. free_iconv: boolean;
  343. {$ifdef aix}
  344. intermediate: rawbytestring;
  345. {$endif aix}
  346. begin
  347. {$ifdef aix}
  348. { AIX libiconv does not support converting cp866 to anything else except
  349. for iso-8859-5 -> always first convert to iso-8859-5, then to UTF-16 }
  350. if cp=866 then
  351. begin
  352. Wide2AnsiMove(source,intermediate,28595,len);
  353. if handle_aix_intermediate(PAnsiChar(intermediate),28595,cp,dest,len) then
  354. exit;
  355. end;
  356. {$endif aix}
  357. if (cp=DefaultSystemCodePage) then
  358. begin
  359. { update iconv converter in case the DefaultSystemCodePage has been
  360. changed }
  361. if current_DefaultSystemCodePage<>DefaultSystemCodePage then
  362. begin
  363. FiniThread;
  364. InitThread;
  365. end;
  366. use_iconv:=iconv_wide2ansi;
  367. free_iconv:=false;
  368. end
  369. else
  370. begin
  371. use_iconv:=open_iconv_for_cps(cp,unicode_encoding2,false);
  372. if (use_iconv<>iconv_t(-1)) and
  373. assigned(iconvctl) then
  374. begin
  375. transliterate:=1;
  376. iconvctl(use_iconv,ICONV_SET_TRANSLITERATE,@transliterate);
  377. end;
  378. free_iconv:=true;
  379. end;
  380. { unsupported encoding -> default move }
  381. if use_iconv=iconv_t(-1) then
  382. begin
  383. DefaultUnicode2AnsiMove(source,dest,DefaultSystemCodePage,len);
  384. exit;
  385. end;
  386. mynil:=nil;
  387. my0:=0;
  388. { rought estimation }
  389. setlength(dest,len*3);
  390. outlength:=len*3;
  391. srclen:=len*2;
  392. srcpos:=source;
  393. destpos:=PAnsiChar(dest);
  394. outleft:=outlength;
  395. while iconv(use_iconv,PPAnsiChar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  396. begin
  397. err:=fpgetCerrno;
  398. case err of
  399. { last character is incomplete sequence }
  400. ESysEINVAL,
  401. { incomplete sequence in the middle }
  402. ESysEILSEQ:
  403. begin
  404. { skip and set to '?' }
  405. inc(srcpos);
  406. dec(srclen,2);
  407. destpos^:='?';
  408. inc(destpos);
  409. dec(outleft);
  410. { reset }
  411. iconv(use_iconv,@mynil,@my0,@mynil,@my0);
  412. if err=ESysEINVAL then
  413. break;
  414. end;
  415. ESysE2BIG:
  416. begin
  417. outoffset:=destpos-PAnsiChar(dest);
  418. { extend }
  419. setlength(dest,outlength+len*3);
  420. inc(outleft,len*3);
  421. inc(outlength,len*3);
  422. { string could have been moved }
  423. destpos:=PAnsiChar(dest)+outoffset;
  424. end;
  425. else
  426. runerror(231);
  427. end;
  428. end;
  429. // truncate string
  430. setlength(dest,length(dest)-outleft);
  431. SetCodePage(dest,cp,false);
  432. if free_iconv then
  433. iconv_close(use_iconv);
  434. end;
  435. procedure Ansi2WideMove(source:PAnsiChar; cp:TSystemCodePage; var dest:widestring; len:SizeInt);
  436. var
  437. outlength,
  438. outoffset,
  439. outleft : size_t;
  440. use_iconv: iconv_t;
  441. srcpos,
  442. destpos: PAnsiChar;
  443. mynil : PAnsiChar;
  444. my0 : size_t;
  445. err: cint;
  446. iconvindex: longint;
  447. free_iconv: boolean;
  448. {$ifdef aix}
  449. intermediate: rawbytestring;
  450. {$endif aix}
  451. begin
  452. {$ifdef aix}
  453. { AIX libiconv does not support converting cp866 to anything else except
  454. for iso-8859-5 -> always first convert to iso-8859-5, then to UTF-16 }
  455. if cp=866 then
  456. begin
  457. if handle_aix_intermediate(source,cp,cp,intermediate,len) then
  458. source:=PAnsiChar(intermediate);
  459. end;
  460. {$endif aix}
  461. if (cp=DefaultSystemCodePage) then
  462. begin
  463. { update iconv converter in case the DefaultSystemCodePage has been
  464. changed }
  465. if current_DefaultSystemCodePage<>DefaultSystemCodePage then
  466. begin
  467. FiniThread;
  468. InitThread;
  469. end;
  470. use_iconv:=iconv_ansi2wide;
  471. free_iconv:=false;
  472. // Using Unicode RTL, we can end up here when iconv_ansi2wide is not yet initialized
  473. if (use_iconv=Nil) then
  474. begin
  475. DefaultAnsi2UnicodeMove(source,DefaultSystemCodePage,dest,len);
  476. exit;
  477. end;
  478. end
  479. else
  480. begin
  481. { TODO: add caching (then we also don't need separate code for
  482. the default system page and other ones)
  483. -- typecasting an ansistring function result to PAnsiChar is
  484. unsafe normally, but these are constant strings -> no
  485. problem }
  486. use_iconv:=open_iconv_for_cps(cp,unicode_encoding2,true);
  487. free_iconv:=true;
  488. end;
  489. { unsupported encoding -> default move }
  490. if use_iconv=iconv_t(-1) then
  491. begin
  492. DefaultAnsi2UnicodeMove(source,DefaultSystemCodePage,dest,len);
  493. exit;
  494. end;
  495. mynil:=nil;
  496. my0:=0;
  497. // extra space
  498. outlength:=len+1;
  499. setlength(dest,outlength);
  500. srcpos:=source;
  501. destpos:=PAnsiChar(dest);
  502. outleft:=outlength*2;
  503. while iconv(use_iconv,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  504. begin
  505. err:=fpgetCerrno;
  506. case err of
  507. ESysEINVAL,
  508. ESysEILSEQ:
  509. begin
  510. { skip and set to '?' }
  511. inc(srcpos);
  512. dec(len);
  513. pwidechar(destpos)^:='?';
  514. inc(destpos,2);
  515. dec(outleft,2);
  516. { reset }
  517. iconv(use_iconv,@mynil,@my0,@mynil,@my0);
  518. if err=ESysEINVAL then
  519. break;
  520. end;
  521. ESysE2BIG:
  522. begin
  523. outoffset:=destpos-PAnsiChar(dest);
  524. { extend }
  525. setlength(dest,outlength+len);
  526. inc(outleft,len*2);
  527. inc(outlength,len);
  528. { string could have been moved }
  529. destpos:=PAnsiChar(dest)+outoffset;
  530. end;
  531. else
  532. runerror(231);
  533. end;
  534. end;
  535. // truncate string
  536. setlength(dest,length(dest)-outleft div 2);
  537. if free_iconv then
  538. iconv_close(use_iconv);
  539. end;
  540. function LowerWideString(const s : WideString) : WideString;
  541. var
  542. i : SizeInt;
  543. begin
  544. SetLength(result,length(s));
  545. for i:=0 to length(s)-1 do
  546. pwidechar(result)[i]:=WideChar(towlower(wint_t(s[i+1])));
  547. end;
  548. function UpperWideString(const s : WideString) : WideString;
  549. var
  550. i : SizeInt;
  551. begin
  552. SetLength(result,length(s));
  553. for i:=0 to length(s)-1 do
  554. pwidechar(result)[i]:=WideChar(towupper(wint_t(s[i+1])));
  555. end;
  556. procedure EnsureAnsiLen(var S: AnsiString; const len: SizeInt); inline;
  557. begin
  558. if (len>length(s)) then
  559. if (length(s) < 10*256) then
  560. setlength(s,length(s)+10)
  561. else
  562. setlength(s,length(s)+length(s) shr 8);
  563. end;
  564. procedure ConcatCharToAnsiStr(const c: AnsiChar; var S: AnsiString; var index: SizeInt);
  565. begin
  566. EnsureAnsiLen(s,index);
  567. PAnsiChar(@s[index])^:=c;
  568. inc(index);
  569. end;
  570. { concatenates an utf-32 Char to a ansistring. S *must* be unique when entering. }
  571. {$if not(defined(beos) and not defined(haiku))}
  572. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt; var mbstate: mbstate_t);
  573. {$else not beos}
  574. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt);
  575. {$endif beos}
  576. var
  577. p : PAnsiChar;
  578. mblen : size_t;
  579. begin
  580. { we know that s is unique -> avoid uniquestring calls}
  581. p:=@s[index];
  582. if (nc<=127) then
  583. ConcatCharToAnsiStr(AnsiChar(nc),s,index)
  584. else
  585. begin
  586. EnsureAnsiLen(s,index+MB_CUR_MAX);
  587. {$if not(defined(beos) and not defined(haiku))}
  588. mblen:=wcrtomb(p,wchar_t(nc),@mbstate);
  589. {$else not beos}
  590. mblen:=wctomb(p,wchar_t(nc));
  591. {$endif not beos}
  592. if (mblen<>size_t(-1)) then
  593. inc(index,mblen)
  594. else
  595. begin
  596. { invalid wide AnsiChar }
  597. p^:='?';
  598. inc(index);
  599. end;
  600. end;
  601. end;
  602. function LowerAnsiString(const s : AnsiString) : AnsiString;
  603. var
  604. i, slen,
  605. resindex : SizeInt;
  606. mblen : size_t;
  607. {$if not(defined(beos) and not defined(haiku))}
  608. ombstate,
  609. nmbstate : mbstate_t;
  610. {$endif beos}
  611. wc : wchar_t;
  612. begin
  613. {$if not(defined(beos) and not defined(haiku))}
  614. fillchar(ombstate,sizeof(ombstate),0);
  615. fillchar(nmbstate,sizeof(nmbstate),0);
  616. {$endif beos}
  617. slen:=length(s);
  618. SetLength(result,slen+10);
  619. i:=1;
  620. resindex:=1;
  621. while (i<=slen) do
  622. begin
  623. if (s[i]<=#127) then
  624. begin
  625. wc:=wchar_t(s[i]);
  626. mblen:= 1;
  627. end
  628. else
  629. {$if not(defined(beos) and not defined(haiku))}
  630. mblen:=mbrtowc(@wc, PAnsiChar(@s[i]), slen-i+1, @ombstate);
  631. {$else not beos}
  632. mblen:=mbtowc(@wc, PAnsiChar(@s[i]), slen-i+1);
  633. {$endif not beos}
  634. case mblen of
  635. size_t(-2):
  636. begin
  637. { partial invalid character, copy literally }
  638. while (i<=slen) do
  639. begin
  640. ConcatCharToAnsiStr(s[i],result,resindex);
  641. inc(i);
  642. end;
  643. end;
  644. size_t(-1), 0:
  645. begin
  646. { invalid or null character }
  647. ConcatCharToAnsiStr(s[i],result,resindex);
  648. inc(i);
  649. end;
  650. else
  651. begin
  652. { a valid sequence }
  653. { even if mblen = 1, the lowercase version may have a }
  654. { different length }
  655. { We can't do anything special if wchar_t is 16 bit... }
  656. {$if not(defined(beos) and not defined(haiku))}
  657. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex,nmbstate);
  658. {$else not beos}
  659. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex);
  660. {$endif not beos}
  661. inc(i,mblen);
  662. end;
  663. end;
  664. end;
  665. SetLength(result,resindex-1);
  666. end;
  667. function UpperAnsiString(const s : AnsiString) : AnsiString;
  668. var
  669. i, slen,
  670. resindex : SizeInt;
  671. mblen : size_t;
  672. {$if not(defined(beos) and not defined(haiku))}
  673. ombstate,
  674. nmbstate : mbstate_t;
  675. {$endif beos}
  676. wc : wchar_t;
  677. begin
  678. {$if not(defined(beos) and not defined(haiku))}
  679. fillchar(ombstate,sizeof(ombstate),0);
  680. fillchar(nmbstate,sizeof(nmbstate),0);
  681. {$endif beos}
  682. slen:=length(s);
  683. SetLength(result,slen+10);
  684. i:=1;
  685. resindex:=1;
  686. while (i<=slen) do
  687. begin
  688. if (s[i]<=#127) then
  689. begin
  690. wc:=wchar_t(s[i]);
  691. mblen:= 1;
  692. end
  693. else
  694. {$if not(defined(beos) and not defined(haiku))}
  695. mblen:=mbrtowc(@wc, PAnsiChar(@s[i]), slen-i+1, @ombstate);
  696. {$else not beos}
  697. mblen:=mbtowc(@wc, PAnsiChar(@s[i]), slen-i+1);
  698. {$endif beos}
  699. case mblen of
  700. size_t(-2):
  701. begin
  702. { partial invalid character, copy literally }
  703. while (i<=slen) do
  704. begin
  705. ConcatCharToAnsiStr(s[i],result,resindex);
  706. inc(i);
  707. end;
  708. end;
  709. size_t(-1), 0:
  710. begin
  711. { invalid or null character }
  712. ConcatCharToAnsiStr(s[i],result,resindex);
  713. inc(i);
  714. end;
  715. else
  716. begin
  717. { a valid sequence }
  718. { even if mblen = 1, the uppercase version may have a }
  719. { different length }
  720. { We can't do anything special if wchar_t is 16 bit... }
  721. {$if not(defined(beos) and not defined(haiku))}
  722. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex,nmbstate);
  723. {$else not beos}
  724. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex);
  725. {$endif not beos}
  726. inc(i,mblen);
  727. end;
  728. end;
  729. end;
  730. SetLength(result,resindex-1);
  731. end;
  732. function WideStringToUCS4StringNoNulls(const s : WideString) : UCS4String;
  733. var
  734. i, slen,
  735. destindex : SizeInt;
  736. uch : UCS4Char;
  737. begin
  738. slen:=length(s);
  739. setlength(result,slen+1);
  740. i:=1;
  741. destindex:=0;
  742. while (i<=slen) do
  743. begin
  744. uch:=UCS4Char(s[i]);
  745. if (uch=0) then
  746. result[destindex]:=32
  747. else if (uch<=$d7ff) or (uch>=$e000) then
  748. result[destindex]:=uch
  749. else if (uch<=$dbff) and
  750. (i<slen) and
  751. (s[i+1]>=#$dc00) and
  752. (s[i+1]<=#$dfff) then
  753. begin
  754. result[destindex]:=(UCS4Char(uch-$d7c0) shl 10)+(UCS4Char(s[i+1]) xor $dc00);
  755. inc(i);
  756. end
  757. else { invalid surrogate pair }
  758. result[destindex]:=uch;
  759. inc(i);
  760. inc(destindex);
  761. end;
  762. result[destindex]:=UCS4Char(0);
  763. { Trimming length in this particular case is just a waste of time,
  764. because result will be interpreted as null-terminated and discarded
  765. almost immediately }
  766. end;
  767. function CompareWideString(const s1, s2 : WideString; Options : TCompareOptions) : PtrInt;
  768. {$if not(defined (aix) and defined(cpupowerpc32))}
  769. var
  770. hs1,hs2 : UCS4String;
  771. us1,us2 : WideString;
  772. begin
  773. { wcscoll interprets null chars as end-of-string -> filter out }
  774. if coIgnoreCase in Options then
  775. begin
  776. us1:=UpperWideString(s1);
  777. us2:=UpperWideString(s2);
  778. end
  779. else
  780. begin
  781. us1:=s1;
  782. us2:=s2;
  783. end;
  784. hs1:=WideStringToUCS4StringNoNulls(us1);
  785. hs2:=WideStringToUCS4StringNoNulls(us2);
  786. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  787. end;
  788. {$else}
  789. { AIX/PPC32 has a 16 bit wchar_t }
  790. var
  791. i, len: longint;
  792. us1,us2 : WideString;
  793. hs1, hs2: array of widechar;
  794. begin
  795. if coIgnoreCase in Options then
  796. begin
  797. us1:=UpperWideString(s1);
  798. us2:=UpperWideString(s2);
  799. end
  800. else
  801. begin
  802. us1:=s1;
  803. us2:=s2;
  804. end;
  805. len:=length(us1);
  806. setlength(hs1,len+1);
  807. for i:=1 to len do
  808. if us1[i]<>#0 then
  809. hs1[i-1]:=us1[i]
  810. else
  811. hs1[i-1]:=#32;
  812. hs1[len]:=#0;
  813. len:=length(us2);
  814. setlength(hs2,len+1);
  815. for i:=1 to len do
  816. if us2[i]<>#0 then
  817. hs2[i-1]:=us2[i]
  818. else
  819. hs2[i-1]:=#32;
  820. hs2[len]:=#0;
  821. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  822. end;
  823. {$endif}
  824. { return value: number of code points in the string. Whenever an invalid
  825. code point is encountered, all characters part of this invalid code point
  826. are considered to form one "character" and the next character is
  827. considered to be the start of a new (possibly also invalid) code point }
  828. function CharLengthPChar(const Str: PAnsiChar): PtrInt;
  829. var
  830. nextlen: ptrint;
  831. s: PAnsiChar;
  832. {$if not(defined(beos) and not defined(haiku))}
  833. mbstate: mbstate_t;
  834. {$endif not beos}
  835. begin
  836. result:=0;
  837. s:=str;
  838. {$if not(defined(beos) and not defined(haiku))}
  839. fillchar(mbstate,sizeof(mbstate),0);
  840. {$endif not beos}
  841. repeat
  842. {$if defined(beos) and not defined(haiku)}
  843. nextlen:=ptrint(mblen(s,MB_CUR_MAX));
  844. {$else beos}
  845. nextlen:=ptrint(mbrlen(s,MB_CUR_MAX,@mbstate));
  846. {$endif beos}
  847. { skip invalid/incomplete sequences }
  848. if (nextlen<0) then
  849. nextlen:=1;
  850. inc(result,1);
  851. inc(s,nextlen);
  852. until (nextlen=0);
  853. end;
  854. function CodePointLength(const Str: PAnsiChar; maxlookahead: ptrint): PtrInt;
  855. {$if not(defined(beos) and not defined(haiku))}
  856. var
  857. mbstate: mbstate_t;
  858. {$endif not beos}
  859. begin
  860. {$if defined(beos) and not defined(haiku)}
  861. result:=ptrint(mblen(str,maxlookahead));
  862. {$else beos}
  863. fillchar(mbstate,sizeof(mbstate),0);
  864. result:=ptrint(mbrlen(str,maxlookahead,@mbstate));
  865. { mbrlen can also return -2 for "incomplete but potially valid character
  866. and data has been processed" }
  867. if result<0 then
  868. result:=-1;
  869. {$endif beos}
  870. end;
  871. function StrCompAnsiIntern(s1,s2 : PAnsiChar; len1, len2: PtrInt; canmodifys1, canmodifys2: boolean): PtrInt;
  872. var
  873. a,b: PAnsiChar;
  874. i: PtrInt;
  875. begin
  876. if not(canmodifys1) then
  877. getmem(a,len1+1)
  878. else
  879. a:=s1;
  880. for i:=0 to len1-1 do
  881. if s1[i]<>#0 then
  882. a[i]:=s1[i]
  883. else
  884. a[i]:=#32;
  885. a[len1]:=#0;
  886. if not(canmodifys2) then
  887. getmem(b,len2+1)
  888. else
  889. b:=s2;
  890. for i:=0 to len2-1 do
  891. if s2[i]<>#0 then
  892. b[i]:=s2[i]
  893. else
  894. b[i]:=#32;
  895. b[len2]:=#0;
  896. result:=strcoll(a,b);
  897. if not(canmodifys1) then
  898. freemem(a);
  899. if not(canmodifys2) then
  900. freemem(b);
  901. end;
  902. function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
  903. begin
  904. result:=StrCompAnsiIntern(PAnsiChar(s1),PAnsiChar(s2),length(s1),length(s2),false,false);
  905. end;
  906. function StrCompAnsi(s1,s2 : PAnsiChar): PtrInt;
  907. begin
  908. result:=strcoll(s1,s2);
  909. end;
  910. function AnsiCompareText(const S1, S2: ansistring): PtrInt;
  911. var
  912. a, b: AnsiString;
  913. begin
  914. a:=UpperAnsistring(s1);
  915. b:=UpperAnsistring(s2);
  916. result:=StrCompAnsiIntern(PAnsiChar(a),PAnsiChar(b),length(a),length(b),true,true);
  917. end;
  918. function AnsiStrIComp(S1, S2: PAnsiChar): PtrInt;
  919. begin
  920. result:=AnsiCompareText(ansistring(s1),ansistring(s2));
  921. end;
  922. function AnsiStrLComp(S1, S2: PAnsiChar; MaxLen: PtrUInt): PtrInt;
  923. var
  924. a, b: PAnsiChar;
  925. begin
  926. if (maxlen=0) then
  927. exit(0);
  928. if (s1[maxlen]<>#0) then
  929. begin
  930. getmem(a,maxlen+1);
  931. move(s1^,a^,maxlen);
  932. a[maxlen]:=#0;
  933. end
  934. else
  935. a:=s1;
  936. if (s2[maxlen]<>#0) then
  937. begin
  938. getmem(b,maxlen+1);
  939. move(s2^,b^,maxlen);
  940. b[maxlen]:=#0;
  941. end
  942. else
  943. b:=s2;
  944. result:=StrCompAnsiIntern(a,b,maxlen,maxlen,a<>s1,b<>s2);
  945. if (a<>s1) then
  946. freemem(a);
  947. if (b<>s2) then
  948. freemem(b);
  949. end;
  950. function AnsiStrLIComp(S1, S2: PAnsiChar; MaxLen: PtrUInt): PtrInt;
  951. var
  952. a, b: ansistring;
  953. begin
  954. if (maxlen=0) then
  955. exit(0);
  956. setlength(a,maxlen);
  957. move(s1^,a[1],maxlen);
  958. setlength(b,maxlen);
  959. move(s2^,b[1],maxlen);
  960. result:=AnsiCompareText(a,b);
  961. end;
  962. procedure ansi2pchar(const s: ansistring; const orgp: PAnsiChar; out p: PAnsiChar);
  963. var
  964. newlen: sizeint;
  965. begin
  966. newlen:=length(s);
  967. if newlen>strlen(orgp) then
  968. fpc_rangeerror;
  969. p:=orgp;
  970. if (newlen>0) then
  971. move(s[1],p[0],newlen);
  972. p[newlen]:=#0;
  973. end;
  974. function AnsiStrLower(Str: PAnsiChar): PAnsiChar;
  975. var
  976. temp: ansistring;
  977. begin
  978. temp:=loweransistring(str);
  979. ansi2pchar(temp,str,result);
  980. end;
  981. function AnsiStrUpper(Str: PAnsiChar): PAnsiChar;
  982. var
  983. temp: ansistring;
  984. begin
  985. temp:=upperansistring(str);
  986. ansi2pchar(temp,str,result);
  987. end;
  988. function envvarset(const varname: PAnsiChar): boolean;
  989. var
  990. varval: PAnsiChar;
  991. begin
  992. varval:=fpgetenv(varname);
  993. result:=
  994. assigned(varval) and
  995. (varval[0]<>#0);
  996. end;
  997. function GetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
  998. var
  999. langinfo: PAnsiChar;
  1000. begin
  1001. {$ifdef FPCRTL_FILESYSTEM_UTF8}
  1002. if stdcp=scpFileSystemSingleByte then
  1003. begin
  1004. result:=CP_UTF8;
  1005. exit;
  1006. end;
  1007. {$endif}
  1008. { if none of the relevant LC_* environment variables are set, fall back to
  1009. UTF-8 (this happens under some versions of OS X for GUI applications, which
  1010. otherwise get CP_ASCII) }
  1011. if envvarset('LC_ALL') or
  1012. envvarset('LC_CTYPE') or
  1013. envvarset('LANG') then
  1014. begin
  1015. langinfo:=nl_langinfo(CODESET);
  1016. { there's a bug in the Mac OS X 10.5 libc (based on FreeBSD's)
  1017. that causes it to return an empty string of UTF-8 locales
  1018. -> patch up (and in general, UTF-8 is a good default on
  1019. Unix platforms) }
  1020. if not assigned(langinfo) or
  1021. (langinfo^=#0) then
  1022. langinfo:='UTF-8';
  1023. Result:=GetCodepageByName(ansistring(langinfo));
  1024. end
  1025. else
  1026. Result:={$IFDEF FPC_DOTTEDUNITS}UnixApi.CP{$ELSE}unixcp{$ENDIF}.GetSystemCodepage;
  1027. end;
  1028. {$ifdef FPC_HAS_CPSTRING}
  1029. procedure SetStdIOCodePage(var T: Text); inline;
  1030. begin
  1031. case TextRec(T).Mode of
  1032. fmInput:TextRec(T).CodePage:=GetStandardCodePage(scpConsoleInput);
  1033. fmOutput:TextRec(T).CodePage:=GetStandardCodePage(scpConsoleOutput);
  1034. end;
  1035. end;
  1036. procedure SetStdIOCodePages; inline;
  1037. begin
  1038. SetStdIOCodePage(Input);
  1039. SetStdIOCodePage(Output);
  1040. SetStdIOCodePage(ErrOutput);
  1041. SetStdIOCodePage(StdOut);
  1042. SetStdIOCodePage(StdErr);
  1043. end;
  1044. {$endif FPC_HAS_CPSTRING}
  1045. var
  1046. OrgWideStringManager: TUnicodeStringManager;
  1047. Procedure SetCWideStringManager;
  1048. Var
  1049. CWideStringManager : TUnicodeStringManager;
  1050. begin
  1051. GetUnicodeStringManager(OrgWideStringManager);
  1052. CWideStringManager:=OrgWideStringManager;
  1053. With CWideStringManager do
  1054. begin
  1055. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  1056. Ansi2WideMoveProc:=@Ansi2WideMove;
  1057. UpperWideStringProc:=@UpperWideString;
  1058. LowerWideStringProc:=@LowerWideString;
  1059. CompareWideStringProc:=@CompareWideString;
  1060. // CompareTextWideStringProc:=@CompareTextWideString;
  1061. CharLengthPCharProc:=@CharLengthPChar;
  1062. CodePointLengthProc:=@CodePointLength;
  1063. UpperAnsiStringProc:=@UpperAnsiString;
  1064. LowerAnsiStringProc:=@LowerAnsiString;
  1065. CompareStrAnsiStringProc:=@CompareStrAnsiString;
  1066. CompareTextAnsiStringProc:=@AnsiCompareText;
  1067. StrCompAnsiStringProc:=@StrCompAnsi;
  1068. StrICompAnsiStringProc:=@AnsiStrIComp;
  1069. StrLCompAnsiStringProc:=@AnsiStrLComp;
  1070. StrLICompAnsiStringProc:=@AnsiStrLIComp;
  1071. StrLowerAnsiStringProc:=@AnsiStrLower;
  1072. StrUpperAnsiStringProc:=@AnsiStrUpper;
  1073. ThreadInitProc:=@InitThread;
  1074. ThreadFiniProc:=@FiniThread;
  1075. { Unicode }
  1076. Unicode2AnsiMoveProc:=@Wide2AnsiMove;
  1077. Ansi2UnicodeMoveProc:=@Ansi2WideMove;
  1078. UpperUnicodeStringProc:=@UpperWideString;
  1079. LowerUnicodeStringProc:=@LowerWideString;
  1080. CompareUnicodeStringProc:=@CompareWideString;
  1081. { CodePage }
  1082. GetStandardCodePageProc:=@GetStandardCodePage;
  1083. end;
  1084. SetUnicodeStringManager(CWideStringManager);
  1085. end;
  1086. var
  1087. iconvlib:TLibHandle;
  1088. initialization
  1089. SetCWideStringManager;
  1090. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  1091. { with the information from the environment variables according to POSIX }
  1092. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  1093. setlocale(LC_ALL,'');
  1094. { load iconv library and iconvctl function }
  1095. iconvlib:=LoadLibrary(libprefix+libiconvname+'.'+SharedSuffix);
  1096. if iconvlib=0 then
  1097. iconvlib:=LoadLibrary(libprefix+libiconvname+'.'+SharedSuffix+'.6');
  1098. if iconvlib<>0 then
  1099. pointer(iconvctl):=GetProcAddress(iconvlib,iconvctlname);
  1100. { set the DefaultSystemCodePage }
  1101. DefaultSystemCodePage:=GetStandardCodePage(scpAnsi);
  1102. DefaultFileSystemCodePage:=GetStandardCodePage(scpFileSystemSingleByte);
  1103. DefaultRTLFileSystemCodePage:=DefaultFileSystemCodePage;
  1104. {$ifdef FPC_HAS_CPSTRING}
  1105. SetStdIOCodePages;
  1106. {$endif FPC_HAS_CPSTRING}
  1107. { init conversion tables for main program }
  1108. InitThread;
  1109. finalization
  1110. { fini conversion tables for main program }
  1111. FiniThread;
  1112. { unload iconv library }
  1113. if iconvlib<>0 then
  1114. FreeLibrary(iconvlib);
  1115. { restore previous (probably default) widestring manager so that subsequent calls
  1116. into the widestring manager won't trigger the finalized functionality }
  1117. SetWideStringManager(OrgWideStringManager);
  1118. end.