1 /* do not edit automatically generated by mc from NumberIO. */
2 /* NumberIO.mod provides conversion of ordinal numbers.
4 Copyright (C) 2001-2024 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)
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/>. */
31 # if !defined (PROC_D)
33 typedef void (*PROC_t
) (void);
34 typedef struct { PROC_t proc
; } PROC
;
47 #include "GNumberIO.h"
53 # define MaxLineLength 79
55 # define MaxHexDigits 20
56 # define MaxOctDigits 40
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]; };
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]; };
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]; };
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]; };
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]; };
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]; };
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]; };
163 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
166 __builtin_unreachable ();
168 buf
.array
[i
-1] = x
% 10;
170 } while (! (x
== 0));
173 while ((n
> i
) && (j
<= Higha
))
175 const_cast<char *>(a
)[j
] = ' ';
179 while ((i
> 0) && (j
<= Higha
))
181 const_cast<char *>(a
)[j
] = ((char) (buf
.array
[i
-1]+ ((unsigned int) ('0'))));
187 const_cast<char *>(a
)[j
] = ASCII_nul
;
191 extern "C" void NumberIO_StrToCard (const char *a_
, unsigned int _a_high
, unsigned int *x
)
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
);
209 if ((a
[i
] < '0') || (a
[i
] > '9'))
228 (*x
) = (10*(*x
))+( ((unsigned int) (a
[i
]))- ((unsigned int) ('0')));
231 /* avoid dangling else. */
233 if ((a
[i
] < '0') || (a
[i
] > '9'))
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]; };
259 if (i
> MaxHexDigits
)
261 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
264 __builtin_unreachable ();
266 buf
.array
[i
-1] = x
% 0x010;
268 } while (! (x
== 0));
271 while ((n
> i
) && (j
<= Higha
))
273 const_cast<char *>(a
)[j
] = '0';
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'))));
285 const_cast<char *>(a
)[j
] = ((char) ((buf
.array
[i
-1]+ ((unsigned int) ('A')))-10));
292 const_cast<char *>(a
)[j
] = ASCII_nul
;
296 extern "C" void NumberIO_StrToHex (const char *a_
, unsigned int _a_high
, unsigned int *x
)
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]; };
322 /* avoid dangling else. */
324 c
= ((unsigned int ) (abs (x
+1)))+1;
340 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
343 __builtin_unreachable ();
345 buf
.array
[i
-1] = c
% 10;
347 } while (! (c
== 0));
350 while ((n
> i
) && (j
<= Higha
))
352 const_cast<char *>(a
)[j
] = ' ';
358 const_cast<char *>(a
)[j
] = '-';
361 while ((i
!= 0) && (j
<= Higha
))
363 const_cast<char *>(a
)[j
] = ((char) (buf
.array
[i
-1]+ ((unsigned int) ('0'))));
369 const_cast<char *>(a
)[j
] = ASCII_nul
;
373 extern "C" void NumberIO_StrToInt (const char *a_
, unsigned int _a_high
, int *x
)
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
);
396 Negative
= ! Negative
;
398 else if ((a
[i
] < '0') || (a
[i
] > '9'))
400 /* avoid dangling else. */
405 /* avoid dangling else. */
421 (*x
) = (10*(*x
))-((int ) ( ((unsigned int) (a
[i
]))- ((unsigned int) ('0'))));
425 (*x
) = (10*(*x
))+((int ) ( ((unsigned int) (a
[i
]))- ((unsigned int) ('0'))));
429 /* avoid dangling else. */
431 if ((a
[i
] < '0') || (a
[i
] > '9'))
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]; };
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]; };
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]; };
479 if (i
> MaxOctDigits
)
481 StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29);
484 __builtin_unreachable ();
486 buf
.array
[i
-1] = x
% 8;
488 } while (! (x
== 0));
491 while ((n
> i
) && (j
<= Higha
))
493 const_cast<char *>(a
)[j
] = ' ';
497 while ((i
> 0) && (j
<= Higha
))
499 const_cast<char *>(a
)[j
] = ((char) (buf
.array
[i
-1]+ ((unsigned int) ('0'))));
505 const_cast<char *>(a
)[j
] = ASCII_nul
;
509 extern "C" void NumberIO_StrToOct (const char *a_
, unsigned int _a_high
, unsigned int *x
)
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]; };
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]; };
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]; };
558 StrIO_WriteString ((const char *) "NumberIO - increase MaxBits", 27);
561 __builtin_unreachable ();
563 buf
.array
[i
-1] = x
% 2;
565 } while (! (x
== 0));
568 while ((n
> i
) && (j
<= Higha
))
570 const_cast<char *>(a
)[j
] = ' ';
574 while ((i
> 0) && (j
<= Higha
))
576 const_cast<char *>(a
)[j
] = ((char) (buf
.array
[i
-1]+ ((unsigned int) ('0'))));
582 const_cast<char *>(a
)[j
] = ASCII_nul
;
586 extern "C" void NumberIO_StrToBin (const char *a_
, unsigned int _a_high
, unsigned int *x
)
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
)
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
);
616 if ((a
[i
] < '0') || (a
[i
] > '1'))
635 (*x
) = (2*(*x
))+((int ) ( ((unsigned int) (a
[i
]))- ((unsigned int) ('0'))));
638 /* avoid dangling else. */
640 if ((a
[i
] < '0') || (a
[i
] > '1'))
653 extern "C" void NumberIO_StrToHexInt (const char *a_
, unsigned int _a_high
, int *x
)
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
);
671 if (((a
[i
] >= '0') && (a
[i
] <= '9')) || ((a
[i
] >= 'A') && (a
[i
] <= 'F')))
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));
701 /* avoid dangling else. */
703 if (((a
[i
] < '0') || (a
[i
] > '9')) && ((a
[i
] < 'A') || (a
[i
] > 'F')))
716 extern "C" void NumberIO_StrToOctInt (const char *a_
, unsigned int _a_high
, int *x
)
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
);
734 if ((a
[i
] < '0') || (a
[i
] > '7'))
753 (*x
) = (8*(*x
))+((int ) ( ((unsigned int) (a
[i
]))- ((unsigned int) ('0'))));
756 /* avoid dangling else. */
758 if ((a
[i
] < '0') || (a
[i
] > '7'))
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
[])