| 1 |
(* Output.mod redirect output. |
| 2 |
|
| 3 |
Copyright (C) 2021-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 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. *) |
| 21 |
|
| 22 |
IMPLEMENTATION MODULE Output ; |
| 23 |
|
| 24 |
|
| 25 |
IMPORT FIO, SFIO ; |
| 26 |
FROM StrLib IMPORT StrEqual ; |
| 27 |
FROM NameKey IMPORT KeyToCharStar, Name ; |
| 28 |
FROM NumberIO IMPORT CardToStr ; |
| 29 |
FROM ASCII IMPORT nl ; |
| 30 |
|
| 31 |
FROM DynamicStrings IMPORT KillString, InitStringCharStar, ConCatChar, |
| 32 |
ConCat, InitString, Mark ; |
| 33 |
|
| 34 |
|
| 35 |
VAR |
| 36 |
stdout: BOOLEAN ; |
| 37 |
outputFile: FIO.File ; |
| 38 |
buffer : String ; |
| 39 |
|
| 40 |
|
| 41 |
(* |
| 42 |
Open - attempt to open filename as the output file. |
| 43 |
TRUE is returned if success, FALSE otherwise. |
| 44 |
*) |
| 45 |
|
| 46 |
PROCEDURE Open (filename: ARRAY OF CHAR) : BOOLEAN ; |
| 47 |
BEGIN |
| 48 |
IF StrEqual (filename, "<stdout>") OR StrEqual (filename, "-") |
| 49 |
THEN |
| 50 |
outputFile := FIO.StdOut ; |
| 51 |
stdout := TRUE ; |
| 52 |
RETURN TRUE |
| 53 |
ELSE |
| 54 |
outputFile := FIO.OpenToWrite (filename) ; |
| 55 |
stdout := FALSE ; |
| 56 |
RETURN FIO.IsNoError (outputFile) |
| 57 |
END |
| 58 |
END Open ; |
| 59 |
|
| 60 |
|
| 61 |
(* |
| 62 |
Close - close the output file. |
| 63 |
*) |
| 64 |
|
| 65 |
PROCEDURE Close ; |
| 66 |
BEGIN |
| 67 |
FIO.Close (outputFile) |
| 68 |
END Close ; |
| 69 |
|
| 70 |
|
| 71 |
(* |
| 72 |
Write - write a single character to the output file. |
| 73 |
*) |
| 74 |
|
| 75 |
PROCEDURE Write (ch: CHAR) ; |
| 76 |
BEGIN |
| 77 |
IF buffer = NIL |
| 78 |
THEN |
| 79 |
FIO.WriteChar (outputFile, ch) |
| 80 |
ELSE |
| 81 |
buffer := ConCatChar (buffer, ch) |
| 82 |
END |
| 83 |
END Write ; |
| 84 |
|
| 85 |
|
| 86 |
(* |
| 87 |
WriteString - write an unformatted string to the output. |
| 88 |
*) |
| 89 |
|
| 90 |
PROCEDURE WriteString (s: ARRAY OF CHAR) ; |
| 91 |
BEGIN |
| 92 |
IF buffer = NIL |
| 93 |
THEN |
| 94 |
FIO.WriteString (outputFile, s) |
| 95 |
ELSE |
| 96 |
buffer := ConCat (buffer, Mark (InitString (s))) |
| 97 |
END |
| 98 |
END WriteString ; |
| 99 |
|
| 100 |
|
| 101 |
(* |
| 102 |
KillWriteS - write a string to the output and free the string afterwards. |
| 103 |
*) |
| 104 |
|
| 105 |
PROCEDURE KillWriteS (s: String) ; |
| 106 |
BEGIN |
| 107 |
IF KillString (SFIO.WriteS (outputFile, s)) = NIL |
| 108 |
THEN |
| 109 |
END |
| 110 |
END KillWriteS ; |
| 111 |
|
| 112 |
|
| 113 |
(* |
| 114 |
WriteS - write a string to the output. The string is not freed. |
| 115 |
*) |
| 116 |
|
| 117 |
PROCEDURE WriteS (s: String) ; |
| 118 |
BEGIN |
| 119 |
IF SFIO.WriteS (outputFile, s) = s |
| 120 |
THEN |
| 121 |
END |
| 122 |
END WriteS ; |
| 123 |
|
| 124 |
|
| 125 |
(* |
| 126 |
WriteKey - write a key to the output. |
| 127 |
*) |
| 128 |
|
| 129 |
PROCEDURE WriteKey (key: Name) ; |
| 130 |
BEGIN |
| 131 |
IF buffer = NIL |
| 132 |
THEN |
| 133 |
KillWriteS (InitStringCharStar (KeyToCharStar (key))) |
| 134 |
ELSE |
| 135 |
buffer := ConCat (buffer, Mark (InitStringCharStar (KeyToCharStar (key)))) |
| 136 |
END |
| 137 |
END WriteKey ; |
| 138 |
|
| 139 |
|
| 140 |
(* |
| 141 |
WriteLn - write a newline to the output. |
| 142 |
*) |
| 143 |
|
| 144 |
PROCEDURE WriteLn ; |
| 145 |
BEGIN |
| 146 |
IF buffer = NIL |
| 147 |
THEN |
| 148 |
FIO.WriteLine (outputFile) |
| 149 |
ELSE |
| 150 |
Write (nl) |
| 151 |
END |
| 152 |
END WriteLn ; |
| 153 |
|
| 154 |
|
| 155 |
(* |
| 156 |
WriteCard - write a cardinal using fieldlength characters. |
| 157 |
*) |
| 158 |
|
| 159 |
PROCEDURE WriteCard (card, fieldlength: CARDINAL) ; |
| 160 |
VAR |
| 161 |
s: ARRAY [0..20] OF CHAR ; |
| 162 |
BEGIN |
| 163 |
CardToStr (card, fieldlength, s) ; |
| 164 |
WriteString (s) |
| 165 |
END WriteCard ; |
| 166 |
|
| 167 |
|
| 168 |
(* |
| 169 |
StartBuffer - create a buffer into which any output is redirected. |
| 170 |
*) |
| 171 |
|
| 172 |
PROCEDURE StartBuffer ; |
| 173 |
BEGIN |
| 174 |
IF buffer # NIL |
| 175 |
THEN |
| 176 |
buffer := KillString (buffer) |
| 177 |
END ; |
| 178 |
buffer := InitString ('') |
| 179 |
END StartBuffer ; |
| 180 |
|
| 181 |
|
| 182 |
(* |
| 183 |
EndBuffer - end the redirection and return the contents of the buffer. |
| 184 |
*) |
| 185 |
|
| 186 |
PROCEDURE EndBuffer () : String ; |
| 187 |
VAR |
| 188 |
s: String ; |
| 189 |
BEGIN |
| 190 |
s := buffer ; |
| 191 |
buffer := NIL ; |
| 192 |
RETURN s |
| 193 |
END EndBuffer ; |
| 194 |
|
| 195 |
|
| 196 |
BEGIN |
| 197 |
stdout := TRUE ; |
| 198 |
buffer := NIL ; |
| 199 |
outputFile := FIO.StdOut ; |
| 200 |
END Output. |