Source code of Windows XP (NT5)
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.

193 lines
3.3 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Environment:
  7. User mode only
  8. Revision History:
  9. 03-26-96 : Created
  10. --*/
  11. //
  12. // this module may be compiled at warning level 4 with the following
  13. // warnings disabled:
  14. //
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <tchar.h>
  19. #include <assert.h>
  20. #include <windows.h>
  21. #include <devioctl.h>
  22. #include "jobmgr.h"
  23. VOID
  24. xprintf(
  25. ULONG Depth,
  26. PTSTR Format,
  27. ...
  28. )
  29. {
  30. va_list args;
  31. ULONG i;
  32. TCHAR DebugBuffer[256];
  33. for (i=0; i<Depth; i++) {
  34. _tprintf (" ");
  35. }
  36. va_start(args, Format);
  37. _vsntprintf(DebugBuffer, 255, Format, args);
  38. _tprintf (DebugBuffer);
  39. va_end(args);
  40. }
  41. VOID
  42. DumpFlags(
  43. ULONG Depth,
  44. PTSTR Name,
  45. ULONG Flags,
  46. PFLAG_NAME FlagTable
  47. )
  48. {
  49. ULONG i;
  50. ULONG mask = 0;
  51. ULONG count = 0;
  52. UCHAR prolog[64];
  53. xprintf(Depth, "%s (0x%08x)%c\n", Name, Flags, Flags ? TEXT(':') : TEXT(' '));
  54. if(Flags == 0) {
  55. return;
  56. }
  57. memset(prolog, 0, sizeof(prolog));
  58. memset(prolog, ' ', min(6, _tcslen(Name)) * sizeof(TCHAR));
  59. xprintf(Depth, "%s", prolog);
  60. for(i = 0; FlagTable[i].Name != 0; i++) {
  61. PFLAG_NAME flag = &(FlagTable[i]);
  62. mask |= flag->Flag;
  63. if((Flags & flag->Flag) == flag->Flag) {
  64. //
  65. // print trailing comma
  66. //
  67. if(count != 0) {
  68. _tprintf(", ");
  69. //
  70. // Only print two flags per line.
  71. //
  72. if((count % 2) == 0) {
  73. _tprintf("\n");
  74. xprintf(Depth, "%s", prolog);
  75. }
  76. }
  77. _tprintf("%s", flag->Name);
  78. count++;
  79. }
  80. }
  81. _tprintf("\n");
  82. if((Flags & (~mask)) != 0) {
  83. xprintf(Depth, "%sUnknown flags %#010lx\n", prolog, (Flags & (~mask)));
  84. }
  85. return;
  86. }
  87. #define MICROSECONDS ((ULONGLONG) 10) // 10 nanoseconds
  88. #define MILLISECONDS (MICROSECONDS * 1000)
  89. #define SECONDS (MILLISECONDS * 1000)
  90. #define MINUTES (SECONDS * 60)
  91. #define HOURS (MINUTES * 60)
  92. #define DAYS (HOURS * 24)
  93. LPCTSTR
  94. TicksToString(
  95. LARGE_INTEGER TimeInTicks
  96. )
  97. {
  98. static TCHAR ticksToStringBuffer[256] = "";
  99. LPTSTR buffer = ticksToStringBuffer;
  100. ULONGLONG t = TimeInTicks.QuadPart;
  101. ULONGLONG days;
  102. ULONGLONG hours;
  103. ULONGLONG minutes;
  104. ULONGLONG seconds;
  105. ULONGLONG ticks;
  106. LPTSTR comma = "";
  107. if(t == 0) {
  108. return TEXT("0 Seconds");
  109. }
  110. days = t / DAYS;
  111. t %= DAYS;
  112. hours = t / HOURS;
  113. t %= HOURS;
  114. minutes = t / MINUTES;
  115. t %= MINUTES;
  116. seconds = t / SECONDS;
  117. t %= SECONDS;
  118. ticks = t;
  119. buffer[0] = TEXT('\0');
  120. if(days) {
  121. _stprintf(buffer, "%I64d Days", days);
  122. comma = ", ";
  123. buffer += _tcslen(buffer);
  124. }
  125. if(hours) {
  126. _stprintf(buffer, "%s%I64d Hours", comma, hours);
  127. comma = ", ";
  128. buffer += _tcslen(buffer);
  129. }
  130. if(minutes) {
  131. _stprintf(buffer, "%s%I64d Minutes", comma, minutes);
  132. comma = ", ";
  133. buffer += _tcslen(buffer);
  134. }
  135. if(seconds | ticks) {
  136. _stprintf(buffer, "%s%I64d.%06I64d Seconds", comma, seconds, ticks);
  137. }
  138. return ticksToStringBuffer;
  139. }