home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d6 / RX275D6.ZIP / Units / rxStrUtils.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-24  |  29KB  |  1,062 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {         Copyright (c) 1995, 1996 AO ROSNO             }
  5. {         Copyright (c) 1997, 1998 Master-Bank          }
  6. {                                                       }
  7. {         This unit based on AlexGraf String Library    }
  8. {         by Alexei Lukin (c) 1992                      }
  9. {                                                       }
  10. {*******************************************************}
  11.  
  12. unit rxStrUtils;
  13.  
  14. {$I RX.INC}
  15. {$A+,B-,E-,R-}
  16.  
  17. interface
  18.  
  19. uses SysUtils;
  20.  
  21. type
  22. {$IFNDEF RX_D4}
  23.   TSysCharSet = set of Char;
  24. {$ENDIF}
  25.   TCharSet = TSysCharSet;
  26.  
  27. { ** Common string handling routines ** }
  28.  
  29. function StrToOem(const AnsiStr: string): string;
  30. { StrToOem translates a string from the Windows character set into the
  31.   OEM character set. }
  32.  
  33. function OemToAnsiStr(const OemStr: string): string;
  34. { OemToAnsiStr translates a string from the OEM character set into the
  35.   Windows character set. }
  36.  
  37. function IsEmptyStr(const S: string; const EmptyChars: TCharSet): Boolean;
  38. { EmptyStr returns true if the given string contains only character
  39.   from the EmptyChars. }
  40.  
  41. function ReplaceStr(const S, Srch, Replace: string): string;
  42. { Returns string with every occurrence of Srch string replaced with
  43.   Replace string. }
  44.  
  45. function DelSpace(const S: string): string;
  46. { DelSpace return a string with all white spaces removed. }
  47.  
  48. function DelChars(const S: string; Chr: Char): string;
  49. { DelChars return a string with all Chr characters removed. }
  50.  
  51. function DelBSpace(const S: string): string;
  52. { DelBSpace trims leading spaces from the given string. }
  53.  
  54. function DelESpace(const S: string): string;
  55. { DelESpace trims trailing spaces from the given string. }
  56.  
  57. function DelRSpace(const S: string): string;
  58. { DelRSpace trims leading and trailing spaces from the given string. }
  59.  
  60. function DelSpace1(const S: string): string;
  61. { DelSpace1 return a string with all non-single white spaces removed. }
  62.  
  63. function Tab2Space(const S: string; Numb: Byte): string;
  64. { Tab2Space converts any tabulation character in the given string to the
  65.   Numb spaces characters. }
  66.  
  67. function NPos(const C: string; S: string; N: Integer): Integer;
  68. { NPos searches for a N-th position of substring C in a given string. }
  69.  
  70. function MakeStr(C: Char; N: Integer): string;
  71. function MS(C: Char; N: Integer): string;
  72. { MakeStr return a string of length N filled with character C. }
  73.  
  74. function AddChar(C: Char; const S: string; N: Integer): string;
  75. { AddChar return a string left-padded to length N with characters C. }
  76.  
  77. function AddCharR(C: Char; const S: string; N: Integer): string;
  78. { AddCharR return a string right-padded to length N with characters C. }
  79.  
  80. function LeftStr(const S: string; N: Integer): string;
  81. { LeftStr return a string right-padded to length N with blanks. }
  82.  
  83. function RightStr(const S: string; N: Integer): string;
  84. { RightStr return a string left-padded to length N with blanks. }
  85.  
  86. function CenterStr(const S: string; Len: Integer): string;
  87. { CenterStr centers the characters in the string based upon the
  88.   Len specified. }
  89.  
  90. function CompStr(const S1, S2: string): Integer;
  91. { CompStr compares S1 to S2, with case-sensitivity. The return value is
  92.   -1 if S1 < S2, 0 if S1 = S2, or 1 if S1 > S2. }
  93.  
  94. function CompText(const S1, S2: string): Integer;
  95. { CompText compares S1 to S2, without case-sensitivity. The return value
  96.   is the same as for CompStr. }
  97.  
  98. function Copy2Symb(const S: string; Symb: Char): string;
  99. { Copy2Symb returns a substring of a string S from begining to first
  100.   character Symb. }
  101.  
  102. function Copy2SymbDel(var S: string; Symb: Char): string;
  103. { Copy2SymbDel returns a substring of a string S from begining to first
  104.   character Symb and removes this substring from S. }
  105.  
  106. function Copy2Space(const S: string): string;
  107. { Copy2Symb returns a substring of a string S from begining to first
  108.   white space. }
  109.  
  110. function Copy2SpaceDel(var S: string): string;
  111. { Copy2SpaceDel returns a substring of a string S from begining to first
  112.   white space and removes this substring from S. }
  113.  
  114. function AnsiProperCase(const S: string; const WordDelims: TCharSet): string;
  115. { Returns string, with the first letter of each word in uppercase,
  116.   all other letters in lowercase. Words are delimited by WordDelims. }
  117.  
  118. function WordCount(const S: string; const WordDelims: TCharSet): Integer;
  119. { WordCount given a set of word delimiters, returns number of words in S. }
  120.  
  121. function WordPosition(const N: Integer; const S: string;
  122.   const WordDelims: TCharSet): Integer;
  123. { Given a set of word delimiters, returns start position of N'th word in S. }
  124.  
  125. function ExtractWord(N: Integer; const S: string;
  126.   const WordDelims: TCharSet): string;
  127. function ExtractWordPos(N: Integer; const S: string;
  128.   const WordDelims: TCharSet; var Pos: Integer): string;
  129. function ExtractDelimited(N: Integer; const S: string;
  130.   const Delims: TCharSet): string;
  131. { ExtractWord, ExtractWordPos and ExtractDelimited given a set of word
  132.   delimiters, return the N'th word in S. }
  133.  
  134. function ExtractSubstr(const S: string; var Pos: Integer;
  135.   const Delims: TCharSet): string;
  136. { ExtractSubstr given a set of word delimiters, returns the substring from S,
  137.   that started from position Pos. }
  138.  
  139. function IsWordPresent(const W, S: string; const WordDelims: TCharSet): Boolean;
  140. { IsWordPresent given a set of word delimiters, returns True if word W is
  141.   present in string S. }
  142.  
  143. function QuotedString(const S: string; Quote: Char): string;
  144. { QuotedString returns the given string as a quoted string, using the
  145.   provided Quote character. }
  146.  
  147. function ExtractQuotedString(const S: string; Quote: Char): string;
  148. { ExtractQuotedString removes the Quote characters from the beginning and
  149.   end of a quoted string, and reduces pairs of Quote characters within
  150.   the quoted string to a single character. }
  151.  
  152. function FindPart(const HelpWilds, InputStr: string): Integer;
  153. { FindPart compares a string with '?' and another, returns the position of
  154.   HelpWilds in InputStr. }
  155.  
  156. function IsWild(InputStr, Wilds: string; IgnoreCase: Boolean): Boolean;
  157. { IsWild compares InputString with WildCard string and returns True
  158.   if corresponds. }
  159.  
  160. function XorString(const Key, Src: ShortString): ShortString;
  161. function XorEncode(const Key, Source: string): string;
  162. function XorDecode(const Key, Source: string): string;
  163.  
  164. { ** Command line routines ** }
  165.  
  166. {$IFNDEF RX_D4}
  167. function FindCmdLineSwitch(const Switch: string; SwitchChars: TCharSet;
  168.   IgnoreCase: Boolean): Boolean;
  169. {$ENDIF}
  170. function GetCmdLineArg(const Switch: string; SwitchChars: TCharSet): string;
  171.  
  172. { ** Numeric string handling routines ** }
  173.  
  174. function Numb2USA(const S: string): string;
  175. { Numb2USA converts numeric string S to USA-format. }
  176.  
  177. function Dec2Hex(N: Longint; A: Byte): string;
  178. function D2H(N: Longint; A: Byte): string;
  179. { Dec2Hex converts the given value to a hexadecimal string representation
  180.   with the minimum number of digits (A) specified. }
  181.  
  182. function Hex2Dec(const S: string): Longint;
  183. function H2D(const S: string): Longint;
  184. { Hex2Dec converts the given hexadecimal string to the corresponding integer
  185.   value. }
  186.  
  187. function Dec2Numb(N: Longint; A, B: Byte): string;
  188. { Dec2Numb converts the given value to a string representation with the
  189.   base equal to B and with the minimum number of digits (A) specified. }
  190.  
  191. function Numb2Dec(S: string; B: Byte): Longint;
  192. { Numb2Dec converts the given B-based numeric string to the corresponding
  193.   integer value. }
  194.  
  195. function IntToBin(Value: Longint; Digits, Spaces: Integer): string;
  196. { IntToBin converts the given value to a binary string representation
  197.   with the minimum number of digits specified. }
  198.  
  199. function IntToRoman(Value: Longint): string;
  200. { IntToRoman converts the given value to a roman numeric string
  201.   representation. }
  202.  
  203. function RomanToInt(const S: string): Longint;
  204. { RomanToInt converts the given string to an integer value. If the string
  205.   doesn't contain a valid roman numeric value, the 0 value is returned. }
  206.  
  207. const
  208.   CRLF = #13#10;
  209.   DigitChars = ['0'..'9'];
  210. {$IFNDEF CBUILDER}
  211.   Brackets = ['(',')','[',']','{','}'];
  212.   StdWordDelims = [#0..' ',',','.',';','/','\',':','''','"','`'] + Brackets;
  213. {$ENDIF}
  214.  
  215. implementation
  216.  
  217. uses {$IFDEF WIN32} Windows {$ELSE} WinTypes, WinProcs {$ENDIF};
  218.  
  219. function StrToOem(const AnsiStr: string): string;
  220. begin
  221.   SetLength(Result, Length(AnsiStr));
  222.   if Length(Result) > 0 then
  223. {$IFDEF WIN32}
  224.     CharToOemBuff(PChar(AnsiStr), PChar(Result), Length(Result));
  225. {$ELSE}
  226.     AnsiToOemBuff(@AnsiStr[1], @Result[1], Length(Result));
  227. {$ENDIF}
  228. end;
  229.  
  230. function OemToAnsiStr(const OemStr: string): string;
  231. begin
  232.   SetLength(Result, Length(OemStr));
  233.   if Length(Result) > 0 then
  234. {$IFDEF WIN32}
  235.     OemToCharBuff(PChar(OemStr), PChar(Result), Length(Result));
  236. {$ELSE}
  237.     OemToAnsiBuff(@OemStr[1], @Result[1], Length(Result));
  238. {$ENDIF}
  239. end;
  240.  
  241. function IsEmptyStr(const S: string; const EmptyChars: TCharSet): Boolean;
  242. var
  243.   I, SLen: Integer;
  244. begin
  245.   SLen := Length(S);
  246.   I := 1;
  247.   while I <= SLen do begin
  248.     if not (S[I] in EmptyChars) then begin
  249.       Result := False;
  250.       Exit;
  251.     end
  252.     else Inc(I);
  253.   end;
  254.   Result := True;
  255. end;
  256.  
  257. function ReplaceStr(const S, Srch, Replace: string): string;
  258. var
  259.   I: Integer;
  260.   Source: string;
  261. begin
  262.   Source := S;
  263.   Result := '';
  264.   repeat
  265.     I := Pos(Srch, Source);
  266.     if I > 0 then begin
  267.       Result := Result + Copy(Source, 1, I - 1) + Replace;
  268.       Source := Copy(Source, I + Length(Srch), MaxInt);
  269.     end
  270.     else Result := Result + Source;
  271.   until I <= 0;
  272. end;
  273.  
  274. function DelSpace(const S: String): string;
  275. begin
  276.   Result := DelChars(S, ' ');
  277. end;
  278.  
  279. function DelChars(const S: string; Chr: Char): string;
  280. var
  281.   I: Integer;
  282. begin
  283.   Result := S;
  284.   for I := Length(Result) downto 1 do begin
  285.     if Result[I] = Chr then Delete(Result, I, 1);
  286.   end;
  287. end;
  288.  
  289. function DelBSpace(const S: string): string;
  290. var
  291.   I, L: Integer;
  292. begin
  293.   L := Length(S);
  294.   I := 1;
  295.   while (I <= L) and (S[I] = ' ') do Inc(I);
  296.   Result := Copy(S, I, MaxInt);
  297. end;
  298.  
  299. function DelESpace(const S: string): string;
  300. var
  301.   I: Integer;
  302. begin
  303.   I := Length(S);
  304.   while (I > 0) and (S[I] = ' ') do Dec(I);
  305.   Result := Copy(S, 1, I);
  306. end;
  307.  
  308. function DelRSpace(const S: string): string;
  309. begin
  310.   Result := DelBSpace(DelESpace(S));
  311. end;
  312.  
  313. function DelSpace1(const S: string): string;
  314. var
  315.   I: Integer;
  316. begin
  317.   Result := S;
  318.   for I := Length(Result) downto 2 do begin
  319.     if (Result[I] = ' ') and (Result[I - 1] = ' ') then
  320.       Delete(Result, I, 1);
  321.   end;
  322. end;
  323.  
  324. function Tab2Space(const S: string; Numb: Byte): string;
  325. var
  326.   I: Integer;
  327. begin
  328.   I := 1;
  329.   Result := S;
  330.   while I <= Length(Result) do begin
  331.     if Result[I] = Chr(9) then begin
  332.       Delete(Result, I, 1);
  333.       Insert(MakeStr(' ', Numb), Result, I);
  334.       Inc(I, Numb);
  335.     end
  336.     else Inc(I);
  337.   end;
  338. end;
  339.  
  340. function MakeStr(C: Char; N: Integer): string;
  341. begin
  342.   if N < 1 then Result := ''
  343.   else begin
  344. {$IFNDEF WIN32}
  345.     if N > 255 then N := 255;
  346. {$ENDIF WIN32}
  347.     SetLength(Result, N);
  348.     FillChar(Result[1], Length(Result), C);
  349.   end;
  350. end;
  351.  
  352. function MS(C: Char; N: Integer): string;
  353. begin
  354.   Result := MakeStr(C, N);
  355. end;
  356.  
  357. function NPos(const C: string; S: string; N: Integer): Integer;
  358. var
  359.   I, P, K: Integer;
  360. begin
  361.   Result := 0;
  362.   K := 0;
  363.   for I := 1 to N do begin
  364.     P := Pos(C, S);
  365.     Inc(K, P);
  366.     if (I = N) and (P > 0) then begin
  367.       Result := K;
  368.       Exit;
  369.     end;
  370.     if P > 0 then Delete(S, 1, P)
  371.     else Exit;
  372.   end;
  373. end;
  374.  
  375. function AddChar(C: Char; const S: string; N: Integer): string;
  376. begin
  377.   if Length(S) < N then
  378.     Result := MakeStr(C, N - Length(S)) + S
  379.   else Result := S;
  380. end;
  381.  
  382. function AddCharR(C: Char; const S: string; N: Integer): string;
  383. begin
  384.   if Length(S) < N then
  385.     Result := S + MakeStr(C, N - Length(S))
  386.   else Result := S;
  387. end;
  388.  
  389. function LeftStr(const S: string; N: Integer): string;
  390. begin
  391.   Result := AddCharR(' ', S, N);
  392. end;
  393.  
  394. function RightStr(const S: string; N: Integer): string;
  395. begin
  396.   Result := AddChar(' ', S, N);
  397. end;
  398.  
  399. function CompStr(const S1, S2: string): Integer;
  400. begin
  401. {$IFDEF WIN32}
  402.   Result := CompareString(GetThreadLocale, SORT_STRINGSORT, PChar(S1),
  403.     Length(S1), PChar(S2), Length(S2)) - 2;
  404. {$ELSE}
  405.   Result := CompareStr(S1, S2);
  406. {$ENDIF}
  407. end;
  408.  
  409. function CompText(const S1, S2: string): Integer;
  410. begin
  411. {$IFDEF WIN32}
  412.   Result := CompareString(GetThreadLocale, SORT_STRINGSORT or NORM_IGNORECASE,
  413.     PChar(S1), Length(S1), PChar(S2), Length(S2)) - 2;
  414. {$ELSE}
  415.   Result := CompareText(S1, S2);
  416. {$ENDIF}
  417. end;
  418.  
  419. function Copy2Symb(const S: string; Symb: Char): string;
  420. var
  421.   P: Integer;
  422. begin
  423.   P := Pos(Symb, S);
  424.   if P = 0 then P := Length(S) + 1;
  425.   Result := Copy(S, 1, P - 1);
  426. end;
  427.  
  428. function Copy2SymbDel(var S: string; Symb: Char): string;
  429. begin
  430.   Result := Copy2Symb(S, Symb);
  431.   S := DelBSpace(Copy(S, Length(Result) + 1, Length(S)));
  432. end;
  433.  
  434. function Copy2Space(const S: string): string;
  435. begin
  436.   Result := Copy2Symb(S, ' ');
  437. end;
  438.  
  439. function Copy2SpaceDel(var S: string): string;
  440. begin
  441.   Result := Copy2SymbDel(S, ' ');
  442. end;
  443.  
  444. function AnsiProperCase(const S: string; const WordDelims: TCharSet): string;
  445. var
  446.   SLen, I: Cardinal;
  447. begin
  448.   Result := AnsiLowerCase(S);
  449.   I := 1;
  450.   SLen := Length(Result);
  451.   while I <= SLen do begin
  452.     while (I <= SLen) and (Result[I] in WordDelims) do Inc(I);
  453.     if I <= SLen then Result[I] := AnsiUpperCase(Result[I])[1];
  454.     while (I <= SLen) and not (Result[I] in WordDelims) do Inc(I);
  455.   end;
  456. end;
  457.  
  458. function WordCount(const S: string; const WordDelims: TCharSet): Integer;
  459. var
  460.   SLen, I: Cardinal;
  461. begin
  462.   Result := 0;
  463.   I := 1;
  464.   SLen := Length(S);
  465.   while I <= SLen do begin
  466.     while (I <= SLen) and (S[I] in WordDelims) do Inc(I);
  467.     if I <= SLen then Inc(Result);
  468.     while (I <= SLen) and not(S[I] in WordDelims) do Inc(I);
  469.   end;
  470. end;
  471.  
  472. function WordPosition(const N: Integer; const S: string;
  473.   const WordDelims: TCharSet): Integer;
  474. var
  475.   Count, I: Integer;
  476. begin
  477.   Count := 0;
  478.   I := 1;
  479.   Result := 0;
  480.   while (I <= Length(S)) and (Count <> N) do begin
  481.     { skip over delimiters }
  482.     while (I <= Length(S)) and (S[I] in WordDelims) do Inc(I);
  483.     { if we're not beyond end of S, we're at the start of a word }
  484.     if I <= Length(S) then Inc(Count);
  485.     { if not finished, find the end of the current word }
  486.     if Count <> N then
  487.       while (I <= Length(S)) and not (S[I] in WordDelims) do Inc(I)
  488.     else Result := I;
  489.   end;
  490. end;
  491.  
  492. function ExtractWord(N: Integer; const S: string;
  493.   const WordDelims: TCharSet): string;
  494. var
  495.   I: Integer;
  496.   Len: Integer;
  497. begin
  498.   Len := 0;
  499.   I := WordPosition(N, S, WordDelims);
  500.   if I <> 0 then
  501.     { find the end of the current word }
  502.     while (I <= Length(S)) and not(S[I] in WordDelims) do begin
  503.       { add the I'th character to result }
  504.       Inc(Len);
  505.       SetLength(Result, Len);
  506.       Result[Len] := S[I];
  507.       Inc(I);
  508.     end;
  509.   SetLength(Result, Len);
  510. end;
  511.  
  512. function ExtractWordPos(N: Integer; const S: string;
  513.   const WordDelims: TCharSet; var Pos: Integer): string;
  514. var
  515.   I, Len: Integer;
  516. begin
  517.   Len := 0;
  518.   I := WordPosition(N, S, WordDelims);
  519.   Pos := I;
  520.   if I <> 0 then
  521.     { find the end of the current word }
  522.     while (I <= Length(S)) and not(S[I] in WordDelims) do begin
  523.       { add the I'th character to result }
  524.       Inc(Len);
  525.       SetLength(Result, Len);
  526.       Result[Len] := S[I];
  527.       Inc(I);
  528.     end;
  529.   SetLength(Result, Len);
  530. end;
  531.  
  532. function ExtractDelimited(N: Integer; const S: string;
  533.   const Delims: TCharSet): string;
  534. var
  535.   CurWord: Integer;
  536.   I, Len, SLen: Integer;
  537. begin
  538.   CurWord := 0;
  539.   I := 1;
  540.   Len := 0;
  541.   SLen := Length(S);
  542.   SetLength(Result, 0);
  543.   while (I <= SLen) and (CurWord <> N) do begin
  544.     if S[I] in Delims then Inc(CurWord)
  545.     else begin
  546.       if CurWord = N - 1 then begin
  547.         Inc(Len);
  548.         SetLength(Result, Len);
  549.         Result[Len] := S[I];
  550.       end;
  551.     end;
  552.     Inc(I);
  553.   end;
  554. end;
  555.  
  556. function ExtractSubstr(const S: string; var Pos: Integer;
  557.   const Delims: TCharSet): string;
  558. var
  559.   I: Integer;
  560. begin
  561.   I := Pos;
  562.   while (I <= Length(S)) and not (S[I] in Delims) do Inc(I);
  563.   Result := Copy(S, Pos, I - Pos);
  564.   if (I <= Length(S)) and (S[I] in Delims) then Inc(I);
  565.   Pos := I;
  566. end;
  567.  
  568. function IsWordPresent(const W, S: string; const WordDelims: TCharSet): Boolean;
  569. var
  570.   Count, I: Integer;
  571. begin
  572.   Result := False;
  573.   Count := WordCount(S, WordDelims);
  574.   for I := 1 to Count do
  575.     if ExtractWord(I, S, WordDelims) = W then begin
  576.       Result := True;
  577.       Exit;
  578.     end;
  579. end;
  580.  
  581. {$IFDEF WIN32}
  582.   {$IFNDEF VER90}
  583.     { C++Builder or Delphi 3.0 }
  584.     {$DEFINE MBCS}
  585.   {$ENDIF}
  586. {$ENDIF}
  587.  
  588. function QuotedString(const S: string; Quote: Char): string;
  589. {$IFDEF MBCS}
  590. begin
  591.   Result := AnsiQuotedStr(S, Quote);
  592. {$ELSE}
  593. var
  594.   I: Integer;
  595. begin
  596.   Result := S;
  597.   for I := Length(Result) downto 1 do
  598.     if Result[I] = Quote then Insert(Quote, Result, I);
  599.   Result := Quote + Result + Quote;
  600. {$ENDIF MBCS}
  601. end;
  602.  
  603. function ExtractQuotedString(const S: string; Quote: Char): string;
  604. var
  605. {$IFDEF MBCS}
  606.   P: PChar;
  607. begin
  608.   P := PChar(S);
  609.   if P^ = Quote then Result := AnsiExtractQuotedStr(P, Quote)
  610.   else Result := S;
  611. {$ELSE}
  612.   I: Integer;
  613. begin
  614.   Result := S;
  615.   I := Length(Result);
  616.   if (I > 0) and (Result[1] = Quote) and
  617.     (Result[I] = Quote) then
  618.   begin
  619.     Delete(Result, I, 1);
  620.     Delete(Result, 1, 1);
  621.     for I := Length(Result) downto 2 do begin
  622.       if (Result[I] = Quote) and (Result[I - 1] = Quote) then
  623.         Delete(Result, I, 1);
  624.     end;
  625.   end;
  626. {$ENDIF MBCS}
  627. end;
  628.  
  629. function Numb2USA(const S: string): string;
  630. var
  631.   I, NA: Integer;
  632. begin
  633.   I := Length(S);
  634.   Result := S;
  635.   NA := 0;
  636.   while (I > 0) do begin
  637.     if ((Length(Result) - I + 1 - NA) mod 3 = 0) and (I <> 1) then
  638.     begin
  639.       Insert(',', Result, I);
  640.       Inc(NA);
  641.     end;
  642.     Dec(I);
  643.   end;
  644. end;
  645.  
  646. function CenterStr(const S: string; Len: Integer): string;
  647. begin
  648.   if Length(S) < Len then begin
  649.     Result := MakeStr(' ', (Len div 2) - (Length(S) div 2)) + S;
  650.     Result := Result + MakeStr(' ', Len - Length(Result));
  651.   end
  652.   else Result := S;
  653. end;
  654.  
  655. function Dec2Hex(N: LongInt; A: Byte): string;
  656. begin
  657.   Result := IntToHex(N, A);
  658. end;
  659.  
  660. function D2H(N: LongInt; A: Byte): string;
  661. begin
  662.   Result := IntToHex(N, A);
  663. end;
  664.  
  665. function Hex2Dec(const S: string): Longint;
  666. var
  667.   HexStr: string;
  668. begin
  669.   if Pos('$', S) = 0 then HexStr := '$' + S
  670.   else HexStr := S;
  671.   Result := StrToIntDef(HexStr, 0);
  672. end;
  673.  
  674. function H2D(const S: string): Longint;
  675. begin
  676.   Result := Hex2Dec(S);
  677. end;
  678.  
  679. function Dec2Numb(N: Longint; A, B: Byte): string;
  680. var
  681.   C: Integer;
  682. {$IFDEF RX_D4}
  683.   Number: Cardinal;
  684. {$ELSE}
  685.   Number: Longint;
  686. {$ENDIF}
  687. begin
  688.   if N = 0 then Result := '0'
  689.   else begin
  690. {$IFDEF RX_D4}
  691.     Number := Cardinal(N);
  692. {$ELSE}
  693.     Number := N;
  694. {$ENDIF}
  695.     Result := '';
  696.     while Number > 0 do begin
  697.       C := Number mod B;
  698.       if C > 9 then C := C + 55
  699.       else C := C + 48;
  700.       Result := Chr(C) + Result;
  701.       Number := Number div B;
  702.     end;
  703.   end;
  704.   if Result <> '' then Result := AddChar('0', Result, A);
  705. end;
  706.  
  707. function Numb2Dec(S: string; B: Byte): Longint;
  708. var
  709.   I, P: Longint;
  710. begin
  711.   I := Length(S);
  712.   Result := 0;
  713.   S := UpperCase(S);
  714.   P := 1;
  715.   while (I >= 1) do begin
  716.     if S[I] > '@' then Result := Result + (Ord(S[I]) - 55) * P
  717.     else Result := Result + (Ord(S[I]) - 48) * P;
  718.     Dec(I);
  719.     P := P * B;
  720.   end;
  721. end;
  722.  
  723. function RomanToInt(const S: string): Longint;
  724. const
  725.   RomanChars = ['C','D','I','L','M','V','X'];
  726.   RomanValues: array['C'..'X'] of Word =
  727.     (100,500,0,0,0,0,1,0,0,50,1000,0,0,0,0,0,0,0,0,5,0,10);
  728. var
  729.   Index, Next: Char;
  730.   I: Integer;
  731.   Negative: Boolean;
  732. begin
  733.   Result := 0;
  734.   I := 0;
  735.   Negative := (Length(S) > 0) and (S[1] = '-');
  736.   if Negative then Inc(I);
  737.   while (I < Length(S)) do begin
  738.     Inc(I);
  739.     Index := UpCase(S[I]);
  740.     if Index in RomanChars then begin
  741.       if Succ(I) <= Length(S) then Next := UpCase(S[I + 1])
  742.       else Next := #0;
  743.       if (Next in RomanChars) and (RomanValues[Index] < RomanValues[Next]) then
  744.       begin
  745.         Inc(Result, RomanValues[Next]);
  746.         Dec(Result, RomanValues[Index]);
  747.         Inc(I);
  748.       end
  749.       else Inc(Result, RomanValues[Index]);
  750.     end
  751.     else begin
  752.       Result := 0;
  753.       Exit;
  754.     end;
  755.   end;
  756.   if Negative then Result := -Result;
  757. end;
  758.  
  759. function IntToRoman(Value: Longint): string;
  760. Label
  761.   A500, A400, A100, A90, A50, A40, A10, A9, A5, A4, A1;
  762. begin
  763.   Result := '';
  764. {$IFNDEF WIN32}
  765.   if (Value > MaxInt * 2) then Exit;
  766. {$ENDIF}
  767.   while Value >= 1000 do begin
  768.     Dec(Value, 1000); Result := Result + 'M';
  769.   end;
  770.   if Value < 900 then goto A500
  771.   else begin
  772.     Dec(Value, 900); Result := Result + 'CM';
  773.   end;
  774.   goto A90;
  775. A400:
  776.   if Value < 400 then goto A100
  777.   else begin
  778.     Dec(Value, 400); Result := Result + 'CD';
  779.   end;
  780.   goto A90;
  781. A500:
  782.   if Value < 500 then goto A400
  783.   else begin
  784.     Dec(Value, 500); Result := Result + 'D';
  785.   end;
  786. A100:
  787.   while Value >= 100 do begin
  788.     Dec(Value, 100); Result := Result + 'C';
  789.   end;
  790. A90:
  791.   if Value < 90 then goto A50
  792.   else begin
  793.     Dec(Value, 90); Result := Result + 'XC';
  794.   end;
  795.   goto A9;
  796. A40:
  797.   if Value < 40 then goto A10
  798.   else begin
  799.     Dec(Value, 40); Result := Result + 'XL';
  800.   end;
  801.   goto A9;
  802. A50:
  803.   if Value < 50 then goto A40
  804.   else begin
  805.     Dec(Value, 50); Result := Result + 'L';
  806.   end;
  807. A10:
  808.   while Value >= 10 do begin
  809.     Dec(Value, 10); Result := Result + 'X';
  810.   end;
  811. A9:
  812.   if Value < 9 then goto A5
  813.   else begin
  814.     Result := Result + 'IX';
  815.   end;
  816.   Exit;
  817. A4:
  818.   if Value < 4 then goto A1
  819.   else begin
  820.     Result := Result + 'IV';
  821.   end;
  822.   Exit;
  823. A5:
  824.   if Value < 5 then goto A4
  825.   else begin
  826.     Dec(Value, 5); Result := Result + 'V';
  827.   end;
  828.   goto A1;
  829. A1:
  830.   while Value >= 1 do begin
  831.     Dec(Value); Result := Result + 'I';
  832.   end;
  833. end;
  834.  
  835. function IntToBin(Value: Longint; Digits, Spaces: Integer): string;
  836. begin
  837.   Result := '';
  838.   if Digits > 32 then Digits := 32;
  839.   while Digits > 0 do begin
  840.     if (Digits mod Spaces) = 0 then Result := Result + ' ';
  841.     Dec(Digits);
  842.     Result := Result + IntToStr((Value shr Digits) and 1);
  843.   end;
  844. end;
  845.  
  846. function FindPart(const HelpWilds, InputStr: string): Integer;
  847. var
  848.   I, J: Integer;
  849.   Diff: Integer;
  850. begin
  851.   I := Pos('?', HelpWilds);
  852.   if I = 0 then begin
  853.     { if no '?' in HelpWilds }
  854.     Result := Pos(HelpWilds, InputStr);
  855.     Exit;
  856.   end;
  857.   { '?' in HelpWilds }
  858.   Diff := Length(InputStr) - Length(HelpWilds);
  859.   if Diff < 0 then begin
  860.     Result := 0;
  861.     Exit;
  862.   end;
  863.   { now move HelpWilds over InputStr }
  864.   for I := 0 to Diff do begin
  865.     for J := 1 to Length(HelpWilds) do begin
  866.       if (InputStr[I + J] = HelpWilds[J]) or
  867.         (HelpWilds[J] = '?') then
  868.       begin
  869.         if J = Length(HelpWilds) then begin
  870.           Result := I + 1;
  871.           Exit;
  872.         end;
  873.       end
  874.       else Break;
  875.     end;
  876.   end;
  877.   Result := 0;
  878. end;
  879.  
  880. function IsWild(InputStr, Wilds: string; IgnoreCase: Boolean): Boolean;
  881.  
  882.  function SearchNext(var Wilds: string): Integer;
  883.  { looking for next *, returns position and string until position }
  884.  begin
  885.    Result := Pos('*', Wilds);
  886.    if Result > 0 then Wilds := Copy(Wilds, 1, Result - 1);
  887.  end;
  888.  
  889. var
  890.   CWild, CInputWord: Integer; { counter for positions }
  891.   I, LenHelpWilds: Integer;
  892.   MaxInputWord, MaxWilds: Integer; { Length of InputStr and Wilds }
  893.   HelpWilds: string;
  894. begin
  895.   if Wilds = InputStr then begin
  896.     Result := True;
  897.     Exit;
  898.   end;
  899.   repeat { delete '**', because '**' = '*' }
  900.     I := Pos('**', Wilds);
  901.     if I > 0 then
  902.       Wilds := Copy(Wilds, 1, I - 1) + '*' + Copy(Wilds, I + 2, MaxInt);
  903.   until I = 0;
  904.   if Wilds = '*' then begin { for fast end, if Wilds only '*' }
  905.     Result := True;
  906.     Exit;
  907.   end;
  908.   MaxInputWord := Length(InputStr);
  909.   MaxWilds := Length(Wilds);
  910.   if IgnoreCase then begin { upcase all letters }
  911.     InputStr := AnsiUpperCase(InputStr);
  912.     Wilds := AnsiUpperCase(Wilds);
  913.   end;
  914.   if (MaxWilds = 0) or (MaxInputWord = 0) then begin
  915.     Result := False;
  916.     Exit;
  917.   end;
  918.   CInputWord := 1;
  919.   CWild := 1;
  920.   Result := True;
  921.   repeat
  922.     if InputStr[CInputWord] = Wilds[CWild] then begin { equal letters }
  923.       { goto next letter }
  924.       Inc(CWild);
  925.       Inc(CInputWord);
  926.       Continue;
  927.     end;
  928.     if Wilds[CWild] = '?' then begin { equal to '?' }
  929.       { goto next letter }
  930.       Inc(CWild);
  931.       Inc(CInputWord);
  932.       Continue;
  933.     end;
  934.     if Wilds[CWild] = '*' then begin { handling of '*' }
  935.       HelpWilds := Copy(Wilds, CWild + 1, MaxWilds);
  936.       I := SearchNext(HelpWilds);
  937.       LenHelpWilds := Length(HelpWilds);
  938.       if I = 0 then begin
  939.         { no '*' in the rest, compare the ends }
  940.         if HelpWilds = '' then Exit; { '*' is the last letter }
  941.         { check the rest for equal Length and no '?' }
  942.         for I := 0 to LenHelpWilds - 1 do begin
  943.           if (HelpWilds[LenHelpWilds - I] <> InputStr[MaxInputWord - I]) and
  944.             (HelpWilds[LenHelpWilds - I]<> '?') then
  945.           begin
  946.             Result := False;
  947.             Exit;
  948.           end;
  949.         end;
  950.         Exit;
  951.       end;
  952.       { handle all to the next '*' }
  953.       Inc(CWild, 1 + LenHelpWilds);
  954.       I := FindPart(HelpWilds, Copy(InputStr, CInputWord, MaxInt));
  955.       if I= 0 then begin
  956.         Result := False;
  957.         Exit;
  958.       end;
  959.       CInputWord := I + LenHelpWilds;
  960.       Continue;
  961.     end;
  962.     Result := False;
  963.     Exit;
  964.   until (CInputWord > MaxInputWord) or (CWild > MaxWilds);
  965.   { no completed evaluation }
  966.   if CInputWord <= MaxInputWord then Result := False;
  967.   if (CWild <= MaxWilds) and (Wilds[MaxWilds] <> '*') then Result := False;
  968. end;
  969.  
  970. function XorString(const Key, Src: ShortString): ShortString;
  971. var
  972.   I: Integer;
  973. begin
  974.   Result := Src;
  975.   if Length(Key) > 0 then
  976.     for I := 1 to Length(Src) do
  977.       Result[I] := Chr(Byte(Key[1 + ((I - 1) mod Length(Key))]) xor Ord(Src[I]));
  978. end;
  979.  
  980. function XorEncode(const Key, Source: string): string;
  981. var
  982.   I: Integer;
  983.   C: Byte;
  984. begin
  985.   Result := '';
  986.   for I := 1 to Length(Source) do begin
  987.     if Length(Key) > 0 then
  988.       C := Byte(Key[1 + ((I - 1) mod Length(Key))]) xor Byte(Source[I])
  989.     else
  990.       C := Byte(Source[I]);
  991.     Result := Result + AnsiLowerCase(IntToHex(C, 2));
  992.   end;
  993. end;
  994.  
  995. function XorDecode(const Key, Source: string): string;
  996. var
  997.   I: Integer;
  998.   C: Char;
  999. begin
  1000.   Result := '';
  1001.   for I := 0 to Length(Source) div 2 - 1 do begin
  1002.     C := Chr(StrToIntDef('$' + Copy(Source, (I * 2) + 1, 2), Ord(' ')));
  1003.     if Length(Key) > 0 then
  1004.       C := Chr(Byte(Key[1 + (I mod Length(Key))]) xor Byte(C));
  1005.     Result := Result + C;
  1006.   end;
  1007. end;
  1008.  
  1009. {$IFNDEF RX_D4}
  1010. function FindCmdLineSwitch(const Switch: string; SwitchChars: TCharSet;
  1011.   IgnoreCase: Boolean): Boolean;
  1012. var
  1013.   I: Integer;
  1014.   S: string;
  1015. begin
  1016.   for I := 1 to ParamCount do begin
  1017.     S := ParamStr(I);
  1018.     if (SwitchChars = []) or ((S[1] in SwitchChars) and (Length(S) > 1)) then
  1019.     begin
  1020.       S := Copy(S, 2, MaxInt);
  1021.       if IgnoreCase then begin
  1022.         if (AnsiCompareText(S, Switch) = 0) then begin
  1023.           Result := True;
  1024.           Exit;
  1025.         end;
  1026.       end
  1027.       else begin
  1028.         if (AnsiCompareStr(S, Switch) = 0) then begin
  1029.           Result := True;
  1030.           Exit;
  1031.         end;
  1032.       end;
  1033.     end;
  1034.   end;
  1035.   Result := False;
  1036. end;
  1037. {$ENDIF RX_D4}
  1038.  
  1039. function GetCmdLineArg(const Switch: string; SwitchChars: TCharSet): string;
  1040. var
  1041.   I: Integer;
  1042.   S: string;
  1043. begin
  1044.   I := 1;
  1045.   while I <= ParamCount do begin
  1046.     S := ParamStr(I);
  1047.     if (SwitchChars = []) or ((S[1] in SwitchChars) and (Length(S) > 1)) then
  1048.     begin
  1049.       if (AnsiCompareText(Copy(S, 2, MaxInt), Switch) = 0) then begin
  1050.         Inc(I);
  1051.         if I <= ParamCount then begin
  1052.           Result := ParamStr(I);
  1053.           Exit;
  1054.         end;
  1055.       end;
  1056.     end;
  1057.     Inc(I);
  1058.   end;
  1059.   Result := '';
  1060. end;
  1061.  
  1062. end.