diff --git a/core/audio.cpp b/core/audio.cpp index c2f6307f..a83ee1b2 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -17,7 +17,7 @@ static AudioDriver *cur_driver = NULL; static void save_state() { - f_cur_track.open(OPEN_WRITE); + file_open(&f_cur_track, OPEN_WRITE); f_cur_track << cur_track->index() << std::endl; f_cur_track.close(); } @@ -80,7 +80,7 @@ void audio :: init() unsigned int id; if (file_exists(&f_cur_track)) { - f_cur_track.open(OPEN_READ); + file_open(&f_cur_track, OPEN_READ); f_cur_track >> id; f_cur_track.close(); audio :: load_track(tags :: get_track(id)); diff --git a/core/deck.cpp b/core/deck.cpp index 636a05bf..22659aa1 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -91,7 +91,7 @@ void deck :: init() bool upgraded = false; std::list::iterator it; - if (!deck_file.open(OPEN_READ)) + if (!file_open(&deck_file, OPEN_READ)) return; if (file_version(&deck_file) == 0) { @@ -114,7 +114,7 @@ void deck :: write() { std::list::iterator it; - if (!deck_file.open(OPEN_WRITE)) + if (!file_open(&deck_file, OPEN_WRITE)) return; deck_file << queue_deck.size() << std :: endl; diff --git a/core/file.cpp b/core/file.cpp index 752c4f36..10e7d40c 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -92,14 +92,15 @@ static bool __file_open_write(struct file *file) return true; } -bool file :: open(OpenMode mode) +bool file_open(struct file *file, OpenMode mode) { - if ((f_name == "") || (mode == NOT_OPEN) || (f_mode != NOT_OPEN)) + if ((file->f_name == "") || (mode == NOT_OPEN) || + (file->f_mode != NOT_OPEN)) return false; if (mode == OPEN_READ) - return __file_open_read(this); - return __file_open_write(this); + return __file_open_read(file); + return __file_open_write(file); } void file :: close() diff --git a/core/library.cpp b/core/library.cpp index c83f81bf..8173d851 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -27,7 +27,7 @@ public: { std::vector::iterator it; - f.open(OPEN_WRITE); + file_open(&f, OPEN_WRITE); f << _flags << " " << _sort_order.size(); for (it = _sort_order.begin(); it != _sort_order.end(); it++) f << " " << it->field << " " << it->ascending; @@ -41,7 +41,7 @@ public: bool ascending; unsigned int n; - if (!f.open(OPEN_READ)) + if (!file_open(&f, OPEN_READ)) return; f >> _flags >> n; diff --git a/include/core/database.hpp b/include/core/database.hpp index ae7be39f..7f6c8a37 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -24,7 +24,7 @@ Database :: ~Database() template void Database :: save() { - if (_file.open(OPEN_WRITE) == false) + if (file_open(&_file, OPEN_WRITE) == false) return; _file << actual_size() << std::endl; @@ -56,7 +56,7 @@ void Database :: load() if (file_exists(&_file) == false) return; - else if (_file.open(OPEN_READ) == false) + else if (file_open(&_file, OPEN_READ) == false) return; _file >> db_size; diff --git a/include/core/file.h b/include/core/file.h index 8af9f807..27be3141 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -61,28 +61,6 @@ struct file : public std::fstream { */ ~file(); - /** - * Call to open a file for either reading or writing. - * - * When opening a file for reading: - * -# Check if the file exists. - * -# Open the file stream. - * -# Read in File::_prev_version from the start of the file. - * - * When opening a file for writing: - * -# Create missing directories as needed. - * -# Write File::_version to the start of the file. - * - * @param mode How the file should be opened. - * @return True if the open was successful. - * @return False under the following conditions: - * - File::_filename is unset - * - mode is ::NOT_OPEN - * - The file is already open (File::_mode != NOT_OPEN) - * - The file does not exist (mode == ::OPEN_READ) - */ - bool open(OpenMode); - /** * Close an open file and set File::mode to ::NOT_OPEN. */ @@ -108,5 +86,22 @@ const unsigned int file_version(struct file *); /* Returns true if the file exists on disk and false otherwise. */ bool file_exists(struct file *); +/* + * Call to open a file for either reading or writing. Callers + * are expected to call file_close() when IO is completed. + * + * When opening a file for reading (OPEN_READ): + * - Check if the file exists. + * - Read in file->_prev_version from the start of the file. + * + * When opening a file for writing (OPEN_WRITE): + * - Create missing directories as needed. + * - Write file->_version to the start of the file. + * + * Returns true if the open was successful and false otherwise. + */ +bool file_open(struct file *, OpenMode); + + #endif /* OCARINA_CORE_FILE_H */ diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index 7cdb631c..c46ed475 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -38,7 +38,7 @@ static void test_init() /* * Test that we saved the deck in the new format */ - f.open(OPEN_READ); + file_open(&f, OPEN_READ); test_equal(file_version(&f), (unsigned)1); f >> val; /* number of queues */ test_equal(val, (unsigned)2); diff --git a/tests/core/file.cpp b/tests/core/file.cpp index 8b6ce057..23d9c286 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.cpp @@ -29,14 +29,15 @@ static void test_open() { file a("", 0); - test_equal(a.open(OPEN_READ), false); - test_equal(a.open(OPEN_WRITE), false); + test_equal(file_open(&a, OPEN_READ), false); + test_equal(file_open(&a, OPEN_WRITE), false); file b("file.txt", 0); - test_equal(b.open(NOT_OPEN), false); - test_equal(b.open(OPEN_READ), false); - test_equal(b.open(OPEN_WRITE), true); - test_equal(b.open(OPEN_WRITE), false); + test_equal(file_open(&b, NOT_OPEN), false); + test_equal(file_open(&b, OPEN_READ), false); + test_equal(file_open(&b, OPEN_WRITE), true); + test_equal(b.f_mode, OPEN_WRITE); + test_equal(file_open(&b, OPEN_WRITE), false); b.close(); test_equal(test_data_file_exists("file.txt"), true); @@ -46,7 +47,7 @@ static void test_io() { file a("file.txt", 1); - test_equal(a.open(OPEN_WRITE), true); + test_equal(file_open(&a, OPEN_WRITE), true); a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl; a.close(); test_equal(file_exists(&a), true); @@ -54,7 +55,7 @@ static void test_io() file b("file.txt", 0); std::string res; - test_equal(b.open(OPEN_READ), true); + test_equal(file_open(&b, OPEN_READ), true); test_equal(file_version(&b), (unsigned)1); b >> res; test_equal(res, (std::string)"ABCDE"); diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 0d4acf6c..51df8c0e 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -287,11 +287,11 @@ void test_saving() test_fill_q(&q); - f.open(OPEN_WRITE); + file_open(&f, OPEN_WRITE); q.write(f); f.close(); - f.open(OPEN_READ); + file_open(&f, OPEN_READ); r.read(f); f.close(); diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index dec65abf..24b499d2 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -14,7 +14,7 @@ static void test_album_tag() test_equal(album.year(), (unsigned int)1998); test_equal(album.primary_key(), (std::string)"1998/Hyrule Symphony"); - f.open(OPEN_WRITE); + file_open(&f, OPEN_WRITE); album.write(f); f.close(); @@ -24,7 +24,7 @@ static void test_album_tag() test_equal(album.year(), (unsigned int)0); test_equal(album.primary_key(), (std::string)"0/"); - f.open(OPEN_READ); + file_open(&f, OPEN_READ); album.read(f); f.close(); diff --git a/tests/core/tags/generic.cpp b/tests/core/tags/generic.cpp index e8457aee..5f0d7dfb 100644 --- a/tests/core/tags/generic.cpp +++ b/tests/core/tags/generic.cpp @@ -13,7 +13,7 @@ static void test_generic_tag() test_equal(tag.lowercase(), (std::string)"generic tag"); test_equal(tag.primary_key(), (std::string)"Generic Tag"); - f.open(OPEN_WRITE); + file_open(&f, OPEN_WRITE); tag.write(f); f.close(); @@ -22,7 +22,7 @@ static void test_generic_tag() test_equal(tag.lowercase(), (std::string)""); test_equal(tag.primary_key(), (std::string)""); - f.open(OPEN_READ); + file_open(&f, OPEN_READ); tag.read(f); f.close(); diff --git a/tests/core/tags/library.cpp b/tests/core/tags/library.cpp index b2e9451f..6e0383f2 100644 --- a/tests/core/tags/library.cpp +++ b/tests/core/tags/library.cpp @@ -23,7 +23,7 @@ static void test_library_tag() test_equal(library.size(), (unsigned)21); - f.open(OPEN_WRITE); + file_open(&f, OPEN_WRITE); library.write(f); f.close(); @@ -32,7 +32,7 @@ static void test_library_tag() test_equal(library.size(), (unsigned)0); test_equal(library.enabled(), false); - f.open(OPEN_READ); + file_open(&f, OPEN_READ); library.read(f); f.close(); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 37625c25..cfc3a2d2 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -72,11 +72,11 @@ static void test_track_tag_constructor() verify_track_tag(&a, 1); - f.open(OPEN_WRITE); + file_open(&f, OPEN_WRITE); a.write(f); f.close(); - f.open(OPEN_READ); + file_open(&f, OPEN_READ); b.read(f); f.close();