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 <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-03-09 10:34:06 -04:00 committed by Anna Schumaker
parent 959cac0fe1
commit b251f27bb5
5 changed files with 29 additions and 30 deletions

16
DESIGN
View File

@ -42,8 +42,6 @@ Error Codes:
E_AUDIO = 1, /* Audio error */ E_AUDIO = 1, /* Audio error */
E_EXIST, /* Requested object does not exist */ E_EXIST, /* Requested object does not exist */
E_INVAL, /* Invalid operation requested */ 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 true if the file exists in the filesystem.
Return false otherwise. 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), 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 - Open the file
- Read in version from the start of the file - Read in version from the start of the file
When opening a file for writing (mode == OPEN_WRITE), 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 - Create missing directories as needed
- Write version information to the start of the file - Write version information to the start of the file
Throw -EINVAL if hint == FILE_TYPE_INVALID. Return true on success.
Throw -EOPEN if the file is already open.
void File :: close(); void File :: close();
Close a file after IO. Close a file after IO.

View File

@ -8,8 +8,6 @@ enum o_error {
E_AUDIO = 1, /* Audio error */ E_AUDIO = 1, /* Audio error */
E_EXIST, /* Requested object does not exist */ E_EXIST, /* Requested object does not exist */
E_INVAL, /* Invalid operation requested */ E_INVAL, /* Invalid operation requested */
E_IO, /* I/O error */
E_NOTRACK, /* Track ID not found */
}; };
#endif /* OCARINA_ERROR_H */ #endif /* OCARINA_ERROR_H */

View File

@ -29,8 +29,8 @@ private:
unsigned int version; unsigned int version;
std::string find_dir(); std::string find_dir();
void open_read(); bool open_read();
void open_write(); bool open_write();
public: public:
File(const std::string &, FileLocHint); File(const std::string &, FileLocHint);
@ -38,7 +38,7 @@ public:
const char *get_filepath(); const char *get_filepath();
const unsigned int get_version(); const unsigned int get_version();
bool exists(); bool exists();
void open(OpenMode); bool open(OpenMode);
void close(); void close();
std::string getline(); std::string getline();
}; };

View File

@ -65,64 +65,66 @@ std::string File :: find_dir()
return res; return res;
} }
void File :: open_read() bool File :: open_read()
{ {
if (!exists()) { if (!exists()) {
dprint("ERROR: File does not exist\n"); dprint("ERROR: File does not exist\n");
throw -E_EXIST; return false;
} }
std::fstream::open(filepath.c_str(), std::fstream::in); std::fstream::open(filepath.c_str(), std::fstream::in);
if (std::fstream::fail()) { if (std::fstream::fail()) {
dprint("ERROR: File could not be opened for reading\n"); dprint("ERROR: File could not be opened for reading\n");
throw -E_IO; return false;
} }
mode = OPEN_READ; mode = OPEN_READ;
std::fstream::operator>>(version); std::fstream::operator>>(version);
getline(); getline();
return true;
} }
void File :: open_write() bool File :: open_write()
{ {
if (hint == FILE_TYPE_LEGACY) { if (hint == FILE_TYPE_LEGACY) {
dprint("ERROR: Cannot write to legacy files\n"); dprint("ERROR: Cannot write to legacy files\n");
throw -E_IO; return false;
} }
std::string dir = find_dir(); std::string dir = find_dir();
if (g_mkdir_with_parents(dir.c_str(), 0755) != 0) { if (g_mkdir_with_parents(dir.c_str(), 0755) != 0) {
dprint("ERROR: Could not make directory\n"); dprint("ERROR: Could not make directory\n");
throw -E_IO; return false;
} }
std::fstream::open(filepath.c_str(), std::fstream::out); std::fstream::open(filepath.c_str(), std::fstream::out);
if (std::fstream::fail()) { if (std::fstream::fail()) {
dprint("ERROR: Could not open file for writing\n"); dprint("ERROR: Could not open file for writing\n");
throw -E_IO; return false;
} }
mode = OPEN_WRITE; mode = OPEN_WRITE;
std::fstream::operator<<(FILE_VERSION) << std::endl; std::fstream::operator<<(FILE_VERSION) << std::endl;
return true;
} }
void File :: open(OpenMode m) bool File :: open(OpenMode m)
{ {
if (hint == FILE_TYPE_INVALID) { if (hint == FILE_TYPE_INVALID) {
dprint("ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened\n"); dprint("ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened\n");
throw -E_INVAL; return false;
} else if (m == NOT_OPEN) { } else if (m == NOT_OPEN) {
dprint("ERROR: NOT_OPEN is not a legal OpenMode\n"); dprint("ERROR: NOT_OPEN is not a legal OpenMode\n");
throw -E_INVAL; return false;
} else if (mode != NOT_OPEN) { } else if (mode != NOT_OPEN) {
dprint("ERROR: File is already open\n"); dprint("ERROR: File is already open\n");
throw -E_IO; return false;
} }
else if (m == OPEN_READ) else if (m == OPEN_READ)
open_read(); return open_read();
else /* m == OPEN_WRITE */ else /* m == OPEN_WRITE */
open_write(); return open_write();
} }
void File :: close() void File :: close()

View File

@ -37,12 +37,9 @@ int print_filepath(File &f)
int open_file(File &f, OpenMode mode) int open_file(File &f, OpenMode mode)
{ {
try { if (f.open(mode) == true)
f.open(mode);
return 0; return 0;
} catch (int err) { return 1;
return err;
}
} }
int main(int argc, char **argv) int main(int argc, char **argv)