Subversion Repositories oidplus

Rev

Rev 744 | Rev 748 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit VTSFUNCS;
  2.  
  3. (************************************************)
  4. (* VTSFUNCS.PAS                                 *)
  5. (* Author:   Daniel Marschall                   *)
  6. (* Revision: 2022-02-16                         *)
  7. (* License:  Apache 2.0                         *)
  8. (* This file contains:                          *)
  9. (* - Various functions                          *)
  10. (************************************************)
  11.  
  12. interface
  13.  
  14. function Max(a, b: integer): integer;
  15. function Min(a, b: integer): integer;
  16.  
  17. function CompareEqualLengthString(a, b: string): integer;
  18. function CompareNumericString(a, b: string): integer;
  19.  
  20. procedure Beep;
  21.  
  22. function Trim(s: string): string;
  23. function TrimLineToWidth(s: string; width: integer): string;
  24. function ZeroPad(i: LongInt; n: integer): string;
  25. function LeftPadStr(s: string; n: integer; ch: char): string;
  26. function RightPadStr(s: string; n: integer; ch: char): string;
  27. function RepeatStr(ch: char; n: integer): string;
  28.  
  29. function DeleteFile(filename: string): boolean;
  30. function FileExists(filename: string): boolean;
  31.  
  32. function IsPositiveIntegerOrZero(s: string): boolean;
  33. function StrToInt(s: string): Integer;
  34. function IntToStr(Value: Integer): string;
  35.  
  36. function StringReplace(s, search, replace: string): string;
  37.  
  38. implementation
  39.  
  40. uses
  41.   Crt;
  42.  
  43. function Max(a, b: integer): integer;
  44. begin
  45.   if a > b then
  46.     Max := a
  47.   else
  48.     Max := b;
  49. end;
  50.  
  51. function Min(a, b: integer): integer;
  52. begin
  53.   if a < b then
  54.     Min := a
  55.   else
  56.     Min := b;
  57. end;
  58.  
  59. function CompareEqualLengthString(a, b: string): integer;
  60. var
  61.   ao, bo, i: integer;
  62. begin
  63.   CompareEqualLengthString := 0;
  64.   for i := 1 to Length(a) do
  65.   begin
  66.     ao := Ord(a[i]);
  67.     bo := Ord(b[i]);
  68.     if ao > bo then
  69.     begin
  70.       CompareEqualLengthString := 1;
  71.       break;
  72.     end;
  73.     if ao < bo then
  74.     begin
  75.       CompareEqualLengthString := -1;
  76.       break;
  77.     end;
  78.   end;
  79. end;
  80.  
  81. function CompareNumericString(a, b: string): integer;
  82. var
  83.   EqualLength: integer;
  84. begin
  85.   EqualLength := Max(Length(a), Length(b));
  86.   a := LeftPadStr(a, EqualLength, '0');
  87.   b := LeftPadStr(b, EqualLength, '0');
  88.   CompareNumericString := CompareEqualLengthString(a, b);
  89. end;
  90.  
  91. procedure Beep;
  92. begin
  93.   Sound(220); (*220Hz*)
  94.   Delay(200); (*200ms*)
  95.   NoSound;
  96. end;
  97.  
  98. function Trim(s: string): string;
  99. begin
  100.   while Length(s) > 0 do
  101.   begin
  102.     if s[1] in [#9,#10,#13,' '] then
  103.       Delete(s,1,1)
  104.     else
  105.       break;
  106.   end;
  107.   while Length(s) > 0 do
  108.   begin
  109.     if s[Length(s)] in [#9,#10,#13,' '] then
  110.       Delete(s,Length(s),1)
  111.     else
  112.       break;
  113.   end;
  114.   Trim := s;
  115. end;
  116.  
  117. function TrimLineToWidth(s: string; width: integer): string;
  118. begin
  119.   if Length(s) > width then
  120.   begin
  121.     s := Copy(s, 1, width-3) + '...';
  122.   end;
  123.   TrimLineToWidth := s;
  124. end;
  125.  
  126. function ZeroPad(i: LongInt; n: integer): string;
  127. var
  128.   s: string;
  129. begin
  130.   s := IntToStr(i);
  131.   ZeroPad := LeftPadStr(s, n, '0');
  132. end;
  133.  
  134. function LeftPadStr(s: string; n: integer; ch: char): string;
  135. begin
  136.   while Length(s) < n do
  137.   begin
  138.     s := ch + s;
  139.   end;
  140.   LeftPadStr := s;
  141. end;
  142.  
  143. function RightPadStr(s: string; n: integer; ch: char): string;
  144. begin
  145.   while Length(s) < n do
  146.   begin
  147.     s := s + ch;
  148.   end;
  149.   RightPadStr := s;
  150. end;
  151.  
  152. function RepeatStr(ch: char; n: integer): string;
  153. var
  154.   i: integer;
  155.   res: string;
  156. begin
  157.   res := '';
  158.   for i := 1 to n do
  159.   begin
  160.     res := res + ch;
  161.   end;
  162.   RepeatStr := res;
  163. end;
  164.  
  165. function DeleteFile(filename: string): boolean;
  166. var
  167.   F:  file;
  168.   Ch: Char;
  169. begin
  170.   Assign(F, filename);
  171.   {$I-}
  172.   Reset(F);
  173.   {$I+}
  174.   if IOResult <> 0 then
  175.   begin
  176.     DeleteFile := false; (* cannot find file *)
  177.   end
  178.   else
  179.   begin
  180.     Close(F);
  181.     {$I-}
  182.     Erase(F);
  183.     {$I+}
  184.     DeleteFile := IOResult = 0;
  185.   end;
  186. end;
  187.  
  188. function FileExists(filename: string): boolean;
  189. var
  190.   F: Text;
  191. begin
  192.   Assign(F, filename);
  193.   {$I-}
  194.   Reset(F);
  195.   {$I+}
  196.   if IoResult = 0 then
  197.   begin
  198.     Close(F);
  199.     FileExists := true;
  200.   end
  201.   else
  202.   begin
  203.     FileExists := false;
  204.   end;
  205. end;
  206.  
  207. function IsPositiveIntegerOrZero(s: string): boolean;
  208. var
  209.   i: integer;
  210. begin
  211.   IsPositiveIntegerOrZero := false;
  212.  
  213.   if Length(s) = 0 then exit;
  214.   if (s[1] = '0') and (s <> '0') then exit;
  215.   for i := 1 to Length(s) do
  216.   begin
  217.     if not (s[i] in ['0'..'9']) then exit;
  218.   end;
  219.  
  220.   IsPositiveIntegerOrZero := true;
  221. end;
  222.  
  223. function StrToInt(s: string): Integer;
  224. var
  225.   i, Error: Integer;
  226. begin
  227.   Val(s, i, Error);
  228.   StrToInt := i;
  229. end;
  230.  
  231. function IntToStr(Value: Integer): string;
  232. var
  233.   s: string;
  234. begin
  235.   Str(Value, s);
  236.   IntToStr := s;
  237. end;
  238.  
  239. function StringReplace(s, search, replace: string): string;
  240. var
  241.   i: integer;
  242.   output: string;
  243. begin
  244.   if s = '' then exit;
  245.   if search = '' then exit; (* invalid arg *)
  246.  
  247.   output := '';
  248.   while s <> '' do
  249.   begin
  250.     if Copy(s, 1, Length(search)) = search then
  251.     begin
  252.       output := output + replace;
  253.       Delete(s, 1, Length(search));
  254.     end
  255.     else
  256.     begin
  257.       output := output + Copy(s, 1, 1);
  258.       Delete(s, 1, 1);
  259.     end;
  260.   end;
  261.  
  262.   StringReplace := output;
  263. end;
  264.  
  265. end.
  266.