Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Win32MessageFormatter.h

Go to the documentation of this file.
00001 /*  Win32Exception.h - Handy exception object that is capable
00002     of calling FormatMessage for sensible API error reporting.
00003 
00004     Copyright (C) 2001-2004 Mark Weaver
00005     Written by Mark Weaver <mark@npsl.co.uk>
00006 
00007     Part of the Open-Win32 library.
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public
00019     License along with this library; if not, write to the
00020     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021     Boston, MA  02111-1307, USA.
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() /* throw() */ : 
00047         m_buffer(NULL),
00048         m_formatted("")
00049     {
00050         DWORD error = ::GetLastError();
00051         formatMessage(error);
00052     }
00053 
00059     CWin32MessageFormatterA(DWORD error) /* throw() */ :
00060         m_buffer(NULL),
00061         m_formatted("")
00062     {
00063         formatMessage(error);
00064     }
00065 
00069     ~CWin32MessageFormatterA() /* throw() */ 
00070     {
00071         if (m_buffer)
00072             ::LocalFree(m_buffer);
00073     }
00074 
00078     operator const char *() const /* throw() */ 
00079     {
00080         return m_formatted;
00081     }
00082 
00083 private:
00089     void formatMessage(DWORD error) /* throw() */ 
00090     {
00091         if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
00092                 0, error, 0, (LPSTR)&m_buffer, 0, NULL) != 0)
00093         {
00094             // Remove \n, \r\n from the string
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() /* throw() */ : 
00144         m_buffer(NULL),
00145         m_formatted(L"")
00146     {
00147         DWORD error = ::GetLastError();
00148         formatMessage(error);
00149     }
00150 
00156     CWin32MessageFormatterW(DWORD error) /* throw() */ :
00157         m_buffer(NULL),
00158         m_formatted(L"")
00159     {
00160         formatMessage(error);
00161     }
00162 
00166     ~CWin32MessageFormatterW() /* throw() */ 
00167     {
00168         if (m_buffer)
00169             ::LocalFree(m_buffer);
00170     }
00171 
00175     operator const wchar_t *() const /* throw() */ 
00176     {
00177         return m_formatted;
00178     }
00179 
00180 private:
00186     void formatMessage(DWORD error) /* throw() */ 
00187     {
00188         if (::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
00189                 0, error, 0, (LPWSTR)&m_buffer, 0, NULL) != 0)
00190         {
00191             // Remove \n, \r\n from the string
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 } // namespace OW32
00238 
00239 #endif // OW32_Win32MessageFormatter_h

Generated on Sun Jun 5 01:29:18 2005 for OW32 by  doxygen 1.3.9.1