]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-compiler/Output.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-compiler / Output.mod
CommitLineData
1eee94d3
GM
1(* Output.mod redirect output.
2
a945c346 3Copyright (C) 2021-2024 Free Software Foundation, Inc.
1eee94d3
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Modula-2; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. *)
21
22IMPLEMENTATION MODULE Output ;
23
24
25IMPORT FIO, SFIO ;
26FROM StrLib IMPORT StrEqual ;
27FROM NameKey IMPORT KeyToCharStar, Name ;
28FROM NumberIO IMPORT CardToStr ;
29FROM ASCII IMPORT nl ;
30
31FROM DynamicStrings IMPORT KillString, InitStringCharStar, ConCatChar,
32 ConCat, InitString, Mark ;
33
34
35VAR
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
46PROCEDURE Open (filename: ARRAY OF CHAR) : BOOLEAN ;
47BEGIN
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
58END Open ;
59
60
61(*
62 Close - close the output file.
63*)
64
65PROCEDURE Close ;
66BEGIN
67 FIO.Close (outputFile)
68END Close ;
69
70
71(*
72 Write - write a single character to the output file.
73*)
74
75PROCEDURE Write (ch: CHAR) ;
76BEGIN
77 IF buffer = NIL
78 THEN
79 FIO.WriteChar (outputFile, ch)
80 ELSE
81 buffer := ConCatChar (buffer, ch)
82 END
83END Write ;
84
85
86(*
87 WriteString - write an unformatted string to the output.
88*)
89
90PROCEDURE WriteString (s: ARRAY OF CHAR) ;
91BEGIN
92 IF buffer = NIL
93 THEN
94 FIO.WriteString (outputFile, s)
95 ELSE
96 buffer := ConCat (buffer, Mark (InitString (s)))
97 END
98END WriteString ;
99
100
101(*
102 KillWriteS - write a string to the output and free the string afterwards.
103*)
104
105PROCEDURE KillWriteS (s: String) ;
106BEGIN
107 IF KillString (SFIO.WriteS (outputFile, s)) = NIL
108 THEN
109 END
110END KillWriteS ;
111
112
113(*
114 WriteS - write a string to the output. The string is not freed.
115*)
116
117PROCEDURE WriteS (s: String) ;
118BEGIN
119 IF SFIO.WriteS (outputFile, s) = s
120 THEN
121 END
122END WriteS ;
123
124
125(*
126 WriteKey - write a key to the output.
127*)
128
129PROCEDURE WriteKey (key: Name) ;
130BEGIN
131 IF buffer = NIL
132 THEN
133 KillWriteS (InitStringCharStar (KeyToCharStar (key)))
134 ELSE
135 buffer := ConCat (buffer, Mark (InitStringCharStar (KeyToCharStar (key))))
136 END
137END WriteKey ;
138
139
140(*
141 WriteLn - write a newline to the output.
142*)
143
144PROCEDURE WriteLn ;
145BEGIN
146 IF buffer = NIL
147 THEN
148 FIO.WriteLine (outputFile)
149 ELSE
150 Write (nl)
151 END
152END WriteLn ;
153
154
155(*
156 WriteCard - write a cardinal using fieldlength characters.
157*)
158
159PROCEDURE WriteCard (card, fieldlength: CARDINAL) ;
160VAR
161 s: ARRAY [0..20] OF CHAR ;
162BEGIN
163 CardToStr (card, fieldlength, s) ;
164 WriteString (s)
165END WriteCard ;
166
167
168(*
169 StartBuffer - create a buffer into which any output is redirected.
170*)
171
172PROCEDURE StartBuffer ;
173BEGIN
174 IF buffer # NIL
175 THEN
176 buffer := KillString (buffer)
177 END ;
178 buffer := InitString ('')
179END StartBuffer ;
180
181
182(*
183 EndBuffer - end the redirection and return the contents of the buffer.
184*)
185
186PROCEDURE EndBuffer () : String ;
187VAR
188 s: String ;
189BEGIN
190 s := buffer ;
191 buffer := NIL ;
192 RETURN s
193END EndBuffer ;
194
195
196BEGIN
197 stdout := TRUE ;
198 buffer := NIL ;
199 outputFile := FIO.StdOut ;
200END Output.