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.

140 lines
3.4 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: atol.cpp (from libc atox.c)
  4. //
  5. // Module: CMUTIL.DLL
  6. //
  7. // Synopsis: Conversion routines
  8. //
  9. // Copyright (c) 1997-1999 Microsoft Corporation
  10. //
  11. // Author: henryt Created 03/01/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. //+----------------------------------------------------------------------------
  16. //
  17. // Function: WINAPI CmAtolW
  18. //
  19. // Synopsis: This function converts a Unicode string to a long
  20. //
  21. // Arguments: *nptr - Unicode string to convert
  22. //
  23. // Returns: LONG - long representation of the string passed in
  24. //
  25. // History: quintinb Rewrote for Unicode conversion 4/8/99
  26. //
  27. //+----------------------------------------------------------------------------
  28. CMUTILAPI LONG WINAPI CmAtolW(const WCHAR *nptr)
  29. {
  30. WCHAR* pszCurrent = (WCHAR*)nptr;
  31. WCHAR sign = L'\0'; // if '-', then negative, otherwise positive
  32. long total = 0; // current total
  33. MYDBGASSERT(nptr);
  34. if (nptr)
  35. {
  36. //
  37. // skip whitespace
  38. //
  39. while (CmIsSpaceW(pszCurrent))
  40. {
  41. pszCurrent = CharNextU(pszCurrent);
  42. }
  43. //
  44. // Save the sign if necessary
  45. //
  46. sign = *pszCurrent;
  47. if ((L'-' == sign) || (L'+' == sign))
  48. {
  49. pszCurrent = CharNextU(pszCurrent);
  50. }
  51. //
  52. // Construct the number
  53. //
  54. total = 0;
  55. while (CmIsDigitW(pszCurrent))
  56. {
  57. total = (10 * total) + (*pszCurrent - L'0'); // accumulate digit
  58. pszCurrent = CharNextU(pszCurrent); // get next char
  59. }
  60. }
  61. if (sign == L'-')
  62. {
  63. return -total;
  64. }
  65. else
  66. {
  67. return total; // return result, negated if necessary
  68. }
  69. }
  70. //+----------------------------------------------------------------------------
  71. //
  72. // Function: WINAPI CmAtolA
  73. //
  74. // Synopsis: This function converts an ANSI string to a long value
  75. //
  76. // Arguments: *nptr - string to convert
  77. //
  78. // Returns: LONG - long representation of the string passed in
  79. //
  80. // History: quintinb Rewrote for Unicode conversion 4/8/99
  81. //
  82. //+----------------------------------------------------------------------------
  83. CMUTILAPI LONG WINAPI CmAtolA(const CHAR *nptr)
  84. {
  85. CHAR* pszCurrent = (CHAR*)nptr;
  86. CHAR sign = '\0'; // if '-', then negative, otherwise positive
  87. long total = 0; // current total
  88. MYDBGASSERT(nptr);
  89. if (nptr)
  90. {
  91. //
  92. // skip whitespace
  93. //
  94. while (CmIsSpaceA(pszCurrent))
  95. {
  96. pszCurrent = CharNextA(pszCurrent);
  97. }
  98. //
  99. // Save the sign if necessary
  100. //
  101. sign = *pszCurrent;
  102. if (('-' == sign) || ('+' == sign))
  103. {
  104. pszCurrent = CharNextA(pszCurrent);
  105. }
  106. //
  107. // Construct the number
  108. //
  109. total = 0;
  110. while (CmIsDigitA(pszCurrent))
  111. {
  112. total = (10 * total) + (*pszCurrent - '0'); // accumulate digit
  113. pszCurrent = CharNextA(pszCurrent); // get next char
  114. }
  115. }
  116. if (sign == '-')
  117. {
  118. return -total;
  119. }
  120. else
  121. {
  122. return total; // return result, negated if necessary
  123. }
  124. }