cwstring.pp 34 KB

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