| 1 |
(* FileSystem.def provides a PIM [234] FileSystem module. |
| 2 |
|
| 3 |
Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
DEFINITION MODULE FileSystem ; |
| 28 |
|
| 29 |
(* Use this module sparingly, FIO or the ISO file modules have a |
| 30 |
much cleaner interface. *) |
| 31 |
|
| 32 |
FROM SYSTEM IMPORT WORD, BYTE, ADDRESS ; |
| 33 |
IMPORT FIO ; |
| 34 |
FROM DynamicStrings IMPORT String ; |
| 35 |
|
| 36 |
|
| 37 |
TYPE |
| 38 |
File = RECORD |
| 39 |
res : Response ; |
| 40 |
flags : FlagSet ; |
| 41 |
eof : BOOLEAN ; |
| 42 |
lastWord: WORD ; |
| 43 |
lastByte: BYTE ; |
| 44 |
fio : FIO.File ; |
| 45 |
highpos, |
| 46 |
lowpos : CARDINAL ; |
| 47 |
name : String ; |
| 48 |
END ; |
| 49 |
|
| 50 |
Flag = ( |
| 51 |
read, (* read access mode *) |
| 52 |
write, (* write access mode *) |
| 53 |
modify, |
| 54 |
truncate, (* truncate file when closed *) |
| 55 |
again, (* reread the last character *) |
| 56 |
temporary, (* file is temporary *) |
| 57 |
opened (* file has been opened *) |
| 58 |
); |
| 59 |
|
| 60 |
FlagSet = SET OF Flag; |
| 61 |
|
| 62 |
Response = (done, notdone, notsupported, callerror, |
| 63 |
unknownfile, paramerror, toomanyfiles, |
| 64 |
userdeverror) ; |
| 65 |
|
| 66 |
Command = (create, close, lookup, rename, delete, |
| 67 |
setread, setwrite, setmodify, setopen, |
| 68 |
doio, setpos, getpos, length) ; |
| 69 |
|
| 70 |
|
| 71 |
(* |
| 72 |
Create - creates a temporary file. To make the file perminant |
| 73 |
the file must be renamed. |
| 74 |
*) |
| 75 |
|
| 76 |
PROCEDURE Create (VAR f: File) ; |
| 77 |
|
| 78 |
|
| 79 |
(* |
| 80 |
Close - closes an open file. |
| 81 |
*) |
| 82 |
|
| 83 |
PROCEDURE Close (f: File) ; |
| 84 |
|
| 85 |
|
| 86 |
(* |
| 87 |
Lookup - looks for a file, filename. If the file is found |
| 88 |
then, f, is opened. If it is not found and, newFile, |
| 89 |
is TRUE then a new file is created and attached to, f. |
| 90 |
If, newFile, is FALSE and no file was found then f.res |
| 91 |
is set to notdone. |
| 92 |
*) |
| 93 |
|
| 94 |
PROCEDURE Lookup (VAR f: File; filename: ARRAY OF CHAR; newFile: BOOLEAN) ; |
| 95 |
|
| 96 |
|
| 97 |
(* |
| 98 |
Rename - rename a file and change a temporary file to a permanent |
| 99 |
file. f.res is set appropriately. |
| 100 |
*) |
| 101 |
|
| 102 |
PROCEDURE Rename (VAR f: File; newname: ARRAY OF CHAR) ; |
| 103 |
|
| 104 |
|
| 105 |
(* |
| 106 |
Delete - deletes a file, name, and sets the f.res field. |
| 107 |
f.res is set appropriately. |
| 108 |
*) |
| 109 |
|
| 110 |
PROCEDURE Delete (name: ARRAY OF CHAR; VAR f: File) ; |
| 111 |
|
| 112 |
|
| 113 |
(* |
| 114 |
ReadWord - reads a WORD, w, from file, f. |
| 115 |
f.res is set appropriately. |
| 116 |
*) |
| 117 |
|
| 118 |
PROCEDURE ReadWord (VAR f: File; VAR w: WORD) ; |
| 119 |
|
| 120 |
|
| 121 |
(* |
| 122 |
WriteWord - writes one word to a file, f. |
| 123 |
f.res is set appropriately. |
| 124 |
*) |
| 125 |
|
| 126 |
PROCEDURE WriteWord (VAR f: File; w: WORD) ; |
| 127 |
|
| 128 |
|
| 129 |
(* |
| 130 |
ReadChar - reads one character from a file, f. |
| 131 |
*) |
| 132 |
|
| 133 |
PROCEDURE ReadChar (VAR f: File; VAR ch: CHAR) ; |
| 134 |
|
| 135 |
|
| 136 |
(* |
| 137 |
WriteChar - writes a character, ch, to a file, f. |
| 138 |
f.res is set appropriately. |
| 139 |
*) |
| 140 |
|
| 141 |
PROCEDURE WriteChar (VAR f: File; ch: CHAR) ; |
| 142 |
|
| 143 |
|
| 144 |
(* |
| 145 |
ReadByte - reads a BYTE, b, from file, f. |
| 146 |
f.res is set appropriately. |
| 147 |
*) |
| 148 |
|
| 149 |
PROCEDURE ReadByte (VAR f: File; VAR b: BYTE) ; |
| 150 |
|
| 151 |
|
| 152 |
(* |
| 153 |
WriteByte - writes one BYTE, b, to a file, f. |
| 154 |
f.res is set appropriately. |
| 155 |
*) |
| 156 |
|
| 157 |
PROCEDURE WriteByte (VAR f: File; b: BYTE) ; |
| 158 |
|
| 159 |
|
| 160 |
(* |
| 161 |
ReadNBytes - reads a sequence of bytes from a file, f. |
| 162 |
*) |
| 163 |
|
| 164 |
PROCEDURE ReadNBytes (VAR f: File; a: ADDRESS; amount: CARDINAL; |
| 165 |
VAR actuallyRead: CARDINAL) ; |
| 166 |
|
| 167 |
|
| 168 |
(* |
| 169 |
WriteNBytes - writes a sequence of bytes to file, f. |
| 170 |
*) |
| 171 |
|
| 172 |
PROCEDURE WriteNBytes (VAR f: File; a: ADDRESS; amount: CARDINAL; |
| 173 |
VAR actuallyWritten: CARDINAL) ; |
| 174 |
|
| 175 |
|
| 176 |
(* |
| 177 |
Again - returns the last character read to the internal buffer |
| 178 |
so that it can be read again. |
| 179 |
*) |
| 180 |
|
| 181 |
PROCEDURE Again (VAR f: File) ; |
| 182 |
|
| 183 |
|
| 184 |
(* |
| 185 |
SetRead - puts the file, f, into the read state. |
| 186 |
The file position is unchanged. |
| 187 |
*) |
| 188 |
|
| 189 |
PROCEDURE SetRead (VAR f: File) ; |
| 190 |
|
| 191 |
|
| 192 |
(* |
| 193 |
SetWrite - puts the file, f, into the write state. |
| 194 |
The file position is unchanged. |
| 195 |
*) |
| 196 |
|
| 197 |
PROCEDURE SetWrite (VAR f: File) ; |
| 198 |
|
| 199 |
|
| 200 |
(* |
| 201 |
SetModify - puts the file, f, into the modify state. |
| 202 |
The file position is unchanged but the file can be |
| 203 |
read and written. |
| 204 |
*) |
| 205 |
|
| 206 |
PROCEDURE SetModify (VAR f: File) ; |
| 207 |
|
| 208 |
|
| 209 |
(* |
| 210 |
SetOpen - places a file, f, into the open state. The file may |
| 211 |
have been in the read/write/modify state before and |
| 212 |
in which case the previous buffer contents are flushed |
| 213 |
and the file state is reset to open. The position is |
| 214 |
unaltered. |
| 215 |
*) |
| 216 |
|
| 217 |
PROCEDURE SetOpen (VAR f: File) ; |
| 218 |
|
| 219 |
|
| 220 |
(* |
| 221 |
Reset - places a file, f, into the open state and reset the |
| 222 |
position to the start of the file. |
| 223 |
*) |
| 224 |
|
| 225 |
PROCEDURE Reset (VAR f: File) ; |
| 226 |
|
| 227 |
|
| 228 |
(* |
| 229 |
SetPos - lseek to a position within a file. |
| 230 |
*) |
| 231 |
|
| 232 |
PROCEDURE SetPos (VAR f: File; high, low: CARDINAL) ; |
| 233 |
|
| 234 |
|
| 235 |
(* |
| 236 |
GetPos - return the position within a file. |
| 237 |
*) |
| 238 |
|
| 239 |
PROCEDURE GetPos (VAR f: File; VAR high, low: CARDINAL) ; |
| 240 |
|
| 241 |
|
| 242 |
(* |
| 243 |
Length - returns the length of file, in, high, and, low. |
| 244 |
*) |
| 245 |
|
| 246 |
PROCEDURE Length (VAR f: File; VAR high, low: CARDINAL) ; |
| 247 |
|
| 248 |
|
| 249 |
(* |
| 250 |
Doio - effectively flushes a file in write mode, rereads the |
| 251 |
current buffer from disk if in read mode and writes |
| 252 |
and rereads the buffer if in modify mode. |
| 253 |
*) |
| 254 |
|
| 255 |
PROCEDURE Doio (VAR f: File) ; |
| 256 |
|
| 257 |
|
| 258 |
(* |
| 259 |
FileNameChar - checks to see whether the character, ch, is |
| 260 |
legal in a filename. nul is returned if the |
| 261 |
character was illegal. |
| 262 |
*) |
| 263 |
|
| 264 |
PROCEDURE FileNameChar (ch: CHAR) : CHAR ; |
| 265 |
|
| 266 |
|
| 267 |
(* |
| 268 |
GetFileName - return a new string containing the name of the file. |
| 269 |
The string should be killed by the caller. |
| 270 |
*) |
| 271 |
|
| 272 |
PROCEDURE GetFileName (file: File) : String ; |
| 273 |
|
| 274 |
|
| 275 |
(* |
| 276 |
WriteString - writes contents to file. The nul char |
| 277 |
will terminate the contents string otherwise |
| 278 |
all characters 0..HIGH (contents) are written. |
| 279 |
*) |
| 280 |
|
| 281 |
PROCEDURE WriteString (file: File; contents: ARRAY OF CHAR) ; |
| 282 |
|
| 283 |
|
| 284 |
END FileSystem. |