str/str_toumax.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
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         /* Single optional + or - */
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 }

Generated on Tue Jun 19 20:38:27 2007 for lucid by  doxygen 1.5.2