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.

176 lines
5.2 KiB

  1. /*
  2. **++
  3. **
  4. ** Copyright (c) 2002 Microsoft Corporation
  5. **
  6. **
  7. ** Module Name:
  8. **
  9. ** utility.h
  10. **
  11. **
  12. ** Abstract:
  13. **
  14. ** declare functions and variable used by the Test writer
  15. **
  16. ** Author:
  17. **
  18. ** Reuven Lax [reuvenl] 04-June-2002
  19. **
  20. **
  21. **
  22. ** Revision History:
  23. **
  24. **--
  25. */
  26. #ifndef _UTILITY_H_
  27. #define _UTILITY_H_
  28. ////////////////////////////////////////////////////////////////////////
  29. // Includes
  30. #include <functional>
  31. #include <string>
  32. #include <sstream>
  33. #include "vs_xml.hxx"
  34. ////////////////////////////////////////////////////////////////////////
  35. // Declarations
  36. using std::wstring;
  37. namespace Utility {
  38. enum Events {
  39. Identify = 0,
  40. PrepareForBackup = Identify+1,
  41. PrepareForSnapshot = PrepareForBackup+1,
  42. Freeze = PrepareForSnapshot+1,
  43. Thaw = Freeze+1,
  44. PostSnapshot = Thaw+1,
  45. Abort = PostSnapshot+1,
  46. BackupComplete = Abort+1,
  47. BackupShutdown = BackupComplete+1,
  48. PreRestore = BackupShutdown+1,
  49. PostRestore = PreRestore+1,
  50. NumEvents = PostRestore+1
  51. };
  52. enum Verbosity {
  53. low = 0,
  54. medium = low+1,
  55. high = medium+1
  56. };
  57. class TestWriterException : public std::exception {
  58. private:
  59. std::string m_what;
  60. public:
  61. TestWriterException(const wstring& what) : m_what("unexpected exception") {
  62. char* buffer = new char[what.size() + 1];
  63. if (buffer == NULL)
  64. return;
  65. if (::WideCharToMultiByte(CP_ACP, 0, what.c_str(), -1, buffer, (int)what.size() + 1,
  66. NULL, NULL))
  67. m_what = buffer;
  68. delete [] buffer;
  69. }
  70. TestWriterException(HRESULT hr, wstring function = L"") :
  71. m_what("Unexpected error") {
  72. std::wstringstream msg;
  73. msg << L"An error code of 0x" << std::hex << hr << L" was encountered";
  74. if (!function.empty())
  75. msg << L" by " << function;
  76. char* buffer = new char[msg.str().size() + 1];
  77. if (buffer == NULL)
  78. return;
  79. if (::WideCharToMultiByte(CP_ACP, 0, msg.str().c_str(), -1, buffer, (int)msg.str().size() + 1,
  80. NULL, NULL))
  81. m_what = buffer;
  82. delete [] buffer;
  83. }
  84. TestWriterException(const TestWriterException& other) : m_what(other.m_what)
  85. {}
  86. virtual const char* what() const { return m_what.c_str(); }
  87. };
  88. // generic class that automatically releases in destructor. Useful for pointers and handles.
  89. template <class ValueType, ValueType invalid, class CloseType, CloseType closeFunction>
  90. class AutoValue {
  91. private:
  92. ValueType m_value;
  93. public:
  94. AutoValue(ValueType v) : m_value(v) {}
  95. ~AutoValue() {
  96. if (m_value != invalid)
  97. closeFunction(m_value);
  98. }
  99. operator ValueType() { return m_value; }
  100. };
  101. typedef BOOL(*CloseType)(HANDLE);
  102. typedef AutoValue<HANDLE, INVALID_HANDLE_VALUE, CloseType, ::FindClose> AutoFindFileHandle;
  103. typedef AutoValue<HANDLE, INVALID_HANDLE_VALUE, CloseType, ::FindVolumeMountPointClose>
  104. AutoFindMountHandle;
  105. // little class to automatically acquire and release a critical section
  106. struct AutoCS {
  107. CComAutoCriticalSection& m_section;
  108. AutoCS(CComAutoCriticalSection& section) : m_section(section) { m_section.Lock(); }
  109. ~AutoCS() { m_section.Unlock(); }
  110. };
  111. // function object to compute the logical and of two function objects
  112. template<class Inner1, class Inner2>
  113. struct unary_and : public std::unary_function<typename Inner1::argument_type, bool> {
  114. protected:
  115. Inner1 m_inner1;
  116. Inner2 m_inner2;
  117. public:
  118. unary_and();
  119. unary_and(const Inner1& inner1, const Inner2& inner2) : m_inner1(inner1), m_inner2(inner2)
  120. {}
  121. bool operator()(const typename Inner2::argument_type& argument) const {
  122. return m_inner1(argument) && m_inner2(argument);
  123. }
  124. };
  125. template<class Inner1, class Inner2>
  126. inline unary_and<Inner1, Inner2> and1(const Inner1& inner1, const Inner2& inner2) {
  127. return unary_and<Inner1, Inner2>(inner1, inner2);
  128. }
  129. void missingAttribute(const wchar_t* name);
  130. void missingElement(const wchar_t* name);
  131. void checkReturn(HRESULT returnCode, wstring function);
  132. void warnReturn(HRESULT returnCode, wstring function);
  133. void parseError(const CXMLDocument& doc);
  134. void printStatus(const wstring& status, Verbosity level = medium);
  135. void printStatus(const std::string& status, Verbosity level = medium);
  136. bool toBoolean(const wchar_t* name);
  137. VSS_USAGE_TYPE toUsage(const wchar_t* name);
  138. VSS_RESTOREMETHOD_ENUM toMethod(const wchar_t* name);
  139. VSS_WRITERRESTORE_ENUM toWriterRestore(const wchar_t* name);
  140. VSS_COMPONENT_TYPE toComponentType(const wchar_t* name);
  141. VSS_RESTORE_TARGET toRestoreTarget(const wchar_t* name);
  142. Events toWriterEvent(const wchar_t* name);
  143. Verbosity toVerbosity(const wchar_t* name);
  144. long toLong(const wchar_t* name);
  145. wstring toString(VSS_USAGE_TYPE usage);
  146. wstring toString(VSS_RESTOREMETHOD_ENUM method);
  147. wstring toString(VSS_WRITERRESTORE_ENUM writerRestore);
  148. wstring toString(VSS_COMPONENT_TYPE type);
  149. wstring toString(VSS_RESTORE_TARGET target);
  150. wstring toString(Events event);
  151. wstring toString(Verbosity verbosity);
  152. wstring toString(bool value);
  153. }
  154. #endif