cwstring.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. {$inline on}
  14. unit cwstring;
  15. interface
  16. procedure SetCWidestringManager;
  17. implementation
  18. {$linklib c}
  19. {$if not defined(linux) and not defined(solaris)} // Linux (and maybe glibc platforms in general), have iconv in glibc.
  20. {$linklib iconv}
  21. {$define useiconv}
  22. {$endif linux}
  23. Uses
  24. BaseUnix,
  25. ctypes,
  26. unix,
  27. unixtype,
  28. initc;
  29. Const
  30. {$ifndef useiconv}
  31. libiconvname='c'; // is in libc under Linux.
  32. {$else}
  33. libiconvname='iconv';
  34. {$endif}
  35. { helper functions from libc }
  36. function towlower(__wc:wint_t):wint_t;cdecl;external clib name 'towlower';
  37. function towupper(__wc:wint_t):wint_t;cdecl;external clib name 'towupper';
  38. function wcscoll (__s1:pwchar_t; __s2:pwchar_t):cint;cdecl;external clib name 'wcscoll';
  39. function strcoll (__s1:pchar; __s2:pchar):cint;cdecl;external clib name 'strcoll';
  40. function setlocale(category: cint; locale: pchar): pchar; cdecl; external clib name 'setlocale';
  41. {$ifndef beos}
  42. function mbrtowc(pwc: pwchar_t; const s: pchar; n: size_t; ps: pmbstate_t): size_t; cdecl; external clib name 'mbrtowc';
  43. function wcrtomb(s: pchar; wc: wchar_t; ps: pmbstate_t): size_t; cdecl; external clib name 'wcrtomb';
  44. function mbrlen(const s: pchar; n: size_t; ps: pmbstate_t): size_t; cdecl; external clib name 'mbrlen';
  45. {$else beos}
  46. function mbtowc(pwc: pwchar_t; const s: pchar; n: size_t): size_t; cdecl; external clib name 'mbtowc';
  47. function wctomb(s: pchar; wc: wchar_t): size_t; cdecl; external clib name 'wctomb';
  48. function mblen(const s: pchar; n: size_t): size_t; cdecl; external clib name 'mblen';
  49. {$endif beos}
  50. const
  51. {$ifdef linux}
  52. __LC_CTYPE = 0;
  53. LC_ALL = 6;
  54. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  55. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  56. CODESET = _NL_CTYPE_CODESET_NAME;
  57. {$else linux}
  58. {$ifdef darwin}
  59. CODESET = 0;
  60. LC_ALL = 0;
  61. {$else darwin}
  62. {$ifdef FreeBSD} // actually FreeBSD5. internationalisation is afaik not default on 4.
  63. __LC_CTYPE = 0;
  64. LC_ALL = 0;
  65. _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
  66. _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
  67. CODESET = 0; // _NL_CTYPE_CODESET_NAME;
  68. {$else freebsd}
  69. {$ifdef solaris}
  70. CODESET=49;
  71. LC_ALL = 6;
  72. {$else solaris}
  73. {$ifdef beos}
  74. {$warning check correct value for BeOS}
  75. CODESET=49;
  76. LC_ALL = 6; // Checked for BeOS, but 0 under Haiku...
  77. ESysEILSEQ = EILSEQ;
  78. {$else}
  79. {$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 }
  80. // and while doing it, check if iconv is in libc, and if the symbols are prefixed with iconv_ or libiconv_
  81. {$endif beos}
  82. {$endif solaris}
  83. {$endif FreeBSD}
  84. {$endif darwin}
  85. {$endif linux}
  86. { unicode encoding name }
  87. {$ifdef FPC_LITTLE_ENDIAN}
  88. unicode_encoding2 = 'UTF-16LE';
  89. unicode_encoding4 = 'UCS-4LE';
  90. {$else FPC_LITTLE_ENDIAN}
  91. unicode_encoding2 = 'UTF-16BE';
  92. unicode_encoding4 = 'UCS-4BE';
  93. {$endif FPC_LITTLE_ENDIAN}
  94. { en_US.UTF-8 needs maximally 6 chars, UCS-4/UTF-32 needs 4 }
  95. { -> 10 should be enough? Should actually use MB_CUR_MAX, but }
  96. { that's a libc macro mapped to internal functions/variables }
  97. { and thus not a stable external API on systems where libc }
  98. { breaks backwards compatibility every now and then }
  99. MB_CUR_MAX = 10;
  100. type
  101. piconv_t = ^iconv_t;
  102. iconv_t = pointer;
  103. nl_item = cint;
  104. {$ifndef beos}
  105. function nl_langinfo(__item:nl_item):pchar;cdecl;external libiconvname name 'nl_langinfo';
  106. {$endif}
  107. {$if (not defined(bsd) and not defined(beos)) or defined(darwin)}
  108. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'iconv_open';
  109. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'iconv';
  110. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'iconv_close';
  111. {$else}
  112. function iconv_open(__tocode:pchar; __fromcode:pchar):iconv_t;cdecl;external libiconvname name 'libiconv_open';
  113. function iconv(__cd:iconv_t; __inbuf:ppchar; __inbytesleft:psize_t; __outbuf:ppchar; __outbytesleft:psize_t):size_t;cdecl;external libiconvname name 'libiconv';
  114. function iconv_close(__cd:iconv_t):cint;cdecl;external libiconvname name 'libiconv_close';
  115. {$endif}
  116. procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];
  117. threadvar
  118. iconv_ansi2wide,
  119. iconv_wide2ansi : iconv_t;
  120. {$ifdef beos}
  121. function nl_langinfo(__item:nl_item):pchar;
  122. begin
  123. {$warning TODO BeOS nl_langinfo or more uptodate port of iconv...}
  124. // Now implement the minimum required to correctly initialize WideString support
  125. case __item of
  126. CODESET : Result := 'UTF-8'; // BeOS use UTF-8
  127. else
  128. begin
  129. Assert(False, 'nl_langinfo was called with an unknown nl_item value');
  130. Result := '';
  131. end;
  132. end;
  133. end;
  134. {$endif}
  135. procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  136. var
  137. outlength,
  138. outoffset,
  139. srclen,
  140. outleft : size_t;
  141. srcpos : pwidechar;
  142. destpos: pchar;
  143. mynil : pchar;
  144. my0 : size_t;
  145. err: cint;
  146. begin
  147. mynil:=nil;
  148. my0:=0;
  149. { rought estimation }
  150. setlength(dest,len*3);
  151. outlength:=len*3;
  152. srclen:=len*2;
  153. srcpos:=source;
  154. destpos:=pchar(dest);
  155. outleft:=outlength;
  156. while iconv(iconv_wide2ansi,ppchar(@srcpos),@srclen,@destpos,@outleft)=size_t(-1) do
  157. begin
  158. err:=fpgetCerrno;
  159. case err of
  160. { last character is incomplete sequence }
  161. ESysEINVAL,
  162. { incomplete sequence in the middle }
  163. ESysEILSEQ:
  164. begin
  165. { skip and set to '?' }
  166. inc(srcpos);
  167. dec(srclen,2);
  168. destpos^:='?';
  169. inc(destpos);
  170. dec(outleft);
  171. { reset }
  172. iconv(iconv_wide2ansi,@mynil,@my0,@mynil,@my0);
  173. if err=ESysEINVAL then
  174. break;
  175. end;
  176. ESysE2BIG:
  177. begin
  178. outoffset:=destpos-pchar(dest);
  179. { extend }
  180. setlength(dest,outlength+len*3);
  181. inc(outleft,len*3);
  182. inc(outlength,len*3);
  183. { string could have been moved }
  184. destpos:=pchar(dest)+outoffset;
  185. end;
  186. else
  187. runerror(231);
  188. end;
  189. end;
  190. // truncate string
  191. setlength(dest,length(dest)-outleft);
  192. end;
  193. procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  194. var
  195. outlength,
  196. outoffset,
  197. outleft : size_t;
  198. srcpos,
  199. destpos: pchar;
  200. mynil : pchar;
  201. my0 : size_t;
  202. err: cint;
  203. begin
  204. mynil:=nil;
  205. my0:=0;
  206. // extra space
  207. outlength:=len+1;
  208. setlength(dest,outlength);
  209. srcpos:=source;
  210. destpos:=pchar(dest);
  211. outleft:=outlength*2;
  212. while iconv(iconv_ansi2wide,@srcpos,psize(@len),@destpos,@outleft)=size_t(-1) do
  213. begin
  214. err:=fpgetCerrno;
  215. case err of
  216. ESysEINVAL,
  217. ESysEILSEQ:
  218. begin
  219. { skip and set to '?' }
  220. inc(srcpos);
  221. dec(len);
  222. pwidechar(destpos)^:='?';
  223. inc(destpos,2);
  224. dec(outleft,2);
  225. { reset }
  226. iconv(iconv_ansi2wide,@mynil,@my0,@mynil,@my0);
  227. if err=ESysEINVAL then
  228. break;
  229. end;
  230. ESysE2BIG:
  231. begin
  232. outoffset:=destpos-pchar(dest);
  233. { extend }
  234. setlength(dest,outlength+len);
  235. inc(outleft,len*2);
  236. inc(outlength,len);
  237. { string could have been moved }
  238. destpos:=pchar(dest)+outoffset;
  239. end;
  240. else
  241. runerror(231);
  242. end;
  243. end;
  244. // truncate string
  245. setlength(dest,length(dest)-outleft div 2);
  246. end;
  247. function LowerWideString(const s : WideString) : WideString;
  248. var
  249. i : SizeInt;
  250. begin
  251. SetLength(result,length(s));
  252. for i:=0 to length(s)-1 do
  253. pwidechar(result)[i]:=WideChar(towlower(wint_t(s[i+1])));
  254. end;
  255. function UpperWideString(const s : WideString) : WideString;
  256. var
  257. i : SizeInt;
  258. begin
  259. SetLength(result,length(s));
  260. for i:=0 to length(s)-1 do
  261. pwidechar(result)[i]:=WideChar(towupper(wint_t(s[i+1])));
  262. end;
  263. procedure EnsureAnsiLen(var S: AnsiString; const len: SizeInt); inline;
  264. begin
  265. if (len>length(s)) then
  266. if (length(s) < 10*256) then
  267. setlength(s,length(s)+10)
  268. else
  269. setlength(s,length(s)+length(s) shr 8);
  270. end;
  271. procedure ConcatCharToAnsiStr(const c: char; var S: AnsiString; var index: SizeInt);
  272. begin
  273. EnsureAnsiLen(s,index);
  274. pchar(@s[index])^:=c;
  275. inc(index);
  276. end;
  277. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  278. {$ifndef beos}
  279. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt; var mbstate: mbstate_t);
  280. {$else not beos}
  281. procedure ConcatUTF32ToAnsiStr(const nc: wint_t; var S: AnsiString; var index: SizeInt);
  282. {$endif beos}
  283. var
  284. p : pchar;
  285. mblen : size_t;
  286. begin
  287. { we know that s is unique -> avoid uniquestring calls}
  288. p:=@s[index];
  289. if (nc<=127) then
  290. ConcatCharToAnsiStr(char(nc),s,index)
  291. else
  292. begin
  293. EnsureAnsiLen(s,index+MB_CUR_MAX);
  294. {$ifndef beos}
  295. mblen:=wcrtomb(p,wchar_t(nc),@mbstate);
  296. {$else not beos}
  297. mblen:=wctomb(p,wchar_t(nc));
  298. {$endif not beos}
  299. if (mblen<>size_t(-1)) then
  300. inc(index,mblen)
  301. else
  302. begin
  303. { invalid wide char }
  304. p^:='?';
  305. inc(index);
  306. end;
  307. end;
  308. end;
  309. function LowerAnsiString(const s : AnsiString) : AnsiString;
  310. var
  311. i, slen,
  312. resindex : SizeInt;
  313. mblen : size_t;
  314. {$ifndef beos}
  315. ombstate,
  316. nmbstate : mbstate_t;
  317. {$endif beos}
  318. wc : wchar_t;
  319. begin
  320. {$ifndef beos}
  321. fillchar(ombstate,sizeof(ombstate),0);
  322. fillchar(nmbstate,sizeof(nmbstate),0);
  323. {$endif beos}
  324. slen:=length(s);
  325. SetLength(result,slen+10);
  326. i:=1;
  327. resindex:=1;
  328. while (i<=slen) do
  329. begin
  330. if (s[i]<=#127) then
  331. begin
  332. wc:=wchar_t(s[i]);
  333. mblen:= 1;
  334. end
  335. else
  336. {$ifndef beos}
  337. mblen:=mbrtowc(@wc, pchar(@s[i]), slen-i+1, @ombstate);
  338. {$else not beos}
  339. mblen:=mbtowc(@wc, pchar(@s[i]), slen-i+1);
  340. {$endif not beos}
  341. case mblen of
  342. size_t(-2):
  343. begin
  344. { partial invalid character, copy literally }
  345. while (i<=slen) do
  346. begin
  347. ConcatCharToAnsiStr(s[i],result,resindex);
  348. inc(i);
  349. end;
  350. end;
  351. size_t(-1), 0:
  352. begin
  353. { invalid or null character }
  354. ConcatCharToAnsiStr(s[i],result,resindex);
  355. inc(i);
  356. end;
  357. else
  358. begin
  359. { a valid sequence }
  360. { even if mblen = 1, the lowercase version may have a }
  361. { different length }
  362. { We can't do anything special if wchar_t is 16 bit... }
  363. {$ifndef beos}
  364. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex,nmbstate);
  365. {$else not beos}
  366. ConcatUTF32ToAnsiStr(towlower(wint_t(wc)),result,resindex);
  367. {$endif not beos}
  368. inc(i,mblen);
  369. end;
  370. end;
  371. end;
  372. SetLength(result,resindex-1);
  373. end;
  374. function UpperAnsiString(const s : AnsiString) : AnsiString;
  375. var
  376. i, slen,
  377. resindex : SizeInt;
  378. mblen : size_t;
  379. {$ifndef beos}
  380. ombstate,
  381. nmbstate : mbstate_t;
  382. {$endif beos}
  383. wc : wchar_t;
  384. begin
  385. {$ifndef beos}
  386. fillchar(ombstate,sizeof(ombstate),0);
  387. fillchar(nmbstate,sizeof(nmbstate),0);
  388. {$endif beos}
  389. slen:=length(s);
  390. SetLength(result,slen+10);
  391. i:=1;
  392. resindex:=1;
  393. while (i<=slen) do
  394. begin
  395. if (s[i]<=#127) then
  396. begin
  397. wc:=wchar_t(s[i]);
  398. mblen:= 1;
  399. end
  400. else
  401. {$ifndef beos}
  402. mblen:=mbrtowc(@wc, pchar(@s[i]), slen-i+1, @ombstate);
  403. {$else not beos}
  404. mblen:=mbtowc(@wc, pchar(@s[i]), slen-i+1);
  405. {$endif beos}
  406. case mblen of
  407. size_t(-2):
  408. begin
  409. { partial invalid character, copy literally }
  410. while (i<=slen) do
  411. begin
  412. ConcatCharToAnsiStr(s[i],result,resindex);
  413. inc(i);
  414. end;
  415. end;
  416. size_t(-1), 0:
  417. begin
  418. { invalid or null character }
  419. ConcatCharToAnsiStr(s[i],result,resindex);
  420. inc(i);
  421. end;
  422. else
  423. begin
  424. { a valid sequence }
  425. { even if mblen = 1, the uppercase version may have a }
  426. { different length }
  427. { We can't do anything special if wchar_t is 16 bit... }
  428. {$ifndef beos}
  429. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex,nmbstate);
  430. {$else not beos}
  431. ConcatUTF32ToAnsiStr(towupper(wint_t(wc)),result,resindex);
  432. {$endif not beos}
  433. inc(i,mblen);
  434. end;
  435. end;
  436. end;
  437. SetLength(result,resindex-1);
  438. end;
  439. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; external name 'FPC_UTF16TOUTF32';
  440. function WideStringToUCS4StringNoNulls(const s : WideString) : UCS4String;
  441. var
  442. i, slen,
  443. destindex : SizeInt;
  444. len : longint;
  445. uch : UCS4Char;
  446. begin
  447. slen:=length(s);
  448. setlength(result,slen+1);
  449. i:=1;
  450. destindex:=0;
  451. while (i<=slen) do
  452. begin
  453. uch:=utf16toutf32(s,i,len);
  454. if (uch=UCS4Char(0)) then
  455. uch:=UCS4Char(32);
  456. result[destindex]:=uch;
  457. inc(destindex);
  458. inc(i,len);
  459. end;
  460. result[destindex]:=UCS4Char(0);
  461. { destindex <= slen }
  462. setlength(result,destindex);
  463. end;
  464. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  465. var
  466. hs1,hs2 : UCS4String;
  467. begin
  468. { wcscoll interprets null chars as end-of-string -> filter out }
  469. hs1:=WideStringToUCS4StringNoNulls(s1);
  470. hs2:=WideStringToUCS4StringNoNulls(s2);
  471. result:=wcscoll(pwchar_t(hs1),pwchar_t(hs2));
  472. end;
  473. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  474. begin
  475. result:=CompareWideString(UpperWideString(s1),UpperWideString(s2));
  476. end;
  477. function CharLengthPChar(const Str: PChar): PtrInt;
  478. var
  479. nextlen: ptrint;
  480. s: pchar;
  481. {$ifndef beos}
  482. mbstate: mbstate_t;
  483. {$endif not beos}
  484. begin
  485. result:=0;
  486. s:=str;
  487. repeat
  488. {$ifdef beos}
  489. nextlen:=ptrint(mblen(str,MB_CUR_MAX));
  490. {$else beos}
  491. nextlen:=ptrint(mbrlen(str,MB_CUR_MAX,@mbstate));
  492. {$endif beos}
  493. { skip invalid/incomplete sequences }
  494. if (nextlen<0) then
  495. nextlen:=1;
  496. inc(result,nextlen);
  497. inc(s,nextlen);
  498. until (nextlen=0);
  499. end;
  500. function StrCompAnsiIntern(const s1,s2 : PChar; len1, len2: PtrInt): PtrInt;
  501. var
  502. a,b: pchar;
  503. i: PtrInt;
  504. begin
  505. getmem(a,len1+1);
  506. getmem(b,len2+1);
  507. for i:=0 to len1-1 do
  508. if s1[i]<>#0 then
  509. a[i]:=s1[i]
  510. else
  511. a[i]:=#32;
  512. a[len1]:=#0;
  513. for i:=0 to len2-1 do
  514. if s2[i]<>#0 then
  515. b[i]:=s2[i]
  516. else
  517. b[i]:=#32;
  518. b[len2]:=#0;
  519. result:=strcoll(a,b);
  520. freemem(a);
  521. freemem(b);
  522. end;
  523. function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
  524. begin
  525. result:=StrCompAnsiIntern(pchar(s1),pchar(s2),length(s1),length(s2));
  526. end;
  527. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  528. begin
  529. result:=strcoll(s1,s2);
  530. end;
  531. function AnsiCompareText(const S1, S2: ansistring): PtrInt;
  532. var
  533. a, b: AnsiString;
  534. begin
  535. a:=UpperAnsistring(s1);
  536. b:=UpperAnsistring(s2);
  537. result:=StrCompAnsiIntern(pchar(a),pchar(b),length(a),length(b));
  538. end;
  539. function AnsiStrIComp(S1, S2: PChar): PtrInt;
  540. begin
  541. result:=AnsiCompareText(ansistring(s1),ansistring(s2));
  542. end;
  543. function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  544. var
  545. a, b: pchar;
  546. begin
  547. if (IndexChar(s1^,maxlen,#0)<0) then
  548. begin
  549. getmem(a,maxlen+1);
  550. move(s1^,a^,maxlen);
  551. a[maxlen]:=#0;
  552. end
  553. else
  554. a:=s1;
  555. if (IndexChar(s2^,maxlen,#0)<0) then
  556. begin
  557. getmem(b,maxlen+1);
  558. move(s2^,b^,maxlen);
  559. b[maxlen]:=#0;
  560. end
  561. else
  562. b:=s2;
  563. result:=strcoll(a,b);
  564. if (a<>s1) then
  565. freemem(a);
  566. if (b<>s2) then
  567. freemem(b);
  568. end;
  569. function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  570. var
  571. a, b: ansistring;
  572. len1,len2: SizeInt;
  573. begin
  574. len1:=IndexChar(s1^,maxlen,#0);
  575. if (len1<0) then
  576. len1:=maxlen;
  577. setlength(a,len1);
  578. if (len1<>0) then
  579. move(s1^,a[1],len1);
  580. len2:=IndexChar(s2^,maxlen,#0);
  581. if (len2<0) then
  582. len2:=maxlen;
  583. setlength(b,len2);
  584. if (len2<>0) then
  585. move(s2^,b[1],len2);
  586. result:=AnsiCompareText(a,b);
  587. end;
  588. procedure ansi2pchar(const s: ansistring; const orgp: pchar; out p: pchar);
  589. var
  590. newlen: sizeint;
  591. begin
  592. newlen:=length(s);
  593. if newlen>strlen(orgp) then
  594. fpc_rangeerror;
  595. p:=orgp;
  596. if (newlen>0) then
  597. move(s[1],p[0],newlen);
  598. p[newlen]:=#0;
  599. end;
  600. function AnsiStrLower(Str: PChar): PChar;
  601. var
  602. temp: ansistring;
  603. begin
  604. temp:=loweransistring(str);
  605. ansi2pchar(temp,str,result);
  606. end;
  607. function AnsiStrUpper(Str: PChar): PChar;
  608. var
  609. temp: ansistring;
  610. begin
  611. temp:=upperansistring(str);
  612. ansi2pchar(temp,str,result);
  613. end;
  614. procedure InitThread;
  615. begin
  616. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  617. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  618. end;
  619. procedure FiniThread;
  620. begin
  621. if (iconv_wide2ansi <> iconv_t(-1)) then
  622. iconv_close(iconv_wide2ansi);
  623. if (iconv_ansi2wide <> iconv_t(-1)) then
  624. iconv_close(iconv_ansi2wide);
  625. end;
  626. Procedure SetCWideStringManager;
  627. Var
  628. CWideStringManager : TWideStringManager;
  629. begin
  630. CWideStringManager:=widestringmanager;
  631. With CWideStringManager do
  632. begin
  633. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  634. Ansi2WideMoveProc:=@Ansi2WideMove;
  635. UpperWideStringProc:=@UpperWideString;
  636. LowerWideStringProc:=@LowerWideString;
  637. CompareWideStringProc:=@CompareWideString;
  638. CompareTextWideStringProc:=@CompareTextWideString;
  639. CharLengthPCharProc:=@CharLengthPChar;
  640. UpperAnsiStringProc:=@UpperAnsiString;
  641. LowerAnsiStringProc:=@LowerAnsiString;
  642. CompareStrAnsiStringProc:=@CompareStrAnsiString;
  643. CompareTextAnsiStringProc:=@AnsiCompareText;
  644. StrCompAnsiStringProc:=@StrCompAnsi;
  645. StrICompAnsiStringProc:=@AnsiStrIComp;
  646. StrLCompAnsiStringProc:=@AnsiStrLComp;
  647. StrLICompAnsiStringProc:=@AnsiStrLIComp;
  648. StrLowerAnsiStringProc:=@AnsiStrLower;
  649. StrUpperAnsiStringProc:=@AnsiStrUpper;
  650. ThreadInitProc:=@InitThread;
  651. ThreadFiniProc:=@FiniThread;
  652. end;
  653. SetWideStringManager(CWideStringManager);
  654. end;
  655. initialization
  656. SetCWideStringManager;
  657. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  658. { with the information from the environment variables according to POSIX }
  659. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  660. setlocale(LC_ALL,'');
  661. { init conversion tables for main program }
  662. InitThread;
  663. finalization
  664. { fini conversion tables for main program }
  665. FiniThread;
  666. end.