home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / lib / astoll.c < prev    next >
C/C++ Source or Header  |  2000-12-03  |  2KB  |  96 lines

  1. /* @(#)astoll.c    1.1 00/12/03 Copyright 1985,2000 J. Schilling */
  2. /*
  3.  *    astoll() converts a string to long long
  4.  *
  5.  *    Leading tabs and spaces are ignored.
  6.  *    Both return pointer to the first char that has not been used.
  7.  *    Caller must check if this means a bad conversion.
  8.  *
  9.  *    leading "+" is ignored
  10.  *    leading "0"  makes conversion octal (base 8)
  11.  *    leading "0x" makes conversion hex   (base 16)
  12.  *
  13.  *    Llong is silently reverted to long if the compiler does not
  14.  *    support long long.
  15.  *
  16.  *    Copyright (c) 1985,2000 J. Schilling
  17.  */
  18. /*
  19.  * This program is free software; you can redistribute it and/or modify
  20.  * it under the terms of the GNU General Public License as published by
  21.  * the Free Software Foundation; either version 2, or (at your option)
  22.  * any later version.
  23.  *
  24.  * This program is distributed in the hope that it will be useful,
  25.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.  * GNU General Public License for more details.
  28.  *
  29.  * You should have received a copy of the GNU General Public License
  30.  * along with this program; see the file COPYING.  If not, write to
  31.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  32.  */
  33.  
  34. #include <mconfig.h>
  35. #include <standard.h>
  36. #include <utypes.h>
  37. #include <schily.h>
  38.  
  39. #define is_space(c)     ((c) == ' ' || (c) == '\t')
  40. #define is_digit(c)     ((c) >= '0' && (c) <= '9')
  41. #define is_hex(c)    (((c) >= 'a' && (c) <= 'f') || \
  42.              ((c) >= 'A' && (c) <= 'F'))
  43.  
  44. #define to_lower(c)    (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A'+'a' : (c))
  45.  
  46. char *astoll(s, l)
  47.     register const char *s;
  48.     Llong *l;
  49. {
  50.     int neg = 0;
  51.     register Llong ret = (Llong)0;
  52.     register int base = 10;
  53.     register int digit;
  54.     register char c;
  55.     
  56.     while (is_space(*s))
  57.         s++;
  58.  
  59.     if (*s == '+') {
  60.         s++;
  61.     } else if (*s == '-') {
  62.         s++;
  63.         neg++;
  64.     }
  65.  
  66.     if (*s == '0') {
  67.         base = 8;
  68.         s++;
  69.         if (*s == 'x' || *s == 'X') {
  70.             s++;
  71.             base = 16;
  72.         }
  73.     }
  74.     for (;(c = *s) != 0; s++) {
  75.  
  76.         if (is_digit(c)) {
  77.             digit = c - '0';
  78.         } else if (is_hex(c)) {
  79.             digit = to_lower(c) - 'a' + 10;
  80.         } else {
  81.             break;
  82.         }
  83.  
  84.         if (digit < base) {
  85.             ret *= base;
  86.             ret += digit;
  87.         } else {
  88.             break;
  89.         }
  90.     }
  91.     if (neg)
  92.         ret = -ret;
  93.     *l = ret;
  94.     return ((char *)s);
  95. }
  96.