home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / compat / strtoul.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-18  |  4.0 KB  |  186 lines

  1. /* 
  2.  * strtoul.c --
  3.  *
  4.  *    Source code for the "strtoul" library procedure.
  5.  *
  6.  * Copyright (c) 1988 The Regents of the University of California.
  7.  * Copyright (c) 1994 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#) strtoul.c 1.4 94/12/17 16:26:26";
  15. #endif /* not lint */
  16.  
  17. #include <ctype.h>
  18.  
  19. /*
  20.  * The table below is used to convert from ASCII digits to a
  21.  * numerical equivalent.  It maps from '0' through 'z' to integers
  22.  * (100 for non-digit characters).
  23.  */
  24.  
  25. static char cvtIn[] = {
  26.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,        /* '0' - '9' */
  27.     100, 100, 100, 100, 100, 100, 100,        /* punctuation */
  28.     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,    /* 'A' - 'Z' */
  29.     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  30.     30, 31, 32, 33, 34, 35,
  31.     100, 100, 100, 100, 100, 100,        /* punctuation */
  32.     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,    /* 'a' - 'z' */
  33.     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  34.     30, 31, 32, 33, 34, 35};
  35.  
  36. /*
  37.  *----------------------------------------------------------------------
  38.  *
  39.  * strtoul --
  40.  *
  41.  *    Convert an ASCII string into an integer.
  42.  *
  43.  * Results:
  44.  *    The return value is the integer equivalent of string.  If endPtr
  45.  *    is non-NULL, then *endPtr is filled in with the character
  46.  *    after the last one that was part of the integer.  If string
  47.  *    doesn't contain a valid integer value, then zero is returned
  48.  *    and *endPtr is set to string.
  49.  *
  50.  * Side effects:
  51.  *    None.
  52.  *
  53.  *----------------------------------------------------------------------
  54.  */
  55.  
  56. unsigned long int
  57. strtoul(string, endPtr, base)
  58.     char *string;        /* String of ASCII digits, possibly
  59.                  * preceded by white space.  For bases
  60.                  * greater than 10, either lower- or
  61.                  * upper-case digits may be used.
  62.                  */
  63.     char **endPtr;        /* Where to store address of terminating
  64.                  * character, or NULL. */
  65.     int base;            /* Base for conversion.  Must be less
  66.                  * than 37.  If 0, then the base is chosen
  67.                  * from the leading characters of string:
  68.                  * "0x" means hex, "0" means octal, anything
  69.                  * else means decimal.
  70.                  */
  71. {
  72.     register char *p;
  73.     register unsigned long int result = 0;
  74.     register unsigned digit;
  75.     int anyDigits = 0;
  76.  
  77.     /*
  78.      * Skip any leading blanks.
  79.      */
  80.  
  81.     p = string;
  82.     while (isspace(*p)) {
  83.     p += 1;
  84.     }
  85.  
  86.     /*
  87.      * If no base was provided, pick one from the leading characters
  88.      * of the string.
  89.      */
  90.     
  91.     if (base == 0)
  92.     {
  93.     if (*p == '0') {
  94.         p += 1;
  95.         if (*p == 'x') {
  96.         p += 1;
  97.         base = 16;
  98.         } else {
  99.  
  100.         /*
  101.          * Must set anyDigits here, otherwise "0" produces a
  102.          * "no digits" error.
  103.          */
  104.  
  105.         anyDigits = 1;
  106.         base = 8;
  107.         }
  108.     }
  109.     else base = 10;
  110.     } else if (base == 16) {
  111.  
  112.     /*
  113.      * Skip a leading "0x" from hex numbers.
  114.      */
  115.  
  116.     if ((p[0] == '0') && (p[1] == 'x')) {
  117.         p += 2;
  118.     }
  119.     }
  120.  
  121.     /*
  122.      * Sorry this code is so messy, but speed seems important.  Do
  123.      * different things for base 8, 10, 16, and other.
  124.      */
  125.  
  126.     if (base == 8) {
  127.     for ( ; ; p += 1) {
  128.         digit = *p - '0';
  129.         if (digit > 7) {
  130.         break;
  131.         }
  132.         result = (result << 3) + digit;
  133.         anyDigits = 1;
  134.     }
  135.     } else if (base == 10) {
  136.     for ( ; ; p += 1) {
  137.         digit = *p - '0';
  138.         if (digit > 9) {
  139.         break;
  140.         }
  141.         result = (10*result) + digit;
  142.         anyDigits = 1;
  143.     }
  144.     } else if (base == 16) {
  145.     for ( ; ; p += 1) {
  146.         digit = *p - '0';
  147.         if (digit > ('z' - '0')) {
  148.         break;
  149.         }
  150.         digit = cvtIn[digit];
  151.         if (digit > 15) {
  152.         break;
  153.         }
  154.         result = (result << 4) + digit;
  155.         anyDigits = 1;
  156.     }
  157.     } else {
  158.     for ( ; ; p += 1) {
  159.         digit = *p - '0';
  160.         if (digit > ('z' - '0')) {
  161.         break;
  162.         }
  163.         digit = cvtIn[digit];
  164.         if (digit >= base) {
  165.         break;
  166.         }
  167.         result = result*base + digit;
  168.         anyDigits = 1;
  169.     }
  170.     }
  171.  
  172.     /*
  173.      * See if there were any digits at all.
  174.      */
  175.  
  176.     if (!anyDigits) {
  177.     p = string;
  178.     }
  179.  
  180.     if (endPtr != 0) {
  181.     *endPtr = p;
  182.     }
  183.  
  184.     return result;
  185. }
  186.