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

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-10 09:46:33 -04:00
parent a88da1dabf
commit c88b796a7a
13 changed files with 50 additions and 53 deletions

View File

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

View File

@ -91,7 +91,7 @@ void deck :: init()
bool upgraded = false;
std::list<TempQueue>::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<TempQueue>::iterator it;
if (!deck_file.open(OPEN_WRITE))
if (!file_open(&deck_file, OPEN_WRITE))
return;
deck_file << queue_deck.size() << std :: endl;

View File

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

View File

@ -27,7 +27,7 @@ public:
{
std::vector<struct sort_info>::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;

View File

@ -24,7 +24,7 @@ Database<T> :: ~Database()
template <class T>
void Database<T> :: 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<T> :: 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;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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