lass_errno.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "util_common.h"
00044 #include "lass_errno.h"
00045
00046 #include <errno.h>
00047 #include <string.h>
00048
00049 #if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
00050 # define NOMINMAX
00051 # define WIN32_LEAN_AND_MEAN
00052 # include <windows.h>
00053 # include "../../meta/meta_assert.h"
00054 #endif
00055
00056 namespace lass
00057 {
00058 namespace util
00059 {
00060 namespace impl
00061 {
00062
00063
00064
00065
00066
00067 const int lass_errno()
00068 {
00069 return errno;
00070 }
00071
00072
00073
00074
00075
00076 void lass_reset_errno()
00077 {
00078 errno = 0;
00079 }
00080
00081
00082
00083
00084
00085 const std::string lass_strerror(int iErrnum)
00086 {
00087 #if LASS_HAVE_FUNC_STRERROR_R
00088
00089 const size_t bufferLength = 256;
00090 char buffer[bufferLength + 1];
00091 errno = 0;
00092 # if LASS_HAVE_STRERROR_R_CHAR_P
00093 const char* result = ::strerror_r(iErrnum, buffer, bufferLength);
00094 if (errno != 0)
00095 {
00096 result = 0;
00097 }
00098 # else
00099 const int rc = ::strerror_r(iErrnum, buffer, bufferLength);
00100 const char* result = (rc != 0 && errno == EINVAL) ? buffer : 0;
00101 # endif
00102 return result ? std::string(buffer) : std::string("[no error message due to strerror_r failure]");
00103
00104 #elif LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
00105
00106 # if LASS_COMPILER_VERSION >= 1400
00107 const size_t bufferLength = 256;
00108 char buffer[bufferLength + 1];
00109 const errno_t rc = strerror_s(buffer, bufferLength, iErrnum);
00110 if (rc != 0)
00111 {
00112 return "[no error message due to strerror_s failure]";
00113 }
00114 return std::string(buffer);
00115 # else
00116 return strerror(iErrnum);
00117 # endif
00118
00119 #else
00120
00121 std::ostringstream buffer;
00122 buffer << "errnum " << iErrnum;
00123 return buffer.str();
00124
00125 #endif
00126 }
00127
00128 #if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
00129
00130 LASS_META_ASSERT(sizeof(DWORD) <= sizeof(unsigned), unsigned_should_be_big_enough_for_a_DWORD);
00131
00132
00133
00134
00135
00136
00137 const unsigned lass_GetLastError()
00138 {
00139 return GetLastError();
00140 }
00141
00142
00143
00144
00145
00146
00147 const std::string lass_FormatMessage(unsigned error)
00148 {
00149 LASS_ASSERT(static_cast<DWORD>(error) == error);
00150 LASS_ASSERT(error != 0);
00151
00152 LPSTR buffer;
00153 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00154 0, static_cast<DWORD>(error), 0, (LPTSTR) &buffer, 0, 0))
00155 {
00156 return "[no error message due to FormatMessage failure]";
00157 }
00158 std::string result = buffer;
00159 LocalFree(buffer);
00160 return result;
00161 }
00162
00163 #endif
00164
00165 }
00166 }
00167 }
00168
00169