sysstr.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. { NewStr creates a new PString and assigns S to it
  20. if length(s) = 0 NewStr returns Nil }
  21. function NewStr(const S: string): PString;
  22. begin
  23. result := Nil;
  24. {
  25. if Length(S) <> 0 then begin
  26. result := New(PString);
  27. result^ := S;
  28. end ;
  29. }
  30. end ;
  31. { DisposeStr frees the memory occupied by S }
  32. procedure DisposeStr(S: PString);
  33. begin
  34. {
  35. if S <> Nil then begin
  36. Dispose(S);
  37. S := Nil;
  38. end ;
  39. }
  40. end ;
  41. { AssignStr assigns S to P^ }
  42. procedure AssignStr(var P: PString; const S: string);
  43. begin
  44. P^ := s;
  45. end ;
  46. { AppendStr appends S to Dest }
  47. procedure AppendStr(var Dest: PString; const S: string);
  48. begin
  49. Dest^ := Dest^ + S;
  50. end ;
  51. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  52. have been converted to uppercase }
  53. function UpperCase(const S: string): string;
  54. var i: integer;
  55. begin
  56. result := S;
  57. i := Length(S);
  58. while i <> 0 do begin
  59. if (result[i] in ['a'..'z']) then result[i] := char(byte(result[i]) - 32);
  60. Dec(i);
  61. end;
  62. end;
  63. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  64. have been converted to lowercase }
  65. function LowerCase(const S: string): string;
  66. var i: integer;
  67. begin
  68. result := S;
  69. i := Length(result);
  70. while i <> 0 do begin
  71. if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
  72. dec(i);
  73. end;
  74. end;
  75. { CompareStr compares S1 and S2, the result is the based on
  76. substraction of the ascii values of the characters in S1 and S2
  77. case result
  78. S1 < S2 < 0
  79. S1 > S2 > 0
  80. S1 = S2 = 0 }
  81. function CompareStr(const S1, S2: string): Integer;
  82. var i, count, count1, count2: integer;
  83. begin
  84. result := 0;
  85. Count1 := Length(S1);
  86. Count2 := Length(S2);
  87. if Count1 > Count2 then Count := Count2
  88. else Count := Count1;
  89. result := CompareMem(Pointer(S1),Pointer(S2), Count);
  90. if (result = 0) and (Count1 <> Count2) then begin
  91. if Count1 > Count2 then result := ord(s1[Count1 + 1])
  92. else result := -ord(s2[Count2 + 1]);
  93. end ;
  94. end ;
  95. { CompareMem returns the result of comparison of Length bytes at P1 and P2
  96. case result
  97. P1 < P2 < 0
  98. P1 > P2 > 0
  99. P1 = P2 = 0 }
  100. function CompareMem(P1, P2: Pointer; Length: cardinal): integer;
  101. var i: integer;
  102. begin
  103. i := 0;
  104. result := 0;
  105. while (result = 0) and (i < length) do begin
  106. result := byte(P1^) - byte(P2^);
  107. P1 := P1 + 1;
  108. P2 := P2 + 1;
  109. i := i + 1;
  110. end ;
  111. end ;
  112. { CompareText compares S1 and S2, the result is the based on
  113. substraction of the ascii values of characters in S1 and S2
  114. comparison is case-insensitive
  115. case result
  116. S1 < S2 < 0
  117. S1 > S2 > 0
  118. S1 = S2 = 0 }
  119. function CompareText(const S1, S2: string): integer;
  120. var i, count, count1, count2: integer; Chr1, Chr2: byte;
  121. begin
  122. result := 0;
  123. Count1 := Length(S1);
  124. Count2 := Length(S2);
  125. if Count1 > Count2 then Count := Count2
  126. else Count := Count1;
  127. i := 0;
  128. while (result = 0) and (i < count) do begin
  129. i := i + 1;
  130. Chr1 := byte(s1[i]);
  131. Chr2 := byte(s2[i]);
  132. if Chr1 in [97..122] then Chr1 := Chr1 - 32;
  133. if Chr2 in [97..122] then Chr2 := Chr2 - 32;
  134. result := Chr1 - Chr2;
  135. end ;
  136. if (result = 0) and (Count1 <> Count2) then begin
  137. if Count1 > Count2 then result := byte(UpCase(s1[Count1 + 1]))
  138. else result := -byte(UpCase(s2[Count2 + 1]));
  139. end ;
  140. end ;
  141. {==============================================================================}
  142. { Ansi string functions }
  143. { these functions rely on the character set loaded by the OS }
  144. {==============================================================================}
  145. type
  146. TCaseTranslationTable = array[0..255] of char;
  147. var
  148. UpperCaseTable: TCaseTranslationTable;
  149. LowerCaseTable: TCaseTranslationTable;
  150. function AnsiUpperCase(const s: string): string;
  151. var len, i: integer;
  152. begin
  153. len := length(s);
  154. SetLength(result, len);
  155. for i := 1 to len do
  156. result[i] := UpperCaseTable[ord(s[i])];
  157. end ;
  158. function AnsiLowerCase(const s: string): string;
  159. var len, i: integer;
  160. begin
  161. len := length(s);
  162. SetLength(result, len);
  163. for i := 1 to len do
  164. result[i] := LowerCaseTable[ord(s[i])];
  165. end ;
  166. function AnsiCompareStr(const S1, S2: string): integer;
  167. begin
  168. end ;
  169. function AnsiCompareText(const S1, S2: string): integer;
  170. begin
  171. end ;
  172. function AnsiStrComp(S1, S2: PChar): integer;
  173. begin
  174. end ;
  175. function AnsiStrIComp(S1, S2: PChar): integer;
  176. begin
  177. end ;
  178. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;
  179. begin
  180. end ;
  181. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;
  182. begin
  183. end ;
  184. function AnsiStrLower(Str: PChar): PChar;
  185. begin
  186. if Str <> Nil then begin
  187. while Str^ <> #0 do begin
  188. Str^ := LowerCaseTable[byte(Str^)];
  189. Str := Str + 1;
  190. end ;
  191. end ;
  192. result := Str;
  193. end ;
  194. function AnsiStrUpper(Str: PChar): PChar;
  195. begin
  196. if Str <> Nil then begin
  197. while Str^ <> #0 do begin
  198. Str^ := UpperCaseTable[byte(Str^)];
  199. Str := Str + 1;
  200. end ;
  201. end ;
  202. result := Str;
  203. end ;
  204. function AnsiLastChar(const S: string): PChar;
  205. begin
  206. end ;
  207. function AnsiStrLastChar(Str: PChar): PChar;
  208. begin
  209. end ;
  210. {==============================================================================}
  211. { End of Ansi functions }
  212. {==============================================================================}
  213. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  214. function Trim(const S: string): string;
  215. var Ofs, Len: integer;
  216. begin
  217. len := Length(S);
  218. while (S[Len] = ' ') and (Len > 0) do
  219. dec(Len);
  220. Ofs := 1;
  221. while (S[Ofs] = ' ') and (Ofs <= Len) do
  222. Inc(Ofs);
  223. result := Copy(S, Ofs, 1 + Len - Ofs);
  224. end ;
  225. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  226. function TrimLeft(const S: string): string;
  227. var i,l:integer;
  228. begin
  229. l := length(s);
  230. i := 1;
  231. while (s[i] = ' ') and (i <= l) do inc(i);
  232. Result := copy(s, i, l);
  233. end ;
  234. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  235. function TrimRight(const S: string): string;
  236. var l:integer;
  237. begin
  238. l := length(s);
  239. while (s[l] = ' ') and (l > 0) do dec(l);
  240. result := copy(s,1,l);
  241. end ;
  242. { QuotedStr returns S quoted left and right and every single quote in S
  243. replaced by two quotes }
  244. function QuotedStr(const S: string): string;
  245. begin
  246. result := AnsiQuotedStr(s, '''');
  247. end ;
  248. { AnsiQuotedStr returns S quoted left and right by Quote,
  249. and every single occurance of Quote replaced by two }
  250. function AnsiQuotedStr(const S: string; Quote: char): string;
  251. var i, j, count: integer;
  252. begin
  253. result := '' + Quote;
  254. count := length(s);
  255. i := 0;
  256. j := 0;
  257. while i < count do begin
  258. i := i + 1;
  259. if S[i] = Quote then begin
  260. result := result + copy(S, 1 + j, i - j) + Quote;
  261. j := i;
  262. end ;
  263. end ;
  264. if i <> j then
  265. result := result + copy(S, 1 + j, i - j);
  266. result := result + Quote;
  267. end ;
  268. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  269. deleted to the left and right and double occurances
  270. of Quote replaced by a single Quote }
  271. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  272. var i: integer; P, Q: PChar;
  273. begin
  274. P := Src;
  275. if Src^ = Quote then P := P + 1;
  276. Q := StrEnd(P);
  277. if PChar(Q - 1)^ = Quote then Q := Q - 1;
  278. SetLength(result, Q - P);
  279. i := 0;
  280. while P <> Q do begin
  281. i := i + 1;
  282. result[i] := P^;
  283. if (P^ = Quote) and (PChar(P + 1)^ = Quote) then
  284. P := P + 1;
  285. P := P + 1;
  286. end ;
  287. SetLength(result, i);
  288. end ;
  289. { AdjustLineBreaks returns S with all CR characters not followed by LF
  290. replaced with CR/LF }
  291. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  292. function AdjustLineBreaks(const S: string): string;
  293. var i, j, count: integer;
  294. begin
  295. result := '';
  296. i := 0;
  297. j := 0;
  298. count := Length(S);
  299. while i < count do begin
  300. i := i + 1;
  301. if (S[i] = #13) and ((i = count) or (S[i + 1] <> #10)) then begin
  302. result := result + Copy(S, 1 + j, i - j) + #10;
  303. j := i;
  304. end ;
  305. end ;
  306. if j <> i then
  307. result := result + copy(S, 1 + j, i - j);
  308. end ;
  309. { IsValidIdent returns true if the first character of Ident is in:
  310. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  311. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  312. function IsValidIdent(const Ident: string): boolean;
  313. var i, len: integer;
  314. begin
  315. result := false;
  316. len := length(Ident);
  317. if len <> 0 then begin
  318. result := Ident[1] in ['A'..'Z', 'a'..'z', '_'];
  319. i := 1;
  320. while (result) and (i < len) do begin
  321. i := i + 1;
  322. result := result and (Ident[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
  323. end ;
  324. end ;
  325. end ;
  326. { IntToStr returns a string representing the value of Value }
  327. function IntToStr(Value: integer): string;
  328. begin
  329. System.Str(Value, result);
  330. end ;
  331. { IntToHex returns a string representing the hexadecimal value of Value }
  332. const
  333. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  334. function IntToHex(Value: integer; Digits: integer): string;
  335. var i: integer;
  336. begin
  337. SetLength(result, digits);
  338. for i := 0 to digits - 1 do begin
  339. result[digits - i] := HexDigits[value and 15];
  340. value := value shr 4;
  341. end ;
  342. end ;
  343. { StrToInt converts the string S to an integer value,
  344. if S does not represent a valid integer value EConvertError is raised }
  345. function StrToInt(const S: string): integer;
  346. var Error: word;
  347. begin
  348. Val(S, result, Error);
  349. if Error <> 0 then raise EConvertError.createfmt(SInValidInteger,[S]);
  350. end ;
  351. { StrToIntDef converts the string S to an integer value,
  352. Default is returned in case S does not represent a valid integer value }
  353. function StrToIntDef(const S: string; Default: integer): integer;
  354. var Error: word;
  355. begin
  356. Val(S, result, Error);
  357. if Error <> 0 then result := Default;
  358. end ;
  359. { LoadStr returns the string resource Ident. }
  360. function LoadStr(Ident: integer): string;
  361. begin
  362. end ;
  363. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  364. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  365. begin
  366. end;
  367. Const
  368. feInvalidFormat = 1;
  369. feMissingArgument = 2;
  370. feInvalidArgIndex = 3;
  371. Procedure Log (Const S: String);
  372. begin
  373. {$ifdef debug}
  374. Writeln (S);
  375. {$endif}
  376. end;
  377. Procedure DoFormatError (ErrCode : Longint);
  378. Var S : String;
  379. begin
  380. //!! must be changed to contain format string...
  381. S:='';
  382. Case ErrCode of
  383. feInvalidFormat : EConvertError.Createfmt(SInvalidFormat,[s]);
  384. feMissingArgument : EConvertError.Createfmt(SArgumentMissing,[s]);
  385. feInvalidArgIndex : EConvertError.Createfmt(SInvalidArgIndex,[s]);
  386. end;
  387. end;
  388. Function Format (Const Fmt : String; const Args : Array of const) : String;
  389. Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
  390. ToAdd : String;
  391. Index,Width,Prec : Longint;
  392. Left : Boolean;
  393. ExtVal: Extended;
  394. Fchar : char;
  395. {
  396. ReadFormat reads the format string. It returns the type character in
  397. uppercase, and sets index, Width, Prec to their correct values,
  398. or -1 if not set. It sets Left to true if left alignment was requested.
  399. In case of an error, DoFormatError is called.
  400. }
  401. Function ReadFormat : Char;
  402. Var Value : longint;
  403. Procedure ReadInteger;
  404. Var Code : Word;
  405. begin
  406. If Value<>-1 then exit; // Was already read.
  407. OldPos:=chPos;
  408. While (Chpos<Len) and
  409. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  410. If Chpos=len then DoFormatError(feInvalidFormat);
  411. If Fmt[Chpos]='*' then
  412. begin
  413. If (Chpos>OldPos) or (ArgPos>High(Args))
  414. or (Args[ArgPos].Vtype<>vtInteger) then
  415. DoFormatError(feInvalidFormat);
  416. Value:=Args[ArgPos].VInteger;
  417. Inc(ArgPos);
  418. Inc(chPos);
  419. end
  420. else
  421. begin
  422. If (OldPos<chPos) Then
  423. begin
  424. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  425. // This should never happen !!
  426. If Code>0 then DoFormatError (feInvalidFormat);
  427. end
  428. else
  429. Value:=-1;
  430. end;
  431. end;
  432. Procedure ReadIndex;
  433. begin
  434. ReadInteger;
  435. If Fmt[ChPos]=':' then
  436. begin
  437. If Value=-1 then DoFormatError(feMissingArgument);
  438. Index:=Value;
  439. Value:=-1;
  440. Inc(Chpos);
  441. end;
  442. Log ('Read index');
  443. end;
  444. Procedure ReadLeft;
  445. begin
  446. If Fmt[chpos]='-' then
  447. begin
  448. left:=True;
  449. Inc(chpos);
  450. end
  451. else
  452. Left:=False;
  453. Log ('Read Left');
  454. end;
  455. Procedure ReadWidth;
  456. begin
  457. ReadInteger;
  458. If Value<>-1 then
  459. begin
  460. Width:=Value;
  461. Value:=-1;
  462. end;
  463. Log ('Read width');
  464. end;
  465. Procedure ReadPrec;
  466. begin
  467. If Fmt[chpos]='.' then
  468. begin
  469. inc(chpos);
  470. ReadInteger;
  471. If Value=-1 then DoFormaterror(feMissingArgument);
  472. prec:=Value;
  473. end;
  474. Log ('Read precision');
  475. end;
  476. begin
  477. Log ('Start format');
  478. Index:=-1;
  479. Width:=-1;
  480. Prec:=-1;
  481. Value:=-1;
  482. inc(chpos);
  483. If Fmt[Chpos]='%' then exit('%');
  484. ReadIndex;
  485. ReadLeft;
  486. ReadWidth;
  487. ReadPrec;
  488. ReadFormat:=Upcase(Fmt[ChPos]);
  489. Log ('End format');
  490. end;
  491. Procedure DumpFormat (C : char);
  492. begin
  493. Write ('Fmt : ',fmt:10);
  494. Write (' Index : ',Index:3);
  495. Write (' Left : ',left:5);
  496. Write (' Width : ',Width:3);
  497. Write (' Prec : ',prec:3);
  498. Writeln (' Type : ',C);
  499. end;
  500. Procedure Checkarg (AT : Longint);
  501. {
  502. Check if argument INDEX is of correct type (AT)
  503. If Index=-1, ArgPos is used, and argpos is augmented with 1
  504. DoArg is set to the argument that must be used.
  505. }
  506. begin
  507. If Index=-1 then
  508. begin
  509. DoArg:=Argpos;
  510. inc(ArgPos);
  511. end
  512. else
  513. DoArg:=Index;
  514. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  515. DoFormatError(feInvalidArgindex);
  516. end;
  517. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  518. begin
  519. Result:='';
  520. Len:=Length(Fmt)+1;
  521. Chpos:=1;
  522. OldPos:=1;
  523. ArgPos:=0;
  524. While chpos<len do
  525. begin
  526. // uses shortcut evaluation !!
  527. While (ChPos<=Len) and (Fmt[chpos]<>'%') do inc(chpos);
  528. If ChPos>OldPos Then
  529. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  530. If ChPos<Len then
  531. begin
  532. FChar:=ReadFormat;
  533. {$ifdef debug}
  534. DumpFormat(FCHar);
  535. {$endif}
  536. Case FChar of
  537. 'D' : begin
  538. Checkarg(vtinteger);
  539. Width:=Abs(width);
  540. Str(Args[Doarg].VInteger,ToAdd);
  541. While Length(ToAdd)<Prec do
  542. begin
  543. Index:=Prec-Length(ToAdd);
  544. If Index>64 then Index:=64;
  545. ToAdd:=Copy(Zero,1,Index)+ToAdd;
  546. end;
  547. end;
  548. 'E' : begin
  549. CheckArg(vtExtended);
  550. If Prec=-1 then prec:=15;
  551. ExtVal:=Args[doarg].VExtended^;
  552. Prec:=Prec+5; // correct dot, eXXX
  553. If ExtVal<0 then Inc(Prec); // Corect for minus sign
  554. If Abs(Extval)<1 then Inc(Prec); // correct for - in E
  555. Writeln('STRING ',prec);
  556. Str(Args[doarg].VExtended^:prec,ToAdd);
  557. WRITELN('DID');
  558. end;
  559. 'F' : begin
  560. end;
  561. 'S' : begin
  562. CheckArg(vtString);
  563. Index:=Length(Args[doarg].VString^);
  564. If (Prec<>-1) and (Index>Prec) then
  565. Index:=Prec;
  566. ToAdd:=Copy(Args[DoArg].VString^,1,Index);
  567. end;
  568. 'P' : Begin
  569. CheckArg(vtpointer);
  570. ToAdd:=HexStr(Longint(Args[DoArg].VPointer),8);
  571. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  572. // Insert(':',ToAdd,5);
  573. end;
  574. 'X' : begin
  575. Checkarg(vtinteger);
  576. If Prec>32 then
  577. ToAdd:=HexStr(Args[Doarg].VInteger,Prec)
  578. else
  579. begin
  580. // determine minimum needed number of hex digits.
  581. Index:=1;
  582. While (1 shl (Index*4))<Args[DoArg].VInteger do
  583. inc(Index);
  584. If Index>Prec then
  585. Prec:=Index;
  586. ToAdd:=HexStr(Args[DoArg].VInteger,Prec);
  587. end;
  588. end;
  589. '%': ToAdd:='%';
  590. end;
  591. If Width<>-1 then
  592. If Length(ToAdd)<Width then
  593. If not Left then
  594. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  595. else
  596. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  597. Result:=Result+ToAdd;
  598. end;
  599. inc(chpos);
  600. Oldpos:=chpos;
  601. end;
  602. end;
  603. {==============================================================================}
  604. { extra functions }
  605. {==============================================================================}
  606. { LeftStr returns Count left-most characters from S }
  607. function LeftStr(const S: string; Count: integer): string;
  608. begin
  609. result := Copy(S, 1, Count);
  610. end ;
  611. { RightStr returns Count right-most characters from S }
  612. function RightStr(const S: string; Count: integer): string;
  613. begin
  614. result := Copy(S, 1 + Length(S) - Count, Count);
  615. end ;
  616. { BCDToInt converts the BCD value Value to an integer }
  617. function BCDToInt(Value: integer): integer;
  618. var i, j: integer;
  619. begin
  620. result := 0;
  621. j := 1;
  622. for i := 0 to SizeOf(Value) shr 1 - 1 do begin
  623. result := result + j * (Value and 15);
  624. j := j * 10;
  625. Value := Value shr 4;
  626. end ;
  627. end ;
  628. { Case Translation Tables }
  629. { Although these tables can be obtained through system calls }
  630. { it is better to not use those, since most implementation are not 100% }
  631. { WARNING: }
  632. { before modifying a translation table make sure that the current codepage }
  633. { of the OS corresponds to the one you make changes to }
  634. const
  635. { upper case translation table for character set 850 }
  636. CP850UCT: array[128..255] of char =
  637. ('€', 'š', '�', '¶', 'Ž', '¶', '�', '€', 'Ò', 'Ó', 'Ô', 'Ø', '×', 'Þ', 'Ž', '�',
  638. '�', '’', '’', 'â', '™', 'ã', 'ê', 'ë', 'Y', '™', 'š', '�', 'œ', '�', 'ž', 'Ÿ',
  639. 'µ', 'Ö', 'à', 'é', '¥', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  640. '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  641. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  642. 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß',
  643. 'à', 'á', 'â', 'ã', 'å', 'å', 'æ', 'í', 'è', 'é', 'ê', 'ë', 'í', 'í', 'î', 'ï',
  644. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  645. { lower case translation table for character set 850 }
  646. CP850LCT: array[128..255] of char =
  647. ('‡', '�', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '�', '„', '†',
  648. '‚', '‘', '‘', '“', '”', '•', '–', '—', '˜', '”', '�', '›', 'œ', '›', 'ž', 'Ÿ',
  649. ' ', '¡', '¢', '£', '¤', '¤', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯',
  650. '°', '±', '²', '³', '´', ' ', 'ƒ', '…', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿',
  651. 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Æ', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï',
  652. 'Ð', 'Ñ', 'ˆ', '‰', 'Š', 'Õ', '¡', 'Œ', '‹', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', '�', 'ß',
  653. '¢', 'á', '“', '•', 'ä', 'ä', 'æ', 'í', 'è', '£', '–', '—', 'ì', 'ì', 'î', 'ï',
  654. 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
  655. { upper case translation table for character set ISO 8859/1 Latin 1 }
  656. CPISO88591UCT: array[192..255] of char =
  657. ( #192, #193, #194, #195, #196, #197, #198, #199,
  658. #200, #201, #202, #203, #204, #205, #206, #207,
  659. #208, #209, #210, #211, #212, #213, #214, #215,
  660. #216, #217, #218, #219, #220, #221, #222, #223,
  661. #192, #193, #194, #195, #196, #197, #198, #199,
  662. #200, #201, #202, #203, #204, #205, #206, #207,
  663. #208, #209, #210, #211, #212, #213, #214, #247,
  664. #216, #217, #218, #219, #220, #221, #222, #89 );
  665. { lower case translation table for character set ISO 8859/1 Latin 1 }
  666. CPISO88591LCT: array[192..255] of char =
  667. ( #224, #225, #226, #227, #228, #229, #230, #231,
  668. #232, #233, #234, #235, #236, #237, #238, #239,
  669. #240, #241, #242, #243, #244, #245, #246, #215,
  670. #248, #249, #250, #251, #252, #253, #254, #223,
  671. #224, #225, #226, #227, #228, #229, #230, #231,
  672. #232, #233, #234, #235, #236, #237, #238, #239,
  673. #240, #241, #242, #243, #244, #245, #246, #247,
  674. #248, #249, #250, #251, #252, #253, #254, #255 );
  675. {$IFDEF GO32V2}
  676. { Codepage constants }
  677. const
  678. CP_US = 437;
  679. CP_MultiLingual = 850;
  680. CP_SlavicLatin2 = 852;
  681. CP_Turkish = 857;
  682. CP_Portugal = 860;
  683. CP_IceLand = 861;
  684. CP_Canada = 863;
  685. CP_NorwayDenmark = 865;
  686. { CountryInfo }
  687. type
  688. TCountryInfo = packed record
  689. InfoId: byte;
  690. case integer of
  691. 1: ( Size: word;
  692. CountryId: word;
  693. CodePage: word;
  694. CountryInfo: array[0..33] of byte );
  695. 2: ( UpperCaseTable: longint );
  696. 4: ( FilenameUpperCaseTable: longint );
  697. 5: ( FilecharacterTable: longint );
  698. 6: ( CollatingTable: longint );
  699. 7: ( DBCSLeadByteTable: longint );
  700. end ;
  701. procedure GetExtendedCountryInfo(InfoId: integer; CodePage, CountryId: word; var CountryInfo: TCountryInfo);
  702. var Regs: Registers;
  703. begin
  704. Regs.AH := $65;
  705. Regs.AL := InfoId;
  706. Regs.BX := CodePage;
  707. Regs.DX := CountryId;
  708. Regs.ES := transfer_buffer div 16;
  709. Regs.DI := transfer_buffer and 15;
  710. Regs.CX := SizeOf(TCountryInfo);
  711. RealIntr($21, Regs);
  712. DosMemGet(transfer_buffer shr 16, transfer_buffer and 65535, CountryInfo, Regs.CX );
  713. end ;
  714. procedure InitAnsi;
  715. var CountryInfo: TCountryInfo; i: integer;
  716. begin
  717. { Fill table entries 0 to 127 }
  718. for i := 0 to 96 do
  719. UpperCaseTable[i] := chr(i);
  720. for i := 97 to 122 do
  721. UpperCaseTable[i] := chr(i - 32);
  722. for i := 123 to 127 do
  723. UpperCaseTable[i] := chr(i);
  724. for i := 0 to 64 do
  725. LowerCaseTable[i] := chr(i);
  726. for i := 65 to 90 do
  727. LowerCaseTable[i] := chr(i + 32);
  728. for i := 91 to 255 do
  729. LowerCaseTable[i] := chr(i);
  730. { Get country and codepage info }
  731. GetExtendedCountryInfo(1, $FFFF, $FFFF, CountryInfo);
  732. if CountryInfo.CodePage = 850 then begin
  733. Move(CP850UCT, UpperCaseTable[128], 128);
  734. Move(CP850LCT, LowerCaseTable[128], 128);
  735. end
  736. else begin
  737. { this needs to be checked !!
  738. this is correct only if UpperCaseTable is
  739. and Offset:Segment word record (PM) }
  740. { get the uppercase table from dosmemory }
  741. GetExtendedCountryInfo(2, $FFFF, $FFFF, CountryInfo);
  742. DosMemGet(CountryInfo.UpperCaseTable shr 16, 2 + CountryInfo.UpperCaseTable and 65535, UpperCaseTable[128], 128);
  743. for i := 128 to 255 do begin
  744. if UpperCaseTable[i] <> chr(i) then
  745. LowerCaseTable[ord(UpperCaseTable[i])] := chr(i);
  746. end ;
  747. end ;
  748. end ;
  749. {$ELSE}
  750. // {$IFDEF LINUX}
  751. procedure InitAnsi;
  752. begin
  753. end ;
  754. // {$ENDIF}
  755. {$ENDIF}
  756. {
  757. $Log$
  758. Revision 1.11 1999-02-10 22:15:12 michael
  759. + Changed to ansistrings
  760. Revision 1.10 1998/12/15 22:43:09 peter
  761. * removed temp symbols
  762. Revision 1.9 1998/11/04 10:20:52 peter
  763. * ansistring fixes
  764. Revision 1.8 1998/10/02 13:57:38 michael
  765. Format error now causes exception
  766. Revision 1.7 1998/10/02 12:17:17 michael
  767. + Made sure it compiles with official 0.99.8
  768. Revision 1.6 1998/10/02 10:42:17 michael
  769. + Initial implementation of format
  770. Revision 1.5 1998/10/01 16:05:37 michael
  771. Added (empty) format function
  772. Revision 1.4 1998/09/17 12:39:52 michael
  773. + Further fixes from GertJan Schouten
  774. Revision 1.3 1998/09/16 14:34:37 pierre
  775. * go32v2 did not compile
  776. * wrong code in systr.inc corrected
  777. Revision 1.2 1998/09/16 08:28:42 michael
  778. Update from gertjan Schouten, plus small fix for linux
  779. Revision 1.1 1998/04/10 15:17:46 michael
  780. + Initial implementation; Donated by Gertjan Schouten
  781. His file was split into several files, to keep it a little bit structured.
  782. }