diff --git a/core/audio.cpp b/core/audio.cpp index a83ee1b2..60183180 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -19,7 +19,7 @@ static void save_state() { file_open(&f_cur_track, OPEN_WRITE); f_cur_track << cur_track->index() << std::endl; - f_cur_track.close(); + file_close(&f_cur_track); } static void _load_track(Track *track, bool start_playback) @@ -82,7 +82,7 @@ void audio :: init() if (file_exists(&f_cur_track)) { file_open(&f_cur_track, OPEN_READ); f_cur_track >> id; - f_cur_track.close(); + file_close(&f_cur_track); audio :: load_track(tags :: get_track(id)); } } diff --git a/core/deck.cpp b/core/deck.cpp index 22659aa1..0058cee6 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -104,7 +104,7 @@ void deck :: init() for (it = queue_deck.begin(); it != queue_deck.end(); it++) it->read(deck_file); - deck_file.close(); + file_close(&deck_file); if (upgraded) deck :: write(); @@ -123,7 +123,7 @@ void deck :: write() deck_file << std::endl; } - deck_file.close(); + file_close(&deck_file); } Queue *deck :: create(bool random) diff --git a/core/file.cpp b/core/file.cpp index 10e7d40c..c3d05997 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -30,11 +30,6 @@ file :: file(const std::string &name, unsigned int version) { } -file :: ~file() -{ - close(); -} - const std::string file_path(struct file *file) { std::string res = ""; @@ -103,11 +98,11 @@ bool file_open(struct file *file, OpenMode mode) return __file_open_write(file); } -void file :: close() +void file_close(struct file *file) { - if (f_mode != NOT_OPEN) - std::fstream::close(); - f_mode = NOT_OPEN; + if (file->f_mode != NOT_OPEN) + file->close(); + file->f_mode = NOT_OPEN; } std::string file :: getline() diff --git a/core/library.cpp b/core/library.cpp index 8173d851..a1c21259 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -32,7 +32,7 @@ public: for (it = _sort_order.begin(); it != _sort_order.end(); it++) f << " " << it->field << " " << it->ascending; f << std::endl; - f.close(); + file_close(&f); } void load() @@ -51,6 +51,7 @@ public: if (ascending == false) Queue :: sort((sort_t)field, false); } + file_close(&f); } void set_flag(queue_flags f) { Queue :: set_flag(f); save(); } diff --git a/include/core/database.hpp b/include/core/database.hpp index 7f6c8a37..3cfcaa21 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -38,7 +38,7 @@ void Database :: save() _file << std::endl; } - _file.close(); + file_close(&_file); } template @@ -74,7 +74,7 @@ void Database :: load() } } - _file.close(); + file_close(&_file); } template diff --git a/include/core/file.h b/include/core/file.h index 27be3141..8b4243b7 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -56,16 +56,6 @@ struct file : public std::fstream { */ file(const std::string &, unsigned int); - /** - * Closes the file stream if it is still open. - */ - ~file(); - - /** - * Close an open file and set File::mode to ::NOT_OPEN. - */ - void close(); - /** * Read an entire line from the file and return it to the caller. * In theory a return value optimization will occur so returning @@ -103,5 +93,8 @@ bool file_exists(struct file *); bool file_open(struct file *, OpenMode); +/* Close an open file, setting file->mode to NOT_OPEN. */ +void file_close(struct file *); + #endif /* OCARINA_CORE_FILE_H */ diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index c46ed475..c893b218 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -56,7 +56,7 @@ static void test_init() } } - f.close(); + file_close(&f); } static class TestNotifier : public QNotifier { diff --git a/tests/core/file.cpp b/tests/core/file.cpp index 23d9c286..ec2d176b 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.cpp @@ -18,6 +18,9 @@ static void test_filepath() test_equal(file_version(&b), (unsigned)0); test_equal((std::string)file_path(&b), filepath); + test_equal(a.f_mode, NOT_OPEN); + test_equal(b.f_mode, NOT_OPEN); + test_equal(file_exists(&a), false); test_equal(file_exists(&b), false); test_equal(test_data_file_exists(NULL), false); @@ -31,6 +34,8 @@ static void test_open() test_equal(file_open(&a, OPEN_READ), false); test_equal(file_open(&a, OPEN_WRITE), false); + file_close(&a); + test_equal(a.f_mode, NOT_OPEN); file b("file.txt", 0); test_equal(file_open(&b, NOT_OPEN), false); @@ -38,7 +43,8 @@ static void test_open() 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(); + file_close(&b); + test_equal(b.f_mode, NOT_OPEN); test_equal(test_data_file_exists("file.txt"), true); } @@ -49,7 +55,7 @@ static void test_io() test_equal(file_open(&a, OPEN_WRITE), true); a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl; - a.close(); + file_close(&a); test_equal(file_exists(&a), true); file b("file.txt", 0); @@ -63,6 +69,7 @@ static void test_io() test_equal(res, (std::string)"FGHIJ"); res = b.getline(); test_equal(res, (std::string)"KLMNO PQRST UVWXYZ"); + file_close(&b); } DECLARE_UNIT_TESTS( diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 51df8c0e..10b8301e 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -289,11 +289,11 @@ void test_saving() file_open(&f, OPEN_WRITE); q.write(f); - f.close(); + file_close(&f); file_open(&f, OPEN_READ); r.read(f); - f.close(); + file_close(&f); test_equal(r.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM)); test_equal(r.size(), q.size()); diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index 24b499d2..ec097b98 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -16,7 +16,7 @@ static void test_album_tag() file_open(&f, OPEN_WRITE); album.write(f); - f.close(); + file_close(&f); album = Album(); test_equal(album.name(), (std::string)""); @@ -26,7 +26,7 @@ static void test_album_tag() file_open(&f, OPEN_READ); album.read(f); - f.close(); + file_close(&f); test_equal(album.name(), (std::string)"Hyrule Symphony"); test_equal(album.lowercase(), (std::string)"hyrule symphony"); diff --git a/tests/core/tags/generic.cpp b/tests/core/tags/generic.cpp index 5f0d7dfb..2247ed1d 100644 --- a/tests/core/tags/generic.cpp +++ b/tests/core/tags/generic.cpp @@ -15,7 +15,7 @@ static void test_generic_tag() file_open(&f, OPEN_WRITE); tag.write(f); - f.close(); + file_close(&f); tag = GenericTag(); test_equal(tag.name(), (std::string)""); @@ -24,7 +24,7 @@ static void test_generic_tag() file_open(&f, OPEN_READ); tag.read(f); - f.close(); + file_close(&f); test_equal(tag.name(), (std::string)"Generic Tag"); test_equal(tag.lowercase(), (std::string)"generic tag"); diff --git a/tests/core/tags/library.cpp b/tests/core/tags/library.cpp index 6e0383f2..2bc4049f 100644 --- a/tests/core/tags/library.cpp +++ b/tests/core/tags/library.cpp @@ -25,7 +25,7 @@ static void test_library_tag() file_open(&f, OPEN_WRITE); library.write(f); - f.close(); + file_close(&f); library = Library(); test_equal(library.primary_key(), (std::string)""); @@ -34,7 +34,7 @@ static void test_library_tag() file_open(&f, OPEN_READ); library.read(f); - f.close(); + file_close(&f); test_equal(library.primary_key(), (std::string)"/home/Zelda/Music"); test_equal(library.size(), (unsigned)0); /* _size is not saved to disk. */ diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index cfc3a2d2..80954799 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -74,11 +74,11 @@ static void test_track_tag_constructor() file_open(&f, OPEN_WRITE); a.write(f); - f.close(); + file_close(&f); file_open(&f, OPEN_READ); b.read(f); - f.close(); + file_close(&f); verify_track_tag(&b, 2);