From b251f27bb51da7f3091ae68fbc3005ee54c941a5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 9 Mar 2014 10:34:06 -0400 Subject: [PATCH] file: Don't use throw / catch to report errors Needing to use throw / catch was getting in the way. Instead, check for a boolean return value. Signed-off-by: Anna Schumaker --- DESIGN | 16 +++++++++------- include/error.h | 2 -- include/file.h | 6 +++--- lib/file.cpp | 28 +++++++++++++++------------- tests/src/file.cpp | 7 ++----- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/DESIGN b/DESIGN index 6545a89a..e212f982 100644 --- a/DESIGN +++ b/DESIGN @@ -42,8 +42,6 @@ Error Codes: E_AUDIO = 1, /* Audio error */ E_EXIST, /* Requested object does not exist */ E_INVAL, /* Invalid operation requested */ - E_IO, /* I/O error */ - E_NOTRACK, /* Track ID not found */ }; @@ -165,19 +163,23 @@ On-disk files: Return true if the file exists in the filesystem. Return false otherwise. - void File :: open(OpenMode mode); + bool File :: open(OpenMode mode); + Return false if: + - hint == FILE_TYPE_INVALID + - mode == NOT_OPEN + - The file is already open + When opening a file for reading (mode == OPEN_READ), - - Throw -EEXIST if the file does not exist + - Return false if the file does not exist - Open the file - Read in version from the start of the file When opening a file for writing (mode == OPEN_WRITE), - - Throw -ELEGACY if the file has FILE_TYPE_LEGACY set + - Return false if the file has FILE_TYPE_LEGACY set - Create missing directories as needed - Write version information to the start of the file - Throw -EINVAL if hint == FILE_TYPE_INVALID. - Throw -EOPEN if the file is already open. + Return true on success. void File :: close(); Close a file after IO. diff --git a/include/error.h b/include/error.h index a63e29bd..0190812d 100644 --- a/include/error.h +++ b/include/error.h @@ -8,8 +8,6 @@ enum o_error { E_AUDIO = 1, /* Audio error */ E_EXIST, /* Requested object does not exist */ E_INVAL, /* Invalid operation requested */ - E_IO, /* I/O error */ - E_NOTRACK, /* Track ID not found */ }; #endif /* OCARINA_ERROR_H */ diff --git a/include/file.h b/include/file.h index 669912aa..ecd7a68e 100644 --- a/include/file.h +++ b/include/file.h @@ -29,8 +29,8 @@ private: unsigned int version; std::string find_dir(); - void open_read(); - void open_write(); + bool open_read(); + bool open_write(); public: File(const std::string &, FileLocHint); @@ -38,7 +38,7 @@ public: const char *get_filepath(); const unsigned int get_version(); bool exists(); - void open(OpenMode); + bool open(OpenMode); void close(); std::string getline(); }; diff --git a/lib/file.cpp b/lib/file.cpp index 0cb20c1b..02497518 100644 --- a/lib/file.cpp +++ b/lib/file.cpp @@ -65,64 +65,66 @@ std::string File :: find_dir() return res; } -void File :: open_read() +bool File :: open_read() { if (!exists()) { dprint("ERROR: File does not exist\n"); - throw -E_EXIST; + return false; } std::fstream::open(filepath.c_str(), std::fstream::in); if (std::fstream::fail()) { dprint("ERROR: File could not be opened for reading\n"); - throw -E_IO; + return false; } mode = OPEN_READ; std::fstream::operator>>(version); getline(); + return true; } -void File :: open_write() +bool File :: open_write() { if (hint == FILE_TYPE_LEGACY) { dprint("ERROR: Cannot write to legacy files\n"); - throw -E_IO; + return false; } std::string dir = find_dir(); if (g_mkdir_with_parents(dir.c_str(), 0755) != 0) { dprint("ERROR: Could not make directory\n"); - throw -E_IO; + return false; } std::fstream::open(filepath.c_str(), std::fstream::out); if (std::fstream::fail()) { dprint("ERROR: Could not open file for writing\n"); - throw -E_IO; + return false; } mode = OPEN_WRITE; std::fstream::operator<<(FILE_VERSION) << std::endl; + return true; } -void File :: open(OpenMode m) +bool File :: open(OpenMode m) { if (hint == FILE_TYPE_INVALID) { dprint("ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened\n"); - throw -E_INVAL; + return false; } else if (m == NOT_OPEN) { dprint("ERROR: NOT_OPEN is not a legal OpenMode\n"); - throw -E_INVAL; + return false; } else if (mode != NOT_OPEN) { dprint("ERROR: File is already open\n"); - throw -E_IO; + return false; } else if (m == OPEN_READ) - open_read(); + return open_read(); else /* m == OPEN_WRITE */ - open_write(); + return open_write(); } void File :: close() diff --git a/tests/src/file.cpp b/tests/src/file.cpp index 24412b11..df651726 100644 --- a/tests/src/file.cpp +++ b/tests/src/file.cpp @@ -37,12 +37,9 @@ int print_filepath(File &f) int open_file(File &f, OpenMode mode) { - try { - f.open(mode); + if (f.open(mode) == true) return 0; - } catch (int err) { - return err; - } + return 1; } int main(int argc, char **argv)