Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
733 daniel-mar 1
unit VTSFUNCS;
2
 
3
(************************************************)
4
(* VTSFUNCS.PAS                                 *)
5
(* Author:   Daniel Marschall                   *)
739 daniel-mar 6
(* Revision: 2022-02-14                         *)
733 daniel-mar 7
(* License:  Apache 2.0                         *)
8
(* This file contains:                          *)
9
(* - Various functions                          *)
10
(************************************************)
11
 
12
interface
13
 
739 daniel-mar 14
function Max(a, b: integer): integer;
15
function Min(a, b: integer): integer;
16
 
733 daniel-mar 17
function CompareEqualLengthString(a, b: string): integer;
18
function CompareNumericString(a, b: string): integer;
739 daniel-mar 19
 
733 daniel-mar 20
procedure Beep;
739 daniel-mar 21
 
733 daniel-mar 22
function Trim(s: string): string;
740 daniel-mar 23
function TrimLineToWidth(s: string; width: integer): string;
733 daniel-mar 24
function ZeroPad(i: LongInt; n: integer): string;
739 daniel-mar 25
function LeftPadStr(s: string; n: integer; ch: char): string;
26
function RightPadStr(s: string; n: integer; ch: char): string;
740 daniel-mar 27
function RepeatStr(ch: char; n: integer): string;
739 daniel-mar 28
 
29
function DeleteFile(filename: string): boolean;
733 daniel-mar 30
function FileExists(filename: string): boolean;
739 daniel-mar 31
 
32
function IsPositiveInteger(s: string): boolean;
733 daniel-mar 33
function StrToInt(s: string): Integer;
34
function IntToStr(Value: Integer): string;
35
 
36
implementation
37
 
38
uses
39
  Crt;
40
 
739 daniel-mar 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
 
733 daniel-mar 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
739 daniel-mar 81
  EqualLength: integer;
733 daniel-mar 82
begin
739 daniel-mar 83
  EqualLength := Max(Length(a), Length(b));
84
  a := LeftPadStr(a, EqualLength, '0');
85
  b := LeftPadStr(b, EqualLength, '0');
86
  CompareNumericString := CompareEqualLengthString(a, b);
733 daniel-mar 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
 
740 daniel-mar 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
 
739 daniel-mar 125
function ZeroPad(i: LongInt; n: integer): string;
733 daniel-mar 126
var
739 daniel-mar 127
  s: string;
733 daniel-mar 128
begin
739 daniel-mar 129
  s := IntToStr(i);
130
  ZeroPad := LeftPadStr(s, n, '0');
131
end;
733 daniel-mar 132
 
739 daniel-mar 133
function LeftPadStr(s: string; n: integer; ch: char): string;
134
begin
135
  while Length(s) < n do
733 daniel-mar 136
  begin
739 daniel-mar 137
    s := ch + s;
733 daniel-mar 138
  end;
739 daniel-mar 139
  LeftPadStr := s;
733 daniel-mar 140
end;
141
 
739 daniel-mar 142
function RightPadStr(s: string; n: integer; ch: char): string;
733 daniel-mar 143
begin
144
  while Length(s) < n do
145
  begin
739 daniel-mar 146
    s := s + ch;
733 daniel-mar 147
  end;
739 daniel-mar 148
  RightPadStr := s;
733 daniel-mar 149
end;
150
 
740 daniel-mar 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
 
739 daniel-mar 164
function DeleteFile(filename: string): boolean;
733 daniel-mar 165
var
739 daniel-mar 166
  F:  file;
167
  Ch: Char;
733 daniel-mar 168
begin
739 daniel-mar 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;
733 daniel-mar 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+}
740 daniel-mar 195
  if IoResult = 0 then
196
  begin
197
    Close(F);
198
    FileExists := true;
199
  end
200
  else
201
  begin
202
    FileExists := false;
203
  end;
733 daniel-mar 204
end;
205
 
739 daniel-mar 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
 
733 daniel-mar 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.