home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / unity / d23456 / SYNAPSE.ZIP / source / lib / SynaUtil.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-15  |  14.3 KB  |  556 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 002.003.000 |
  3. |==============================================================================|
  4. | Content: support procedures and functions                                    |
  5. |==============================================================================|
  6. | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
  7. | (the "License"); you may not use this file except in compliance with the     |
  8. | License. You may obtain a Copy of the License at http://www.mozilla.org/MPL/ |
  9. |                                                                              |
  10. | Software distributed under the License is distributed on an "AS IS" basis,   |
  11. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
  12. | the specific language governing rights and limitations under the License.    |
  13. |==============================================================================|
  14. | The Original Code is Synapse Delphi Library.                                 |
  15. |==============================================================================|
  16. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  17. | Portions created by Lukas Gebauer are Copyright (c) 1999,2000,2001.          |
  18. | Portions created by Hernan Sanchez are Copyright (c) 2000.                   |
  19. | All Rights Reserved.                                                         |
  20. |==============================================================================|
  21. | Contributor(s):                                                              |
  22. |   Hernan Sanchez (hernan.sanchez@iname.com)                                  |
  23. |==============================================================================|
  24. | History: see HISTORY.HTM from distribution package                           |
  25. |          (Found at URL: http://www.ararat.cz/synapse/)                       |
  26. |==============================================================================}
  27.  
  28. {$Q-}
  29.  
  30. unit SynaUtil;
  31.  
  32. interface
  33.  
  34. uses
  35.   SysUtils, Classes,
  36. {$IFDEF LINUX}
  37.   Libc;
  38. {$ELSE}
  39.   Windows;
  40. {$ENDIF}
  41.  
  42. function Timezone: string;
  43. function Rfc822DateTime(t: TDateTime): string;
  44. function CDateTime(t: TDateTime): string;
  45. function CodeInt(Value: Word): string;
  46. function DecodeInt(const Value: string; Index: Integer): Word;
  47. function IsIP(const Value: string): Boolean;
  48. function ReverseIP(Value: string): string;
  49. function IPToID(Host: string): string;
  50. procedure Dump(const Buffer, DumpFile: string);
  51. function SeparateLeft(const Value, Delimiter: string): string;
  52. function SeparateRight(const Value, Delimiter: string): string;
  53. function GetParameter(const Value, Parameter: string): string;
  54. function GetEmailAddr(const Value: string): string;
  55. function GetEmailDesc(Value: string): string;
  56. function StrToHex(const Value: string): string;
  57. function IntToBin(Value: Integer; Digits: Byte): string;
  58. function BinToInt(const Value: string): Integer;
  59. function ParseURL(URL: string; var Prot, User, Pass, Host, Port, Path,
  60.   Para: string): string;
  61. function StringReplace(Value, Search, Replace: string): string;
  62. function RPos(const Sub, Value: String): Integer;
  63. function Fetch(var Value: string; const Delimiter: string): string;
  64.  
  65. implementation
  66. {==============================================================================}
  67. var
  68.   SaveDayNames: array[1..7] of string;
  69.   SaveMonthNames: array[1..12] of string;
  70. const
  71.   MyDayNames: array[1..7] of string =
  72.   ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
  73.   MyMonthNames: array[1..12] of string =
  74.   ('Jan', 'Feb', 'Mar', 'Apr',
  75.     'May', 'Jun', 'Jul', 'Aug',
  76.     'Sep', 'Oct', 'Nov', 'Dec');
  77.  
  78. procedure SaveNames;
  79. var
  80.   I: integer;
  81. begin
  82.   for I := Low(ShortDayNames) to High(ShortDayNames) do
  83.   begin
  84.     SaveDayNames[I] := ShortDayNames[I];
  85.     ShortDayNames[I] := MyDayNames[I];
  86.   end;
  87.   for I := Low(ShortMonthNames) to High(ShortMonthNames) do
  88.   begin
  89.     SaveMonthNames[I] := ShortMonthNames[I];
  90.     ShortMonthNames[I] := MyMonthNames[I];
  91.   end;
  92. end;
  93.  
  94. procedure RestoreNames;
  95. var
  96.   I: integer;
  97. begin
  98.   for I := Low(ShortDayNames) to High(ShortDayNames) do
  99.     ShortDayNames[I] := SaveDayNames[I];
  100.   for I := Low(ShortMonthNames) to High(ShortMonthNames) do
  101.     ShortMonthNames[I] := SaveMonthNames[I];
  102. end;
  103. {==============================================================================}
  104.  
  105. function Timezone: string;
  106. {$IFDEF LINUX}
  107. var
  108.   t: TTime_T;
  109.   UT: TUnixTime;
  110.   bias: Integer;
  111.   h, m: Integer;
  112. begin
  113.   __time(@T);
  114.   localtime_r(@T, UT);
  115.   bias := ut.__tm_gmtoff div 60;
  116.   if bias >= 0 then
  117.     Result := '+'
  118.   else
  119.     Result := '-';
  120. {$ELSE}
  121. var
  122.   zoneinfo: TTimeZoneInformation;
  123.   bias: Integer;
  124.   h, m: Integer;
  125. begin
  126.   case GetTimeZoneInformation(Zoneinfo) of
  127.     2:
  128.       bias := zoneinfo.Bias + zoneinfo.DaylightBias;
  129.     1:
  130.       bias := zoneinfo.Bias + zoneinfo.StandardBias;
  131.   else
  132.     bias := zoneinfo.Bias;
  133.   end;
  134.   if bias <= 0 then
  135.     Result := '+'
  136.   else
  137.     Result := '-';
  138. {$ENDIF}
  139.   bias := Abs(bias);
  140.   h := bias div 60;
  141.   m := bias mod 60;
  142.   Result := Result + Format('%.2d%.2d', [h, m]);
  143. end;
  144.  
  145. {==============================================================================}
  146.  
  147. function Rfc822DateTime(t: TDateTime): string;
  148. begin
  149.   SaveNames;
  150.   try
  151.     Result := FormatDateTime('ddd, d mmm yyyy hh:mm:ss', t);
  152.     Result := Result + ' ' + Timezone;
  153.   finally
  154.     RestoreNames;
  155.   end;
  156. end;
  157.  
  158. {==============================================================================}
  159.  
  160. function CDateTime(t: TDateTime): string;
  161. begin
  162.   SaveNames;
  163.   try
  164.     Result := FormatDateTime('mmm dd hh:mm:ss', t);
  165.     if Result[5] = '0' then
  166.       Result[5] := ' ';
  167.   finally
  168.     RestoreNames;
  169.   end;
  170. end;
  171.  
  172. {==============================================================================}
  173.  
  174. function CodeInt(Value: Word): string;
  175. begin
  176.   Result := Chr(Hi(Value)) + Chr(Lo(Value))
  177. end;
  178.  
  179. {==============================================================================}
  180.  
  181. function DecodeInt(const Value: string; Index: Integer): Word;
  182. var
  183.   x, y: Byte;
  184. begin
  185.   if Length(Value) > Index then
  186.     x := Ord(Value[Index])
  187.   else
  188.     x := 0;
  189.   if Length(Value) >= (Index + 1) then
  190.     y := Ord(Value[Index + 1])
  191.   else
  192.     y := 0;
  193.   Result := x * 256 + y;
  194. end;
  195.  
  196. {==============================================================================}
  197.  
  198. function IsIP(const Value: string): Boolean;
  199. var
  200.   n, x: Integer;
  201. begin
  202.   Result := true;
  203.   x := 0;
  204.   for n := 1 to Length(Value) do
  205.     if not (Value[n] in ['0'..'9', '.']) then
  206.     begin
  207.       Result := False;
  208.       Break;
  209.     end
  210.     else
  211.     begin
  212.       if Value[n] = '.' then
  213.         Inc(x);
  214.     end;
  215.   if x <> 3 then
  216.     Result := False;
  217. end;
  218.  
  219. {==============================================================================}
  220.  
  221. function ReverseIP(Value: string): string;
  222. var
  223.   x: Integer;
  224. begin
  225.   Result := '';
  226.   repeat
  227.     x := LastDelimiter('.', Value);
  228.     Result := Result + '.' + Copy(Value, x + 1, Length(Value) - x);
  229.     Delete(Value, x, Length(Value) - x + 1);
  230.   until x < 1;
  231.   if Length(Result) > 0 then
  232.     if Result[1] = '.' then
  233.       Delete(Result, 1, 1);
  234. end;
  235.  
  236. {==============================================================================}
  237. //Hernan Sanchez
  238. function IPToID(Host: string): string;
  239. var
  240.   s, t: string;
  241.   i, x: Integer;
  242. begin
  243.   Result := '';
  244.   for x := 1 to 3 do
  245.   begin
  246.     t := '';
  247.     s := StrScan(PChar(Host), '.');
  248.     t := Copy(Host, 1, (Length(Host) - Length(s)));
  249.     Delete(Host, 1, (Length(Host) - Length(s) + 1));
  250.     i := StrToIntDef(t, 0);
  251.     Result := Result + Chr(i);
  252.   end;
  253.   i := StrToIntDef(Host, 0);
  254.   Result := Result + Chr(i);
  255. end;
  256.  
  257. {==============================================================================}
  258.  
  259. procedure Dump(const Buffer, DumpFile: string);
  260. var
  261.   n: Integer;
  262.   s: string;
  263.   f: Text;
  264. begin
  265.   s := '';
  266.   for n := 1 to Length(Buffer) do
  267.     s := s + ' +#$' + IntToHex(Ord(Buffer[n]), 2);
  268.   AssignFile(f, DumpFile);
  269.   if FileExists(DumpFile) then
  270.     DeleteFile(PChar(DumpFile));
  271.   Rewrite(f);
  272.   try
  273.     Writeln(f, s);
  274.   finally
  275.     CloseFile(f);
  276.   end;
  277. end;
  278.  
  279. {==============================================================================}
  280.  
  281. function SeparateLeft(const Value, Delimiter: string): string;
  282. var
  283.   x: Integer;
  284. begin
  285.   x := Pos(Delimiter, Value);
  286.   if x < 1 then
  287.     Result := Trim(Value)
  288.   else
  289.     Result := Trim(Copy(Value, 1, x - 1));
  290. end;
  291.  
  292. {==============================================================================}
  293.  
  294. function SeparateRight(const Value, Delimiter: string): string;
  295. var
  296.   x: Integer;
  297. begin
  298.   x := Pos(Delimiter, Value);
  299.   if x > 0 then
  300.     x := x + Length(Delimiter) - 1;
  301.   Result := Trim(Copy(Value, x + 1, Length(Value) - x));
  302. end;
  303.  
  304. {==============================================================================}
  305.  
  306. function GetParameter(const Value, Parameter: string): string;
  307. var
  308.   x, x1: Integer;
  309.   s: string;
  310. begin
  311.   x := Pos(UpperCase(Parameter), UpperCase(Value));
  312.   Result := '';
  313.   if x > 0 then
  314.   begin
  315.     s := Copy(Value, x + Length(Parameter), Length(Value)
  316.       - (x + Length(Parameter)) + 1);
  317.     s := Trim(s);
  318.     x1 := Length(s);
  319.     if Length(s) > 1 then
  320.     begin
  321.       if s[1] = '"' then
  322.       begin
  323.         s := Copy(s, 2, Length(s) - 1);
  324.         x := Pos('"', s);
  325.         if x > 0 then
  326.           x1 := x - 1;
  327.       end
  328.       else
  329.       begin
  330.         x := Pos(' ', s);
  331.         if x > 0 then
  332.           x1 := x - 1;
  333.       end;
  334.     end;
  335.     Result := Copy(s, 1, x1);
  336.   end;
  337. end;
  338.  
  339. {==============================================================================}
  340.  
  341. function GetEmailAddr(const Value: string): string;
  342. var
  343.   s: string;
  344. begin
  345.   s := SeparateRight(Value, '<');
  346.   s := SeparateLeft(s, '>');
  347.   Result := Trim(s);
  348. end;
  349.  
  350. {==============================================================================}
  351.  
  352. function GetEmailDesc(Value: string): string;
  353. var
  354.   s: string;
  355. begin
  356.   Value := Trim(Value);
  357.   s := SeparateRight(Value, '"');
  358.   if s <> Value then
  359.     s := SeparateLeft(s, '"')
  360.   else
  361.   begin
  362.     s := SeparateRight(Value, '(');
  363.     if s <> Value then
  364.       s := SeparateLeft(s, ')')
  365.     else
  366.     begin
  367.       s := SeparateLeft(Value, '<');
  368.       if s = Value then
  369.         s := '';
  370.     end;
  371.   end;
  372.   Result := Trim(s);
  373. end;
  374.  
  375. {==============================================================================}
  376.  
  377. function StrToHex(const Value: string): string;
  378. var
  379.   n: Integer;
  380. begin
  381.   Result := '';
  382.   for n := 1 to Length(Value) do
  383.     Result := Result + IntToHex(Byte(Value[n]), 2);
  384.   Result := LowerCase(Result);
  385. end;
  386.  
  387. {==============================================================================}
  388.  
  389. function IntToBin(Value: Integer; Digits: Byte): string;
  390. var
  391.   x, y, n: Integer;
  392. begin
  393.   Result := '';
  394.   x := Value;
  395.   repeat
  396.     y := x mod 2;
  397.     x := x div 2;
  398.     if y > 0 then
  399.       Result := '1' + Result
  400.     else
  401.       Result := '0' + Result;
  402.   until x = 0;
  403.   x := Length(Result);
  404.   for n := x to Digits - 1 do
  405.     Result := '0' + Result;
  406. end;
  407.  
  408. {==============================================================================}
  409.  
  410. function BinToInt(const Value: string): Integer;
  411. var
  412.   n: Integer;
  413. begin
  414.   Result := 0;
  415.   for n := 1 to Length(Value) do
  416.   begin
  417.     if Value[n] = '0' then
  418.       Result := Result * 2
  419.     else
  420.       if Value[n] = '1' then
  421.         Result := Result * 2 + 1
  422.       else
  423.         Break;
  424.   end;
  425. end;
  426.  
  427. {==============================================================================}
  428.  
  429. function ParseURL(URL: string; var Prot, User, Pass, Host, Port, Path,
  430.   Para: string): string;
  431. var
  432.   x: Integer;
  433.   sURL: string;
  434.   s: string;
  435.   s1, s2: string;
  436. begin
  437.   Prot := 'http';
  438.   User := '';
  439.   Pass := '';
  440.   Port := '80';
  441.   Para := '';
  442.  
  443.   x := Pos('://', URL);
  444.   if x > 0 then
  445.   begin
  446.     Prot := SeparateLeft(URL, '://');
  447.     sURL := SeparateRight(URL, '://');
  448.   end
  449.   else
  450.     sURL := URL;
  451.   x := Pos('@', sURL);
  452.   if x > 0 then
  453.   begin
  454.     s := SeparateLeft(sURL, '@');
  455.     sURL := SeparateRight(sURL, '@');
  456.     x := Pos(':', s);
  457.     if x > 0 then
  458.     begin
  459.       User := SeparateLeft(s, ':');
  460.       Pass := SeparateRight(s, ':');
  461.     end
  462.     else
  463.       User := s;
  464.   end;
  465.   x := Pos('/', sURL);
  466.   if x > 0 then
  467.   begin
  468.     s1 := SeparateLeft(sURL, '/');
  469.     s2 := SeparateRight(sURL, '/');
  470.   end
  471.   else
  472.   begin
  473.     s1 := sURL;
  474.     s2 := '';
  475.   end;
  476.   x := Pos(':', s1);
  477.   if x > 0 then
  478.   begin
  479.     Host := SeparateLeft(s1, ':');
  480.     Port := SeparateRight(s1, ':');
  481.   end
  482.   else
  483.     Host := s1;
  484.   Result := '/' + s2;
  485.   x := Pos('?', s2);
  486.   if x > 0 then
  487.   begin
  488.     Path := '/' + SeparateLeft(s2, '?');
  489.     Para := SeparateRight(s2, '?');
  490.   end
  491.   else
  492.     Path := '/' + s2;
  493.   if Host = '' then
  494.     Host := 'localhost';
  495. end;
  496.  
  497. {==============================================================================}
  498.  
  499. function StringReplace(Value, Search, Replace: string): string;
  500. var
  501.   x, l, ls, lr: Integer;
  502. begin
  503.   if (Value = '') or (Search = '') then
  504.   begin
  505.     Result := Value;
  506.     Exit;
  507.   end;
  508.   ls := Length(Search);
  509.   lr := Length(Replace);
  510.   Result := '';
  511.   x := Pos(Search, Value);
  512.   while x > 0 do
  513.   begin
  514.     l := Length(Result);
  515.     SetLength(Result, l + x - 1);
  516.     Move(Pointer(Value)^, Pointer(@Result[l + 1])^, x - 1);
  517. //      Result:=Result+Copy(Value,1,x-1);
  518.     l := Length(Result);
  519.     SetLength(Result, l + lr);
  520.     Move(Pointer(Replace)^, Pointer(@Result[l + 1])^, lr);
  521. //      Result:=Result+Replace;
  522.     Delete(Value, 1, x - 1 + ls);
  523.     x := Pos(Search, Value);
  524.   end;
  525.   Result := Result + Value;
  526. end;
  527.  
  528. {==============================================================================}
  529.  
  530. function RPos(const Sub, Value: String): Integer;
  531. var
  532.   n: Integer;
  533.   l: Integer;
  534. begin
  535.   result := 0;
  536.   l := Length(Sub);
  537.   for n := Length(Value) - l + 1 downto 1 do
  538.   begin
  539.     if Copy(Value, n, l) = Sub then
  540.     begin
  541.       result := n;
  542.       break;
  543.     end;
  544.   end;
  545. end;
  546.  
  547. {==============================================================================}
  548.  
  549. function Fetch(var Value: string; const Delimiter: string): string;
  550. begin
  551.   Result := SeparateLeft(Value, Delimiter);
  552.   Value := SeparateRight(Value, Delimiter);
  553. end;
  554.  
  555. end.
  556.