2020-05-25 23:48:33 +03:00
|
|
|
(*
|
2019-03-11 11:59:55 +03:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
Copyright (c) 2018-2020, 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
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
counter*: INTEGER;
|
2020-10-13 10:58:51 +03:00
|
|
|
file: FILES.FILE;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE align* (n, _align: INTEGER): INTEGER;
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
|
|
|
IF n MOD _align # 0 THEN
|
2020-10-13 10:58:51 +03:00
|
|
|
INC(n, _align - (n MOD _align))
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
|
|
|
|
RETURN n
|
|
|
|
END align;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE WriteByte* (n: BYTE);
|
2019-03-11 11:59:55 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Write* (chunk: ARRAY OF BYTE; bytes: INTEGER);
|
2019-03-11 11:59:55 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Write64LE* (n: INTEGER);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
FOR i := 0 TO 7 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteByte(UTILS.Byte(n, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END Write64LE;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Write32LE* (n: INTEGER);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
FOR i := 0 TO 3 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteByte(UTILS.Byte(n, i))
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END Write32LE;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Write16LE* (n: INTEGER);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteByte(UTILS.Byte(n, 0));
|
|
|
|
WriteByte(UTILS.Byte(n, 1))
|
2019-03-11 11:59:55 +03:00
|
|
|
END Write16LE;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Padding* (FileAlignment: INTEGER);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
i := align(counter, FileAlignment) - counter;
|
|
|
|
WHILE i > 0 DO
|
2020-10-13 10:58:51 +03:00
|
|
|
WriteByte(0);
|
2019-03-11 11:59:55 +03:00
|
|
|
DEC(i)
|
|
|
|
END
|
|
|
|
END Padding;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Create* (FileName: ARRAY OF CHAR);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
2020-10-13 10:58:51 +03:00
|
|
|
counter := 0;
|
|
|
|
file := FILES.create(FileName)
|
2019-03-11 11:59:55 +03:00
|
|
|
END Create;
|
|
|
|
|
|
|
|
|
2020-10-13 10:58:51 +03:00
|
|
|
PROCEDURE Close*;
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
|
|
|
FILES.close(file)
|
|
|
|
END Close;
|
|
|
|
|
|
|
|
|
2020-05-25 23:48:33 +03:00
|
|
|
END WRITER.
|