2020-05-25 22:48:33 +02:00
|
|
|
(*
|
|
|
|
BSD 2-Clause License
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
Copyright (c) 2019-2020, Anton Krotov
|
2020-05-25 22:48:33 +02:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE File;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
IMPORT SYSTEM, WINAPI, API;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
OPEN_R* = 0; OPEN_W* = 1; OPEN_RW* = 2;
|
|
|
|
SEEK_BEG* = 0; SEEK_CUR* = 1; SEEK_END* = 2;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Exists* (FName: ARRAY OF CHAR): BOOLEAN;
|
|
|
|
VAR
|
|
|
|
FindData: WINAPI.TWin32FindData;
|
|
|
|
Handle: INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
attr: SET;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
Handle := WINAPI.FindFirstFileA(SYSTEM.ADR(FName[0]), FindData);
|
2020-05-25 22:48:33 +02:00
|
|
|
IF Handle # -1 THEN
|
|
|
|
WINAPI.FindClose(Handle);
|
2020-10-13 09:58:51 +02:00
|
|
|
SYSTEM.GET32(SYSTEM.ADR(FindData.dwFileAttributes), attr);
|
|
|
|
IF 4 IN attr THEN
|
2020-05-25 22:48:33 +02:00
|
|
|
Handle := -1
|
|
|
|
END
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN Handle # -1
|
|
|
|
END Exists;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Delete* (FName: ARRAY OF CHAR): BOOLEAN;
|
2020-10-13 09:58:51 +02:00
|
|
|
RETURN WINAPI.DeleteFileA(SYSTEM.ADR(FName[0])) # 0
|
2020-05-25 22:48:33 +02:00
|
|
|
END Delete;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Create* (FName: ARRAY OF CHAR): INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
RETURN WINAPI.CreateFileA(SYSTEM.ADR(FName[0]), 0C0000000H, 0, NIL, 2, 80H, 0)
|
2020-05-25 22:48:33 +02:00
|
|
|
END Create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Close* (F: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
WINAPI.CloseHandle(F)
|
|
|
|
END Close;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Open* (FName: ARRAY OF CHAR; Mode: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
ofstr: WINAPI.OFSTRUCT;
|
|
|
|
BEGIN
|
|
|
|
RETURN WINAPI.OpenFile(SYSTEM.ADR(FName[0]), ofstr, Mode)
|
|
|
|
END Open;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Seek* (F, Offset, Origin: INTEGER): INTEGER;
|
|
|
|
RETURN WINAPI.SetFilePointer(F, Offset, 0, Origin)
|
|
|
|
END Seek;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Read* (F, Buffer, Count: INTEGER): INTEGER;
|
|
|
|
VAR
|
2020-10-13 09:58:51 +02:00
|
|
|
res: INTEGER;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
IF WINAPI.ReadFile(F, Buffer, Count, SYSTEM.ADR(res), NIL) = 0 THEN
|
2020-05-25 22:48:33 +02:00
|
|
|
res := -1
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END Read;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Write* (F, Buffer, Count: INTEGER): INTEGER;
|
|
|
|
VAR
|
2020-10-13 09:58:51 +02:00
|
|
|
res: INTEGER;
|
2020-05-25 22:48:33 +02:00
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
IF WINAPI.WriteFile(F, Buffer, Count, SYSTEM.ADR(res), NIL) = 0 THEN
|
2020-05-25 22:48:33 +02:00
|
|
|
res := -1
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END Write;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Load* (FName: ARRAY OF CHAR; VAR Size: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
res, n, F: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
res := 0;
|
|
|
|
F := Open(FName, OPEN_R);
|
|
|
|
|
|
|
|
IF F # -1 THEN
|
|
|
|
Size := Seek(F, 0, SEEK_END);
|
|
|
|
n := Seek(F, 0, SEEK_BEG);
|
2020-10-13 09:58:51 +02:00
|
|
|
res := API._NEW(Size);
|
2020-05-25 22:48:33 +02:00
|
|
|
IF (res = 0) OR (Read(F, res, Size) # Size) THEN
|
|
|
|
IF res # 0 THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
res := API._DISPOSE(res);
|
2020-05-25 22:48:33 +02:00
|
|
|
Size := 0
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
Close(F)
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END Load;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE RemoveDir* (DirName: ARRAY OF CHAR): BOOLEAN;
|
2020-10-13 09:58:51 +02:00
|
|
|
RETURN WINAPI.RemoveDirectoryA(SYSTEM.ADR(DirName[0])) # 0
|
2020-05-25 22:48:33 +02:00
|
|
|
END RemoveDir;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ExistsDir* (DirName: ARRAY OF CHAR): BOOLEAN;
|
|
|
|
VAR
|
|
|
|
Code: SET;
|
|
|
|
|
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
Code := WINAPI.GetFileAttributesA(SYSTEM.ADR(DirName[0]))
|
2020-05-25 22:48:33 +02:00
|
|
|
RETURN (Code # {0..31}) & (4 IN Code)
|
|
|
|
END ExistsDir;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE CreateDir* (DirName: ARRAY OF CHAR): BOOLEAN;
|
2020-10-13 09:58:51 +02:00
|
|
|
RETURN WINAPI.CreateDirectoryA(SYSTEM.ADR(DirName[0]), NIL) # 0
|
2020-05-25 22:48:33 +02:00
|
|
|
END CreateDir;
|
|
|
|
|
|
|
|
|
|
|
|
END File.
|