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 / astoi.c next >
C/C++ Source or Header  |  2000-05-07  |  2KB  |  124 lines

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