Author: wmb Date: Wed Mar 10 10:28:16 2010 New Revision: 1767 URL: http://tracker.coreboot.org/trac/openfirmware/changeset/1767
Log: Added some new string parsing routines to the client library.
Modified: clients/lib/printf.c clients/lib/strings.c
Modified: clients/lib/printf.c ============================================================================== --- clients/lib/printf.c Wed Mar 10 10:27:21 2010 (r1766) +++ clients/lib/printf.c Wed Mar 10 10:28:16 2010 (r1767) @@ -7,37 +7,65 @@ #include <stdarg.h> #include "stdio.h"
-int -atoi(const char *s) +long +memtol(const char *s, int len, char **endptr, int base) { - int temp = 0, base = 10; + int temp = 0; + int minus = 0; + int digit; + const char *send = s+len;
- if (*s == '0') { - ++s; - if (*s == 'x') { + if (s != send && *s == '+' || *s == '-') { + minus = *s == '-'; + s++; + } + if (base == 0) { + if (s!=send && *s == '0') { ++s; - base = 16; + if (s!=send && toupper(*s) == 'X') { + ++s; + base = 16; + } else { + base = 8; + } } else { - base = 8; + base = 10; + } + } else { + if (base == 16 && (send-s) > 1 && *s == '0' && toupper(s[1]) == 'X') { + s += 2; } } - while (*s) { - switch (*s) { - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - temp = (temp * base) + (*s++ - '0'); - break; - case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': - temp = (temp * base) + (*s++ - 'a' + 10); - break; - case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': - temp = (temp * base) + (*s++ - 'A' + 10); - break; - default: - return (temp); + while (s!=send) { + digit = toupper(*s) - '0'; + if (digit >= 0 && digit <= 9) { + temp = (temp * base) + digit; + s++; + } else { + digit = digit + '0' - 'A' + 10; + if (digit >= 10 && digit < base) { + temp = (temp * base) + digit; + s++; + } else { + break; + } } } - return (temp); + if (endptr) + *endptr = (char *)s; + return minus ? -temp : temp; +} + +long +strtol(const char *s, char **endptr, int base) +{ + return memtol(s, strlen(s), endptr, base); +} + +int +atoi(const char *s) +{ + return (int)strtol(s, NULL, 10); }
STATIC int
Modified: clients/lib/strings.c ============================================================================== --- clients/lib/strings.c Wed Mar 10 10:27:21 2010 (r1766) +++ clients/lib/strings.c Wed Mar 10 10:28:16 2010 (r1767) @@ -114,6 +114,23 @@ return(temp); }
+void *memchr(const void *s, int c, int len) +{ + unsigned char *p = s; + while (len--) { + if (*p == (unsigned char)c) + return p; + p++; + } + return NULL; +} + +int toupper(int c) +{ + return (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c; +} + + // LICENSE_BEGIN // Copyright (c) 2006 FirmWorks //
openfirmware@openfirmware.info