(* BSD 2-Clause License Copyright (c) 2018-2021, Anton Krotov All rights reserved. *) MODULE WRITER; IMPORT FILES, ERRORS, UTILS; VAR counter*: INTEGER; file: FILES.FILE; PROCEDURE align* (n, _align: INTEGER): INTEGER; BEGIN ASSERT(UTILS.Align(n, _align)) RETURN n END align; PROCEDURE WriteByte* (n: BYTE); BEGIN IF FILES.WriteByte(file, n) THEN INC(counter) ELSE ERRORS.Error(201) END END WriteByte; PROCEDURE Write* (chunk: ARRAY OF BYTE; bytes: INTEGER); VAR n: INTEGER; BEGIN n := FILES.write(file, chunk, bytes); IF n # bytes THEN ERRORS.Error(201) END; INC(counter, n) END Write; PROCEDURE Write64LE* (n: INTEGER); VAR i: INTEGER; BEGIN FOR i := 0 TO 7 DO WriteByte(UTILS.Byte(n, i)) END END Write64LE; PROCEDURE Write32LE* (n: INTEGER); VAR i: INTEGER; BEGIN FOR i := 0 TO 3 DO WriteByte(UTILS.Byte(n, i)) END END Write32LE; PROCEDURE Write16LE* (n: INTEGER); BEGIN WriteByte(UTILS.Byte(n, 0)); WriteByte(UTILS.Byte(n, 1)) END Write16LE; PROCEDURE Padding* (FileAlignment: INTEGER); VAR i: INTEGER; BEGIN i := align(counter, FileAlignment) - counter; WHILE i > 0 DO WriteByte(0); DEC(i) END END Padding; PROCEDURE Create* (FileName: ARRAY OF CHAR); BEGIN counter := 0; file := FILES.create(FileName) END Create; PROCEDURE Close*; BEGIN FILES.close(file) END Close; END WRITER.