2020-05-25 22:48:33 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2023-01-21 15:34:25 +01:00
|
|
|
Copyright (c) 2018-2022, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE FILES;
|
|
|
|
|
2023-01-21 15:34:25 +01:00
|
|
|
IMPORT UTILS, C := COLLECTIONS;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
FILE* = POINTER TO RECORD (C.ITEM)
|
|
|
|
|
|
|
|
ptr: INTEGER;
|
|
|
|
|
|
|
|
buffer: ARRAY 64*1024 OF BYTE;
|
2020-10-13 09:58:51 +02:00
|
|
|
count: INTEGER
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
files: C.COLLECTION;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE copy (src: ARRAY OF BYTE; src_idx: INTEGER; VAR dst: ARRAY OF BYTE; dst_idx: INTEGER; bytes: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
WHILE bytes > 0 DO
|
|
|
|
dst[dst_idx] := src[src_idx];
|
|
|
|
INC(dst_idx);
|
|
|
|
INC(src_idx);
|
|
|
|
DEC(bytes)
|
|
|
|
END
|
|
|
|
END copy;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE flush (file: FILE): INTEGER;
|
|
|
|
VAR
|
|
|
|
res: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
IF file # NIL THEN
|
|
|
|
res := UTILS.FileWrite(file.ptr, file.buffer, file.count);
|
|
|
|
IF res < 0 THEN
|
|
|
|
res := 0
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
res := 0
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END flush;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE NewFile (): FILE;
|
|
|
|
VAR
|
|
|
|
file: FILE;
|
|
|
|
citem: C.ITEM;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
citem := C.pop(files);
|
|
|
|
IF citem = NIL THEN
|
|
|
|
NEW(file)
|
|
|
|
ELSE
|
|
|
|
file := citem(FILE)
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN file
|
|
|
|
END NewFile;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE create* (name: ARRAY OF CHAR): FILE;
|
|
|
|
VAR
|
|
|
|
file: FILE;
|
|
|
|
ptr: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
ptr := UTILS.FileCreate(name);
|
|
|
|
|
|
|
|
IF ptr > 0 THEN
|
|
|
|
file := NewFile();
|
|
|
|
file.ptr := ptr;
|
2020-10-13 09:58:51 +02:00
|
|
|
file.count := 0
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
|
|
|
file := NIL
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN file
|
|
|
|
END create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE open* (name: ARRAY OF CHAR): FILE;
|
|
|
|
VAR
|
|
|
|
file: FILE;
|
|
|
|
ptr: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
ptr := UTILS.FileOpen(name);
|
|
|
|
|
|
|
|
IF ptr > 0 THEN
|
|
|
|
file := NewFile();
|
|
|
|
file.ptr := ptr;
|
|
|
|
file.count := -1
|
|
|
|
ELSE
|
|
|
|
file := NIL
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN file
|
|
|
|
END open;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE close* (VAR file: FILE);
|
|
|
|
VAR
|
|
|
|
n: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
IF file # NIL THEN
|
|
|
|
|
|
|
|
IF file.count > 0 THEN
|
|
|
|
n := flush(file)
|
|
|
|
END;
|
|
|
|
|
|
|
|
file.count := -1;
|
|
|
|
|
|
|
|
UTILS.FileClose(file.ptr);
|
|
|
|
file.ptr := 0;
|
|
|
|
|
|
|
|
C.push(files, file);
|
|
|
|
file := NIL
|
|
|
|
END
|
|
|
|
END close;
|
|
|
|
|
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
PROCEDURE read* (file: FILE; VAR chunk: ARRAY OF CHAR; bytes: INTEGER): INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
res: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
IF file # NIL THEN
|
|
|
|
res := UTILS.FileRead(file.ptr, chunk, MAX(MIN(bytes, LEN(chunk)), 0));
|
|
|
|
IF res < 0 THEN
|
|
|
|
res := 0
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
res := 0
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END read;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE write* (file: FILE; chunk: ARRAY OF BYTE; bytes: INTEGER): INTEGER;
|
|
|
|
VAR
|
2021-06-15 19:33:16 +02:00
|
|
|
free, n, idx: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
idx := 0;
|
|
|
|
IF (file # NIL) & (file.count >= 0) THEN
|
|
|
|
|
|
|
|
free := LEN(file.buffer) - file.count;
|
|
|
|
WHILE bytes > 0 DO
|
|
|
|
n := MIN(free, bytes);
|
|
|
|
copy(chunk, idx, file.buffer, file.count, n);
|
|
|
|
DEC(free, n);
|
|
|
|
DEC(bytes, n);
|
|
|
|
INC(idx, n);
|
|
|
|
INC(file.count, n);
|
|
|
|
IF free = 0 THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
IF flush(file) # LEN(file.buffer) THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
bytes := 0;
|
2021-06-15 19:33:16 +02:00
|
|
|
DEC(idx, n)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
|
|
|
file.count := 0;
|
|
|
|
free := LEN(file.buffer)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
RETURN idx
|
2019-03-11 09:59:55 +01:00
|
|
|
END write;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE WriteByte* (file: FILE; byte: BYTE): BOOLEAN;
|
|
|
|
VAR
|
2020-05-25 22:48:33 +02:00
|
|
|
arr: ARRAY 1 OF BYTE;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
2020-05-25 22:48:33 +02:00
|
|
|
arr[0] := byte
|
|
|
|
RETURN write(file, arr, 1) = 1
|
2019-03-11 09:59:55 +01:00
|
|
|
END WriteByte;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
files := C.create()
|
2020-05-25 22:48:33 +02:00
|
|
|
END FILES.
|