00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "char.h"
00018 #include "str.h"
00019
00020 static inline
00021 int char_todigit(char c)
00022 {
00023 if (c >= '0' && c <= '9')
00024 return c - '0';
00025
00026 else if (c >= 'A' && c <= 'Z')
00027 return c - 'A' + 10;
00028
00029 else if (c >= 'a' && c <= 'z')
00030 return c - 'a' + 10;
00031
00032 else
00033 return -1;
00034 }
00035
00036 int str_toumax(const char *str, unsigned long long int *val, int base, int n)
00037 {
00038 char c;
00039 const char *p = str;
00040 int d, minus = 0;
00041 unsigned long long int v = 0;
00042
00043 while (n && char_isspace((unsigned char) *p)) {
00044 p++;
00045 n--;
00046 }
00047
00048
00049 if (n) {
00050 c = *p;
00051
00052 if (c == '-' || c == '+') {
00053 minus = (c == '-');
00054 p++;
00055 n--;
00056 }
00057 }
00058
00059 if (base == 0) {
00060 if (n >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
00061 n -= 2;
00062 p += 2;
00063 base = 16;
00064 }
00065
00066 else if (n >= 1 && p[0] == '0') {
00067 n--;
00068 p++;
00069 base = 8;
00070 }
00071
00072 else {
00073 base = 10;
00074 }
00075 }
00076
00077 else if (base == 16) {
00078 if (n >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
00079 n -= 2;
00080 p += 2;
00081 }
00082 }
00083
00084 while (n && (d = char_todigit(*p)) >= 0 && d < base) {
00085 v = v * base + d;
00086 n--;
00087 p++;
00088 }
00089
00090 if (p - str > 0)
00091 *val = minus ? -v : v;
00092
00093 return p - str;
00094 }