1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
MODULE MyFiles;
(***************************************************************************)
(**************** Copyright 2015--2021 Dieter Gloetzel ********************)
(************************ Version016 multi platform ***********************)
(***************************************************************************)
(* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. *)
IMPORT Files;
TYPE File* =
POINTER TO RECORD
native: Files.File;
current: INTEGER;
END;
TYPE Rider* =
RECORD
base: File;
id: INTEGER;
eof-: BOOLEAN;
END;
VAR stamp: INTEGER;
PROCEDURE Open(name, mode: ARRAY OF CHAR): File;
VAR f: File; g: Files.File;
BEGIN
g := Files.Open(name, mode);
IF g = NIL THEN
RETURN NIL
ELSE
NEW(f);
f.native := g; f.current := 0;
RETURN f
END
END Open;
PROCEDURE New*(name: ARRAY OF CHAR): File;
BEGIN
RETURN Open(name, "w+")
END New;
PROCEDURE Old*(name: ARRAY OF CHAR): File;
BEGIN
RETURN Open(name, "r")
END Old;
(* End of file behaviour: EOF is initially false; trying to read beyond the
end of the file returns a CHR(0) and sets the EOF flag. *)
PROCEDURE Set*(VAR r: Rider; f: File; off: INTEGER);
BEGIN
Files.Seek(f.native, off, Files.SeekSet);
r.base := f;
r.eof := FALSE;
stamp := stamp+1;
r.id := stamp;
f.current := stamp;
END Set;
PROCEDURE Read*(VAR r: Rider; VAR ch: CHAR);
VAR f: File;
BEGIN
f := r.base;
ASSERT(f.current = r.id);
IF Files.Eof(f.native) THEN
ch := CHR(0); r.eof := TRUE
ELSE
Files.ReadChar(f.native, ch)
END
END Read;
PROCEDURE Write*(VAR r: Rider; ch: CHAR);
BEGIN
ASSERT(r.base.current = r.id);
Files.WriteChar(r.base.native, ch)
END Write;
PROCEDURE WriteBytes*(VAR r: Rider; buf: ARRAY OF CHAR; len: INTEGER);
VAR i: INTEGER;
BEGIN
ASSERT(r.base.current = r.id);
FOR i := 0 TO len-1 DO
Files.WriteChar(r.base.native, buf[i])
END
END WriteBytes;
PROCEDURE WriteLongReal*(VAR r: Rider; x: LONGREAL);
BEGIN
ASSERT(r.base.current = r.id);
IF x = 0.0 THEN
Files.WriteString(r.base.native, "0")
ELSE
Files.WriteLongReal(r.base.native, x)
END
END WriteLongReal;
PROCEDURE Close*(f: File);
BEGIN
Files.Close(f.native)
END Close;
PROCEDURE Register*(f: File);
BEGIN
END Register;
(* Unix system call *)
PROCEDURE unlink(name: ARRAY OF CHAR): INTEGER IS "=unlink";
PROCEDURE Delete*(name: ARRAY OF CHAR; VAR res: INTEGER);
BEGIN
res := unlink(name)
END Delete;
END MyFiles.
|