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:
parent
959cac0fe1
commit
b251f27bb5
16
DESIGN
16
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.
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
|
28
lib/file.cpp
28
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()
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue