png++
0.2.9
|
00001 /* 00002 * Copyright (C) 2007,2008 Alex Shulgin 00003 * 00004 * This file is part of png++ the C++ wrapper for libpng. PNG++ is free 00005 * software; the exact copying conditions are as follows: 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright notice, 00011 * this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 00017 * 3. The name of the author may not be used to endorse or promote products 00018 * derived from this software without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00021 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 #ifndef PNGPP_ERROR_HPP_INCLUDED 00032 #define PNGPP_ERROR_HPP_INCLUDED 00033 00034 /* check if we have strerror_s or strerror_r, prefer the former which is C11 std */ 00035 #ifdef __STDC_LIB_EXT1__ 00036 #define __STDC_WANT_LIB_EXT1__ 1 00037 #include <string.h> 00038 00039 #define HAVE_STRERROR_S 1 00040 #else 00041 #undef HAVE_STRERROR_S 00042 #endif 00043 00044 #include <string> 00045 #include <stdexcept> 00046 #include <cerrno> 00047 #include <cstdlib> 00048 #include <cstring> 00049 00050 namespace png 00051 { 00052 00057 class error 00058 : public std::runtime_error 00059 { 00060 public: 00064 explicit error(std::string const& message) 00065 : std::runtime_error(message) 00066 { 00067 } 00068 }; 00069 00076 class std_error 00077 : public std::runtime_error 00078 { 00079 public: 00088 explicit std_error(std::string const& message, int errnum = errno) 00089 : std::runtime_error((message + ": ") + thread_safe_strerror(errnum)) 00090 { 00091 } 00092 00093 protected: 00094 static std::string thread_safe_strerror(int errnum) 00095 { 00096 #define ERRBUF_SIZE 512 00097 char buf[ERRBUF_SIZE] = { 0 }; 00098 00099 #ifdef HAVE_STRERROR_S 00100 strerror_s(buf, ERRBUF_SIZE, errnum); 00101 return std::string(buf); 00102 #else 00103 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE 00104 strerror_r(errnum, buf, ERRBUF_SIZE); 00105 return std::string(buf); 00106 #else 00107 /* GNU variant can return a pointer to static buffer instead of buf */ 00108 return std::string(strerror_r(errnum, buf, ERRBUF_SIZE)); 00109 #endif 00110 #endif 00111 00112 #undef ERRBUF_SIZE 00113 } 00114 }; 00115 00116 } // namespace png 00117 00118 #endif // PNGPP_ERROR_HPP_INCLUDED