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 WRITER;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
IMPORT FILES, ERRORS, UTILS;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
counter*: INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
file: FILES.FILE;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE align* (n, _align: INTEGER): INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
ASSERT(UTILS.Align(n, _align))
|
2019-03-11 09:59:55 +01:00
|
|
|
RETURN n
|
|
|
|
END align;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE WriteByte* (n: BYTE);
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
|
|
|
IF FILES.WriteByte(file, n) THEN
|
|
|
|
INC(counter)
|
|
|
|
ELSE
|
2019-09-26 22:23:06 +02:00
|
|
|
ERRORS.Error(201)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
END WriteByte;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Write* (chunk: ARRAY OF BYTE; bytes: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
n: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
n := FILES.write(file, chunk, bytes);
|
|
|
|
IF n # bytes THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
ERRORS.Error(201)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
INC(counter, n)
|
|
|
|
END Write;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Write64LE* (n: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
FOR i := 0 TO 7 DO
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteByte(UTILS.Byte(n, i))
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
END Write64LE;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Write32LE* (n: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
FOR i := 0 TO 3 DO
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteByte(UTILS.Byte(n, i))
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
END Write32LE;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Write16LE* (n: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteByte(UTILS.Byte(n, 0));
|
|
|
|
WriteByte(UTILS.Byte(n, 1))
|
2019-03-11 09:59:55 +01:00
|
|
|
END Write16LE;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Padding* (FileAlignment: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
i := align(counter, FileAlignment) - counter;
|
|
|
|
WHILE i > 0 DO
|
2020-10-13 09:58:51 +02:00
|
|
|
WriteByte(0);
|
2019-03-11 09:59:55 +01:00
|
|
|
DEC(i)
|
|
|
|
END
|
|
|
|
END Padding;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Create* (FileName: ARRAY OF CHAR);
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2020-10-13 09:58:51 +02:00
|
|
|
counter := 0;
|
|
|
|
file := FILES.create(FileName)
|
2019-03-11 09:59:55 +01:00
|
|
|
END Create;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE Close*;
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
|
|
|
FILES.close(file)
|
|
|
|
END Close;
|
|
|
|
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
END WRITER.
|