123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /* Copyright (C) 1991, 1997 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
- The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- The GNU C Library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
- #include "api.h"
- long strtol(const char* s, char** endptr, int base) {
- int neg = 0;
- long val = 0;
- // gobble initial whitespace
- while (*s == ' ' || *s == '\t') {
- s++;
- }
- // plus/minus sign
- if (*s == '+')
- s++;
- else if (*s == '-')
- s++, neg = 1;
- // hex or octal base prefix
- if ((base == 0 || base == 16) && (s[0] == '0' && s[1] == 'x'))
- s += 2, base = 16;
- else if (base == 0 && s[0] == '0')
- s++, base = 8;
- else if (base == 0)
- base = 10;
- // digits
- while (1) {
- int dig;
- if (*s >= '0' && *s <= '9')
- dig = *s - '0';
- else if (*s >= 'a' && *s <= 'z')
- dig = *s - 'a' + 10;
- else if (*s >= 'A' && *s <= 'Z')
- dig = *s - 'A' + 10;
- else
- break;
- if (dig >= base)
- break;
- s++, val = (val * base) + dig;
- // we don't properly detect overflow!
- }
- if (endptr)
- *endptr = (char*)s;
- return (neg ? -val : val);
- }
- /* Convert a string to an int. */
- int atoi(const char* nptr) {
- return (int)strtol(nptr, (char**)NULL, 10);
- }
- /* Convert a string to an long int. */
- long int atol(const char* nptr) {
- return strtol(nptr, (char**)NULL, 10);
- }
|