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.

128 lines
3.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1996, Microsoft Corporation.
  4. //
  5. // File: xtow.cxx
  6. //
  7. // Contents: formats numbers into wide strings
  8. //
  9. // History: 96/Jan/3 DwightKr Created
  10. // 96/Apr/3 dlee optimized and cleaned up
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Note - added to C run-times; remove when new run-times are available.
  18. // On second thought, just use this one -- it saves an ascii=>wide and
  19. // radix checking.
  20. // This routine isn't for real formatting -- it's just for input to
  21. // Win32's real number formatting routine
  22. //
  23. //----------------------------------------------------------------------------
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Function: IDQ_ulltow_format
  27. //
  28. // Synopsis: formats an _int64 in a wide string.
  29. //
  30. // Arguments: [val] - value to format
  31. // [buf] - buffer for the result
  32. // [fIsNegative] - true if val is negative, false otherwise
  33. //
  34. // History: 96/Apr/05 dlee Created.
  35. //
  36. //----------------------------------------------------------------------------
  37. void IDQ_ulltow_format(
  38. ULONGLONG val,
  39. WCHAR * buf,
  40. int fIsNegative )
  41. {
  42. WCHAR *p = buf;
  43. if ( fIsNegative )
  44. {
  45. // negative, so output '-' and negate
  46. *p++ = L'-';
  47. val = (ULONGLONG) (-(LONGLONG)val);
  48. }
  49. WCHAR *firstdig = p; // save pointer to first digit
  50. do
  51. {
  52. unsigned digval = (unsigned) (val % 10);
  53. val /= 10; // get next digit
  54. Win4Assert( digval <= 9 );
  55. // convert to unicode and store
  56. *p++ = (WCHAR) (digval + L'0');
  57. } while ( val > 0 );
  58. // We now have the digit of the number in the buffer, but in reverse
  59. // order. Thus we reverse them now.
  60. *p-- = 0;
  61. do
  62. {
  63. WCHAR temp = *p;
  64. *p = *firstdig;
  65. *firstdig = temp; // swap *p and *firstdig
  66. --p;
  67. ++firstdig; // advance to next two digits
  68. } while ( firstdig < p ); // repeat until halfway
  69. }
  70. void IDQ_ultow_format(
  71. ULONG val,
  72. WCHAR * buf,
  73. int fIsNegative )
  74. {
  75. WCHAR *p = buf;
  76. if ( fIsNegative )
  77. {
  78. // negative, so output '-' and negate
  79. *p++ = L'-';
  80. val = (ULONG) (-(LONG)val);
  81. }
  82. WCHAR *firstdig = p; // save pointer to first digit
  83. do
  84. {
  85. unsigned digval = (unsigned) (val % 10);
  86. val /= 10; // get next digit
  87. Win4Assert( digval <= 9 );
  88. // convert to unicode and store
  89. *p++ = (WCHAR) (digval + L'0');
  90. } while ( val > 0 );
  91. // We now have the digit of the number in the buffer, but in reverse
  92. // order. Thus we reverse them now.
  93. *p-- = 0;
  94. do
  95. {
  96. WCHAR temp = *p;
  97. *p = *firstdig;
  98. *firstdig = temp; // swap *p and *firstdig
  99. --p;
  100. ++firstdig; // advance to next two digits
  101. } while ( firstdig < p ); // repeat until halfway
  102. }