3,7 → 3,7 |
(************************************************) |
(* VTSFUNCS.PAS *) |
(* Author: Daniel Marschall *) |
(* Revision: 2022-02-14 *) |
(* Revision: 2022-02-13 *) |
(* License: Apache 2.0 *) |
(* This file contains: *) |
(* - Various functions *) |
11,23 → 11,14 |
|
interface |
|
function Max(a, b: integer): integer; |
function Min(a, b: integer): integer; |
|
function CompareEqualLengthString(a, b: string): integer; |
function CompareNumericString(a, b: string): integer; |
|
procedure Beep; |
|
function Trim(s: string): string; |
function IsNumeric(s: string): boolean; |
function ZeroPad(i: LongInt; n: integer): string; |
function LeftPadStr(s: string; n: integer; ch: char): string; |
function RightPadStr(s: string; n: integer; ch: char): string; |
|
function DeleteFile(filename: string): boolean; |
procedure DeleteFile(filename: string); |
function FileExists(filename: string): boolean; |
|
function IsPositiveInteger(s: string): boolean; |
function StrToInt(s: string): Integer; |
function IntToStr(Value: Integer): string; |
|
36,22 → 27,6 |
uses |
Crt; |
|
function Max(a, b: integer): integer; |
begin |
if a > b then |
Max := a |
else |
Max := b; |
end; |
|
function Min(a, b: integer): integer; |
begin |
if a < b then |
Min := a |
else |
Min := b; |
end; |
|
function CompareEqualLengthString(a, b: string): integer; |
var |
ao, bo, i: integer; |
76,14 → 51,27 |
|
function CompareNumericString(a, b: string): integer; |
var |
EqualLength: integer; |
i, maxlen: integer; |
prefix_a, prefix_b: string; |
begin |
EqualLength := Max(Length(a), Length(b)); |
a := LeftPadStr(a, EqualLength, '0'); |
b := LeftPadStr(b, EqualLength, '0'); |
CompareNumericString := CompareEqualLengthString(a, b); |
maxlen := Length(a); |
if Length(b) > maxlen then maxlen := Length(b); |
|
prefix_a := ''; |
for i := 1 to maxlen-Length(a) do |
begin |
prefix_a := prefix_a + '0'; |
end; |
|
prefix_b := ''; |
for i := 1 to maxlen-Length(b) do |
begin |
prefix_b := prefix_b + '0'; |
end; |
|
CompareNumericString := CompareEqualLengthString(prefix_a+a, prefix_b+b); |
end; |
|
procedure Beep; |
begin |
Sound(220); (*220Hz*) |
110,53 → 98,60 |
Trim := s; |
end; |
|
function ZeroPad(i: LongInt; n: integer): string; |
function IsNumeric(s: string): boolean; |
var |
s: string; |
i: integer; |
begin |
s := IntToStr(i); |
ZeroPad := LeftPadStr(s, n, '0'); |
end; |
IsNumeric := false; |
|
function LeftPadStr(s: string; n: integer; ch: char): string; |
if Length(s) = 0 then exit; |
if (s[1] = '0') and (s <> '0') then exit; |
for i := 1 to Length(s) do |
begin |
while Length(s) < n do |
begin |
s := ch + s; |
if not (s[i] in ['0'..'9']) then exit; |
end; |
LeftPadStr := s; |
|
IsNumeric := true; |
end; |
|
function RightPadStr(s: string; n: integer; ch: char): string; |
function ZeroPad(i: LongInt; n: integer): string; |
var |
s: string; |
begin |
Str(i, s); |
while Length(s) < n do |
begin |
s := s + ch; |
s := '0' + s; |
end; |
RightPadStr := s; |
ZeroPad := s; |
end; |
|
function DeleteFile(filename: string): boolean; |
procedure DeleteFile(filename: string); |
var |
F: file; |
Ch: Char; |
begin |
{ Get file to delete from command line } |
Assign(F, filename); |
{$I-} |
Reset(F); |
{$I+} |
(* |
if IOResult <> 0 then |
begin |
DeleteFile := false; (* cannot find file *) |
end |
Writeln('Cannot find ', filename) |
else |
begin |
*) |
Close(F); |
{$I-} |
(* |
Write('Erase ', filename, '? '); |
Readln(Ch); |
if UpCase(CH) = 'Y' then |
*) |
Erase(F); |
{$I+} |
DeleteFile := IOResult = 0; |
(* |
end; |
*) |
end; |
|
function FileExists(filename: string): boolean; |
170,22 → 165,6 |
FileExists := IoResult = 0; |
end; |
|
function IsPositiveInteger(s: string): boolean; |
var |
i: integer; |
begin |
IsPositiveInteger := false; |
|
if Length(s) = 0 then exit; |
if (s[1] = '0') and (s <> '0') then exit; |
for i := 1 to Length(s) do |
begin |
if not (s[i] in ['0'..'9']) then exit; |
end; |
|
IsPositiveInteger := true; |
end; |
|
function StrToInt(s: string): Integer; |
var |
i, Error: Integer; |