Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

error.c

00001 /*
00002  *  This program is free software; you can redistribute it and/or
00003  *  modify it under the terms of the GNU General Public License
00004  *  as published by the Free Software Foundation; either version
00005  *  2 of the License, or (at your option) any later version.
00006  *
00007  *  Copyright 2004 Spiro Trikaliotis
00008  *
00009  */
00010 
00011 #include <windows.h>
00012 
00013 #include <stdarg.h>
00014 #include <stdio.h>
00015 #include <assert.h>
00016 
00017 #include "arch.h"
00018 
00019 
00034 char *arch_strerror(unsigned int ErrorCode)
00035 {
00036     static char ErrorMessageBuffer[2048];
00037     int n;
00038 
00039     // Write the error message into the buffer
00040 
00041     n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS
00042                       | FORMAT_MESSAGE_MAX_WIDTH_MASK,
00043                       NULL,
00044                       ErrorCode,
00045                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00046                       (LPTSTR) &ErrorMessageBuffer,
00047                       sizeof(ErrorMessageBuffer)-1,
00048                       NULL);
00049 
00050     // make sure there is a trailing zero
00051 
00052     ErrorMessageBuffer[n] = 0;
00053 
00054     return ErrorMessageBuffer;
00055 }
00056 
00073 void arch_error(int AUnused, unsigned int ErrorCode, const char *Format, ...)
00074 {
00075     va_list ap;
00076     char ErrorMessageBuffer[2048];
00077     char ErrorMessageBuffer2[2048];
00078     char *errorText;
00079 
00080     UNREFERENCED_PARAMETER(AUnused);
00081 
00082     // Write the optional string into the output
00083 
00084     if (Format && *Format)
00085     {
00086         va_start(ap, Format);
00087 
00088         _vsnprintf(ErrorMessageBuffer2, sizeof(ErrorMessageBuffer2), Format, ap);
00089 
00090         va_end(ap);
00091 
00092         // make sure there is a trailing zero
00093 
00094         ErrorMessageBuffer2[sizeof(ErrorMessageBuffer2) - 1] = 0;
00095     }
00096 
00097     // Get the error message
00098 
00099     errorText = arch_strerror(ErrorCode);
00100 
00101     // Append the message to the buffer. Make sure not to overwrite the buffer
00102 
00103     if (errorText)
00104     {
00105         _snprintf(ErrorMessageBuffer, sizeof(ErrorMessageBuffer), "%s: %s", ErrorMessageBuffer2, errorText);
00106 
00107         ErrorMessageBuffer[sizeof(ErrorMessageBuffer)-1] = 0;
00108     }
00109     else
00110     {
00111         assert(sizeof(ErrorMessageBuffer) >= sizeof(ErrorMessageBuffer2));
00112 
00113         strcpy(ErrorMessageBuffer, ErrorMessageBuffer2);
00114     }
00115 
00116     fprintf(stderr, "%s\n", ErrorMessageBuffer);
00117 
00118 #if DBG
00119 
00120     {
00121         int n = strlen(ErrorMessageBuffer);
00122 
00123         if (n == sizeof(ErrorMessageBuffer))
00124             --n;
00125 
00126         ErrorMessageBuffer[n] = '\n';
00127         ErrorMessageBuffer[n+1] = 0;
00128         OutputDebugString(ErrorMessageBuffer);
00129     }
00130 
00131 #endif
00132 }

Generated on Sun Apr 30 18:45:48 2006 for opencbm by  doxygen 1.4.2