2020-05-25 23:48:33 +03:00
|
|
|
(*
|
2019-03-11 11:59:55 +03:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
Copyright (c) 2018-2019, Anton Krotov
|
2019-03-11 11:59:55 +03:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE WRITER;
|
|
|
|
|
2019-09-26 23:23:06 +03:00
|
|
|
IMPORT FILES, ERRORS, UTILS;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
FILE* = FILES.FILE;
|
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
counter*: INTEGER;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE align (n, _align: INTEGER): INTEGER;
|
|
|
|
BEGIN
|
|
|
|
IF n MOD _align # 0 THEN
|
|
|
|
n := n + _align - (n MOD _align)
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN n
|
|
|
|
END align;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE WriteByte* (file: FILE; n: BYTE);
|
|
|
|
BEGIN
|
|
|
|
IF FILES.WriteByte(file, n) THEN
|
|
|
|
INC(counter)
|
|
|
|
ELSE
|
2019-09-26 23:23:06 +03:00
|
|
|
ERRORS.Error(201)
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END WriteByte;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Write* (file: FILE; chunk: ARRAY OF BYTE; bytes: INTEGER);
|
|
|
|
VAR
|
|
|
|
n: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
n := FILES.write(file, chunk, bytes);
|
|
|
|
IF n # bytes THEN
|
2019-09-26 23:23:06 +03:00
|
|
|
ERRORS.Error(201)
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
INC(counter, n)
|
|
|
|
END Write;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Write64LE* (file: FILE; n: INTEGER);
|
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
FOR i := 0 TO 7 DO
|
2019-09-26 23:23:06 +03:00
|
|
|
WriteByte(file, UTILS.Byte(n, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END Write64LE;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Write32LE* (file: FILE; n: INTEGER);
|
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
FOR i := 0 TO 3 DO
|
2019-09-26 23:23:06 +03:00
|
|
|
WriteByte(file, UTILS.Byte(n, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END Write32LE;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Write16LE* (file: FILE; n: INTEGER);
|
|
|
|
BEGIN
|
2019-09-26 23:23:06 +03:00
|
|
|
WriteByte(file, UTILS.Byte(n, 0));
|
|
|
|
WriteByte(file, UTILS.Byte(n, 1))
|
2019-03-11 11:59:55 +03:00
|
|
|
END Write16LE;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Padding* (file: FILE; FileAlignment: INTEGER);
|
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
i := align(counter, FileAlignment) - counter;
|
|
|
|
WHILE i > 0 DO
|
|
|
|
WriteByte(file, 0);
|
|
|
|
DEC(i)
|
|
|
|
END
|
|
|
|
END Padding;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Create* (FileName: ARRAY OF CHAR): FILE;
|
|
|
|
BEGIN
|
|
|
|
counter := 0
|
|
|
|
RETURN FILES.create(FileName)
|
|
|
|
END Create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Close* (VAR file: FILE);
|
|
|
|
BEGIN
|
|
|
|
FILES.close(file)
|
|
|
|
END Close;
|
|
|
|
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
END WRITER.
|