Algorithms and Data Structures © N. Wirth 1985 (Oberon version: August 2004). § 1.3 File implementation MODULE Files; CONST MaxLength = 4096; TYPE File* = POINTER TO RECORD len: INTEGER; a: ARRAY MaxLength OF CHAR END; Rider* = RECORD (* 0 <= pos <= f.len <= MaxLength *) f: File; pos: INTEGER; eof*: BOOLEAN END; PROCEDURE New*(name: ARRAY OF CHAR): File; VAR f: File; BEGIN NEW(f); f.len := 0; RETURN f END New; PROCEDURE Old*(name: ARRAY OF CHAR): File; BEGIN ASSERT(FALSE); RETURN NIL END Old; PROCEDURE Close*(VAR f: File); BEGIN END Close; PROCEDURE Set*(VAR r: Rider; f: File; pos: INTEGER); BEGIN ASSERT(f # NIL); r.f := f; r.eof := FALSE; IF r.pos < 0 THEN r.pos := 0 ELSIF pos > f.len THEN r.pos := f.len ELSE r.pos := pos END END Set; PROCEDURE Write*(VAR r: Rider; ch: CHAR); BEGIN ASSERT(r.pos <= r.f.len); IF r.pos < MaxLength THEN r.f.a[ r.pos ] := ch; INC(r.pos); IF r.pos > r.f.len THEN INC(r.f.len) END ELSE r.eof := TRUE END END Write; PROCEDURE Read*(VAR r: Rider; VAR ch: CHAR); BEGIN IF r.pos < r.f.len THEN ch := r.f.a[r.pos]; INC(r.pos) ELSE r.eof := TRUE END END Read; PROCEDURE WriteInt*(VAR r: Rider; x: INTEGER); BEGIN Write(r, CHR(x MOD 100H)); Write(r, CHR(x DIV 100H MOD 100H)); Write(r, CHR(x DIV 10000H MOD 100H)); Write(r, CHR(x DIV 1000000H MOD 100H)) END WriteInt; PROCEDURE ReadInt*(VAR r: Rider; VAR x: INTEGER); VAR ch: CHAR; BEGIN Read(r, ch); x := ORD(ch); Read(r, ch); INC(x, ORD(ch) * 100H); Read(r, ch); INC(x, ORD(ch) * 10000H); Read(r, ch); INC(x, ORD(ch) MOD 80H * 1000000H); IF ch < 80X THEN ; ELSIF x > 0 THEN x := x - 7FFFFFFFH - 1 ELSE (* Vostok is not accepting -80000000H *) x := 7FFFFFFFH END END ReadInt; END Files.