Debian Codegraph @

gcc/m2/gm2-libs-iso/RndFile.mod

raw
1 (* RndFile.mod implement the ISO RndFile specification.
2
3 Copyright (C) 2008-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 IMPLEMENTATION MODULE RndFile ;
28
29
30 FROM RTgen IMPORT ChanDev, DeviceType,
31 InitChanDev, doLook, doSkip, doSkipLook, doWriteLn,
32 doReadText, doWriteText, doReadLocs, doWriteLocs,
33 checkErrno ;
34
35 FROM RTfio IMPORT doreadchar, dounreadchar, dogeterrno, dorbytes,
36 dowbytes, dowriteln, iseof, iseoln, iserror ;
37
38 FROM IOLink IMPORT DeviceId, DeviceTablePtr, IsDevice, MakeChan, UnMakeChan,
39 DeviceTablePtrValue, RAISEdevException, AllocateDeviceId,
40 ResetProc ;
41
42 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
43 FROM RTgenif IMPORT GenDevIF, InitGenDevIF ;
44 FROM FIO IMPORT File ;
45 FROM libc IMPORT memcpy ;
46 FROM errno IMPORT geterrno ;
47 FROM IOConsts IMPORT ReadResults ;
48 FROM ChanConsts IMPORT readFlag, writeFlag ;
49
50 FROM EXCEPTIONS IMPORT ExceptionNumber, RAISE,
51 AllocateSource, ExceptionSource, IsCurrentSource,
52 IsExceptionalExecution ;
53
54 IMPORT FIO, RTio, errno, ErrnoCategory ;
55
56
57 VAR
58 dev : ChanDev ;
59 did : DeviceId ;
60 rndfileException: ExceptionSource ;
61
62
63 PROCEDURE look (d: DeviceTablePtr;
64 VAR ch: CHAR; VAR r: ReadResults) ;
65 BEGIN
66 checkRW(FALSE, d) ;
67 doLook(dev, d, ch, r)
68 END look ;
69
70
71 PROCEDURE skip (d: DeviceTablePtr) ;
72 BEGIN
73 doSkip(dev, d)
74 END skip ;
75
76
77 PROCEDURE skiplook (d: DeviceTablePtr;
78 VAR ch: CHAR; VAR r: ReadResults) ;
79 BEGIN
80 checkRW(FALSE, d) ;
81 doSkipLook(dev, d, ch, r)
82 END skiplook ;
83
84
85 PROCEDURE lnwrite (d: DeviceTablePtr) ;
86 BEGIN
87 checkRW(TRUE, d) ;
88 doWriteLn(dev, d)
89 END lnwrite ;
90
91
92 PROCEDURE textread (d: DeviceTablePtr;
93 to: SYSTEM.ADDRESS;
94 maxChars: CARDINAL;
95 VAR charsRead: CARDINAL) ;
96 BEGIN
97 checkRW(FALSE, d) ;
98 doReadText(dev, d, to, maxChars, charsRead)
99 END textread ;
100
101
102 PROCEDURE textwrite (d: DeviceTablePtr;
103 from: SYSTEM.ADDRESS;
104 charsToWrite: CARDINAL);
105 BEGIN
106 checkRW(TRUE, d) ;
107 doWriteText(dev, d, from, charsToWrite)
108 END textwrite ;
109
110
111 PROCEDURE rawread (d: DeviceTablePtr;
112 to: SYSTEM.ADDRESS;
113 maxLocs: CARDINAL;
114 VAR locsRead: CARDINAL) ;
115 BEGIN
116 checkRW(FALSE, d) ;
117 doReadLocs(dev, d, to, maxLocs, locsRead)
118 END rawread ;
119
120
121 PROCEDURE rawwrite (d: DeviceTablePtr;
122 from: SYSTEM.ADDRESS;
123 locsToWrite: CARDINAL) ;
124 BEGIN
125 checkRW(TRUE, d) ;
126 doWriteLocs(dev, d, from, locsToWrite)
127 END rawwrite ;
128
129
130 PROCEDURE getname (d: DeviceTablePtr;
131 VAR a: ARRAY OF CHAR) ;
132 BEGIN
133 FIO.GetFileName(RTio.GetFile(d^.cid), a)
134 END getname ;
135
136
137 PROCEDURE flush (d: DeviceTablePtr) ;
138 BEGIN
139 FIO.FlushBuffer(RTio.GetFile(d^.cid))
140 END flush ;
141
142
143 (*
144 checkOpenErrno - assigns, e, and, res, depending upon file result of opening,
145 file.
146 *)
147
148 PROCEDURE checkOpenErrno (file: FIO.File; VAR e: INTEGER; VAR res: OpenResults) ;
149 BEGIN
150 IF FIO.IsNoError(file)
151 THEN
152 e := 0 ;
153 ELSE
154 e := errno.geterrno()
155 END ;
156 res := ErrnoCategory.GetOpenResults(e)
157 END checkOpenErrno ;
158
159
160 (*
161 checkRW - ensures that the file attached to, p, has been opened, towrite.
162 *)
163
164 PROCEDURE checkRW (towrite: BOOLEAN; p: DeviceTablePtr) ;
165 VAR
166 pb : POINTER TO BOOLEAN ;
167 fp : FilePos ;
168 file : File ;
169 name : SYSTEM.ADDRESS ;
170 size : CARDINAL ;
171 contents: SYSTEM.ADDRESS ;
172 BEGIN
173 pb := p^.cd ;
174 IF pb^#towrite
175 THEN
176 WITH p^ DO
177 pb^ := towrite ;
178 fp := CurrentPos(cid) ;
179 file := RTio.GetFile(RTio.ChanId(cid)) ;
180 name := FIO.getFileName(file) ;
181 size := FIO.getFileNameLength(file) ;
182 ALLOCATE(contents, size+1) ;
183 contents := memcpy(contents, name, size) ;
184 FIO.Close(file) ;
185 file := FIO.openForRandom(contents, size, towrite, FALSE) ;
186 RTio.SetFile(cid, file) ;
187 SetPos(cid, fp) ;
188 DEALLOCATE(contents, size+1)
189 END
190 END
191 END checkRW ;
192
193
194 (*
195 newCid - returns a ChanId which represents the opened file, name.
196 res is set appropriately on return.
197 *)
198
199 PROCEDURE newCid (fname: ARRAY OF CHAR;
200 f: FlagSet;
201 VAR res: OpenResults;
202 toWrite, newfile: BOOLEAN;
203 whichreset: ResetProc) : ChanId ;
204 VAR
205 c : RTio.ChanId ;
206 file: FIO.File ;
207 e : INTEGER ;
208 p : DeviceTablePtr ;
209 pb : POINTER TO BOOLEAN ;
210 BEGIN
211 file := FIO.OpenForRandom(fname, toWrite, newfile) ;
212 checkOpenErrno(file, e, res) ;
213
214 IF FIO.IsNoError(file)
215 THEN
216 NEW(pb) ;
217 pb^ := toWrite ;
218 MakeChan(did, c) ;
219 RTio.SetFile(c, file) ;
220 p := DeviceTablePtrValue(c, did) ;
221 WITH p^ DO
222 cd := pb ;
223 flags := f ;
224 errNum := e ;
225 doLook := look ;
226 doSkip := skip ;
227 doSkipLook := skiplook ;
228 doLnWrite := lnwrite ;
229 doTextRead := textread ;
230 doTextWrite := textwrite ;
231 doRawRead := rawread ;
232 doRawWrite := rawwrite ;
233 doGetName := getname ;
234 doReset := whichreset ;
235 doFlush := flush ;
236 doFree := handlefree
237 END ;
238 RETURN( c )
239 ELSE
240 RETURN( IOChan.InvalidChan() )
241 END
242 END newCid ;
243
244
245 (*
246 handlefree -
247 *)
248
249 PROCEDURE handlefree (d: DeviceTablePtr) ;
250 VAR
251 f : File ;
252 pb: POINTER TO BOOLEAN ;
253 BEGIN
254 WITH d^ DO
255 doFlush(d) ;
256 checkErrno(dev, d) ;
257 f := RTio.GetFile(RTio.ChanId(cid)) ;
258 IF FIO.IsNoError(f)
259 THEN
260 FIO.Close(f) ;
261 END ;
262 checkErrno(dev, d) ;
263 pb := cd ;
264 DISPOSE(pb) ;
265 cd := NIL
266 END
267 END handlefree ;
268
269
270 PROCEDURE resetRandom (d: DeviceTablePtr) ;
271 BEGIN
272 WITH d^ DO
273 IF IsRndFile(cid)
274 THEN
275 (* --fixme --, finish this *)
276 ELSE
277 RAISEdevException(cid, did, IOChan.wrongDevice,
278 'RndFile.' + __FUNCTION__ +
279 ': channel is not a random file')
280 END
281 END
282 END resetRandom ;
283
284
285 PROCEDURE OpenOld (VAR cid: ChanId; name: ARRAY OF CHAR; flags: FlagSet;
286 VAR res: OpenResults);
287 (* Attempts to obtain and open a channel connected to a stored random
288 access file of the given name.
289 The old flag is implied; without the write flag, read is implied;
290 without the text flag, raw is implied.
291 If successful, assigns to cid the identity of the opened channel,
292 assigns the value opened to res, and sets the read/write position
293 to the start of the file.
294 If a channel cannot be opened as required, the value of res indicates
295 the reason, and cid identifies the invalid channel.
296 *)
297 BEGIN
298 INCL(flags, ChanConsts.oldFlag) ;
299 IF NOT (ChanConsts.writeFlag IN flags)
300 THEN
301 INCL(flags, ChanConsts.readFlag)
302 END ;
303 IF NOT (ChanConsts.textFlag IN flags)
304 THEN
305 INCL(flags, ChanConsts.rawFlag)
306 END ;
307 cid := newCid(name, flags, res, FALSE, FALSE, resetRandom)
308 END OpenOld ;
309
310
311 PROCEDURE OpenClean (VAR cid: ChanId; name: ARRAY OF CHAR; flags: FlagSet;
312 VAR res: OpenResults);
313 (* Attempts to obtain and open a channel connected to a stored random
314 access file of the given name.
315 The write flag is implied; without the text flag, raw is implied.
316 If successful, assigns to cid the identity of the opened channel,
317 assigns the value opened to res, and truncates the file to zero length.
318 If a channel cannot be opened as required, the value of res indicates
319 the reason, and cid identifies the invalid channel.
320 *)
321 BEGIN
322 INCL(flags, ChanConsts.writeFlag) ;
323 IF NOT (ChanConsts.textFlag IN flags)
324 THEN
325 INCL(flags, ChanConsts.rawFlag)
326 END ;
327 cid := newCid(name, flags, res, TRUE, TRUE, resetRandom)
328 END OpenClean ;
329
330
331 PROCEDURE IsRndFile (cid: ChanId): BOOLEAN;
332 (* Tests if the channel identified by cid is open to a random access file. *)
333 BEGIN
334 RETURN( (cid # NIL) AND (IOChan.InvalidChan() # cid) AND
335 (IsDevice(cid, did)) AND
336 ((ChanConsts.readFlag IN IOChan.CurrentFlags(cid)) OR
337 (ChanConsts.writeFlag IN IOChan.CurrentFlags(cid))) )
338 END IsRndFile ;
339
340
341 PROCEDURE IsRndFileException (): BOOLEAN;
342 (* Returns TRUE if the current coroutine is in the exceptional execution
343 state because of the raising of a RndFile exception; otherwise returns
344 FALSE.
345 *)
346 BEGIN
347 RETURN( IsCurrentSource (rndfileException) )
348 END IsRndFileException ;
349
350
351
352 PROCEDURE StartPos (cid: ChanId): FilePos;
353 (* If the channel identified by cid is not open to a random access file,
354 the exception wrongDevice is raised; otherwise returns the position of
355 the start of the file.
356 *)
357 VAR
358 d: DeviceTablePtr ;
359 BEGIN
360 IF IsRndFile(cid)
361 THEN
362 d := DeviceTablePtrValue(cid, did)
363 ELSE
364 RAISEdevException(cid, did, IOChan.wrongDevice,
365 'RndFile.' + __FUNCTION__ +
366 ': channel is not a random file')
367 END ;
368 RETURN( 0 )
369 END StartPos ;
370
371
372 PROCEDURE CurrentPos (cid: ChanId): FilePos;
373 (* If the channel identified by cid is not open to a random access file,
374 the exception wrongDevice is raised; otherwise returns the position
375 of the current read/write position.
376 *)
377 VAR
378 d: DeviceTablePtr ;
379 BEGIN
380 IF IsRndFile(cid)
381 THEN
382 d := DeviceTablePtrValue(cid, did) ;
383 WITH d^ DO
384 RETURN( FIO.FindPosition(RTio.GetFile(cid)) )
385 END
386 ELSE
387 RAISEdevException(cid, did, IOChan.wrongDevice,
388 'RndFile.' + __FUNCTION__ +
389 ': channel is not a random file') ;
390 RETURN 0
391 END
392 END CurrentPos ;
393
394
395 PROCEDURE EndPos (cid: ChanId): FilePos;
396 (* If the channel identified by cid is not open to a random access file,
397 the exception wrongDevice is raised; otherwise returns the first
398 position after which there have been no writes.
399 *)
400 VAR
401 d : DeviceTablePtr ;
402 end,
403 old: FilePos ;
404 BEGIN
405 IF IsRndFile(cid)
406 THEN
407 d := DeviceTablePtrValue(cid, did) ;
408 old := CurrentPos(cid) ;
409 WITH d^ DO
410 old := CurrentPos(cid) ;
411 FIO.SetPositionFromEnd(RTio.GetFile(cid), 0) ;
412 checkErrno(dev, d) ;
413 end := CurrentPos(cid) ;
414 FIO.SetPositionFromBeginning(RTio.GetFile(cid), old) ;
415 RETURN( end )
416 END
417 ELSE
418 RAISEdevException(cid, did, IOChan.wrongDevice,
419 'RndFile.' + __FUNCTION__ +
420 ': channel is not a random file') ;
421 RETURN 0
422 END
423 END EndPos ;
424
425
426 PROCEDURE NewPos (cid: ChanId; chunks: INTEGER; chunkSize: CARDINAL;
427 from: FilePos): FilePos;
428 (* If the channel identified by cid is not open to a random access file,
429 the exception wrongDevice is raised; otherwise returns the position
430 (chunks * chunkSize) relative to the position given by from, or
431 raises the exception posRange if the required position cannot be
432 represented as a value of type FilePos.
433 *)
434 VAR
435 d: DeviceTablePtr ;
436 BEGIN
437 IF IsRndFile(cid)
438 THEN
439 d := DeviceTablePtrValue(cid, did) ;
440 WITH d^ DO
441 RETURN( from+VAL(FilePos, chunks*VAL(INTEGER, chunkSize))-
442 CurrentPos(cid) )
443 END
444 ELSE
445 RAISEdevException(cid, did, IOChan.wrongDevice,
446 'RndFile.' + __FUNCTION__ +
447 ': channel is not a random file') ;
448 RETURN 0
449 END
450 END NewPos ;
451
452
453 PROCEDURE SetPos (cid: ChanId; pos: FilePos);
454 (* If the channel identified by cid is not open to a random access file,
455 the exception wrongDevice is raised; otherwise sets the read/write
456 position to the value given by pos.
457 *)
458 VAR
459 d: DeviceTablePtr ;
460 BEGIN
461 IF IsRndFile(cid)
462 THEN
463 d := DeviceTablePtrValue(cid, did) ;
464 WITH d^ DO
465 FIO.SetPositionFromBeginning(RTio.GetFile(cid), pos) ;
466 checkErrno(dev, d)
467 END
468 ELSE
469 RAISEdevException(cid, did, IOChan.wrongDevice,
470 'RndFile.' + __FUNCTION__ +
471 ': channel is not a random file')
472 END
473 END SetPos ;
474
475
476 PROCEDURE Close (VAR cid: ChanId);
477 (* If the channel identified by cid is not open to a random access file,
478 the exception wrongDevice is raised; otherwise closes the channel,
479 and assigns the value identifying the invalid channel to cid.
480 *)
481 BEGIN
482 IF IsRndFile(cid)
483 THEN
484 UnMakeChan(did, cid) ;
485 cid := IOChan.InvalidChan()
486 ELSE
487 RAISEdevException(cid, did, IOChan.wrongDevice,
488 'RndFile.' + __FUNCTION__ +
489 ': channel is not a random file')
490 END
491 END Close ;
492
493
494 (*
495 Init -
496 *)
497
498 PROCEDURE Init ;
499 VAR
500 gen: GenDevIF ;
501 BEGIN
502 AllocateDeviceId(did) ;
503 gen := InitGenDevIF(did, doreadchar, dounreadchar,
504 dogeterrno, dorbytes, dowbytes,
505 dowriteln,
506 iseof, iseoln, iserror) ;
507 dev := InitChanDev(streamfile, did, gen) ;
508 AllocateSource (rndfileException)
509 END Init ;
510
511
512 BEGIN
513 Init
514 END RndFile.