From db55d9bd7e9b35113dde0379b75efdc3fea03ec1 Mon Sep 17 00:00:00 2001 From: Chris Frey Date: Tue, 7 Dec 2010 22:08:02 -0500 Subject: [PATCH] lib: added a few error checks, and exceptions, to a_osloader.c file handling Also wrapped the opendir/closedir operations in a smart pointer so there are no handle leaks in exception cases. --- src/a_osloader.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/a_osloader.cc b/src/a_osloader.cc index 6f496b75..cc34091c 100644 --- a/src/a_osloader.cc +++ b/src/a_osloader.cc @@ -26,9 +26,12 @@ #include #include +#include #include "a_osloader.h" #include "a_alxparser.h" +#include "vsmartptr.h" +#include "error.h" namespace Barry { @@ -52,7 +55,6 @@ void OSLoader::load(const std::string& pathname) int offset; - DIR *path; struct dirent *entry; std::string alxfile; @@ -63,9 +65,13 @@ void OSLoader::load(const std::string& pathname) loadALXFile(alxfile, false); // Then, we can read all ALX files - path = opendir(pathname.c_str()); + // Wrap it in a smart pointer so exceptions are safe + vLateSmartPtr path(&closedir); + path.reset( opendir(pathname.c_str()) ); + if( path.get() == NULL ) + throw Barry::ErrnoError("Could not opendir: " + pathname, errno); - while ((entry = readdir(path)) != NULL) { + while ((entry = readdir(path.get())) != NULL) { alxfile = entry->d_name; if (alxfile.length() < ext.length()) @@ -83,14 +89,14 @@ void OSLoader::load(const std::string& pathname) loadALXFile(pathname + "/" + alxfile, true); } - - closedir(path); } void OSLoader::loadALXFile(const std::string& alxfile, const bool enable) { std::ifstream file(alxfile.c_str()); + if( !file ) + throw Barry::Error("Cannot open ALX file: " + alxfile); ALX::ALXParser parser(*this, file); -- 2.11.4.GIT