Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.9 KiB

  1. /***
  2. *atox.c - atoi and atol conversion
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Converts a character string into an int or long.
  8. *
  9. *Revision History:
  10. * 06-05-89 PHG Module created, based on asm version
  11. * 03-05-90 GJF Fixed calling type, added #include <cruntime.h> and
  12. * cleaned up the formatting a bit. Also, fixed the
  13. * copyright.
  14. * 09-27-90 GJF New-style function declarators.
  15. * 10-21-92 GJF Fixed conversions of char to int.
  16. * 04-06-93 SKS Replace _CRTAPI* with _cdecl
  17. * 01-19-96 BWT Add __int64 version.
  18. * 08-27-98 GJF Revised multithread support based on threadlocinfo
  19. * struct.
  20. * 05-23-00 GB Added Unicode function.
  21. * 08-16-00 GB Added multilingual support to unicode wtox fundtions.
  22. * 11-01-00 PML Fix _NTSUBSET_ build.
  23. * 05-11-01 BWT A NULL string returns 0, not AV.
  24. *
  25. *******************************************************************************/
  26. #include <cruntime.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <mtdll.h>
  30. #ifdef _MBCS
  31. #undef _MBCS
  32. #endif
  33. #include <tchar.h>
  34. #ifndef _UNICODE
  35. #define _tchartodigit(c) ((c) >= '0' && (c) <= '9' ? (c) - '0' : -1)
  36. #else
  37. int _wchartodigit(wchar_t);
  38. #define _tchartodigit(c) _wchartodigit((wchar_t)(c))
  39. #endif
  40. /***
  41. *long atol(char *nptr) - Convert string to long
  42. *
  43. *Purpose:
  44. * Converts ASCII string pointed to by nptr to binary.
  45. * Overflow is not detected.
  46. *
  47. *Entry:
  48. * nptr = ptr to string to convert
  49. *
  50. *Exit:
  51. * return long int value of the string
  52. *
  53. *Exceptions:
  54. * None - overflow is not detected.
  55. *
  56. *******************************************************************************/
  57. long __cdecl _tstol(
  58. const _TCHAR *nptr
  59. )
  60. {
  61. int c; /* current char */
  62. long total; /* current total */
  63. int sign; /* if '-', then negative, otherwise positive */
  64. #if defined( _MT) && !defined(_UNICODE)
  65. pthreadlocinfo ptloci;
  66. #endif
  67. if (!nptr)
  68. return 0;
  69. #if defined( _MT) && !defined(_UNICODE)
  70. ptloci = _getptd()->ptlocinfo;
  71. if ( ptloci != __ptlocinfo )
  72. ptloci = __updatetlocinfo();
  73. /* skip whitespace */
  74. while ( __isspace_mt(ptloci, (int)(_TUCHAR)*nptr) )
  75. #else
  76. while ( _istspace((int)(_TUCHAR)*nptr) )
  77. #endif
  78. ++nptr;
  79. c = (int)(_TUCHAR)*nptr++;
  80. sign = c; /* save sign indication */
  81. if (c == _T('-') || c == _T('+'))
  82. c = (int)(_TUCHAR)*nptr++; /* skip sign */
  83. total = 0;
  84. while ( (c = _tchartodigit(c)) != -1 ) {
  85. total = 10 * total + c; /* accumulate digit */
  86. c = (_TUCHAR)*nptr++; /* get next char */
  87. }
  88. if (sign == '-')
  89. return -total;
  90. else
  91. return total; /* return result, negated if necessary */
  92. }
  93. /***
  94. *int atoi(char *nptr) - Convert string to long
  95. *
  96. *Purpose:
  97. * Converts ASCII string pointed to by nptr to binary.
  98. * Overflow is not detected. Because of this, we can just use
  99. * atol().
  100. *
  101. *Entry:
  102. * nptr = ptr to string to convert
  103. *
  104. *Exit:
  105. * return int value of the string
  106. *
  107. *Exceptions:
  108. * None - overflow is not detected.
  109. *
  110. *******************************************************************************/
  111. int __cdecl _tstoi(
  112. const _TCHAR *nptr
  113. )
  114. {
  115. return (int)_tstol(nptr);
  116. }
  117. #ifndef _NO_INT64
  118. __int64 __cdecl _tstoi64(
  119. const _TCHAR *nptr
  120. )
  121. {
  122. int c; /* current char */
  123. __int64 total; /* current total */
  124. int sign; /* if '-', then negative, otherwise positive */
  125. #if defined(_MT) && !defined(_UNICODE)
  126. pthreadlocinfo ptloci;
  127. #endif
  128. if (!nptr)
  129. return 0i64;
  130. #if defined(_MT) && !defined(_UNICODE)
  131. ptloci = _getptd()->ptlocinfo;
  132. if ( ptloci != __ptlocinfo )
  133. ptloci = __updatetlocinfo();
  134. /* skip whitespace */
  135. while ( __isspace_mt(ptloci, (int)(_TUCHAR)*nptr) )
  136. #else
  137. while ( _istspace((int)(_TUCHAR)*nptr) )
  138. #endif
  139. ++nptr;
  140. c = (int)(_TUCHAR)*nptr++;
  141. sign = c; /* save sign indication */
  142. if (c == _T('-') || c == _T('+'))
  143. c = (int)(_TUCHAR)*nptr++; /* skip sign */
  144. total = 0;
  145. while ( (c = _tchartodigit(c)) != -1 ) {
  146. total = 10 * total + c; /* accumulate digit */
  147. c = (_TUCHAR)*nptr++; /* get next char */
  148. }
  149. if (sign == _T('-'))
  150. return -total;
  151. else
  152. return total; /* return result, negated if necessary */
  153. }
  154. #endif /* _NO_INT64 */