[Lemon-commits] [lemon_svn] deba: r2274 - hugo/trunk/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:51:26 CET 2006


Author: deba
Date: Fri Oct 28 10:40:42 2005
New Revision: 2274

Modified:
   hugo/trunk/lemon/error.h
   hugo/trunk/lemon/lemon_reader.h

Log:
If the file does not exist LemonReader throws exception



Modified: hugo/trunk/lemon/error.h
==============================================================================
--- hugo/trunk/lemon/error.h	(original)
+++ hugo/trunk/lemon/error.h	Fri Oct 28 10:40:42 2005
@@ -326,6 +326,56 @@
     virtual ~DataFormatError() throw() {}
   };
 
+  ///\e 
+  class FileOpenError : public IOError {
+  protected:
+    ExceptionMember<std::string> _file;
+
+    mutable ExceptionMember<std::string> _message_holder;
+  public:
+
+    FileOpenError(const FileOpenError &foe) : 
+      IOError(foe), _file(foe._file) {}
+
+    ///\e 
+    explicit FileOpenError(const std::string& file)
+      : _file(file) {}
+
+
+    ///\e 
+    void file(const std::string &file) { _file.set(file); }
+ 
+    /// \brief Returns the filename.
+    ///
+    /// Returns \e null if the filename was not specified.
+    const char* file() const {
+      if (_file.valid() && !_file.get().empty()) {
+	return _file.get().c_str();
+      } else {
+	return 0;
+      }
+    }
+
+    ///\e 
+    virtual const char* what() const throw() {
+      try {
+	std::ostringstream ostr;
+	ostr << exceptionName() << ": ";
+	ostr << "Cannot open file - " << file();
+	_message_holder.set(ostr.str());
+      }
+      catch (...) {}
+      if( _message_holder.valid()) return _message_holder.get().c_str();
+      return exceptionName();
+    }
+
+    virtual const char* exceptionName() const {
+      return "lemon::FileOpenError";
+    }
+
+    virtual ~FileOpenError() throw() {}
+  };
+
   class IOParameterError : public LogicError {
   protected:
     ExceptionMember<std::string> _message;

Modified: hugo/trunk/lemon/lemon_reader.h
==============================================================================
--- hugo/trunk/lemon/lemon_reader.h	(original)
+++ hugo/trunk/lemon/lemon_reader.h	Fri Oct 28 10:40:42 2005
@@ -409,6 +409,9 @@
     LemonReader(const std::string& filename) 
       : is(0), own_is(true) {
       is = new std::ifstream(filename.c_str());
+      if (is->fail()) {
+	throw FileOpenError(filename);
+      }
     }
 
     /// \brief Desctructor for LemonReader.



More information about the Lemon-commits mailing list