Subversion Repositories oidplus

Rev

Rev 739 | Rev 747 | 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-14                         *)
  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 IsPositiveInteger(s: string): boolean;
  33. function StrToInt(s: string): Integer;
  34. function IntToStr(Value: Integer): string;
  35.  
  36. implementation
  37.  
  38. uses
  39.   Crt;
  40.  
  41. function Max(a, b: integer): integer;
  42. begin
  43.   if a > b then
  44.     Max := a
  45.   else
  46.     Max := b;
  47. end;
  48.  
  49. function Min(a, b: integer): integer;
  50. begin
  51.   if a < b then
  52.     Min := a
  53.   else
  54.     Min := b;
  55. end;
  56.  
  57. function CompareEqualLengthString(a, b: string): integer;
  58. var
  59.   ao, bo, i: integer;
  60. begin
  61.   CompareEqualLengthString := 0;
  62.   for i := 1 to Length(a) do
  63.   begin
  64.     ao := Ord(a[i]);
  65.     bo := Ord(b[i]);
  66.     if ao > bo then
  67.     begin
  68.       CompareEqualLengthString := 1;
  69.       break;
  70.     end;
  71.     if ao < bo then
  72.     begin
  73.       CompareEqualLengthString := -1;
  74.       break;
  75.     end;
  76.   end;
  77. end;
  78.  
  79. function CompareNumericString(a, b: string): integer;
  80. var
  81.   EqualLength: integer;
  82. begin
  83.   EqualLength := Max(Length(a), Length(b));
  84.   a := LeftPadStr(a, EqualLength, '0');
  85.   b := LeftPadStr(b, EqualLength, '0');
  86.   CompareNumericString := CompareEqualLengthString(a, b);
  87. end;
  88.  
  89. procedure Beep;
  90. begin
  91.   Sound(220); (*220Hz*)
  92.   Delay(200); (*200ms*)
  93.   NoSound;
  94. end;
  95.  
  96. function Trim(s: string): string;
  97. begin
  98.   while Length(s) > 0 do
  99.   begin
  100.     if s[1] in [#9,#10,#13,' '] then
  101.       Delete(s,1,1)
  102.     else
  103.       break;
  104.   end;
  105.   while Length(s) > 0 do
  106.   begin
  107.     if s[Length(s)] in [#9,#10,#13,' '] then
  108.       Delete(s,Length(s),1)
  109.     else
  110.       break;
  111.   end;
  112.   Trim := s;
  113. end;
  114.  
  115. function TrimLineToWidth(s: string; width: integer): string;
  116. begin
  117.   (* TODO: Put into VTSUTILS, also use for VTSCUI menu *)
  118.   if Length(s) > width then
  119.   begin
  120.     s := Copy(s, 1, width-3) + '...';
  121.   end;
  122.   TrimLineToWidth := s;
  123. end;
  124.  
  125. function ZeroPad(i: LongInt; n: integer): string;
  126. var
  127.   s: string;
  128. begin
  129.   s := IntToStr(i);
  130.   ZeroPad := LeftPadStr(s, n, '0');
  131. end;
  132.  
  133. function LeftPadStr(s: string; n: integer; ch: char): string;
  134. begin
  135.   while Length(s) < n do
  136.   begin
  137.     s := ch + s;
  138.   end;
  139.   LeftPadStr := s;
  140. end;
  141.  
  142. function RightPadStr(s: string; n: integer; ch: char): string;
  143. begin
  144.   while Length(s) < n do
  145.   begin
  146.     s := s + ch;
  147.   end;
  148.   RightPadStr := s;
  149. end;
  150.  
  151. function RepeatStr(ch: char; n: integer): string;
  152. var
  153.   i: integer;
  154.   res: string;
  155. begin
  156.   res := '';
  157.   for i := 1 to n do
  158.   begin
  159.     res := res + ch;
  160.   end;
  161.   RepeatStr := res;
  162. end;
  163.  
  164. function DeleteFile(filename: string): boolean;
  165. var
  166.   F:  file;
  167.   Ch: Char;
  168. begin
  169.   Assign(F, filename);
  170.   {$I-}
  171.   Reset(F);
  172.   {$I+}
  173.   if IOResult <> 0 then
  174.   begin
  175.     DeleteFile := false; (* cannot find file *)
  176.   end
  177.   else
  178.   begin
  179.     Close(F);
  180.     {$I-}
  181.     Erase(F);
  182.     {$I+}
  183.     DeleteFile := IOResult = 0;
  184.   end;
  185. end;
  186.  
  187. function FileExists(filename: string): boolean;
  188. var
  189.   F: Text;
  190. begin
  191.   Assign(F, filename);
  192.   {$I-}
  193.   Reset(F);
  194.   {$I+}
  195.   if IoResult = 0 then
  196.   begin
  197.     Close(F);
  198.     FileExists := true;
  199.   end
  200.   else
  201.   begin
  202.     FileExists := false;
  203.   end;
  204. end;
  205.  
  206. function IsPositiveInteger(s: string): boolean;
  207. var
  208.   i: integer;
  209. begin
  210.   IsPositiveInteger := false;
  211.  
  212.   if Length(s) = 0 then exit;
  213.   if (s[1] = '0') and (s <> '0') then exit;
  214.   for i := 1 to Length(s) do
  215.   begin
  216.     if not (s[i] in ['0'..'9']) then exit;
  217.   end;
  218.  
  219.   IsPositiveInteger := true;
  220. end;
  221.  
  222. function StrToInt(s: string): Integer;
  223. var
  224.   i, Error: Integer;
  225. begin
  226.   Val(s, i, Error);
  227.   StrToInt := i;
  228. end;
  229.  
  230. function IntToStr(Value: Integer): string;
  231. var
  232.   s: string;
  233. begin
  234.   Str(Value, s);
  235.   IntToStr := s;
  236. end;
  237.  
  238. end.
  239.