Loading src/TNL/Exceptions/FileCloseError.h 0 → 100644 +32 −0 Original line number Diff line number Diff line /*************************************************************************** FileCloseError.h - description ------------------- begin : Mar 5, 2019 copyright : (C) 2019 by Tomas Oberhuber et al. email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ // Implemented by: Tomas Oberhuber #pragma once #include <string> #include <stdexcept> #include <TNL/String.h> namespace TNL { namespace Exceptions { class FileCloseError : public std::runtime_error { public: FileCloseError( const String& fileName ) : std::runtime_error( "An error occurred when closing file " + fileName + "." ) {} }; } // namespace Exceptions } // namespace TNL src/TNL/File.hpp +11 −6 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <TNL/Exceptions/FileSerializationError.h> #include <TNL/Exceptions/FileDeserializationError.h> #include <TNL/Exceptions/FileOpenError.h> #include <TNL/Exceptions/FileCloseError.h> namespace TNL { Loading Loading @@ -55,14 +56,18 @@ inline bool File::open( const String& fileName, Mode mode ) inline bool File::close() { if( file.is_open() ) { if( file.is_open() ) { try { file.close(); if( ! file.good() ) { std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl; return false; } catch(...) { throw Exceptions::FileCloseError( fileName ); } } // reset all attributes // reset file name fileName = ""; return true; } Loading Loading
src/TNL/Exceptions/FileCloseError.h 0 → 100644 +32 −0 Original line number Diff line number Diff line /*************************************************************************** FileCloseError.h - description ------------------- begin : Mar 5, 2019 copyright : (C) 2019 by Tomas Oberhuber et al. email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ // Implemented by: Tomas Oberhuber #pragma once #include <string> #include <stdexcept> #include <TNL/String.h> namespace TNL { namespace Exceptions { class FileCloseError : public std::runtime_error { public: FileCloseError( const String& fileName ) : std::runtime_error( "An error occurred when closing file " + fileName + "." ) {} }; } // namespace Exceptions } // namespace TNL
src/TNL/File.hpp +11 −6 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <TNL/Exceptions/FileSerializationError.h> #include <TNL/Exceptions/FileDeserializationError.h> #include <TNL/Exceptions/FileOpenError.h> #include <TNL/Exceptions/FileCloseError.h> namespace TNL { Loading Loading @@ -55,14 +56,18 @@ inline bool File::open( const String& fileName, Mode mode ) inline bool File::close() { if( file.is_open() ) { if( file.is_open() ) { try { file.close(); if( ! file.good() ) { std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl; return false; } catch(...) { throw Exceptions::FileCloseError( fileName ); } } // reset all attributes // reset file name fileName = ""; return true; } Loading