home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / STRTOUL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.3 KB  |  138 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strtoul.c
  3.  *
  4.  * function(s)
  5.  *        Get     - gets the next character in a string
  6.  *        UnGet   - moves a character pointer one position forward
  7.  *        strtoul - convert a string to an unsigned long integer
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1987, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <limits.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <_scanf.h>
  24. #include <stddef.h>
  25.  
  26.  
  27. /*---------------------------------------------------------------------*
  28.  
  29. Name            Get - gets the next character in a string
  30.  
  31. Usage           static int Get(char **strPP);
  32.  
  33. Return value    the next character in a string.  It return -1 if the next
  34.                 character is the null character.
  35.  
  36. *---------------------------------------------------------------------*/
  37.  
  38. static int near Get(char **strPP)
  39. {
  40.         register unsigned       c;
  41.  
  42.         return ((c = *((*strPP) ++)) == 0) ? -1 : c;
  43. }
  44.  
  45.  
  46. /*---------------------------------------------------------------------*
  47.  
  48. Name            UnGet - moves a character pointer one position forward
  49.  
  50. Usage           static void UnGet(char c, char **strPP);
  51.  
  52. Description     decrements a character pointer
  53.  
  54. *---------------------------------------------------------------------*/
  55. #pragma argsused
  56. static void near UnGet(char c, char **strPP)
  57. {
  58.         --(*strPP);     /* ignore c, we don't allow the string to change */
  59. }
  60.  
  61.  
  62. /*-------------------------------------------------------------------------*
  63.  
  64. Name            strtoul - convert a string to an unsigned long integer
  65.  
  66. Usage           unsigned long strtoul(const char *strP, char **suffixPP,
  67.                                       int radix);
  68.  
  69. Prototype in    stdlib.h
  70.  
  71. Description     Convert a string to an unsigned long integer. The syntax of
  72.                 the string must be:
  73.  
  74.                 unsigned long   ::= [isspace]* [+] numeral;
  75.  
  76.                 numeral         ::= { '0' ['x'|'X'] digit [digit]* } |
  77.                                     { digit [digit] }
  78.  
  79.                 "strP"  is a  pointer to  the ASCII  string to  be scanned.
  80.                 "suffixPP" is a pointer to  a string pointer to be updated.
  81.                 If suffixPP  is not NULL  then the updated  pointer will be
  82.                 set to point  to the first character following  the section
  83.                 of  the string  which was   consumed. Thus  the caller  can
  84.                 easily analyze subsequent contents of the string.
  85.  
  86.                 The radix may be zero, or any number 2..36. If the radix is
  87.                 zero, then a radix will be chosen from the possibilities 8,
  88.                 10, or 16, by the usual "C" rules for distinguishing octal,
  89.                 decimal, and hex numerals.
  90.  
  91.                 If radix > 10 then the  letters of the alphabet "A..Z" form
  92.                 the extended set of valid digits.
  93.  
  94. Return value    If the  radix is invalid or  no number could be  found then
  95.                 the result  value is zero   and the next  char pointer will
  96.                 equal the starting string pointer.
  97.  
  98.                 If  the  number  overflows,  LONG_MAX  or  LONG_MIN will be
  99.                 returned and errno will be set to ERANGE.
  100.  
  101. ----------------------------------------------------------------------------*/
  102. unsigned long _FARFUNC strtoul(const char *strP, char **suffixPP, int radix)
  103. {
  104.         int     charCt = 0;
  105.         int     status = 0;
  106.         long    result = 0L;
  107.  
  108.  
  109.         while (isspace(*strP))
  110.         {
  111.                 strP++;
  112.                 charCt++;
  113.         }
  114.  
  115.         errno = 0;
  116.         result = _scantol (
  117.                 (int near (*)(void *))Get,
  118.                 (void near (*)(int, void *))UnGet,
  119.                 &strP,
  120.                 radix,
  121.                 0x7FFF,
  122.                 &charCt,
  123.                 &status
  124.                 );
  125.  
  126.         if (status <= 0)
  127.                 strP -= charCt;
  128.         else if (status == 2)
  129.         {
  130.                 result = ULONG_MAX;
  131.                 errno = ERANGE;
  132.         }
  133.         if (NULL != suffixPP)
  134.                 *suffixPP = (char *)strP;
  135.  
  136.         return (result);
  137. }
  138.