00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00028 #ifndef OW32_Win32MessageFormatter_h
00029 #define OW32_Win32MessageFormatter_h
00030
00031 #include <OW32/OW32Libs.h>
00032 #include <stdio.h>
00033
00034 namespace OW32
00035 {
00036
00040 class OW32_LIB_EXPORT CWin32MessageFormatterA
00041 {
00042 public:
00046 CWin32MessageFormatterA() :
00047 m_buffer(NULL),
00048 m_formatted("")
00049 {
00050 DWORD error = ::GetLastError();
00051 formatMessage(error);
00052 }
00053
00059 CWin32MessageFormatterA(DWORD error) :
00060 m_buffer(NULL),
00061 m_formatted("")
00062 {
00063 formatMessage(error);
00064 }
00065
00069 ~CWin32MessageFormatterA()
00070 {
00071 if (m_buffer)
00072 ::LocalFree(m_buffer);
00073 }
00074
00078 operator const char *() const
00079 {
00080 return m_formatted;
00081 }
00082
00083 private:
00089 void formatMessage(DWORD error)
00090 {
00091 if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00092 0, error, 0, (LPSTR)&m_buffer, 0, NULL) != 0)
00093 {
00094
00095 bool prevCR = false;
00096 size_t len = strlen(m_buffer);
00097
00098 for (size_t i = 0; i < len; )
00099 {
00100 if (m_buffer[i] == '\n')
00101 {
00102 memmove(m_buffer + i - prevCR, m_buffer + i + 1, sizeof(char) * (len - i));
00103 len -= prevCR + 1;
00104 i -= prevCR;
00105 prevCR = false;
00106 }
00107 else
00108 {
00109 if (m_buffer[i] == '\r')
00110 prevCR = true;
00111 ++i;
00112 }
00113 }
00114
00115 m_formatted = m_buffer;
00116 }
00117 else
00118 {
00119 _snprintf(m_fixedBuffer, sizeof(m_fixedBuffer)/sizeof(m_fixedBuffer[0]),
00120 "API error code %lu", error);
00121 m_fixedBuffer[sizeof(m_fixedBuffer)/sizeof(m_fixedBuffer[0]) - 1] = '\0';
00122 m_formatted = m_fixedBuffer;
00123 }
00124 }
00125
00127 char *m_formatted;
00129 char m_fixedBuffer[64];
00131 char *m_buffer;
00132 };
00133
00137 class OW32_LIB_EXPORT CWin32MessageFormatterW
00138 {
00139 public:
00143 CWin32MessageFormatterW() :
00144 m_buffer(NULL),
00145 m_formatted(L"")
00146 {
00147 DWORD error = ::GetLastError();
00148 formatMessage(error);
00149 }
00150
00156 CWin32MessageFormatterW(DWORD error) :
00157 m_buffer(NULL),
00158 m_formatted(L"")
00159 {
00160 formatMessage(error);
00161 }
00162
00166 ~CWin32MessageFormatterW()
00167 {
00168 if (m_buffer)
00169 ::LocalFree(m_buffer);
00170 }
00171
00175 operator const wchar_t *() const
00176 {
00177 return m_formatted;
00178 }
00179
00180 private:
00186 void formatMessage(DWORD error)
00187 {
00188 if (::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00189 0, error, 0, (LPWSTR)&m_buffer, 0, NULL) != 0)
00190 {
00191
00192 bool prevCR = false;
00193 size_t len = wcslen(m_buffer);
00194
00195 for (size_t i = 0; i < len; )
00196 {
00197 if (m_buffer[i] == L'\n')
00198 {
00199 memmove(m_buffer + i - prevCR, m_buffer + i + 1, sizeof(wchar_t) * (len - i));
00200 len -= prevCR + 1;
00201 i -= prevCR;
00202 prevCR = false;
00203 }
00204 else
00205 {
00206 if (m_buffer[i] == L'\r')
00207 prevCR = true;
00208 ++i;
00209 }
00210 }
00211
00212 m_formatted = m_buffer;
00213 }
00214 else
00215 {
00216 _snwprintf(m_fixedBuffer, sizeof(m_fixedBuffer)/sizeof(m_fixedBuffer[0]),
00217 L"API error code %lu", error);
00218 m_fixedBuffer[sizeof(m_fixedBuffer)/sizeof(m_fixedBuffer[0]) - 1] = L'\0';
00219 m_formatted = m_fixedBuffer;
00220 }
00221 }
00222
00224 wchar_t *m_formatted;
00226 wchar_t m_fixedBuffer[64];
00228 wchar_t *m_buffer;
00229 };
00230
00231 #ifdef _UNICODE
00232 typedef CWin32MessageFormatterW CWin32MessageFormatter;
00233 #else
00234 typedef CWin32MessageFormatterA CWin32MessageFormatter;
00235 #endif
00236
00237 }
00238
00239 #endif // OW32_Win32MessageFormatter_h