Debian Codegraph @

gcc/m2/gm2-libs-log/RealInOut.mod

raw
1 (* RealInOut.mod provides a compatible RealInOut PIM 234 module.
2
3 Copyright (C) 2004-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 RealInOut ;
28
29 FROM DynamicStrings IMPORT String, InitString, KillString, RemoveWhitePrefix,
30 Length, Mult, InitStringChar, Mark, ConCat,
31 InitStringDB, InitStringCharStarDB,
32 InitStringCharDB, MultDB, DupDB, SliceDB ;
33
34 FROM StringConvert IMPORT StringToLongreal, LongrealToString ;
35 FROM SYSTEM IMPORT ADR, BYTE ;
36 IMPORT InOut ;
37
38 VAR
39 DecimalPlacesLength: CARDINAL ;
40
41 (*
42 #define InitString(X) InitStringDB(X, __FILE__, __LINE__)
43 #define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__)
44 #define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__)
45 #define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__)
46 #define Dup(X) DupDB(X, __FILE__, __LINE__)
47 #define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__)
48 *)
49
50 (*
51 SetNoOfDecimalPlaces - number of decimal places WriteReal and
52 WriteLongReal should emit. This procedure
53 can be used to override the default
54 DefaultDecimalPlaces constant.
55 *)
56
57 PROCEDURE SetNoOfDecimalPlaces (places: CARDINAL) ;
58 BEGIN
59 DecimalPlacesLength := places
60 END SetNoOfDecimalPlaces ;
61
62
63 (*
64 Pad - return a padded string with prefixed white space to ensure
65 that at least, n, characters are used.
66 *)
67
68 PROCEDURE Pad (s: String; n: CARDINAL) : String ;
69 BEGIN
70 IF Length(s)<n
71 THEN
72 s := ConCat(Mult(Mark(InitStringChar(' ')), n-Length(s)), Mark(s))
73 END ;
74 RETURN( s )
75 END Pad ;
76
77
78 (*
79 ReadReal - reads a real number, legal syntaxes include:
80 100, 100.0, 100e0, 100E0, 100E-1, E2, +1E+2, 1e+2
81 *)
82
83 PROCEDURE ReadReal (VAR x: REAL) ;
84 VAR
85 s: String ;
86 BEGIN
87 s := RemoveWhitePrefix(InOut.ReadS()) ;
88 IF InOut.Done
89 THEN
90 x := VAL(REAL, StringToLongreal(s, Done))
91 ELSE
92 Done := FALSE
93 END ;
94 s := KillString(s)
95 END ReadReal ;
96
97
98 (*
99 WriteReal - writes a real to the terminal. The real number
100 is right justified and, n, is the minimum field
101 width.
102 *)
103
104 PROCEDURE WriteReal (x: REAL; n: CARDINAL) ;
105 VAR
106 s: String ;
107 BEGIN
108 s := LongrealToString(VAL(LONGREAL, x), 0, DecimalPlacesLength) ;
109 s := Pad(s, n) ;
110 s := KillString(InOut.WriteS(s)) ;
111 Done := TRUE
112 END WriteReal ;
113
114
115 (*
116 WriteRealOct - writes the real to terminal in octal words.
117 *)
118
119 PROCEDURE WriteRealOct (x: REAL) ;
120 VAR
121 p: POINTER TO BYTE ;
122 i: CARDINAL ;
123 BEGIN
124 p := ADR(x) ;
125 i := 0 ;
126 WHILE i<SIZE(x) DO
127 InOut.WriteOct(VAL(CARDINAL, p^), 3) ;
128 INC(p) ;
129 INC(i)
130 END
131 END WriteRealOct ;
132
133
134 (*
135 ReadLongReal - reads a LONGLONGREAL number, legal syntaxes include:
136 100, 100.0, 100e0, 100E0, 100E-1, E2, +1E+2, 1e+2
137 *)
138
139 PROCEDURE ReadLongReal (VAR x: LONGREAL) ;
140 VAR
141 s: String ;
142 BEGIN
143 s := RemoveWhitePrefix(InOut.ReadS()) ;
144 IF InOut.Done
145 THEN
146 x := StringToLongreal(s, Done)
147 ELSE
148 Done := FALSE
149 END ;
150 s := KillString(s)
151 END ReadLongReal ;
152
153
154 (*
155 WriteLongReal - writes a real to the terminal. The real number
156 is right justified and, n, is the minimum field
157 width.
158 *)
159
160 PROCEDURE WriteLongReal (x: LONGREAL; n: CARDINAL) ;
161 VAR
162 s: String ;
163 BEGIN
164 s := LongrealToString(VAL(LONGREAL, x), 0, DecimalPlacesLength) ;
165 s := Pad(s, n) ;
166 s := KillString(InOut.WriteS(s)) ;
167 Done := TRUE
168 END WriteLongReal ;
169
170
171 (*
172 WriteLongRealOct - writes the real to terminal in octal words.
173 *)
174
175 PROCEDURE WriteLongRealOct (x: LONGREAL) ;
176 VAR
177 p: POINTER TO BYTE ;
178 i: CARDINAL ;
179 BEGIN
180 p := ADR(x) ;
181 i := 0 ;
182 WHILE i<SIZE(x) DO
183 InOut.WriteOct(VAL(CARDINAL, p^), 3) ;
184 INC(p) ;
185 INC(i)
186 END
187 END WriteLongRealOct ;
188
189
190 (*
191 ReadShortReal - reads a SHORTREAL number, legal syntaxes include:
192 100, 100.0, 100e0, 100E0, 100E-1, E2, +1E+2, 1e+2
193 *)
194
195 PROCEDURE ReadShortReal (VAR x: SHORTREAL) ;
196 VAR
197 s: String ;
198 BEGIN
199 s := RemoveWhitePrefix(InOut.ReadS()) ;
200 IF InOut.Done
201 THEN
202 x := VAL(SHORTREAL, StringToLongreal(s, Done))
203 ELSE
204 Done := FALSE
205 END ;
206 s := KillString(s)
207 END ReadShortReal ;
208
209
210 (*
211 WriteShortReal - writes a real to the terminal. The real number
212 is right justified and, n, is the minimum field
213 width.
214 *)
215
216 PROCEDURE WriteShortReal (x: SHORTREAL; n: CARDINAL) ;
217 VAR
218 s: String ;
219 BEGIN
220 s := LongrealToString(VAL(LONGREAL, x), 0, DecimalPlacesLength) ;
221 s := Pad(s, n) ;
222 s := KillString(InOut.WriteS(s)) ;
223 Done := TRUE
224 END WriteShortReal ;
225
226
227 (*
228 WriteShortRealOct - writes the real to terminal in octal words.
229 *)
230
231 PROCEDURE WriteShortRealOct (x: SHORTREAL) ;
232 VAR
233 p: POINTER TO BYTE ;
234 i: CARDINAL ;
235 BEGIN
236 p := ADR(x) ;
237 i := 0 ;
238 WHILE i<SIZE(x) DO
239 InOut.WriteOct(VAL(CARDINAL, p^), 3) ;
240 INC(p) ;
241 INC(i)
242 END
243 END WriteShortRealOct ;
244
245
246 BEGIN
247 DecimalPlacesLength := DefaultDecimalPlaces
248 END RealInOut.