2020-05-25 22:48:33 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
Copyright (c) 2018-2021, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE PATHS;
|
|
|
|
|
|
|
|
IMPORT STRINGS, UTILS;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
slash = UTILS.slash;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PATHLEN = 2048;
|
2019-09-26 22:23:06 +02:00
|
|
|
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
TYPE
|
|
|
|
|
|
|
|
PATH* = ARRAY PATHLEN OF CHAR;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE split* (fname: ARRAY OF CHAR; VAR path, name, ext: ARRAY OF CHAR);
|
|
|
|
VAR
|
|
|
|
pos1, pos2, len: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
len := LENGTH(fname);
|
|
|
|
pos1 := len - 1;
|
2021-06-15 19:33:16 +02:00
|
|
|
pos2 := pos1;
|
2019-03-11 09:59:55 +01:00
|
|
|
STRINGS.search(fname, pos1, slash, FALSE);
|
|
|
|
STRINGS.search(fname, pos2, ".", FALSE);
|
|
|
|
|
|
|
|
path := fname;
|
|
|
|
path[pos1 + 1] := 0X;
|
|
|
|
|
|
|
|
IF (pos2 = -1) OR (pos2 < pos1) THEN
|
|
|
|
pos2 := len
|
|
|
|
END;
|
|
|
|
|
|
|
|
INC(pos1);
|
|
|
|
|
|
|
|
STRINGS.copy(fname, name, pos1, 0, pos2 - pos1);
|
|
|
|
name[pos2 - pos1] := 0X;
|
|
|
|
STRINGS.copy(fname, ext, pos2, 0, len - pos2);
|
2021-06-15 19:33:16 +02:00
|
|
|
ext[len - pos2] := 0X
|
2019-03-11 09:59:55 +01:00
|
|
|
END split;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE RelPath* (absolute, relative: ARRAY OF CHAR; VAR res: ARRAY OF CHAR);
|
|
|
|
VAR
|
|
|
|
i, j: INTEGER;
|
|
|
|
error: BOOLEAN;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
COPY(absolute, res);
|
|
|
|
i := LENGTH(res) - 1;
|
|
|
|
WHILE (i >= 0) & (res[i] # slash) DO
|
|
|
|
DEC(i)
|
|
|
|
END;
|
|
|
|
|
|
|
|
INC(i);
|
|
|
|
res[i] := 0X;
|
|
|
|
|
|
|
|
error := FALSE;
|
|
|
|
j := 0;
|
2021-06-15 19:33:16 +02:00
|
|
|
WHILE (relative[j] = ".") & (relative[j + 1] = slash) DO
|
|
|
|
INC(j, 2)
|
|
|
|
ELSIF relative[j] = slash DO
|
|
|
|
INC(j)
|
|
|
|
END;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
WHILE ~error & (relative[j] # 0X) DO
|
|
|
|
IF (relative[j] = ".") & (relative[j + 1] = ".") & (relative[j + 2] = slash) & (i > 0) & (res[i - 1] = slash) THEN
|
|
|
|
DEC(i, 2);
|
|
|
|
WHILE (i >= 0) & (res[i] # slash) DO
|
|
|
|
DEC(i)
|
|
|
|
END;
|
|
|
|
IF i < 0 THEN
|
|
|
|
error := TRUE
|
|
|
|
ELSE
|
|
|
|
INC(i);
|
|
|
|
INC(j, 3)
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
res[i] := relative[j];
|
|
|
|
INC(i);
|
|
|
|
INC(j)
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF error THEN
|
|
|
|
COPY(relative, res)
|
|
|
|
ELSE
|
|
|
|
res[i] := 0X
|
|
|
|
END
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
END RelPath;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE DelSlashes* (VAR path: ARRAY OF CHAR);
|
|
|
|
VAR
|
|
|
|
i, j, k: INTEGER;
|
|
|
|
c: CHAR;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
i := 0;
|
|
|
|
j := 0;
|
|
|
|
k := 0;
|
|
|
|
REPEAT
|
|
|
|
c := path[j];
|
|
|
|
INC(j);
|
|
|
|
IF c = slash THEN
|
|
|
|
INC(k)
|
|
|
|
ELSE
|
|
|
|
k := 0
|
|
|
|
END;
|
|
|
|
IF k <= 1 THEN
|
|
|
|
path[i] := c;
|
|
|
|
INC(i)
|
|
|
|
END
|
|
|
|
UNTIL c = 0X;
|
|
|
|
|
|
|
|
i := 0;
|
|
|
|
j := 0;
|
|
|
|
REPEAT
|
|
|
|
c := path[j];
|
|
|
|
INC(j);
|
|
|
|
path[i] := c;
|
|
|
|
INC(i);
|
|
|
|
IF (c = slash) & (path[j] = ".") & (path[j + 1] = slash) THEN
|
|
|
|
INC(j, 2)
|
|
|
|
END
|
|
|
|
UNTIL c = 0X
|
|
|
|
END DelSlashes;
|
|
|
|
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE isRelative* (path: ARRAY OF CHAR): BOOLEAN;
|
|
|
|
RETURN UTILS.isRelative(path)
|
2019-09-26 22:23:06 +02:00
|
|
|
END isRelative;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GetCurrentDirectory* (VAR path: ARRAY OF CHAR);
|
|
|
|
BEGIN
|
|
|
|
UTILS.GetCurrentDirectory(path)
|
|
|
|
END GetCurrentDirectory;
|
|
|
|
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
END PATHS.
|