summaryrefslogtreecommitdiff
path: root/lib/numbers.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/numbers.c')
-rw-r--r--lib/numbers.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/numbers.c b/lib/numbers.c
new file mode 100644
index 0000000..ba45f29
--- /dev/null
+++ b/lib/numbers.c
@@ -0,0 +1,59 @@
+#include "global.h"
+#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;
+}