| 1 |
(* M2Printf.mod provides a simple printf capability. |
| 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 |
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 M2Printf ; |
| 23 |
|
| 24 |
FROM SFIO IMPORT WriteS ; |
| 25 |
FROM FIO IMPORT StdOut ; |
| 26 |
FROM DynamicStrings IMPORT String, string, InitString, KillString, InitStringCharStar, Mark ; |
| 27 |
FROM StrLib IMPORT StrLen ; |
| 28 |
FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2, Sprintf3, Sprintf4 ; |
| 29 |
FROM NameKey IMPORT Name, KeyToCharStar ; |
| 30 |
|
| 31 |
|
| 32 |
(* |
| 33 |
IsDigit - returns TRUE if, ch, is a character 0..9 |
| 34 |
*) |
| 35 |
|
| 36 |
(* |
| 37 |
PROCEDURE IsDigit (ch: CHAR) : BOOLEAN ; |
| 38 |
BEGIN |
| 39 |
RETURN (ch>='0') AND (ch<='9') |
| 40 |
END IsDigit ; |
| 41 |
*) |
| 42 |
|
| 43 |
|
| 44 |
(* |
| 45 |
Cast - casts a := b |
| 46 |
*) |
| 47 |
|
| 48 |
PROCEDURE Cast (VAR a: ARRAY OF BYTE; b: ARRAY OF BYTE) ; |
| 49 |
VAR |
| 50 |
i: CARDINAL ; |
| 51 |
BEGIN |
| 52 |
IF HIGH(a) = HIGH(b) |
| 53 |
THEN |
| 54 |
FOR i := 0 TO HIGH(a) DO |
| 55 |
a[i] := b[i] |
| 56 |
END |
| 57 |
ELSE |
| 58 |
HALT |
| 59 |
END |
| 60 |
END Cast ; |
| 61 |
|
| 62 |
|
| 63 |
(* |
| 64 |
TranslateNameToCharStar - takes a format specification string, a, and |
| 65 |
if it contains %a then this is translated |
| 66 |
into a String and %a is replaced by %s. |
| 67 |
*) |
| 68 |
|
| 69 |
PROCEDURE TranslateNameToCharStar (VAR a: ARRAY OF CHAR; |
| 70 |
n: CARDINAL) : BOOLEAN ; |
| 71 |
VAR |
| 72 |
argno, |
| 73 |
i, h : CARDINAL ; |
| 74 |
BEGIN |
| 75 |
argno := 1 ; |
| 76 |
i := 0 ; |
| 77 |
h := StrLen(a) ; |
| 78 |
WHILE i<h DO |
| 79 |
IF (a[i]='%') AND (i+1<h) |
| 80 |
THEN |
| 81 |
IF (a[i+1] = 'a') AND (argno = n) |
| 82 |
THEN |
| 83 |
a[i+1] := 's' ; |
| 84 |
RETURN TRUE |
| 85 |
END ; |
| 86 |
INC (argno) ; |
| 87 |
IF argno>n |
| 88 |
THEN |
| 89 |
(* all done *) |
| 90 |
RETURN FALSE |
| 91 |
END |
| 92 |
END ; |
| 93 |
INC (i) |
| 94 |
END ; |
| 95 |
RETURN FALSE |
| 96 |
END TranslateNameToCharStar ; |
| 97 |
|
| 98 |
|
| 99 |
(* |
| 100 |
fprintf0 - writes out an array to, file, after the escape sequences |
| 101 |
have been translated. |
| 102 |
*) |
| 103 |
|
| 104 |
PROCEDURE fprintf0 (file: File; a: ARRAY OF CHAR) ; |
| 105 |
BEGIN |
| 106 |
IF KillString (WriteS (file, Sprintf0 (InitString(a)))) = NIL |
| 107 |
THEN |
| 108 |
END |
| 109 |
END fprintf0 ; |
| 110 |
|
| 111 |
|
| 112 |
PROCEDURE fprintf1 (file: File; a: ARRAY OF CHAR; w: ARRAY OF BYTE) ; |
| 113 |
VAR |
| 114 |
s, t: String ; |
| 115 |
n : Name ; |
| 116 |
BEGIN |
| 117 |
IF TranslateNameToCharStar (a, 1) |
| 118 |
THEN |
| 119 |
Cast (n, w) ; |
| 120 |
s := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 121 |
t := Mark (InitString (a)) ; |
| 122 |
s := Sprintf1 (t, s) |
| 123 |
ELSE |
| 124 |
t := Mark (InitString (a)) ; |
| 125 |
s := Sprintf1 (t, w) |
| 126 |
END ; |
| 127 |
IF KillString (WriteS (file, s)) = NIL |
| 128 |
THEN |
| 129 |
END |
| 130 |
END fprintf1 ; |
| 131 |
|
| 132 |
|
| 133 |
PROCEDURE fprintf2 (file: File; a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) ; |
| 134 |
VAR |
| 135 |
n : Name ; |
| 136 |
s, |
| 137 |
s1, s2: String ; |
| 138 |
b : BITSET ; |
| 139 |
BEGIN |
| 140 |
b := {} ; |
| 141 |
IF TranslateNameToCharStar (a, 1) |
| 142 |
THEN |
| 143 |
Cast (n, w1) ; |
| 144 |
s1 := Mark(InitStringCharStar (KeyToCharStar (n))) ; |
| 145 |
INCL (b, 1) |
| 146 |
END ; |
| 147 |
IF TranslateNameToCharStar (a, 2) |
| 148 |
THEN |
| 149 |
Cast(n, w2) ; |
| 150 |
s2 := Mark(InitStringCharStar (KeyToCharStar (n))) ; |
| 151 |
INCL (b, 2) |
| 152 |
END ; |
| 153 |
CASE b OF |
| 154 |
|
| 155 |
{} : s := Sprintf2 (Mark (InitString(a)), w1, w2) | |
| 156 |
{1} : s := Sprintf2 (Mark (InitString(a)), s1, w2) | |
| 157 |
{2} : s := Sprintf2 (Mark (InitString(a)), w1, s2) | |
| 158 |
{1,2}: s := Sprintf2 (Mark (InitString(a)), s1, s2) |
| 159 |
|
| 160 |
ELSE |
| 161 |
HALT |
| 162 |
END ; |
| 163 |
IF KillString (WriteS (file, s)) = NIL |
| 164 |
THEN |
| 165 |
END |
| 166 |
END fprintf2 ; |
| 167 |
|
| 168 |
|
| 169 |
PROCEDURE fprintf3 (file: File; a: ARRAY OF CHAR; |
| 170 |
w1, w2, w3: ARRAY OF BYTE) ; |
| 171 |
VAR |
| 172 |
n : Name ; |
| 173 |
s, s1, s2, s3: String ; |
| 174 |
b : BITSET ; |
| 175 |
BEGIN |
| 176 |
b := {} ; |
| 177 |
IF TranslateNameToCharStar (a, 1) |
| 178 |
THEN |
| 179 |
Cast (n, w1) ; |
| 180 |
s1 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 181 |
INCL (b, 1) |
| 182 |
END ; |
| 183 |
IF TranslateNameToCharStar(a, 2) |
| 184 |
THEN |
| 185 |
Cast (n, w2) ; |
| 186 |
s2 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 187 |
INCL (b, 2) |
| 188 |
END ; |
| 189 |
IF TranslateNameToCharStar(a, 3) |
| 190 |
THEN |
| 191 |
Cast (n, w3) ; |
| 192 |
s3 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 193 |
INCL (b, 3) |
| 194 |
END ; |
| 195 |
CASE b OF |
| 196 |
|
| 197 |
{} : s := Sprintf3 (Mark (InitString (a)), w1, w2, w3) | |
| 198 |
{1} : s := Sprintf3 (Mark (InitString (a)), s1, w2, w3) | |
| 199 |
{2} : s := Sprintf3 (Mark (InitString (a)), w1, s2, w3) | |
| 200 |
{1,2} : s := Sprintf3 (Mark (InitString (a)), s1, s2, w3) | |
| 201 |
{3} : s := Sprintf3 (Mark (InitString (a)), w1, w2, s3) | |
| 202 |
{1,3} : s := Sprintf3 (Mark (InitString (a)), s1, w2, s3) | |
| 203 |
{2,3} : s := Sprintf3 (Mark (InitString (a)), w1, s2, s3) | |
| 204 |
{1,2,3}: s := Sprintf3 (Mark (InitString (a)), s1, s2, s3) |
| 205 |
|
| 206 |
ELSE |
| 207 |
HALT |
| 208 |
END ; |
| 209 |
IF KillString (WriteS (file, s)) = NIL |
| 210 |
THEN |
| 211 |
END |
| 212 |
END fprintf3 ; |
| 213 |
|
| 214 |
|
| 215 |
PROCEDURE fprintf4 (file: File; a: ARRAY OF CHAR; |
| 216 |
w1, w2, w3, w4: ARRAY OF BYTE) ; |
| 217 |
VAR |
| 218 |
n : Name ; |
| 219 |
s, s1, s2, s3, s4: String ; |
| 220 |
b : BITSET ; |
| 221 |
BEGIN |
| 222 |
b := {} ; |
| 223 |
IF TranslateNameToCharStar (a, 1) |
| 224 |
THEN |
| 225 |
Cast (n, w1) ; |
| 226 |
s1 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 227 |
INCL (b, 1) |
| 228 |
END ; |
| 229 |
IF TranslateNameToCharStar (a, 2) |
| 230 |
THEN |
| 231 |
Cast (n, w2) ; |
| 232 |
s2 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 233 |
INCL (b, 2) |
| 234 |
END ; |
| 235 |
IF TranslateNameToCharStar (a, 3) |
| 236 |
THEN |
| 237 |
Cast (n, w3) ; |
| 238 |
s3 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 239 |
INCL (b, 3) |
| 240 |
END ; |
| 241 |
IF TranslateNameToCharStar (a, 4) |
| 242 |
THEN |
| 243 |
Cast (n, w4) ; |
| 244 |
s4 := Mark (InitStringCharStar (KeyToCharStar (n))) ; |
| 245 |
INCL (b, 4) |
| 246 |
END ; |
| 247 |
CASE b OF |
| 248 |
|
| 249 |
{} : s := Sprintf4 (Mark (InitString (a)), w1, w2, w3, w4) | |
| 250 |
{1} : s := Sprintf4 (Mark (InitString (a)), s1, w2, w3, w4) | |
| 251 |
{2} : s := Sprintf4 (Mark (InitString (a)), w1, s2, w3, w4) | |
| 252 |
{1,2} : s := Sprintf4 (Mark (InitString (a)), s1, s2, w3, w4) | |
| 253 |
{3} : s := Sprintf4 (Mark (InitString (a)), w1, w2, s3, w4) | |
| 254 |
{1,3} : s := Sprintf4 (Mark (InitString (a)), s1, w2, s3, w4) | |
| 255 |
{2,3} : s := Sprintf4 (Mark (InitString (a)), w1, s2, s3, w4) | |
| 256 |
{1,2,3} : s := Sprintf4 (Mark (InitString (a)), s1, s2, s3, w4) | |
| 257 |
{4} : s := Sprintf4 (Mark (InitString (a)), w1, w2, w3, s4) | |
| 258 |
{1,4} : s := Sprintf4 (Mark (InitString (a)), s1, w2, w3, s4) | |
| 259 |
{2,4} : s := Sprintf4 (Mark (InitString (a)), w1, s2, w3, s4) | |
| 260 |
{1,2,4} : s := Sprintf4 (Mark (InitString (a)), s1, s2, w3, s4) | |
| 261 |
{3,4} : s := Sprintf4 (Mark (InitString (a)), w1, w2, s3, s4) | |
| 262 |
{1,3,4} : s := Sprintf4 (Mark (InitString (a)), s1, w2, s3, s4) | |
| 263 |
{2,3,4} : s := Sprintf4 (Mark (InitString (a)), w1, s2, s3, s4) | |
| 264 |
{1,2,3,4}: s := Sprintf4 (Mark (InitString (a)), s1, s2, s3, s4) |
| 265 |
|
| 266 |
ELSE |
| 267 |
HALT |
| 268 |
END ; |
| 269 |
IF KillString (WriteS (file, s)) = NIL |
| 270 |
THEN |
| 271 |
END |
| 272 |
END fprintf4 ; |
| 273 |
|
| 274 |
|
| 275 |
(* |
| 276 |
printf0 - writes out an array to, StdOut, after the escape |
| 277 |
sequences have been translated. |
| 278 |
*) |
| 279 |
|
| 280 |
PROCEDURE printf0 (a: ARRAY OF CHAR) ; |
| 281 |
BEGIN |
| 282 |
fprintf0 (StdOut, a) |
| 283 |
END printf0 ; |
| 284 |
|
| 285 |
|
| 286 |
PROCEDURE printf1 (a: ARRAY OF CHAR; |
| 287 |
w: ARRAY OF BYTE) ; |
| 288 |
BEGIN |
| 289 |
fprintf1 (StdOut, a, w) |
| 290 |
END printf1 ; |
| 291 |
|
| 292 |
|
| 293 |
PROCEDURE printf2 (a: ARRAY OF CHAR; |
| 294 |
w1, w2: ARRAY OF BYTE) ; |
| 295 |
BEGIN |
| 296 |
fprintf2 (StdOut, a, w1, w2) |
| 297 |
END printf2 ; |
| 298 |
|
| 299 |
|
| 300 |
PROCEDURE printf3 (a: ARRAY OF CHAR; |
| 301 |
w1, w2, w3: ARRAY OF BYTE) ; |
| 302 |
BEGIN |
| 303 |
fprintf3 (StdOut, a, w1, w2, w3) |
| 304 |
END printf3 ; |
| 305 |
|
| 306 |
|
| 307 |
PROCEDURE printf4 (a: ARRAY OF CHAR; |
| 308 |
w1, w2, w3, w4: ARRAY OF BYTE) ; |
| 309 |
BEGIN |
| 310 |
fprintf4 (StdOut, a, w1, w2, w3, w4) |
| 311 |
END printf4 ; |
| 312 |
|
| 313 |
|
| 314 |
END M2Printf. |