From deb75e32768c82d2945427d558f73c64c4c2f72d Mon Sep 17 00:00:00 2001 From: Pixel <> Date: Mon, 5 Mar 2001 14:25:57 +0000 Subject: Ajout de numbers --- lib/numbers.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/numbers.c (limited to 'lib') diff --git a/lib/numbers.c b/lib/numbers.c new file mode 100644 index 0000000..8334dd4 --- /dev/null +++ b/lib/numbers.c @@ -0,0 +1,58 @@ +#include "numbers.h" + +int char_to_number(char *st, int *valid) +{ + int whattype = 0, result = 0; + + *valid = 0; + + if (*st == '0') { + st++; + if (*st == 'x') { + whattype = 1; + st++; + } else if (*st) { + whattype = 2; + } else { + *valid = 1; + return 0; + } + } + + while (*st) { + switch (whattype) { + case 0: + if ((*st < '0') || (*st > '9')) { + return 0; + } + result *= 10; + result += *st - '0'; + break; + case 1: + if (((*st < '0') || (*st > '9')) && ((*st < 'A') || (*st > 'F')) + && ((*st < 'a') || (*st > 'f'))) { + return 0; + } + result *= 16; + if ((*st >= '0') && (*st <= '9')) { + result += *st - '0'; + } else if ((*st >= 'A') && (*st <= 'F')) { + result += *st - 'A' + 10; + } else { + result += *st - 'a' + 10; + } + break; + case 2: + if ((*st < '0') || (*st > '7')) { + return 0; + } + result *= 8; + result += *st - '0'; + break; + } + st++; + } + + *valid = 1; + return result; +} -- cgit v1.2.3