]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-libs/FpuIO.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs / FpuIO.mod
1 (* FpuIO.mod implements a fixed format input/output for REAL/LONGREAL.
2
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
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 FpuIO ;
28
29 FROM StrIO IMPORT ReadString, WriteString, WriteLn ;
30 FROM StrLib IMPORT StrLen, StrRemoveWhitePrefix ;
31 FROM ASCII IMPORT nul ;
32 FROM DynamicStrings IMPORT String, InitString, KillString, CopyOut,
33 InitStringDB, InitStringCharStarDB,
34 InitStringCharDB, MultDB, DupDB, SliceDB ;
35
36 FROM StringConvert IMPORT StringToLongreal, LongrealToString,
37 LongIntegerToString, StringToLongInteger ;
38
39 (*
40 #undef GM2_DEBUG_FPUIO
41 if defined(GM2_DEBUG_FPUIO)
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 #endif
49 *)
50
51
52 CONST
53 MaxLineLength = 100 ;
54
55
56 PROCEDURE ReadReal (VAR x: REAL) ;
57 VAR
58 a: ARRAY [0..MaxLineLength] OF CHAR ;
59 BEGIN
60 ReadString(a) ;
61 StrToReal(a, x)
62 END ReadReal ;
63
64
65 (*
66 WriteReal - converts a REAL number, x, which has a, TotalWidth, and
67 FractionWidth into, string, a.
68 *)
69
70 PROCEDURE WriteReal (x: REAL; TotalWidth, FractionWidth: CARDINAL) ;
71 VAR
72 a: ARRAY [0..MaxLineLength] OF CHAR ;
73 BEGIN
74 RealToStr(x, TotalWidth, FractionWidth, a) ;
75 WriteString(a)
76 END WriteReal ;
77
78
79 PROCEDURE StrToReal (a: ARRAY OF CHAR ; VAR x: REAL) ;
80 VAR
81 lr: LONGREAL ;
82 BEGIN
83 StrToLongReal(a, lr) ; (* let StrToLongReal do the work and we convert the result back to REAL *)
84 x := VAL(REAL, lr)
85 END StrToReal ;
86
87
88 PROCEDURE ReadLongReal (VAR x: LONGREAL) ;
89 VAR
90 a: ARRAY [0..MaxLineLength] OF CHAR ;
91 BEGIN
92 ReadString(a) ;
93 StrToLongReal(a, x)
94 END ReadLongReal ;
95
96
97 (*
98 WriteLongReal - converts a LONGREAL number, x, which has a, TotalWidth, and
99 FractionWidth into a string.
100 *)
101
102 PROCEDURE WriteLongReal (x: LONGREAL; TotalWidth, FractionWidth: CARDINAL) ;
103 VAR
104 a: ARRAY [0..MaxLineLength] OF CHAR ;
105 BEGIN
106 LongRealToStr(x, TotalWidth, FractionWidth, a) ;
107 WriteString(a)
108 END WriteLongReal ;
109
110
111 PROCEDURE StrToLongReal (a: ARRAY OF CHAR ; VAR x: LONGREAL) ;
112 VAR
113 found: BOOLEAN ;
114 s : String ;
115 BEGIN
116 s := InitString(a) ;
117 x := StringToLongreal(s, found) ;
118 s := KillString(s)
119 END StrToLongReal ;
120
121
122 (*
123 RealToStr - converts a LONGREAL number, Real, which has, TotalWidth, and
124 FractionWidth into a string.
125 *)
126
127 PROCEDURE RealToStr (x: REAL; TotalWidth, FractionWidth: CARDINAL; VAR a: ARRAY OF CHAR) ;
128 VAR
129 lr: LONGREAL ;
130 BEGIN
131 lr := VAL(LONGREAL, x) ;
132 LongRealToStr(lr, TotalWidth, FractionWidth, a)
133 END RealToStr ;
134
135
136 (*
137 LongRealToStr - converts a LONGREAL number, Real, which has, TotalWidth, and
138 FractionWidth into a string.
139 *)
140
141 PROCEDURE LongRealToStr (x: LONGREAL; TotalWidth, FractionWidth: CARDINAL; VAR a: ARRAY OF CHAR) ;
142 VAR
143 s: String ;
144 BEGIN
145 s := LongrealToString(x, TotalWidth, FractionWidth) ;
146 CopyOut(a, s) ;
147 s := KillString(s)
148 END LongRealToStr ;
149
150
151 PROCEDURE ReadLongInt (VAR x: LONGINT) ;
152 VAR
153 a : ARRAY [0..MaxLineLength] OF CHAR ;
154 BEGIN
155 ReadString( a ) ;
156 StrToLongInt(a, x)
157 END ReadLongInt ;
158
159
160 PROCEDURE WriteLongInt (x: LONGINT; n: CARDINAL) ;
161 VAR
162 a : ARRAY [0..MaxLineLength] OF CHAR ;
163 BEGIN
164 LongIntToStr(x, n, a) ;
165 WriteString(a)
166 END WriteLongInt ;
167
168
169 PROCEDURE LongIntToStr (x: LONGINT; n: CARDINAL ; VAR a: ARRAY OF CHAR) ;
170 VAR
171 s: String ;
172 BEGIN
173 s := LongIntegerToString(x, n, ' ', FALSE, 10, TRUE) ;
174 CopyOut(a, s) ;
175 s := KillString(s)
176 END LongIntToStr ;
177
178
179 PROCEDURE StrToLongInt (a: ARRAY OF CHAR ; VAR x: LONGINT) ;
180 VAR
181 s : String ;
182 found: BOOLEAN ;
183 BEGIN
184 s := InitString(a) ;
185 x := StringToLongInteger(s, 10, found) ;
186 s := KillString(s)
187 END StrToLongInt ;
188
189
190 END FpuIO.