getbase.c (762B)
1 /* Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail> 2 * See LICENSE file for license details. 3 */ 4 #include <ctype.h> 5 #include <string.h> 6 #include <stuff/util.h> 7 8 #define strbcmp(str, const) (strncmp((str), (const), sizeof(const)-1)) 9 int 10 getbase(const char **s, long *sign) { 11 const char *p; 12 int ret; 13 14 ret = 10; 15 *sign = 1; 16 if(**s == '-') { 17 *sign = -1; 18 *s += 1; 19 }else if(**s == '+') 20 *s += 1; 21 22 p = *s; 23 if(!strbcmp(p, "0x")) { 24 *s += 2; 25 ret = 16; 26 } 27 else if(isdigit(p[0])) { 28 if(p[1] == 'r') { 29 *s += 2; 30 ret = p[0] - '0'; 31 } 32 else if(isdigit(p[1]) && p[2] == 'r') { 33 *s += 3; 34 ret = 10*(p[0]-'0') + (p[1]-'0'); 35 } 36 } 37 else if(p[0] == '0') { 38 ret = 8; 39 } 40 if(ret != 10 && (**s == '-' || **s == '+')) 41 *sign = 0; 42 return ret; 43 }