home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / strtol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  2.4 KB  |  125 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: strtol.c,v 1.1 1996/12/12 16:07:05 aros Exp $
  4.  
  5.     Desc: ANSI C function strtol()
  6.     Lang: english
  7. */
  8. #include <ctype.h>
  9. #include <errno.h>
  10. #include <limits.h>
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15. #include <stdlib.h>
  16.  
  17.     long strtol (
  18.  
  19. /*  SYNOPSIS */
  20.     const char * str,
  21.     char      ** endptr,
  22.     int         base)
  23.  
  24. /*  FUNCTION
  25.     Convert a string of digits into an integer according to the
  26.     given base.
  27.  
  28.     INPUTS
  29.     str - The string which should be converted. Leading
  30.         whitespace are ignored. The number may be prefixed
  31.         by a '+' or '-'. If base is above 10, then the
  32.         alphabetic characters from 'A' are used to specify
  33.         digits above 9 (ie. 'A' or 'a' is 10, 'B' or 'b' is
  34.         11 and so on until 'Z' or 'z' is 35).
  35.     endptr - If this is non-NULL, then the address of the first
  36.         character after the number in the string is stored
  37.         here.
  38.     base - The base for the number. May be 0 or between 2 and 36,
  39.         including both. 0 means to autodetect the base. strtoul()
  40.         selects the base by inspecting the first characters
  41.         of the string. If they are "0x", then base 16 is
  42.         assumed. If they are "0", then base 8 is assumed. Any
  43.         other digit will assume base 10. This is like in C.
  44.  
  45.         If you give base 16, then an optional "0x" may
  46.         precede the number in the string.
  47.  
  48.     RESULT
  49.     The value of the string. The first character after the number
  50.     is returned in *endptr, if endptr is non-NULL. If no digits can
  51.     be converted, *endptr contains str (if non-NULL) and 0 is
  52.     returned.
  53.  
  54.     NOTES
  55.  
  56.     EXAMPLE
  57.     // returns 1, ptr points to the 0-Byte
  58.     strol ("  \t +0x1", &ptr, 0);
  59.  
  60.     // Returns 15. ptr points to the a
  61.     strol ("017a", &ptr, 0);
  62.  
  63.     // Returns 215 (5*36 + 35)
  64.     strol ("5z", &ptr, 36);
  65.  
  66.     BUGS
  67.  
  68.     SEE ALSO
  69.     labs(), fabs()
  70.  
  71.     INTERNALS
  72.  
  73.     HISTORY
  74.     12.12.1996 digulla created
  75.  
  76. ******************************************************************************/
  77. {
  78.     unsigned long val    = 0;
  79.     char    * ptr;
  80.     char    * copy;
  81.  
  82.     copy = (char *)str;
  83.  
  84.     while (isspace (*str))
  85.     str ++;
  86.  
  87.     if (*str)
  88.     {
  89.     val = strtoul (str, &ptr, base);
  90.  
  91.     if (endptr)
  92.     {
  93.         if (ptr == str)
  94.         str = copy;
  95.         else
  96.         str = ptr;
  97.     }
  98.  
  99.     if (*str == '-')
  100.     {
  101.         if ((signed long)val > 0)
  102.         {
  103.         errno = ERANGE;
  104.  
  105.         val = LONG_MIN;
  106.         }
  107.     }
  108.     else
  109.     {
  110.         if ((signed long)val < 0)
  111.         {
  112.         errno = ERANGE;
  113.  
  114.         val = LONG_MAX;
  115.         }
  116.     }
  117.     }
  118.  
  119.     if (endptr)
  120.     *endptr = (char *)str;
  121.  
  122.     return val;
  123. } /* strtoul */
  124.  
  125.