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