home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / STRTOL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.3 KB  |  123 lines

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