core/file: Move the close() function out of the file struct

This change also lets me remove the destructor, since code is expected
to call file_close() when IO is complete.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-10 09:12:48 -04:00
parent c88b796a7a
commit 09c7164aee
13 changed files with 35 additions and 39 deletions

View File

@ -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));
}
}

View File

@ -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)

View File

@ -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()

View File

@ -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(); }

View File

@ -38,7 +38,7 @@ void Database<T> :: save()
_file << std::endl;
}
_file.close();
file_close(&_file);
}
template <class T>
@ -74,7 +74,7 @@ void Database<T> :: load()
}
}
_file.close();
file_close(&_file);
}
template <class T>

View File

@ -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 */

View File

@ -56,7 +56,7 @@ static void test_init()
}
}
f.close();
file_close(&f);
}
static class TestNotifier : public QNotifier {

View File

@ -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(

View File

@ -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());

View File

@ -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");

View File

@ -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");

View File

@ -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. */

View File

@ -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);