home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / atox.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  123 lines

  1. /***
  2. *atox.c - atoi and atol conversion
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Converts a character string into an int or long.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14.  
  15. /***
  16. *long atol(char *nptr) - Convert string to long
  17. *
  18. *Purpose:
  19. *       Converts ASCII string pointed to by nptr to binary.
  20. *       Overflow is not detected.
  21. *
  22. *Entry:
  23. *       nptr = ptr to string to convert
  24. *
  25. *Exit:
  26. *       return long int value of the string
  27. *
  28. *Exceptions:
  29. *       None - overflow is not detected.
  30. *
  31. *******************************************************************************/
  32.  
  33. long __cdecl atol(
  34.         const char *nptr
  35.         )
  36. {
  37.         int c;              /* current char */
  38.         long total;         /* current total */
  39.         int sign;           /* if '-', then negative, otherwise positive */
  40.  
  41.         /* skip whitespace */
  42.         while ( isspace((int)(unsigned char)*nptr) )
  43.             ++nptr;
  44.  
  45.         c = (int)(unsigned char)*nptr++;
  46.         sign = c;           /* save sign indication */
  47.         if (c == '-' || c == '+')
  48.             c = (int)(unsigned char)*nptr++;    /* skip sign */
  49.  
  50.         total = 0;
  51.  
  52.         while (isdigit(c)) {
  53.             total = 10 * total + (c - '0');     /* accumulate digit */
  54.             c = (int)(unsigned char)*nptr++;    /* get next char */
  55.         }
  56.  
  57.         if (sign == '-')
  58.             return -total;
  59.         else
  60.             return total;   /* return result, negated if necessary */
  61. }
  62.  
  63.  
  64. /***
  65. *int atoi(char *nptr) - Convert string to long
  66. *
  67. *Purpose:
  68. *       Converts ASCII string pointed to by nptr to binary.
  69. *       Overflow is not detected.  Because of this, we can just use
  70. *       atol().
  71. *
  72. *Entry:
  73. *       nptr = ptr to string to convert
  74. *
  75. *Exit:
  76. *       return int value of the string
  77. *
  78. *Exceptions:
  79. *       None - overflow is not detected.
  80. *
  81. *******************************************************************************/
  82.  
  83. int __cdecl atoi(
  84.         const char *nptr
  85.         )
  86. {
  87.         return (int)atol(nptr);
  88. }
  89.  
  90. #ifndef _NO_INT64
  91.  
  92. __int64 __cdecl _atoi64(
  93.         const char *nptr
  94.         )
  95. {
  96.         int c;              /* current char */
  97.         __int64 total;      /* current total */
  98.         int sign;           /* if '-', then negative, otherwise positive */
  99.  
  100.         /* skip whitespace */
  101.         while ( isspace((int)(unsigned char)*nptr) )
  102.             ++nptr;
  103.  
  104.         c = (int)(unsigned char)*nptr++;
  105.         sign = c;           /* save sign indication */
  106.         if (c == '-' || c == '+')
  107.             c = (int)(unsigned char)*nptr++;    /* skip sign */
  108.  
  109.         total = 0;
  110.  
  111.         while (isdigit(c)) {
  112.             total = 10 * total + (c - '0');     /* accumulate digit */
  113.             c = (int)(unsigned char)*nptr++;    /* get next char */
  114.         }
  115.  
  116.         if (sign == '-')
  117.             return -total;
  118.         else
  119.             return total;   /* return result, negated if necessary */
  120. }
  121.  
  122. #endif  /* _NO_INT64 */
  123.