c++: add fixed test [PR94100]
[official-gcc.git] / gcc / m2 / mc-boot / GNumberIO.cc
blob8a04cbff7e63bb5d7136f8ab690769069d200edb
1 /* do not edit automatically generated by mc from NumberIO. */
2 /* NumberIO.mod provides conversion of ordinal numbers.
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
37 # if !defined (TRUE)
38 # define TRUE (1==1)
39 # endif
41 # if !defined (FALSE)
42 # define FALSE (1==0)
43 # endif
45 #define _NumberIO_C
47 #include "GNumberIO.h"
48 # include "GASCII.h"
49 # include "GStrIO.h"
50 # include "GStrLib.h"
51 # include "GM2RTS.h"
53 # define MaxLineLength 79
54 # define MaxDigits 20
55 # define MaxHexDigits 20
56 # define MaxOctDigits 40
57 # define MaxBits 64
58 extern "C" void NumberIO_ReadCard (unsigned int *x);
59 extern "C" void NumberIO_WriteCard (unsigned int x, unsigned int n);
60 extern "C" void NumberIO_ReadHex (unsigned int *x);
61 extern "C" void NumberIO_WriteHex (unsigned int x, unsigned int n);
62 extern "C" void NumberIO_ReadInt (int *x);
63 extern "C" void NumberIO_WriteInt (int x, unsigned int n);
64 extern "C" void NumberIO_CardToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high);
65 extern "C" void NumberIO_StrToCard (const char *a_, unsigned int _a_high, unsigned int *x);
66 extern "C" void NumberIO_HexToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high);
67 extern "C" void NumberIO_StrToHex (const char *a_, unsigned int _a_high, unsigned int *x);
68 extern "C" void NumberIO_IntToStr (int x, unsigned int n, char *a, unsigned int _a_high);
69 extern "C" void NumberIO_StrToInt (const char *a_, unsigned int _a_high, int *x);
70 extern "C" void NumberIO_ReadOct (unsigned int *x);
71 extern "C" void NumberIO_WriteOct (unsigned int x, unsigned int n);
72 extern "C" void NumberIO_OctToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high);
73 extern "C" void NumberIO_StrToOct (const char *a_, unsigned int _a_high, unsigned int *x);
74 extern "C" void NumberIO_ReadBin (unsigned int *x);
75 extern "C" void NumberIO_WriteBin (unsigned int x, unsigned int n);
76 extern "C" void NumberIO_BinToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high);
77 extern "C" void NumberIO_StrToBin (const char *a_, unsigned int _a_high, unsigned int *x);
78 extern "C" void NumberIO_StrToBinInt (const char *a_, unsigned int _a_high, int *x);
79 extern "C" void NumberIO_StrToHexInt (const char *a_, unsigned int _a_high, int *x);
80 extern "C" void NumberIO_StrToOctInt (const char *a_, unsigned int _a_high, int *x);
82 extern "C" void NumberIO_ReadCard (unsigned int *x)
84 typedef struct ReadCard__T1_a ReadCard__T1;
86 struct ReadCard__T1_a { char array[MaxLineLength+1]; };
87 ReadCard__T1 a;
89 StrIO_ReadString ((char *) &a.array[0], MaxLineLength);
90 NumberIO_StrToCard ((const char *) &a.array[0], MaxLineLength, x);
93 extern "C" void NumberIO_WriteCard (unsigned int x, unsigned int n)
95 typedef struct WriteCard__T2_a WriteCard__T2;
97 struct WriteCard__T2_a { char array[MaxLineLength+1]; };
98 WriteCard__T2 a;
100 NumberIO_CardToStr (x, n, (char *) &a.array[0], MaxLineLength);
101 StrIO_WriteString ((const char *) &a.array[0], MaxLineLength);
104 extern "C" void NumberIO_ReadHex (unsigned int *x)
106 typedef struct ReadHex__T3_a ReadHex__T3;
108 struct ReadHex__T3_a { char array[MaxLineLength+1]; };
109 ReadHex__T3 a;
111 StrIO_ReadString ((char *) &a.array[0], MaxLineLength);
112 NumberIO_StrToHex ((const char *) &a.array[0], MaxLineLength, x);
115 extern "C" void NumberIO_WriteHex (unsigned int x, unsigned int n)
117 typedef struct WriteHex__T4_a WriteHex__T4;
119 struct WriteHex__T4_a { char array[MaxLineLength+1]; };
120 WriteHex__T4 a;
122 NumberIO_HexToStr (x, n, (char *) &a.array[0], MaxLineLength);
123 StrIO_WriteString ((const char *) &a.array[0], MaxLineLength);
126 extern "C" void NumberIO_ReadInt (int *x)
128 typedef struct ReadInt__T5_a ReadInt__T5;
130 struct ReadInt__T5_a { char array[MaxLineLength+1]; };
131 ReadInt__T5 a;
133 StrIO_ReadString ((char *) &a.array[0], MaxLineLength);
134 NumberIO_StrToInt ((const char *) &a.array[0], MaxLineLength, x);
137 extern "C" void NumberIO_WriteInt (int x, unsigned int n)
139 typedef struct WriteInt__T6_a WriteInt__T6;
141 struct WriteInt__T6_a { char array[MaxLineLength+1]; };
142 WriteInt__T6 a;
144 NumberIO_IntToStr (x, n, (char *) &a.array[0], MaxLineLength);
145 StrIO_WriteString ((const char *) &a.array[0], MaxLineLength);
148 extern "C" void NumberIO_CardToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high)
150 typedef struct CardToStr__T7_a CardToStr__T7;
152 struct CardToStr__T7_a { unsigned int array[MaxDigits-1+1]; };
153 unsigned int i;
154 unsigned int j;
155 unsigned int Higha;
156 CardToStr__T7 buf;
158 i = 0;
159 do {
160 i += 1;
161 if (i > MaxDigits)
163 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
164 StrIO_WriteLn ();
165 M2RTS_HALT (-1);
166 __builtin_unreachable ();
168 buf.array[i-1] = x % 10;
169 x = x / 10;
170 } while (! (x == 0));
171 j = 0;
172 Higha = _a_high;
173 while ((n > i) && (j <= Higha))
175 const_cast<char *>(a)[j] = ' ';
176 j += 1;
177 n -= 1;
179 while ((i > 0) && (j <= Higha))
181 const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0'))));
182 j += 1;
183 i -= 1;
185 if (j <= Higha)
187 const_cast<char *>(a)[j] = ASCII_nul;
191 extern "C" void NumberIO_StrToCard (const char *a_, unsigned int _a_high, unsigned int *x)
193 unsigned int i;
194 bool ok;
195 unsigned int higha;
196 char a[_a_high+1];
198 /* make a local copy of each unbounded array. */
199 memcpy (a, a_, _a_high+1);
201 StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high);
202 higha = StrLib_StrLen ((const char *) a, _a_high);
203 i = 0;
204 ok = true;
205 while (ok)
207 if (i < higha)
209 if ((a[i] < '0') || (a[i] > '9'))
211 i += 1;
213 else
215 ok = false;
218 else
220 ok = false;
223 (*x) = 0;
224 if (i < higha)
226 ok = true;
227 do {
228 (*x) = (10*(*x))+( ((unsigned int) (a[i]))- ((unsigned int) ('0')));
229 if (i < higha)
231 /* avoid dangling else. */
232 i += 1;
233 if ((a[i] < '0') || (a[i] > '9'))
235 ok = false;
238 else
240 ok = false;
242 } while (! (! ok));
246 extern "C" void NumberIO_HexToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high)
248 typedef struct HexToStr__T8_a HexToStr__T8;
250 struct HexToStr__T8_a { unsigned int array[MaxHexDigits-1+1]; };
251 unsigned int i;
252 unsigned int j;
253 unsigned int Higha;
254 HexToStr__T8 buf;
256 i = 0;
257 do {
258 i += 1;
259 if (i > MaxHexDigits)
261 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
262 StrIO_WriteLn ();
263 M2RTS_HALT (-1);
264 __builtin_unreachable ();
266 buf.array[i-1] = x % 0x010;
267 x = x / 0x010;
268 } while (! (x == 0));
269 j = 0;
270 Higha = _a_high;
271 while ((n > i) && (j <= Higha))
273 const_cast<char *>(a)[j] = '0';
274 j += 1;
275 n -= 1;
277 while ((i != 0) && (j <= Higha))
279 if (buf.array[i-1] < 10)
281 const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0'))));
283 else
285 const_cast<char *>(a)[j] = ((char) ((buf.array[i-1]+ ((unsigned int) ('A')))-10));
287 j += 1;
288 i -= 1;
290 if (j <= Higha)
292 const_cast<char *>(a)[j] = ASCII_nul;
296 extern "C" void NumberIO_StrToHex (const char *a_, unsigned int _a_high, unsigned int *x)
298 int i;
299 char a[_a_high+1];
301 /* make a local copy of each unbounded array. */
302 memcpy (a, a_, _a_high+1);
304 NumberIO_StrToHexInt ((const char *) a, _a_high, &i);
305 (*x) = (unsigned int ) (i);
308 extern "C" void NumberIO_IntToStr (int x, unsigned int n, char *a, unsigned int _a_high)
310 typedef struct IntToStr__T9_a IntToStr__T9;
312 struct IntToStr__T9_a { unsigned int array[MaxDigits-1+1]; };
313 unsigned int i;
314 unsigned int j;
315 unsigned int c;
316 unsigned int Higha;
317 IntToStr__T9 buf;
318 bool Negative;
320 if (x < 0)
322 /* avoid dangling else. */
323 Negative = true;
324 c = ((unsigned int ) (abs (x+1)))+1;
325 if (n > 0)
327 n -= 1;
330 else
332 c = x;
333 Negative = false;
335 i = 0;
336 do {
337 i += 1;
338 if (i > MaxDigits)
340 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
341 StrIO_WriteLn ();
342 M2RTS_HALT (-1);
343 __builtin_unreachable ();
345 buf.array[i-1] = c % 10;
346 c = c / 10;
347 } while (! (c == 0));
348 j = 0;
349 Higha = _a_high;
350 while ((n > i) && (j <= Higha))
352 const_cast<char *>(a)[j] = ' ';
353 j += 1;
354 n -= 1;
356 if (Negative)
358 const_cast<char *>(a)[j] = '-';
359 j += 1;
361 while ((i != 0) && (j <= Higha))
363 const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0'))));
364 j += 1;
365 i -= 1;
367 if (j <= Higha)
369 const_cast<char *>(a)[j] = ASCII_nul;
373 extern "C" void NumberIO_StrToInt (const char *a_, unsigned int _a_high, int *x)
375 unsigned int i;
376 bool ok;
377 bool Negative;
378 unsigned int higha;
379 char a[_a_high+1];
381 /* make a local copy of each unbounded array. */
382 memcpy (a, a_, _a_high+1);
384 StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high);
385 higha = StrLib_StrLen ((const char *) a, _a_high);
386 i = 0;
387 Negative = false;
388 ok = true;
389 while (ok)
391 if (i < higha)
393 if (a[i] == '-')
395 i += 1;
396 Negative = ! Negative;
398 else if ((a[i] < '0') || (a[i] > '9'))
400 /* avoid dangling else. */
401 i += 1;
403 else
405 /* avoid dangling else. */
406 ok = false;
409 else
411 ok = false;
414 (*x) = 0;
415 if (i < higha)
417 ok = true;
418 do {
419 if (Negative)
421 (*x) = (10*(*x))-((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0'))));
423 else
425 (*x) = (10*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0'))));
427 if (i < higha)
429 /* avoid dangling else. */
430 i += 1;
431 if ((a[i] < '0') || (a[i] > '9'))
433 ok = false;
436 else
438 ok = false;
440 } while (! (! ok));
444 extern "C" void NumberIO_ReadOct (unsigned int *x)
446 typedef struct ReadOct__T10_a ReadOct__T10;
448 struct ReadOct__T10_a { char array[MaxLineLength+1]; };
449 ReadOct__T10 a;
451 StrIO_ReadString ((char *) &a.array[0], MaxLineLength);
452 NumberIO_StrToOct ((const char *) &a.array[0], MaxLineLength, x);
455 extern "C" void NumberIO_WriteOct (unsigned int x, unsigned int n)
457 typedef struct WriteOct__T11_a WriteOct__T11;
459 struct WriteOct__T11_a { char array[MaxLineLength+1]; };
460 WriteOct__T11 a;
462 NumberIO_OctToStr (x, n, (char *) &a.array[0], MaxLineLength);
463 StrIO_WriteString ((const char *) &a.array[0], MaxLineLength);
466 extern "C" void NumberIO_OctToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high)
468 typedef struct OctToStr__T12_a OctToStr__T12;
470 struct OctToStr__T12_a { unsigned int array[MaxOctDigits-1+1]; };
471 unsigned int i;
472 unsigned int j;
473 unsigned int Higha;
474 OctToStr__T12 buf;
476 i = 0;
477 do {
478 i += 1;
479 if (i > MaxOctDigits)
481 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
482 StrIO_WriteLn ();
483 M2RTS_HALT (-1);
484 __builtin_unreachable ();
486 buf.array[i-1] = x % 8;
487 x = x / 8;
488 } while (! (x == 0));
489 j = 0;
490 Higha = _a_high;
491 while ((n > i) && (j <= Higha))
493 const_cast<char *>(a)[j] = ' ';
494 j += 1;
495 n -= 1;
497 while ((i > 0) && (j <= Higha))
499 const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0'))));
500 j += 1;
501 i -= 1;
503 if (j <= Higha)
505 const_cast<char *>(a)[j] = ASCII_nul;
509 extern "C" void NumberIO_StrToOct (const char *a_, unsigned int _a_high, unsigned int *x)
511 int i;
512 char a[_a_high+1];
514 /* make a local copy of each unbounded array. */
515 memcpy (a, a_, _a_high+1);
517 NumberIO_StrToOctInt ((const char *) a, _a_high, &i);
518 (*x) = (unsigned int ) (i);
521 extern "C" void NumberIO_ReadBin (unsigned int *x)
523 typedef struct ReadBin__T13_a ReadBin__T13;
525 struct ReadBin__T13_a { char array[MaxLineLength+1]; };
526 ReadBin__T13 a;
528 StrIO_ReadString ((char *) &a.array[0], MaxLineLength);
529 NumberIO_StrToBin ((const char *) &a.array[0], MaxLineLength, x);
532 extern "C" void NumberIO_WriteBin (unsigned int x, unsigned int n)
534 typedef struct WriteBin__T14_a WriteBin__T14;
536 struct WriteBin__T14_a { char array[MaxLineLength+1]; };
537 WriteBin__T14 a;
539 NumberIO_BinToStr (x, n, (char *) &a.array[0], MaxLineLength);
540 StrIO_WriteString ((const char *) &a.array[0], MaxLineLength);
543 extern "C" void NumberIO_BinToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high)
545 typedef struct BinToStr__T15_a BinToStr__T15;
547 struct BinToStr__T15_a { unsigned int array[MaxBits-1+1]; };
548 unsigned int i;
549 unsigned int j;
550 unsigned int Higha;
551 BinToStr__T15 buf;
553 i = 0;
554 do {
555 i += 1;
556 if (i > MaxBits)
558 StrIO_WriteString ((const char *) "NumberIO - increase MaxBits", 27);
559 StrIO_WriteLn ();
560 M2RTS_HALT (-1);
561 __builtin_unreachable ();
563 buf.array[i-1] = x % 2;
564 x = x / 2;
565 } while (! (x == 0));
566 j = 0;
567 Higha = _a_high;
568 while ((n > i) && (j <= Higha))
570 const_cast<char *>(a)[j] = ' ';
571 j += 1;
572 n -= 1;
574 while ((i > 0) && (j <= Higha))
576 const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0'))));
577 j += 1;
578 i -= 1;
580 if (j <= Higha)
582 const_cast<char *>(a)[j] = ASCII_nul;
586 extern "C" void NumberIO_StrToBin (const char *a_, unsigned int _a_high, unsigned int *x)
588 int i;
589 char a[_a_high+1];
591 /* make a local copy of each unbounded array. */
592 memcpy (a, a_, _a_high+1);
594 NumberIO_StrToBinInt ((const char *) a, _a_high, &i);
595 (*x) = (unsigned int ) (i);
598 extern "C" void NumberIO_StrToBinInt (const char *a_, unsigned int _a_high, int *x)
600 unsigned int i;
601 bool ok;
602 unsigned int higha;
603 char a[_a_high+1];
605 /* make a local copy of each unbounded array. */
606 memcpy (a, a_, _a_high+1);
608 StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high);
609 higha = StrLib_StrLen ((const char *) a, _a_high);
610 i = 0;
611 ok = true;
612 while (ok)
614 if (i < higha)
616 if ((a[i] < '0') || (a[i] > '1'))
618 i += 1;
620 else
622 ok = false;
625 else
627 ok = false;
630 (*x) = 0;
631 if (i < higha)
633 ok = true;
634 do {
635 (*x) = (2*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0'))));
636 if (i < higha)
638 /* avoid dangling else. */
639 i += 1;
640 if ((a[i] < '0') || (a[i] > '1'))
642 ok = false;
645 else
647 ok = false;
649 } while (! (! ok));
653 extern "C" void NumberIO_StrToHexInt (const char *a_, unsigned int _a_high, int *x)
655 unsigned int i;
656 bool ok;
657 unsigned int higha;
658 char a[_a_high+1];
660 /* make a local copy of each unbounded array. */
661 memcpy (a, a_, _a_high+1);
663 StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high);
664 higha = StrLib_StrLen ((const char *) a, _a_high);
665 i = 0;
666 ok = true;
667 while (ok)
669 if (i < higha)
671 if (((a[i] >= '0') && (a[i] <= '9')) || ((a[i] >= 'A') && (a[i] <= 'F')))
673 ok = false;
675 else
677 i += 1;
680 else
682 ok = false;
685 (*x) = 0;
686 if (i < higha)
688 ok = true;
689 do {
690 if ((a[i] >= '0') && (a[i] <= '9'))
692 (*x) = (0x010*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0'))));
694 else if ((a[i] >= 'A') && (a[i] <= 'F'))
696 /* avoid dangling else. */
697 (*x) = (0x010*(*x))+((int ) (( ((unsigned int) (a[i]))- ((unsigned int) ('A')))+10));
699 if (i < higha)
701 /* avoid dangling else. */
702 i += 1;
703 if (((a[i] < '0') || (a[i] > '9')) && ((a[i] < 'A') || (a[i] > 'F')))
705 ok = false;
708 else
710 ok = false;
712 } while (! (! ok));
716 extern "C" void NumberIO_StrToOctInt (const char *a_, unsigned int _a_high, int *x)
718 unsigned int i;
719 bool ok;
720 unsigned int higha;
721 char a[_a_high+1];
723 /* make a local copy of each unbounded array. */
724 memcpy (a, a_, _a_high+1);
726 StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high);
727 higha = StrLib_StrLen ((const char *) a, _a_high);
728 i = 0;
729 ok = true;
730 while (ok)
732 if (i < higha)
734 if ((a[i] < '0') || (a[i] > '7'))
736 i += 1;
738 else
740 ok = false;
743 else
745 ok = false;
748 (*x) = 0;
749 if (i < higha)
751 ok = true;
752 do {
753 (*x) = (8*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0'))));
754 if (i < higha)
756 /* avoid dangling else. */
757 i += 1;
758 if ((a[i] < '0') || (a[i] > '7'))
760 ok = false;
763 else
765 ok = false;
767 } while (! (! ok));
771 extern "C" void _M2_NumberIO_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
775 extern "C" void _M2_NumberIO_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])