home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Python / mystrtoul.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-01  |  3.9 KB  |  165 lines  |  [TEXT/KAHL]

  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. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28.  
  29. /* strtol and strtoul, renamed to avoid conflicts */
  30.  
  31. /*
  32. **    strtoul
  33. **        This is a general purpose routine for converting
  34. **        an ascii string to an integer in an arbitrary base.
  35. **        Leading white space is ignored.  If 'base' is zero
  36. **        it looks for a leading 0, 0x or 0X to tell which
  37. **        base.  If these are absent it defaults to 10.
  38. **        Base must be 0 or between 2 and 36 (inclusive).
  39. **        If 'ptr' is non-NULL it will contain a pointer to
  40. **        the end of the scan.
  41. **        Errors due to bad pointers will probably result in
  42. **        exceptions - we don't check for them.
  43. */
  44.  
  45. #include <ctype.h>
  46. #include <errno.h>
  47.  
  48. unsigned long
  49. mystrtoul(str, ptr, base)
  50. register char *    str;
  51. char **        ptr;
  52. int        base;
  53. {
  54.     register unsigned long    result;    /* return value of the function */
  55.     register int        c;    /* current input character */
  56.     register unsigned long    temp;    /* used in overflow testing */
  57.     int                ovf;    /* true if overflow occurred */
  58.  
  59.     result = 0;
  60.     ovf = 0;
  61.  
  62. /* catch silly bases */
  63.     if (base != 0 && (base < 2 || base > 36))
  64.     {
  65.     if (ptr)
  66.         *ptr = str;
  67.     return 0;
  68.     }
  69.  
  70. /* skip leading white space */
  71.     while (*str && isspace(*str))
  72.     str++;
  73.  
  74. /* check for leading 0 or 0x for auto-base or base 16 */
  75.     switch (base)
  76.     {
  77.     case 0:        /* look for leading 0, 0x or 0X */
  78.     if (*str == '0')
  79.     {
  80.         str++;
  81.         if (*str == 'x' || *str == 'X')
  82.         {
  83.         str++;
  84.         base = 16;
  85.         }
  86.         else
  87.         base = 8;
  88.     }
  89.     else
  90.         base = 10;
  91.     break;
  92.  
  93.     case 16:    /* skip leading 0x or 0X */
  94.     if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
  95.         str += 2;
  96.     break;
  97.     }
  98.  
  99. /* do the conversion */
  100.     while (c = *str)
  101.     {
  102.     if (isdigit(c) && c - '0' < base)
  103.         c -= '0';
  104.     else
  105.     {
  106.         if (isupper(c))
  107.         c = tolower(c);
  108.         if (c >= 'a' && c <= 'z')
  109.         c -= 'a' - 10;
  110.         else    /* non-"digit" character */
  111.         break;
  112.         if (c >= base)    /* non-"digit" character */
  113.         break;
  114.     }
  115.     temp = result;
  116.     result = result * base + c;
  117. #ifndef MPW
  118.     if ((result - c) / base != temp)    /* overflow */
  119.         ovf = 1;
  120. #endif
  121.     str++;
  122.     }
  123.  
  124. /* set pointer to point to the last character scanned */
  125.     if (ptr)
  126.     *ptr = str;
  127.     if (ovf)
  128.     {
  129.     result = ~0;
  130.     errno = ERANGE;
  131.     }
  132.     return result;
  133. }
  134.  
  135. long
  136. mystrtol(str, ptr, base)
  137. char *    str;
  138. char ** ptr;
  139. int    base;
  140. {
  141.     long result;
  142.     char sign;
  143.     
  144.     while (*str && isspace(*str))
  145.         str++;
  146.     
  147.     sign = *str;
  148.     if (sign == '+' || sign == '-')
  149.         str++;
  150.     
  151.     result = (long) mystrtoul(str, ptr, base);
  152.     
  153.     /* Signal overflow if the result appears negative,
  154.        except for the largest negative integer */
  155.     if (result < 0 && !(sign == '-' && result == -result)) {
  156.         errno = ERANGE;
  157.         result = 0x7fffffff;
  158.     }
  159.     
  160.     if (sign == '-')
  161.         result = -result;
  162.     
  163.     return result;
  164. }
  165.