From 0c772c4c3f71874b53f3e280da388e64b8aebc19 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Thu, 8 Nov 2012 18:14:26 -0600 Subject: [PATCH] Set LoadFile and LoadAllFromFile to throw an exception if we can't load the file --- include/yaml-cpp/exceptions.h | 6 ++++++ src/parse.cpp | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/include/yaml-cpp/exceptions.h b/include/yaml-cpp/exceptions.h index 02aeb61..fabb33a 100644 --- a/include/yaml-cpp/exceptions.h +++ b/include/yaml-cpp/exceptions.h @@ -70,6 +70,7 @@ namespace YAML const char * const INVALID_ANCHOR = "invalid anchor"; const char * const INVALID_ALIAS = "invalid alias"; const char * const INVALID_TAG = "invalid tag"; + const char * const BAD_FILE = "bad file"; template inline const std::string KEY_NOT_FOUND_WITH_KEY(const T&, typename disable_if >::type * = 0) { @@ -190,6 +191,11 @@ namespace YAML EmitterException(const std::string& msg_) : Exception(Mark::null(), msg_) {} }; + + class BadFile: public Exception { + public: + BadFile(): Exception(Mark::null(), ErrorMsg::BAD_FILE) {} + }; } #endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/parse.cpp b/src/parse.cpp index 3a33931..1537d55 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -30,6 +30,8 @@ namespace YAML Node LoadFile(const std::string& filename) { std::ifstream fin(filename.c_str()); + if(!fin) + throw BadFile(); return Load(fin); } @@ -59,6 +61,8 @@ namespace YAML std::vector LoadAllFromFile(const std::string& filename) { std::ifstream fin(filename.c_str()); + if(!fin) + throw BadFile(); return LoadAll(fin); } }