1 (* SFIO.mod provides a String interface to the opening routines of FIO.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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.
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/>. *)
27 IMPLEMENTATION MODULE SFIO
;
29 FROM ASCII
IMPORT nul
;
31 FROM DynamicStrings
IMPORT string
, Length
, InitString
, ConCatChar
,
32 InitStringDB
, InitStringCharStarDB
,
33 InitStringCharDB
, MultDB
, DupDB
, SliceDB
;
35 FROM FIO
IMPORT exists
, openToRead
, openToWrite
, openForRandom
, WriteNBytes
, ReadChar
,
36 EOLN
, EOF
, IsNoError
;
40 #if defined(GM2_DEBUG_SFIO)
41 # define InitString(X) InitStringDB(X, __FILE__, __LINE__)
42 # define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__)
43 # define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__)
44 # define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__)
45 # define Dup(X) DupDB(X, __FILE__, __LINE__)
46 # define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__)
52 Exists - returns TRUE if a file named, fname exists for reading.
55 PROCEDURE Exists (fname
: String
) : BOOLEAN ;
57 RETURN exists (string (fname
), Length (fname
))
62 OpenToRead - attempts to open a file, fname, for reading and
64 The success of this operation can be checked by
68 PROCEDURE OpenToRead (fname
: String
) : File
;
70 RETURN openToRead (string (fname
), Length (fname
))
75 OpenToWrite - attempts to open a file, fname, for write and
77 The success of this operation can be checked by
81 PROCEDURE OpenToWrite (fname
: String
) : File
;
83 RETURN openToWrite (string (fname
), Length (fname
))
88 OpenForRandom - attempts to open a file, fname, for random access
89 read or write and it returns this file.
90 The success of this operation can be checked by
92 towrite, determines whether the file should be
93 opened for writing or reading.
94 if towrite is TRUE or whether the previous file should
95 be left alone, allowing this descriptor to seek
96 and modify an existing file.
99 PROCEDURE OpenForRandom (fname
: String
; towrite
, newfile
: BOOLEAN) : File
;
101 RETURN openForRandom (string (fname
), Length (fname
), towrite
, newfile
)
106 WriteS - writes a string, s, to, file. It returns the String, s.
109 PROCEDURE WriteS (file
: File
; s
: String
) : String
;
115 nBytes
:= WriteNBytes(file
, Length(s
), string(s
))
122 ReadS - reads and returns a string from, file.
123 It stops reading the string at the end of line or end of file.
124 It consumes the newline at the end of line but does not place
125 this into the returned string.
128 PROCEDURE ReadS (file
: File
) : String
;
132 s
:= InitString ('') ;
133 WHILE (NOT EOLN (file
)) AND (NOT EOF (file
)) AND IsNoError (file
) DO
134 s
:= ConCatChar (s
, ReadChar (file
))
139 IF ReadChar (file
) = nul