Commit 0b81f7e7 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Added FileOpenError exception.

parent 2c8f1be7
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/***************************************************************************
                          FileOpenError.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 FileOpenError
   : public std::runtime_error
{
public:
   FileOpenError( const String& fileName )
   : std::runtime_error( "Unable to open file " + fileName + "." )
   {}
};

} // namespace Exceptions
} // namespace TNL
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ class NotTNLFile
{
public:
   NotTNLFile()
   : std::runtime_error( "Wring magic number found in a binary file. It is not TNL compatible file." )
   : std::runtime_error( "Wrong magic number found in a binary file. It is not TNL compatible file." )
   {}
};

+9 −10
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <TNL/Exceptions/MICSupportMissing.h>
#include <TNL/Exceptions/FileSerializationError.h>
#include <TNL/Exceptions/FileDeserializationError.h>
#include <TNL/Exceptions/FileOpenError.h>

namespace TNL {

@@ -39,18 +40,16 @@ inline bool File::open( const String& fileName, Mode mode )
   if( mode & Mode::Append ) ios_mode |= std::ios::app;
   if( mode & Mode::AtEnd ) ios_mode |= std::ios::ate;
   if( mode & Mode::Truncate ) ios_mode |= std::ios::trunc;
   try
   {
      file.open( fileName.getString(), ios_mode );
   
   /*if( mode == Mode::In )
      file.open( fileName.getString(), std::ios::binary | std::ios::in );
   else
      file.open( fileName.getString(), std::ios::binary | std::ios::out );*/
   }
   catch(...)
   {
      throw Exceptions::FileOpenError( fileName );
   }

   this->fileName = fileName;
   if( ! file.good() ) {
      std::cerr << "I am not able to open the file " << fileName << ". ";
      return false;
   }
   return true;
}