library of assembled shared sources

http://lass.cocamware.com

lass_errno.cpp

Go to the documentation of this file.
00001 /**     @file
00002  *      @author Bram de Greve (bramz@users.sourceforge.net)
00003  *      @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *      *** BEGIN LICENSE INFORMATION ***
00006  *      
00007  *      The contents of this file are subject to the Common Public Attribution License 
00008  *      Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *      the License. You may obtain a copy of the License at 
00010  *      http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *      Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *      use of software over a computer network and provide for limited attribution for 
00013  *      the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *      with Exhibit B.
00015  *      
00016  *      Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *      WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *      language governing rights and limitations under the License.
00019  *      
00020  *      The Original Code is LASS - Library of Assembled Shared Sources.
00021  *      
00022  *      The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *      The Original Developer is the Initial Developer.
00024  *      
00025  *      All portions of the code written by the Initial Developer are:
00026  *      Copyright (C) 2004-2007 the Initial Developer.
00027  *      All Rights Reserved.
00028  *      
00029  *      Contributor(s):
00030  *
00031  *      Alternatively, the contents of this file may be used under the terms of the 
00032  *      GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *      provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *      of your version of this file only under the terms of the GPL and not to allow 
00035  *      others to use your version of this file under the CPAL, indicate your decision by 
00036  *      deleting the provisions above and replace them with the notice and other 
00037  *      provisions required by the GPL License. If you do not delete the provisions above,
00038  *      a recipient may use your version of this file under either the CPAL or the GPL.
00039  *      
00040  *      *** END LICENSE INFORMATION ***
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 /** returns CLIB errno
00064  *  @ingroup LassErrno
00065  *  @internal
00066  */
00067 const int lass_errno()
00068 {
00069         return errno;
00070 }
00071 
00072 /** sets CLIB errno to zero.
00073  *  @ingroup LassErrno
00074  *  @internal
00075  */
00076 void lass_reset_errno()
00077 {
00078         errno = 0;
00079 }
00080 
00081 /** returns message associated to an CLIB error code
00082  *  @ingroup LassErrno
00083  *  @internal
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 /** returns GetLastError()
00133  *  @ingroup LassErrno
00134  *  @internal
00135  *  @arg only available on Windows platforms
00136  */
00137 const unsigned lass_GetLastError()
00138 {
00139         return GetLastError();
00140 }
00141 
00142 /** returns message associated to an Win API error code.
00143  *  @ingroup LassErrno
00144  *  @internal
00145  *  @arg only available on Windows platforms
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 // EOF

Generated on Mon Nov 10 14:20:06 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo