io_error.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_IO_ERROR_H
  10. #define GIL_IO_ERROR_H
  11. /// \file
  12. /// \brief Handle input-output errors
  13. /// \author Lubomir Bourdev and Hailin Jin \n
  14. /// Adobe Systems Incorporated
  15. /// \date 2005-2007 \n Last updated on May 30, 2006
  16. #include <ios>
  17. #include "../../gil_config.hpp"
  18. #include <boost/shared_ptr.hpp>
  19. namespace boost { namespace gil {
  20. inline void io_error(const char* descr) { throw std::ios_base::failure(descr); }
  21. inline void io_error_if(bool expr, const char* descr="") { if (expr) io_error(descr); }
  22. namespace detail {
  23. class file_mgr {
  24. protected:
  25. shared_ptr<FILE> _fp;
  26. struct null_deleter { void operator()(void const*) const {} };
  27. file_mgr(FILE* file) : _fp(file, null_deleter()) {}
  28. file_mgr(const char* filename, const char* flags) {
  29. FILE* fp;
  30. io_error_if((fp=fopen(filename,flags))==NULL, "file_mgr: failed to open file");
  31. _fp=shared_ptr<FILE>(fp,fclose);
  32. }
  33. public:
  34. FILE* get() { return _fp.get(); }
  35. };
  36. }
  37. } } // namespace boost::gil
  38. #endif