From: Erik M. <er...@us...> - 2001-09-27 21:31:02
|
Update of /cvsroot/blob/blob/src In directory usw-pr-cvs1:/tmp/cvs-serv3370/src Modified Files: util.c clock.c Log Message: remove GetHexValue() from clock.c and replace it with strtoval() in util.c Index: util.c =================================================================== RCS file: /cvsroot/blob/blob/src/util.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- util.c 2001/09/17 00:05:31 1.5 +++ util.c 2001/09/27 21:30:58 1.6 @@ -130,3 +130,65 @@ return dest; } + + + + +/* test for a digit. return value if digit or -1 otherwise */ +static int digitvalue(char isdigit) +{ + if (isdigit >= '0' && isdigit <= '9' ) + return isdigit - '0'; + else + return -1; +} + + + + +/* test for a hexidecimal digit. return value if digit or -1 otherwise */ +static int xdigitvalue(char isdigit) +{ + if (isdigit >= '0' && isdigit <= '9' ) + return isdigit - '0'; + if (isdigit >= 'a' && isdigit <= 'f') + return 10 + isdigit - 'a'; + return -1; +} + + + + +/* convert a string to an u32 value. if the string starts with 0x, it + * is a hexidecimal string, otherwise we treat is as decimal. returns + * the converted value on success, or -1 on failure. no, we don't care + * about overflows if the string is too long. + */ +int strtoval(const char *str, u32 *value) +{ + u32 i; + + *value = 0; + + if(strncmp(str, "0x", 2) == 0) { + /* hexadecimal mode */ + str += 2; + + while(*str != '\0') { + if((i = xdigitvalue(*str)) < 0) + return -1; + + *value = (*value << 4) | i; + } + } else { + /* decimal mode */ + while(*str != '\0') { + if((i = digitvalue(*str)) < 0) + return -1; + + *value = (*value * 10) + i; + } + } + + return 0; +} Index: clock.c =================================================================== RCS file: /cvsroot/blob/blob/src/clock.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- clock.c 2001/09/23 14:57:00 1.5 +++ clock.c 2001/09/27 21:30:58 1.6 @@ -53,37 +53,6 @@ }; -static int MyXDigitValue(char isdigit) -{ - if (isdigit >= '0' && isdigit <= '9' ) - return isdigit - '0'; - if (isdigit >= 'a' && isdigit <= 'f') - return 10 + isdigit - 'a'; - return -1; -} /* MyXDigitValue */ - - -/* Converts hex string into value. no, we don't care if the string is - too long */ -static int GetHexValue(char *str, u32 *value) -{ - int i; - - /* skip any leading 0x */ - if(strncmp(str, "0x", 2) == 0) - str += 2; - - *value=0x00; - - while(*str != '\0') { - if((i = MyXDigitValue(*str)) < 0) - return -1; - - *value = (*value << 4) | i; - } - - return 0; -} /* GetHexValue */ int SetClock(int argc, char *argv[]) @@ -99,8 +68,8 @@ } for(i = 0; i < 5; i++) { - if(GetHexValue(argv[i + 1], ®s[i]) < 0) { - SerialOutputString("*** not a hexidecimal number: "); + if(strtoval(argv[i + 1], ®s[i]) < 0) { + SerialOutputString("*** not a number: "); SerialOutputString(argv[i + 1]); SerialOutputByte('\n'); return 0; |