| 1 |
/* do not edit automatically generated by mc from NumberIO. */ |
| 2 |
/* NumberIO.mod provides conversion of ordinal numbers. |
| 3 |
|
| 4 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 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. |
| 13 |
|
| 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. |
| 18 |
|
| 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. |
| 22 |
|
| 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/>. */ |
| 27 |
|
| 28 |
#include <stdbool.h> |
| 29 |
# if !defined (PROC_D) |
| 30 |
# define PROC_D |
| 31 |
typedef void (*PROC_t) (void); |
| 32 |
typedef struct { PROC_t proc; } PROC; |
| 33 |
# endif |
| 34 |
|
| 35 |
# if !defined (TRUE) |
| 36 |
# define TRUE (1==1) |
| 37 |
# endif |
| 38 |
|
| 39 |
# if !defined (FALSE) |
| 40 |
# define FALSE (1==0) |
| 41 |
# endif |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
#include <limits.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#define _NumberIO_C |
| 47 |
|
| 48 |
#include "GNumberIO.h" |
| 49 |
# include "GASCII.h" |
| 50 |
# include "GStrIO.h" |
| 51 |
# include "GStrLib.h" |
| 52 |
# include "GM2RTS.h" |
| 53 |
|
| 54 |
# define MaxLineLength 79 |
| 55 |
# define MaxDigits 20 |
| 56 |
# define MaxHexDigits 20 |
| 57 |
# define MaxOctDigits 40 |
| 58 |
# define MaxBits 64 |
| 59 |
extern "C" void NumberIO_ReadCard (unsigned int *x); |
| 60 |
extern "C" void NumberIO_WriteCard (unsigned int x, unsigned int n); |
| 61 |
extern "C" void NumberIO_ReadHex (unsigned int *x); |
| 62 |
extern "C" void NumberIO_WriteHex (unsigned int x, unsigned int n); |
| 63 |
extern "C" void NumberIO_ReadInt (int *x); |
| 64 |
extern "C" void NumberIO_WriteInt (int x, unsigned int n); |
| 65 |
extern "C" void NumberIO_CardToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high); |
| 66 |
extern "C" void NumberIO_StrToCard (const char *a_, unsigned int _a_high, unsigned int *x); |
| 67 |
extern "C" void NumberIO_HexToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high); |
| 68 |
extern "C" void NumberIO_StrToHex (const char *a_, unsigned int _a_high, unsigned int *x); |
| 69 |
extern "C" void NumberIO_IntToStr (int x, unsigned int n, char *a, unsigned int _a_high); |
| 70 |
extern "C" void NumberIO_StrToInt (const char *a_, unsigned int _a_high, int *x); |
| 71 |
extern "C" void NumberIO_ReadOct (unsigned int *x); |
| 72 |
extern "C" void NumberIO_WriteOct (unsigned int x, unsigned int n); |
| 73 |
extern "C" void NumberIO_OctToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high); |
| 74 |
extern "C" void NumberIO_StrToOct (const char *a_, unsigned int _a_high, unsigned int *x); |
| 75 |
extern "C" void NumberIO_ReadBin (unsigned int *x); |
| 76 |
extern "C" void NumberIO_WriteBin (unsigned int x, unsigned int n); |
| 77 |
extern "C" void NumberIO_BinToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high); |
| 78 |
extern "C" void NumberIO_StrToBin (const char *a_, unsigned int _a_high, unsigned int *x); |
| 79 |
extern "C" void NumberIO_StrToBinInt (const char *a_, unsigned int _a_high, int *x); |
| 80 |
extern "C" void NumberIO_StrToHexInt (const char *a_, unsigned int _a_high, int *x); |
| 81 |
extern "C" void NumberIO_StrToOctInt (const char *a_, unsigned int _a_high, int *x); |
| 82 |
|
| 83 |
extern "C" void NumberIO_ReadCard (unsigned int *x) |
| 84 |
{ |
| 85 |
typedef struct ReadCard__T1_a ReadCard__T1; |
| 86 |
|
| 87 |
struct ReadCard__T1_a { char array[MaxLineLength+1]; }; |
| 88 |
ReadCard__T1 a; |
| 89 |
|
| 90 |
StrIO_ReadString ((char *) &a.array[0], MaxLineLength); |
| 91 |
NumberIO_StrToCard ((const char *) &a.array[0], MaxLineLength, x); |
| 92 |
} |
| 93 |
|
| 94 |
extern "C" void NumberIO_WriteCard (unsigned int x, unsigned int n) |
| 95 |
{ |
| 96 |
typedef struct WriteCard__T2_a WriteCard__T2; |
| 97 |
|
| 98 |
struct WriteCard__T2_a { char array[MaxLineLength+1]; }; |
| 99 |
WriteCard__T2 a; |
| 100 |
|
| 101 |
NumberIO_CardToStr (x, n, (char *) &a.array[0], MaxLineLength); |
| 102 |
StrIO_WriteString ((const char *) &a.array[0], MaxLineLength); |
| 103 |
} |
| 104 |
|
| 105 |
extern "C" void NumberIO_ReadHex (unsigned int *x) |
| 106 |
{ |
| 107 |
typedef struct ReadHex__T3_a ReadHex__T3; |
| 108 |
|
| 109 |
struct ReadHex__T3_a { char array[MaxLineLength+1]; }; |
| 110 |
ReadHex__T3 a; |
| 111 |
|
| 112 |
StrIO_ReadString ((char *) &a.array[0], MaxLineLength); |
| 113 |
NumberIO_StrToHex ((const char *) &a.array[0], MaxLineLength, x); |
| 114 |
} |
| 115 |
|
| 116 |
extern "C" void NumberIO_WriteHex (unsigned int x, unsigned int n) |
| 117 |
{ |
| 118 |
typedef struct WriteHex__T4_a WriteHex__T4; |
| 119 |
|
| 120 |
struct WriteHex__T4_a { char array[MaxLineLength+1]; }; |
| 121 |
WriteHex__T4 a; |
| 122 |
|
| 123 |
NumberIO_HexToStr (x, n, (char *) &a.array[0], MaxLineLength); |
| 124 |
StrIO_WriteString ((const char *) &a.array[0], MaxLineLength); |
| 125 |
} |
| 126 |
|
| 127 |
extern "C" void NumberIO_ReadInt (int *x) |
| 128 |
{ |
| 129 |
typedef struct ReadInt__T5_a ReadInt__T5; |
| 130 |
|
| 131 |
struct ReadInt__T5_a { char array[MaxLineLength+1]; }; |
| 132 |
ReadInt__T5 a; |
| 133 |
|
| 134 |
StrIO_ReadString ((char *) &a.array[0], MaxLineLength); |
| 135 |
NumberIO_StrToInt ((const char *) &a.array[0], MaxLineLength, x); |
| 136 |
} |
| 137 |
|
| 138 |
extern "C" void NumberIO_WriteInt (int x, unsigned int n) |
| 139 |
{ |
| 140 |
typedef struct WriteInt__T6_a WriteInt__T6; |
| 141 |
|
| 142 |
struct WriteInt__T6_a { char array[MaxLineLength+1]; }; |
| 143 |
WriteInt__T6 a; |
| 144 |
|
| 145 |
NumberIO_IntToStr (x, n, (char *) &a.array[0], MaxLineLength); |
| 146 |
StrIO_WriteString ((const char *) &a.array[0], MaxLineLength); |
| 147 |
} |
| 148 |
|
| 149 |
extern "C" void NumberIO_CardToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high) |
| 150 |
{ |
| 151 |
typedef struct CardToStr__T7_a CardToStr__T7; |
| 152 |
|
| 153 |
struct CardToStr__T7_a { unsigned int array[MaxDigits-1+1]; }; |
| 154 |
unsigned int i; |
| 155 |
unsigned int j; |
| 156 |
unsigned int Higha; |
| 157 |
CardToStr__T7 buf; |
| 158 |
|
| 159 |
i = 0; |
| 160 |
do { |
| 161 |
i += 1; |
| 162 |
if (i > MaxDigits) |
| 163 |
{ |
| 164 |
StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29); |
| 165 |
StrIO_WriteLn (); |
| 166 |
M2RTS_HALT (-1); |
| 167 |
__builtin_unreachable (); |
| 168 |
} |
| 169 |
buf.array[i-1] = x % 10; |
| 170 |
x = x / 10; |
| 171 |
} while (! (x == 0)); |
| 172 |
j = 0; |
| 173 |
Higha = _a_high; |
| 174 |
while ((n > i) && (j <= Higha)) |
| 175 |
{ |
| 176 |
const_cast<char *>(a)[j] = ' '; |
| 177 |
j += 1; |
| 178 |
n -= 1; |
| 179 |
} |
| 180 |
while ((i > 0) && (j <= Higha)) |
| 181 |
{ |
| 182 |
const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0')))); |
| 183 |
j += 1; |
| 184 |
i -= 1; |
| 185 |
} |
| 186 |
if (j <= Higha) |
| 187 |
{ |
| 188 |
const_cast<char *>(a)[j] = ASCII_nul; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
extern "C" void NumberIO_StrToCard (const char *a_, unsigned int _a_high, unsigned int *x) |
| 193 |
{ |
| 194 |
unsigned int i; |
| 195 |
bool ok; |
| 196 |
unsigned int higha; |
| 197 |
char a[_a_high+1]; |
| 198 |
|
| 199 |
/* make a local copy of each unbounded array. */ |
| 200 |
memcpy (a, a_, _a_high+1); |
| 201 |
|
| 202 |
StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high); |
| 203 |
higha = StrLib_StrLen ((const char *) a, _a_high); |
| 204 |
i = 0; |
| 205 |
ok = true; |
| 206 |
while (ok) |
| 207 |
{ |
| 208 |
if (i < higha) |
| 209 |
{ |
| 210 |
if ((a[i] < '0') || (a[i] > '9')) |
| 211 |
{ |
| 212 |
i += 1; |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
ok = false; |
| 217 |
} |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
ok = false; |
| 222 |
} |
| 223 |
} |
| 224 |
(*x) = 0; |
| 225 |
if (i < higha) |
| 226 |
{ |
| 227 |
ok = true; |
| 228 |
do { |
| 229 |
(*x) = (10*(*x))+( ((unsigned int) (a[i]))- ((unsigned int) ('0'))); |
| 230 |
if (i < higha) |
| 231 |
{ |
| 232 |
/* avoid dangling else. */ |
| 233 |
i += 1; |
| 234 |
if ((a[i] < '0') || (a[i] > '9')) |
| 235 |
{ |
| 236 |
ok = false; |
| 237 |
} |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
ok = false; |
| 242 |
} |
| 243 |
} while (! (! ok)); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
extern "C" void NumberIO_HexToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high) |
| 248 |
{ |
| 249 |
typedef struct HexToStr__T8_a HexToStr__T8; |
| 250 |
|
| 251 |
struct HexToStr__T8_a { unsigned int array[MaxHexDigits-1+1]; }; |
| 252 |
unsigned int i; |
| 253 |
unsigned int j; |
| 254 |
unsigned int Higha; |
| 255 |
HexToStr__T8 buf; |
| 256 |
|
| 257 |
i = 0; |
| 258 |
do { |
| 259 |
i += 1; |
| 260 |
if (i > MaxHexDigits) |
| 261 |
{ |
| 262 |
StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29); |
| 263 |
StrIO_WriteLn (); |
| 264 |
M2RTS_HALT (-1); |
| 265 |
__builtin_unreachable (); |
| 266 |
} |
| 267 |
buf.array[i-1] = x % 0x010; |
| 268 |
x = x / 0x010; |
| 269 |
} while (! (x == 0)); |
| 270 |
j = 0; |
| 271 |
Higha = _a_high; |
| 272 |
while ((n > i) && (j <= Higha)) |
| 273 |
{ |
| 274 |
const_cast<char *>(a)[j] = '0'; |
| 275 |
j += 1; |
| 276 |
n -= 1; |
| 277 |
} |
| 278 |
while ((i != 0) && (j <= Higha)) |
| 279 |
{ |
| 280 |
if (buf.array[i-1] < 10) |
| 281 |
{ |
| 282 |
const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0')))); |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
const_cast<char *>(a)[j] = ((char) ((buf.array[i-1]+ ((unsigned int) ('A')))-10)); |
| 287 |
} |
| 288 |
j += 1; |
| 289 |
i -= 1; |
| 290 |
} |
| 291 |
if (j <= Higha) |
| 292 |
{ |
| 293 |
const_cast<char *>(a)[j] = ASCII_nul; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
extern "C" void NumberIO_StrToHex (const char *a_, unsigned int _a_high, unsigned int *x) |
| 298 |
{ |
| 299 |
int i; |
| 300 |
char a[_a_high+1]; |
| 301 |
|
| 302 |
/* make a local copy of each unbounded array. */ |
| 303 |
memcpy (a, a_, _a_high+1); |
| 304 |
|
| 305 |
NumberIO_StrToHexInt ((const char *) a, _a_high, &i); |
| 306 |
(*x) = (unsigned int ) (i); |
| 307 |
} |
| 308 |
|
| 309 |
extern "C" void NumberIO_IntToStr (int x, unsigned int n, char *a, unsigned int _a_high) |
| 310 |
{ |
| 311 |
typedef struct IntToStr__T9_a IntToStr__T9; |
| 312 |
|
| 313 |
struct IntToStr__T9_a { unsigned int array[MaxDigits-1+1]; }; |
| 314 |
unsigned int i; |
| 315 |
unsigned int j; |
| 316 |
unsigned int c; |
| 317 |
unsigned int Higha; |
| 318 |
IntToStr__T9 buf; |
| 319 |
bool Negative; |
| 320 |
|
| 321 |
if (x < 0) |
| 322 |
{ |
| 323 |
/* avoid dangling else. */ |
| 324 |
Negative = true; |
| 325 |
c = ((unsigned int ) (abs (x+1)))+1; |
| 326 |
if (n > 0) |
| 327 |
{ |
| 328 |
n -= 1; |
| 329 |
} |
| 330 |
} |
| 331 |
else |
| 332 |
{ |
| 333 |
c = x; |
| 334 |
Negative = false; |
| 335 |
} |
| 336 |
i = 0; |
| 337 |
do { |
| 338 |
i += 1; |
| 339 |
if (i > MaxDigits) |
| 340 |
{ |
| 341 |
StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29); |
| 342 |
StrIO_WriteLn (); |
| 343 |
M2RTS_HALT (-1); |
| 344 |
__builtin_unreachable (); |
| 345 |
} |
| 346 |
buf.array[i-1] = c % 10; |
| 347 |
c = c / 10; |
| 348 |
} while (! (c == 0)); |
| 349 |
j = 0; |
| 350 |
Higha = _a_high; |
| 351 |
while ((n > i) && (j <= Higha)) |
| 352 |
{ |
| 353 |
const_cast<char *>(a)[j] = ' '; |
| 354 |
j += 1; |
| 355 |
n -= 1; |
| 356 |
} |
| 357 |
if (Negative) |
| 358 |
{ |
| 359 |
const_cast<char *>(a)[j] = '-'; |
| 360 |
j += 1; |
| 361 |
} |
| 362 |
while ((i != 0) && (j <= Higha)) |
| 363 |
{ |
| 364 |
const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0')))); |
| 365 |
j += 1; |
| 366 |
i -= 1; |
| 367 |
} |
| 368 |
if (j <= Higha) |
| 369 |
{ |
| 370 |
const_cast<char *>(a)[j] = ASCII_nul; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
extern "C" void NumberIO_StrToInt (const char *a_, unsigned int _a_high, int *x) |
| 375 |
{ |
| 376 |
unsigned int i; |
| 377 |
bool ok; |
| 378 |
bool Negative; |
| 379 |
unsigned int higha; |
| 380 |
char a[_a_high+1]; |
| 381 |
|
| 382 |
/* make a local copy of each unbounded array. */ |
| 383 |
memcpy (a, a_, _a_high+1); |
| 384 |
|
| 385 |
StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high); |
| 386 |
higha = StrLib_StrLen ((const char *) a, _a_high); |
| 387 |
i = 0; |
| 388 |
Negative = false; |
| 389 |
ok = true; |
| 390 |
while (ok) |
| 391 |
{ |
| 392 |
if (i < higha) |
| 393 |
{ |
| 394 |
if (a[i] == '-') |
| 395 |
{ |
| 396 |
i += 1; |
| 397 |
Negative = ! Negative; |
| 398 |
} |
| 399 |
else if ((a[i] < '0') || (a[i] > '9')) |
| 400 |
{ |
| 401 |
/* avoid dangling else. */ |
| 402 |
i += 1; |
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
/* avoid dangling else. */ |
| 407 |
ok = false; |
| 408 |
} |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
ok = false; |
| 413 |
} |
| 414 |
} |
| 415 |
(*x) = 0; |
| 416 |
if (i < higha) |
| 417 |
{ |
| 418 |
ok = true; |
| 419 |
do { |
| 420 |
if (Negative) |
| 421 |
{ |
| 422 |
(*x) = (10*(*x))-((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0')))); |
| 423 |
} |
| 424 |
else |
| 425 |
{ |
| 426 |
(*x) = (10*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0')))); |
| 427 |
} |
| 428 |
if (i < higha) |
| 429 |
{ |
| 430 |
/* avoid dangling else. */ |
| 431 |
i += 1; |
| 432 |
if ((a[i] < '0') || (a[i] > '9')) |
| 433 |
{ |
| 434 |
ok = false; |
| 435 |
} |
| 436 |
} |
| 437 |
else |
| 438 |
{ |
| 439 |
ok = false; |
| 440 |
} |
| 441 |
} while (! (! ok)); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
extern "C" void NumberIO_ReadOct (unsigned int *x) |
| 446 |
{ |
| 447 |
typedef struct ReadOct__T10_a ReadOct__T10; |
| 448 |
|
| 449 |
struct ReadOct__T10_a { char array[MaxLineLength+1]; }; |
| 450 |
ReadOct__T10 a; |
| 451 |
|
| 452 |
StrIO_ReadString ((char *) &a.array[0], MaxLineLength); |
| 453 |
NumberIO_StrToOct ((const char *) &a.array[0], MaxLineLength, x); |
| 454 |
} |
| 455 |
|
| 456 |
extern "C" void NumberIO_WriteOct (unsigned int x, unsigned int n) |
| 457 |
{ |
| 458 |
typedef struct WriteOct__T11_a WriteOct__T11; |
| 459 |
|
| 460 |
struct WriteOct__T11_a { char array[MaxLineLength+1]; }; |
| 461 |
WriteOct__T11 a; |
| 462 |
|
| 463 |
NumberIO_OctToStr (x, n, (char *) &a.array[0], MaxLineLength); |
| 464 |
StrIO_WriteString ((const char *) &a.array[0], MaxLineLength); |
| 465 |
} |
| 466 |
|
| 467 |
extern "C" void NumberIO_OctToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high) |
| 468 |
{ |
| 469 |
typedef struct OctToStr__T12_a OctToStr__T12; |
| 470 |
|
| 471 |
struct OctToStr__T12_a { unsigned int array[MaxOctDigits-1+1]; }; |
| 472 |
unsigned int i; |
| 473 |
unsigned int j; |
| 474 |
unsigned int Higha; |
| 475 |
OctToStr__T12 buf; |
| 476 |
|
| 477 |
i = 0; |
| 478 |
do { |
| 479 |
i += 1; |
| 480 |
if (i > MaxOctDigits) |
| 481 |
{ |
| 482 |
StrIO_WriteString ((const char *) "NumberIO - increase MaxDigits", 29); |
| 483 |
StrIO_WriteLn (); |
| 484 |
M2RTS_HALT (-1); |
| 485 |
__builtin_unreachable (); |
| 486 |
} |
| 487 |
buf.array[i-1] = x % 8; |
| 488 |
x = x / 8; |
| 489 |
} while (! (x == 0)); |
| 490 |
j = 0; |
| 491 |
Higha = _a_high; |
| 492 |
while ((n > i) && (j <= Higha)) |
| 493 |
{ |
| 494 |
const_cast<char *>(a)[j] = ' '; |
| 495 |
j += 1; |
| 496 |
n -= 1; |
| 497 |
} |
| 498 |
while ((i > 0) && (j <= Higha)) |
| 499 |
{ |
| 500 |
const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0')))); |
| 501 |
j += 1; |
| 502 |
i -= 1; |
| 503 |
} |
| 504 |
if (j <= Higha) |
| 505 |
{ |
| 506 |
const_cast<char *>(a)[j] = ASCII_nul; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
extern "C" void NumberIO_StrToOct (const char *a_, unsigned int _a_high, unsigned int *x) |
| 511 |
{ |
| 512 |
int i; |
| 513 |
char a[_a_high+1]; |
| 514 |
|
| 515 |
/* make a local copy of each unbounded array. */ |
| 516 |
memcpy (a, a_, _a_high+1); |
| 517 |
|
| 518 |
NumberIO_StrToOctInt ((const char *) a, _a_high, &i); |
| 519 |
(*x) = (unsigned int ) (i); |
| 520 |
} |
| 521 |
|
| 522 |
extern "C" void NumberIO_ReadBin (unsigned int *x) |
| 523 |
{ |
| 524 |
typedef struct ReadBin__T13_a ReadBin__T13; |
| 525 |
|
| 526 |
struct ReadBin__T13_a { char array[MaxLineLength+1]; }; |
| 527 |
ReadBin__T13 a; |
| 528 |
|
| 529 |
StrIO_ReadString ((char *) &a.array[0], MaxLineLength); |
| 530 |
NumberIO_StrToBin ((const char *) &a.array[0], MaxLineLength, x); |
| 531 |
} |
| 532 |
|
| 533 |
extern "C" void NumberIO_WriteBin (unsigned int x, unsigned int n) |
| 534 |
{ |
| 535 |
typedef struct WriteBin__T14_a WriteBin__T14; |
| 536 |
|
| 537 |
struct WriteBin__T14_a { char array[MaxLineLength+1]; }; |
| 538 |
WriteBin__T14 a; |
| 539 |
|
| 540 |
NumberIO_BinToStr (x, n, (char *) &a.array[0], MaxLineLength); |
| 541 |
StrIO_WriteString ((const char *) &a.array[0], MaxLineLength); |
| 542 |
} |
| 543 |
|
| 544 |
extern "C" void NumberIO_BinToStr (unsigned int x, unsigned int n, char *a, unsigned int _a_high) |
| 545 |
{ |
| 546 |
typedef struct BinToStr__T15_a BinToStr__T15; |
| 547 |
|
| 548 |
struct BinToStr__T15_a { unsigned int array[MaxBits-1+1]; }; |
| 549 |
unsigned int i; |
| 550 |
unsigned int j; |
| 551 |
unsigned int Higha; |
| 552 |
BinToStr__T15 buf; |
| 553 |
|
| 554 |
i = 0; |
| 555 |
do { |
| 556 |
i += 1; |
| 557 |
if (i > MaxBits) |
| 558 |
{ |
| 559 |
StrIO_WriteString ((const char *) "NumberIO - increase MaxBits", 27); |
| 560 |
StrIO_WriteLn (); |
| 561 |
M2RTS_HALT (-1); |
| 562 |
__builtin_unreachable (); |
| 563 |
} |
| 564 |
buf.array[i-1] = x % 2; |
| 565 |
x = x / 2; |
| 566 |
} while (! (x == 0)); |
| 567 |
j = 0; |
| 568 |
Higha = _a_high; |
| 569 |
while ((n > i) && (j <= Higha)) |
| 570 |
{ |
| 571 |
const_cast<char *>(a)[j] = ' '; |
| 572 |
j += 1; |
| 573 |
n -= 1; |
| 574 |
} |
| 575 |
while ((i > 0) && (j <= Higha)) |
| 576 |
{ |
| 577 |
const_cast<char *>(a)[j] = ((char) (buf.array[i-1]+ ((unsigned int) ('0')))); |
| 578 |
j += 1; |
| 579 |
i -= 1; |
| 580 |
} |
| 581 |
if (j <= Higha) |
| 582 |
{ |
| 583 |
const_cast<char *>(a)[j] = ASCII_nul; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
extern "C" void NumberIO_StrToBin (const char *a_, unsigned int _a_high, unsigned int *x) |
| 588 |
{ |
| 589 |
int i; |
| 590 |
char a[_a_high+1]; |
| 591 |
|
| 592 |
/* make a local copy of each unbounded array. */ |
| 593 |
memcpy (a, a_, _a_high+1); |
| 594 |
|
| 595 |
NumberIO_StrToBinInt ((const char *) a, _a_high, &i); |
| 596 |
(*x) = (unsigned int ) (i); |
| 597 |
} |
| 598 |
|
| 599 |
extern "C" void NumberIO_StrToBinInt (const char *a_, unsigned int _a_high, int *x) |
| 600 |
{ |
| 601 |
unsigned int i; |
| 602 |
bool ok; |
| 603 |
unsigned int higha; |
| 604 |
char a[_a_high+1]; |
| 605 |
|
| 606 |
/* make a local copy of each unbounded array. */ |
| 607 |
memcpy (a, a_, _a_high+1); |
| 608 |
|
| 609 |
StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high); |
| 610 |
higha = StrLib_StrLen ((const char *) a, _a_high); |
| 611 |
i = 0; |
| 612 |
ok = true; |
| 613 |
while (ok) |
| 614 |
{ |
| 615 |
if (i < higha) |
| 616 |
{ |
| 617 |
if ((a[i] < '0') || (a[i] > '1')) |
| 618 |
{ |
| 619 |
i += 1; |
| 620 |
} |
| 621 |
else |
| 622 |
{ |
| 623 |
ok = false; |
| 624 |
} |
| 625 |
} |
| 626 |
else |
| 627 |
{ |
| 628 |
ok = false; |
| 629 |
} |
| 630 |
} |
| 631 |
(*x) = 0; |
| 632 |
if (i < higha) |
| 633 |
{ |
| 634 |
ok = true; |
| 635 |
do { |
| 636 |
(*x) = (2*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0')))); |
| 637 |
if (i < higha) |
| 638 |
{ |
| 639 |
/* avoid dangling else. */ |
| 640 |
i += 1; |
| 641 |
if ((a[i] < '0') || (a[i] > '1')) |
| 642 |
{ |
| 643 |
ok = false; |
| 644 |
} |
| 645 |
} |
| 646 |
else |
| 647 |
{ |
| 648 |
ok = false; |
| 649 |
} |
| 650 |
} while (! (! ok)); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
extern "C" void NumberIO_StrToHexInt (const char *a_, unsigned int _a_high, int *x) |
| 655 |
{ |
| 656 |
unsigned int i; |
| 657 |
bool ok; |
| 658 |
unsigned int higha; |
| 659 |
char a[_a_high+1]; |
| 660 |
|
| 661 |
/* make a local copy of each unbounded array. */ |
| 662 |
memcpy (a, a_, _a_high+1); |
| 663 |
|
| 664 |
StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high); |
| 665 |
higha = StrLib_StrLen ((const char *) a, _a_high); |
| 666 |
i = 0; |
| 667 |
ok = true; |
| 668 |
while (ok) |
| 669 |
{ |
| 670 |
if (i < higha) |
| 671 |
{ |
| 672 |
if (((a[i] >= '0') && (a[i] <= '9')) || ((a[i] >= 'A') && (a[i] <= 'F'))) |
| 673 |
{ |
| 674 |
ok = false; |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
i += 1; |
| 679 |
} |
| 680 |
} |
| 681 |
else |
| 682 |
{ |
| 683 |
ok = false; |
| 684 |
} |
| 685 |
} |
| 686 |
(*x) = 0; |
| 687 |
if (i < higha) |
| 688 |
{ |
| 689 |
ok = true; |
| 690 |
do { |
| 691 |
if ((a[i] >= '0') && (a[i] <= '9')) |
| 692 |
{ |
| 693 |
(*x) = (0x010*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0')))); |
| 694 |
} |
| 695 |
else if ((a[i] >= 'A') && (a[i] <= 'F')) |
| 696 |
{ |
| 697 |
/* avoid dangling else. */ |
| 698 |
(*x) = (0x010*(*x))+((int ) (( ((unsigned int) (a[i]))- ((unsigned int) ('A')))+10)); |
| 699 |
} |
| 700 |
if (i < higha) |
| 701 |
{ |
| 702 |
/* avoid dangling else. */ |
| 703 |
i += 1; |
| 704 |
if (((a[i] < '0') || (a[i] > '9')) && ((a[i] < 'A') || (a[i] > 'F'))) |
| 705 |
{ |
| 706 |
ok = false; |
| 707 |
} |
| 708 |
} |
| 709 |
else |
| 710 |
{ |
| 711 |
ok = false; |
| 712 |
} |
| 713 |
} while (! (! ok)); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
extern "C" void NumberIO_StrToOctInt (const char *a_, unsigned int _a_high, int *x) |
| 718 |
{ |
| 719 |
unsigned int i; |
| 720 |
bool ok; |
| 721 |
unsigned int higha; |
| 722 |
char a[_a_high+1]; |
| 723 |
|
| 724 |
/* make a local copy of each unbounded array. */ |
| 725 |
memcpy (a, a_, _a_high+1); |
| 726 |
|
| 727 |
StrLib_StrRemoveWhitePrefix ((const char *) a, _a_high, (char *) a, _a_high); |
| 728 |
higha = StrLib_StrLen ((const char *) a, _a_high); |
| 729 |
i = 0; |
| 730 |
ok = true; |
| 731 |
while (ok) |
| 732 |
{ |
| 733 |
if (i < higha) |
| 734 |
{ |
| 735 |
if ((a[i] < '0') || (a[i] > '7')) |
| 736 |
{ |
| 737 |
i += 1; |
| 738 |
} |
| 739 |
else |
| 740 |
{ |
| 741 |
ok = false; |
| 742 |
} |
| 743 |
} |
| 744 |
else |
| 745 |
{ |
| 746 |
ok = false; |
| 747 |
} |
| 748 |
} |
| 749 |
(*x) = 0; |
| 750 |
if (i < higha) |
| 751 |
{ |
| 752 |
ok = true; |
| 753 |
do { |
| 754 |
(*x) = (8*(*x))+((int ) ( ((unsigned int) (a[i]))- ((unsigned int) ('0')))); |
| 755 |
if (i < higha) |
| 756 |
{ |
| 757 |
/* avoid dangling else. */ |
| 758 |
i += 1; |
| 759 |
if ((a[i] < '0') || (a[i] > '7')) |
| 760 |
{ |
| 761 |
ok = false; |
| 762 |
} |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
ok = false; |
| 767 |
} |
| 768 |
} while (! (! ok)); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
extern "C" void _M2_NumberIO_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 773 |
{ |
| 774 |
} |
| 775 |
|
| 776 |
extern "C" void _M2_NumberIO_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 777 |
{ |
| 778 |
} |