home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Python / mystrtoul.c < prev    next >
C/C++ Source or Header  |  1994-01-02  |  4KB  |  159 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* strtol and strtoul, renamed to avoid conflicts */
  26.  
  27. /*
  28. **    strtoul
  29. **        This is a general purpose routine for converting
  30. **        an ascii string to an integer in an arbitrary base.
  31. **        Leading white space is ignored.  If 'base' is zero
  32. **        it looks for a leading 0, 0x or 0X to tell which
  33. **        base.  If these are absent it defaults to 10.
  34. **        Base must be 0 or between 2 and 36 (inclusive).
  35. **        If 'ptr' is non-NULL it will contain a pointer to
  36. **        the end of the scan.
  37. **        Errors due to bad pointers will probably result in
  38. **        exceptions - we don't check for them.
  39. */
  40.  
  41. #include <ctype.h>
  42. #include <errno.h>
  43.  
  44. unsigned long
  45. mystrtoul(str, ptr, base)
  46. register char *    str;
  47. char **        ptr;
  48. int        base;
  49. {
  50.     register unsigned long    result;    /* return value of the function */
  51.     register int        c;    /* current input character */
  52.     register unsigned long    temp;    /* used in overflow testing */
  53.     int                ovf;    /* true if overflow occurred */
  54.  
  55.     result = 0;
  56.     ovf = 0;
  57.  
  58. /* catch silly bases */
  59.     if (base != 0 && (base < 2 || base > 36))
  60.     {
  61.     if (ptr)
  62.         *ptr = str;
  63.     return 0;
  64.     }
  65.  
  66. /* skip leading white space */
  67.     while (*str && isspace(*str))
  68.     str++;
  69.  
  70. /* check for leading 0 or 0x for auto-base or base 16 */
  71.     switch (base)
  72.     {
  73.     case 0:        /* look for leading 0, 0x or 0X */
  74.     if (*str == '0')
  75.     {
  76.         str++;
  77.         if (*str == 'x' || *str == 'X')
  78.         {
  79.         str++;
  80.         base = 16;
  81.         }
  82.         else
  83.         base = 8;
  84.     }
  85.     else
  86.         base = 10;
  87.     break;
  88.  
  89.     case 16:    /* skip leading 0x or 0X */
  90.     if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
  91.         str += 2;
  92.     break;
  93.     }
  94.  
  95. /* do the conversion */
  96.     while (c = *str)
  97.     {
  98.     if (isdigit(c) && c - '0' < base)
  99.         c -= '0';
  100.     else
  101.     {
  102.         if (isupper(c))
  103.         c = tolower(c);
  104.         if (c >= 'a' && c <= 'z')
  105.         c -= 'a' - 10;
  106.         else    /* non-"digit" character */
  107.         break;
  108.         if (c >= base)    /* non-"digit" character */
  109.         break;
  110.     }
  111.     temp = result;
  112.     result = result * base + c;
  113.     if ((result - c) / base != temp)    /* overflow */
  114.         ovf = 1;
  115.     str++;
  116.     }
  117.  
  118. /* set pointer to point to the last character scanned */
  119.     if (ptr)
  120.     *ptr = str;
  121.     if (ovf)
  122.     {
  123.     result = ~0;
  124.     errno = ERANGE;
  125.     }
  126.     return result;
  127. }
  128.  
  129. long
  130. mystrtol(str, ptr, base)
  131. char *    str;
  132. char ** ptr;
  133. int    base;
  134. {
  135.     long result;
  136.     char sign;
  137.     
  138.     while (*str && isspace(*str))
  139.         str++;
  140.     
  141.     sign = *str;
  142.     if (sign == '+' || sign == '-')
  143.         str++;
  144.     
  145.     result = (long) mystrtoul(str, ptr, base);
  146.     
  147.     /* Signal overflow if the result appears negative,
  148.        except for the largest negative integer */
  149.     if (result < 0 && !(sign == '-' && result == -result)) {
  150.         errno = ERANGE;
  151.         result = 0x7fffffff;
  152.     }
  153.     
  154.     if (sign == '-')
  155.         result = -result;
  156.     
  157.     return result;
  158. }
  159.