| 1 |
(* SFIO.mod provides a String interface to the opening routines of FIO. |
| 2 |
|
| 3 |
Copyright (C) 2001-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 SFIO ; |
| 28 |
|
| 29 |
FROM ASCII IMPORT nul ; |
| 30 |
|
| 31 |
FROM DynamicStrings IMPORT string, Length, InitString, ConCatChar, |
| 32 |
InitStringCharStar, |
| 33 |
InitStringDB, InitStringCharStarDB, |
| 34 |
InitStringCharDB, MultDB, DupDB, SliceDB ; |
| 35 |
|
| 36 |
FROM FIO IMPORT exists, openToRead, openToWrite, openForRandom, |
| 37 |
WriteNBytes, ReadChar, getFileName, |
| 38 |
EOLN, EOF, IsNoError ; |
| 39 |
|
| 40 |
(* |
| 41 |
#undef GM2_DEBUG_SFIO |
| 42 |
#if defined(GM2_DEBUG_SFIO) |
| 43 |
# define InitString(X) InitStringDB(X, __FILE__, __LINE__) |
| 44 |
# define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__) |
| 45 |
# define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__) |
| 46 |
# define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__) |
| 47 |
# define Dup(X) DupDB(X, __FILE__, __LINE__) |
| 48 |
# define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__) |
| 49 |
#endif |
| 50 |
*) |
| 51 |
|
| 52 |
|
| 53 |
(* |
| 54 |
Exists - returns TRUE if a file named, fname exists for reading. |
| 55 |
*) |
| 56 |
|
| 57 |
PROCEDURE Exists (fname: String) : BOOLEAN ; |
| 58 |
BEGIN |
| 59 |
RETURN exists (string (fname), Length (fname)) |
| 60 |
END Exists ; |
| 61 |
|
| 62 |
|
| 63 |
(* |
| 64 |
OpenToRead - attempts to open a file, fname, for reading and |
| 65 |
it returns this file. |
| 66 |
The success of this operation can be checked by |
| 67 |
calling IsNoError. |
| 68 |
*) |
| 69 |
|
| 70 |
PROCEDURE OpenToRead (fname: String) : File ; |
| 71 |
BEGIN |
| 72 |
RETURN openToRead (string (fname), Length (fname)) |
| 73 |
END OpenToRead ; |
| 74 |
|
| 75 |
|
| 76 |
(* |
| 77 |
OpenToWrite - attempts to open a file, fname, for write and |
| 78 |
it returns this file. |
| 79 |
The success of this operation can be checked by |
| 80 |
calling IsNoError. |
| 81 |
*) |
| 82 |
|
| 83 |
PROCEDURE OpenToWrite (fname: String) : File ; |
| 84 |
BEGIN |
| 85 |
RETURN openToWrite (string (fname), Length (fname)) |
| 86 |
END OpenToWrite ; |
| 87 |
|
| 88 |
|
| 89 |
(* |
| 90 |
OpenForRandom - attempts to open a file, fname, for random access |
| 91 |
read or write and it returns this file. |
| 92 |
The success of this operation can be checked by |
| 93 |
calling IsNoError. |
| 94 |
towrite, determines whether the file should be |
| 95 |
opened for writing or reading. |
| 96 |
if towrite is TRUE or whether the previous file should |
| 97 |
be left alone, allowing this descriptor to seek |
| 98 |
and modify an existing file. |
| 99 |
*) |
| 100 |
|
| 101 |
PROCEDURE OpenForRandom (fname: String; towrite, newfile: BOOLEAN) : File ; |
| 102 |
BEGIN |
| 103 |
RETURN openForRandom (string (fname), Length (fname), towrite, newfile) |
| 104 |
END OpenForRandom ; |
| 105 |
|
| 106 |
|
| 107 |
(* |
| 108 |
WriteS - writes a string, s, to, file. It returns the String, s. |
| 109 |
*) |
| 110 |
|
| 111 |
PROCEDURE WriteS (file: File; s: String) : String ; |
| 112 |
VAR |
| 113 |
nBytes: CARDINAL ; |
| 114 |
BEGIN |
| 115 |
IF s#NIL |
| 116 |
THEN |
| 117 |
nBytes := WriteNBytes(file, Length(s), string(s)) |
| 118 |
END ; |
| 119 |
RETURN( s ) |
| 120 |
END WriteS ; |
| 121 |
|
| 122 |
|
| 123 |
(* |
| 124 |
ReadS - reads and returns a string from, file. |
| 125 |
It stops reading the string at the end of line or end of file. |
| 126 |
It consumes the newline at the end of line but does not place |
| 127 |
this into the returned string. |
| 128 |
*) |
| 129 |
|
| 130 |
PROCEDURE ReadS (file: File) : String ; |
| 131 |
VAR |
| 132 |
s: String ; |
| 133 |
BEGIN |
| 134 |
s := InitString ('') ; |
| 135 |
WHILE (NOT EOLN (file)) AND (NOT EOF (file)) AND IsNoError (file) DO |
| 136 |
s := ConCatChar (s, ReadChar (file)) |
| 137 |
END ; |
| 138 |
IF EOLN (file) |
| 139 |
THEN |
| 140 |
(* consume nl *) |
| 141 |
IF ReadChar (file) = nul |
| 142 |
THEN |
| 143 |
END |
| 144 |
END ; |
| 145 |
RETURN s |
| 146 |
END ReadS ; |
| 147 |
|
| 148 |
|
| 149 |
(* |
| 150 |
GetFileName - return a new string containing the name of the file. |
| 151 |
The string should be killed by the caller. |
| 152 |
*) |
| 153 |
|
| 154 |
PROCEDURE GetFileName (file: File) : String ; |
| 155 |
BEGIN |
| 156 |
RETURN InitStringCharStar (getFileName (file)) |
| 157 |
END GetFileName ; |
| 158 |
|
| 159 |
|
| 160 |
END SFIO. |