cwstring.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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) and not defined(haiku)} // 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) or defined(haiku)}
  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+1);
  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(s1,s2 : PChar; len1, len2: PtrInt; canmodifys1, canmodifys2: boolean): PtrInt;
  501. var
  502. a,b: pchar;
  503. i: PtrInt;
  504. begin
  505. if not(canmodifys1) then
  506. getmem(a,len1+1)
  507. else
  508. a:=s1;
  509. for i:=0 to len1-1 do
  510. if s1[i]<>#0 then
  511. a[i]:=s1[i]
  512. else
  513. a[i]:=#32;
  514. a[len1]:=#0;
  515. if not(canmodifys2) then
  516. getmem(b,len2+1)
  517. else
  518. b:=s2;
  519. for i:=0 to len2-1 do
  520. if s2[i]<>#0 then
  521. b[i]:=s2[i]
  522. else
  523. b[i]:=#32;
  524. b[len2]:=#0;
  525. result:=strcoll(a,b);
  526. if not(canmodifys1) then
  527. freemem(a);
  528. if not(canmodifys2) then
  529. freemem(b);
  530. end;
  531. function CompareStrAnsiString(const s1, s2: ansistring): PtrInt;
  532. begin
  533. result:=StrCompAnsiIntern(pchar(s1),pchar(s2),length(s1),length(s2),false,false);
  534. end;
  535. function StrCompAnsi(s1,s2 : PChar): PtrInt;
  536. begin
  537. result:=strcoll(s1,s2);
  538. end;
  539. function AnsiCompareText(const S1, S2: ansistring): PtrInt;
  540. var
  541. a, b: AnsiString;
  542. begin
  543. a:=UpperAnsistring(s1);
  544. b:=UpperAnsistring(s2);
  545. result:=StrCompAnsiIntern(pchar(a),pchar(b),length(a),length(b),true,true);
  546. end;
  547. function AnsiStrIComp(S1, S2: PChar): PtrInt;
  548. begin
  549. result:=AnsiCompareText(ansistring(s1),ansistring(s2));
  550. end;
  551. function AnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  552. var
  553. a, b: pchar;
  554. begin
  555. if (maxlen=0) then
  556. exit(0);
  557. if (s1[maxlen]<>#0) then
  558. begin
  559. getmem(a,maxlen+1);
  560. move(s1^,a^,maxlen);
  561. a[maxlen]:=#0;
  562. end
  563. else
  564. a:=s1;
  565. if (s2[maxlen]<>#0) then
  566. begin
  567. getmem(b,maxlen+1);
  568. move(s2^,b^,maxlen);
  569. b[maxlen]:=#0;
  570. end
  571. else
  572. b:=s2;
  573. result:=StrCompAnsiIntern(a,b,maxlen,maxlen,a<>s1,b<>s2);
  574. if (a<>s1) then
  575. freemem(a);
  576. if (b<>s2) then
  577. freemem(b);
  578. end;
  579. function AnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  580. var
  581. a, b: ansistring;
  582. begin
  583. if (maxlen=0) then
  584. exit(0);
  585. setlength(a,maxlen);
  586. move(s1^,a[1],maxlen);
  587. setlength(b,maxlen);
  588. move(s2^,b[1],maxlen);
  589. result:=AnsiCompareText(a,b);
  590. end;
  591. procedure ansi2pchar(const s: ansistring; const orgp: pchar; out p: pchar);
  592. var
  593. newlen: sizeint;
  594. begin
  595. newlen:=length(s);
  596. if newlen>strlen(orgp) then
  597. fpc_rangeerror;
  598. p:=orgp;
  599. if (newlen>0) then
  600. move(s[1],p[0],newlen);
  601. p[newlen]:=#0;
  602. end;
  603. function AnsiStrLower(Str: PChar): PChar;
  604. var
  605. temp: ansistring;
  606. begin
  607. temp:=loweransistring(str);
  608. ansi2pchar(temp,str,result);
  609. end;
  610. function AnsiStrUpper(Str: PChar): PChar;
  611. var
  612. temp: ansistring;
  613. begin
  614. temp:=upperansistring(str);
  615. ansi2pchar(temp,str,result);
  616. end;
  617. procedure InitThread;
  618. begin
  619. iconv_wide2ansi:=iconv_open(nl_langinfo(CODESET),unicode_encoding2);
  620. iconv_ansi2wide:=iconv_open(unicode_encoding2,nl_langinfo(CODESET));
  621. end;
  622. procedure FiniThread;
  623. begin
  624. if (iconv_wide2ansi <> iconv_t(-1)) then
  625. iconv_close(iconv_wide2ansi);
  626. if (iconv_ansi2wide <> iconv_t(-1)) then
  627. iconv_close(iconv_ansi2wide);
  628. end;
  629. Procedure SetCWideStringManager;
  630. Var
  631. CWideStringManager : TUnicodeStringManager;
  632. begin
  633. CWideStringManager:=widestringmanager;
  634. With CWideStringManager do
  635. begin
  636. Wide2AnsiMoveProc:=@Wide2AnsiMove;
  637. Ansi2WideMoveProc:=@Ansi2WideMove;
  638. UpperWideStringProc:=@UpperWideString;
  639. LowerWideStringProc:=@LowerWideString;
  640. CompareWideStringProc:=@CompareWideString;
  641. CompareTextWideStringProc:=@CompareTextWideString;
  642. CharLengthPCharProc:=@CharLengthPChar;
  643. UpperAnsiStringProc:=@UpperAnsiString;
  644. LowerAnsiStringProc:=@LowerAnsiString;
  645. CompareStrAnsiStringProc:=@CompareStrAnsiString;
  646. CompareTextAnsiStringProc:=@AnsiCompareText;
  647. StrCompAnsiStringProc:=@StrCompAnsi;
  648. StrICompAnsiStringProc:=@AnsiStrIComp;
  649. StrLCompAnsiStringProc:=@AnsiStrLComp;
  650. StrLICompAnsiStringProc:=@AnsiStrLIComp;
  651. StrLowerAnsiStringProc:=@AnsiStrLower;
  652. StrUpperAnsiStringProc:=@AnsiStrUpper;
  653. ThreadInitProc:=@InitThread;
  654. ThreadFiniProc:=@FiniThread;
  655. {$ifndef VER2_2}
  656. { Unicode }
  657. Unicode2AnsiMoveProc:=@Wide2AnsiMove;
  658. Ansi2UnicodeMoveProc:=@Ansi2WideMove;
  659. UpperUnicodeStringProc:=@UpperWideString;
  660. LowerUnicodeStringProc:=@LowerWideString;
  661. {$endif VER2_2}
  662. end;
  663. SetUnicodeStringManager(CWideStringManager);
  664. end;
  665. initialization
  666. SetCWideStringManager;
  667. { you have to call setlocale(LC_ALL,'') to initialise the langinfo stuff }
  668. { with the information from the environment variables according to POSIX }
  669. { (some OSes do this automatically, but e.g. Darwin and Solaris don't) }
  670. setlocale(LC_ALL,'');
  671. { init conversion tables for main program }
  672. InitThread;
  673. finalization
  674. { fini conversion tables for main program }
  675. FiniThread;
  676. end.