core/file: Convert class to a struct

In addition, we lowercase variable names and add an f_ prefix to all
struct mebers.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-10 08:00:42 -04:00
parent 068afd6276
commit 1dcc56c87e
26 changed files with 77 additions and 80 deletions

View File

@ -11,7 +11,7 @@ static bool _pause_enabled = false;
static unsigned int _pause_count = 0;
static Track *cur_track = NULL;
static File f_cur_track("cur_track", 0);
static file f_cur_track("cur_track", 0);
static AudioDriver *cur_driver = NULL;

View File

@ -22,7 +22,7 @@ public:
static std::list<TempQueue> queue_deck;
static RecentQueue recent_queue;
static File deck_file("deck", 1);
static file deck_file("deck", 1);
TempQueue :: TempQueue() {}

View File

@ -20,40 +20,40 @@ static const std::string find_ocarina_dir()
}
File :: File(const std::string &name, unsigned int version)
: _mode(NOT_OPEN), _filename(name), _version(version), _prev_version(0)
file :: file(const std::string &name, unsigned int version)
: f_mode(NOT_OPEN), f_version(version), f_prev(0), f_name(name)
{
}
File :: ~File()
file :: ~file()
{
close();
}
const std::string File :: get_filepath()
const std::string file :: get_filepath()
{
std::string res = "";
if (_filename != "") {
if (f_name != "") {
res = find_ocarina_dir();
res += "/" + _filename;
res += "/" + f_name;
}
return res;
}
const unsigned int File :: get_version()
const unsigned int file :: get_version()
{
if (_mode == OPEN_READ)
return _prev_version;
return _version;
if (f_mode == OPEN_READ)
return f_prev;
return f_version;
}
bool File :: exists()
bool file :: exists()
{
return g_file_test(get_filepath().c_str(), G_FILE_TEST_EXISTS);
}
bool File :: _open_read()
bool file :: _open_read()
{
if (!exists())
return false;
@ -62,13 +62,13 @@ bool File :: _open_read()
if (std::fstream::fail())
return false;
_mode = OPEN_READ;
std::fstream::operator>>(_prev_version);
f_mode = OPEN_READ;
std::fstream::operator>>(f_prev);
getline();
return true;
}
bool File :: _open_write()
bool file :: _open_write()
{
if (g_mkdir_with_parents(find_ocarina_dir().c_str(), 0755) != 0)
return false;
@ -77,14 +77,14 @@ bool File :: _open_write()
if (std::fstream::fail())
return false;
_mode = OPEN_WRITE;
std::fstream::operator<<(_version) << std::endl;
f_mode = OPEN_WRITE;
std::fstream::operator<<(f_version) << std::endl;
return true;
}
bool File :: open(OpenMode mode)
bool file :: open(OpenMode mode)
{
if ((_filename == "") || (mode == NOT_OPEN) || (_mode != NOT_OPEN))
if ((f_name == "") || (mode == NOT_OPEN) || (f_mode != NOT_OPEN))
return false;
else if (mode == OPEN_READ)
@ -93,14 +93,14 @@ bool File :: open(OpenMode mode)
return _open_write();
}
void File :: close()
void file :: close()
{
if (_mode != NOT_OPEN)
if (f_mode != NOT_OPEN)
std::fstream::close();
_mode = NOT_OPEN;
f_mode = NOT_OPEN;
}
std::string File :: getline()
std::string file :: getline()
{
char c;
std::string res;

View File

@ -44,7 +44,7 @@ typename IndexEntry::iterator IndexEntry :: end()
return _values.end();
}
void IndexEntry :: write(File &file)
void IndexEntry :: write(file &file)
{
std::set<unsigned int>::iterator it;
file << _key << std::endl << _values.size() << " ";
@ -52,7 +52,7 @@ void IndexEntry :: write(File &file)
file << *it << " ";
}
void IndexEntry :: read(File &file)
void IndexEntry :: read(file &file)
{
unsigned int num, val;

View File

@ -12,7 +12,7 @@
class LibraryQueue : public Queue {
private:
File f;
file f;
public:

View File

@ -31,14 +31,14 @@ Queue :: Queue()
Queue :: ~Queue()
{}
void Queue :: write(File &file)
void Queue :: write(file &file)
{
file << _flags << " " << _tracks.size();
for (unsigned int i = 0; i < _tracks.size(); i++)
file << " " << _tracks[i]->index();
}
void Queue :: read(File &file)
void Queue :: read(file &file)
{
unsigned int n, id;
file >> _flags >> n;

View File

@ -30,13 +30,13 @@ const std::string Album :: primary_key() const
return make_key(GenericTag :: primary_key(), _year);
}
void Album :: read(File &file)
void Album :: read(file &file)
{
file >> _year;
GenericTag :: read(file);
}
void Album :: write(File &file)
void Album :: write(file &file)
{
file << _year << " ";
GenericTag :: write(file);

View File

@ -26,7 +26,7 @@ const std::string GenericTag :: primary_key() const
return _name;
}
void GenericTag :: read(File &file)
void GenericTag :: read(file &file)
{
gchar *g_lc;
@ -36,7 +36,7 @@ void GenericTag :: read(File &file)
g_free(g_lc);
}
void GenericTag :: write(File &file)
void GenericTag :: write(file &file)
{
file << _name;
}

View File

@ -22,13 +22,13 @@ const std::string Library :: primary_key() const
return _path;
}
void Library :: read(File &file)
void Library :: read(file &file)
{
file >> _enabled;
_path = file.getline();
}
void Library :: write(File &file)
void Library :: write(file &file)
{
file << _enabled << " " << _path;
}

View File

@ -116,7 +116,7 @@ int Track :: compare_date(const Track *rhs)
return ret;
}
void Track :: read(File &file)
void Track :: read(file &file)
{
unsigned int library_id, artist_id, album_id, genre_id;
@ -138,7 +138,7 @@ void Track :: read(File &file)
_library->inc_size();
}
void Track :: write(File &file)
void Track :: write(file &file)
{
file << _library->index() << " " << _artist->index() << " ";
file << _album->index() << " " << _genre->index() << " " << _track << " ";

View File

@ -48,7 +48,7 @@ public:
*
* @param file File to use when writing data.
*/
virtual void write(File &) = 0;
virtual void write(file &) = 0;
/**
* This function is called by the Database to read a single
@ -56,7 +56,7 @@ public:
*
* @param file File to use when reading data.
*/
virtual void read(File &) = 0;
virtual void read(file &) = 0;
};
@ -94,7 +94,7 @@ private:
bool _autosave;
/** File object for reading and writing a Database. */
File _file;
file _file;
public:
/** Iterator access for our backing std::vector */

View File

@ -42,30 +42,27 @@ enum OpenMode {
* Data should be written to files using either a space or newline as
* a delimiter.
*/
class File : public std::fstream {
private:
OpenMode _mode; /**< The file's current open mode. */
std::string _filename; /**< The file's basename. */
unsigned int _version; /**< The file's current data version. */
unsigned int _prev_version; /**< The file's on-disk data version. */
struct file : public std::fstream {
OpenMode f_mode; /* The file's current open mode. */
unsigned int f_version; /* The file's current data version. */
unsigned int f_prev; /* The file's on-disk data version. */
std::string f_name; /* The file's basename. */
bool _open_read();
bool _open_write();
public:
/**
* Set up a new file object.
*
* @param name The name of the file.
* @param version The file version of the new file.
*/
File(const std::string &, unsigned int);
file(const std::string &, unsigned int);
/**
* Closes the file stream if it is still open.
*/
~File();
~file();
/**
* @return The full filepath of the file or an empty string if

View File

@ -88,14 +88,14 @@ public:
*
* @param file The file to use when writing data.
*/
void write(File &);
void write(file &);
/**
* Read an IndexEntry from file.
*
* @param file The file read from.
*/
void read(File &);
void read(file &);
};

View File

@ -115,14 +115,14 @@ public:
*
* @param file File that Queue data will be written to.
*/
void write(File &);
void write(file &);
/**
* Read a queue from disk.
*
* @param file File to read Queue data from.
*/
void read(File &);
void read(file &);
/**

View File

@ -46,14 +46,14 @@ public:
*
* @param file The file to read from.
*/
void read(File &);
void read(file &);
/**
* Write album information to file.
*
* @param file The file to write to.
*/
void write(File &);
void write(file &);
/**
* Called to access the year associated with this album.

View File

@ -53,14 +53,14 @@ public:
*
* @param file The file to read from.
*/
virtual void read(File &);
virtual void read(file &);
/**
* Write GenericTag::_name to file.
*
* @param file The file to write to.
*/
virtual void write(File &);
virtual void write(file &);
/**
* Called to access the name associated with this tag.

View File

@ -46,14 +46,14 @@ public:
*
* @param file The file to read from.
*/
void read(File &);
void read(file &);
/**
* Write library information to file.
*
* @param file The file to write to.
*/
void write(File &);
void write(file &);
/**

View File

@ -112,14 +112,14 @@ public:
*
* @param file The file to read from.
*/
void read(File &);
void read(file &);
/**
* Write track data to file.
*
* @param file The file to write to.
*/
void write(File &);
void write(file &);
};
namespace tags

View File

@ -27,8 +27,8 @@ public:
return res;
}
void write(File &f) { f << val; }
void read(File &f) { f >> val; }
void write(file &f) { f << val; }
void read(file &f) { f >> val; }
};

View File

@ -12,7 +12,7 @@ static Track *TRACK_NULL = NULL;
static void test_init()
{
unsigned int val;
File f("deck", 0);
file f("deck", 0);
std::list<TempQueue>::iterator it;
test_equal(deck :: next(), TRACK_NULL);

View File

@ -9,8 +9,8 @@ static void test_filepath()
{
gchar *filepath = test_data_file("file.txt");
File a("", 0);
File b("file.txt", 0);
file a("", 0);
file b("file.txt", 0);
test_equal(a.get_version(), (unsigned)0);
test_equal((std::string)a.get_filepath(), (std::string)"");
@ -27,12 +27,12 @@ static void test_filepath()
static void test_open()
{
File a("", 0);
file a("", 0);
test_equal(a.open(OPEN_READ), false);
test_equal(a.open(OPEN_WRITE), false);
File b("file.txt", 0);
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);
@ -44,14 +44,14 @@ static void test_open()
static void test_io()
{
File a("file.txt", 1);
file a("file.txt", 1);
test_equal(a.open(OPEN_WRITE), true);
a << "ABCDE FGHIJ KLMNO PQRST UVWXYZ" << std::endl;
a.close();
test_equal(a.exists(), true);
File b("file.txt", 0);
file b("file.txt", 0);
std::string res;
test_equal(b.open(OPEN_READ), true);
@ -65,7 +65,7 @@ static void test_io()
}
DECLARE_UNIT_TESTS(
UNIT_TEST("File Constructor", test_filepath),
UNIT_TEST("File Opening", test_open),
UNIT_TEST("File I/O", test_io),
UNIT_TEST("file Constructor", test_filepath),
UNIT_TEST("file Opening", test_open),
UNIT_TEST("file I/O", test_io),
);

View File

@ -283,7 +283,7 @@ void test_saving()
{
TestQueue q(Q_RANDOM);
TestQueue r(0);
File f("test.q", 0);
file f("test.q", 0);
test_fill_q(&q);

View File

@ -7,7 +7,7 @@
static void test_album_tag()
{
Album album("Hyrule Symphony", 1998);
File f("album_tag", 0);
file f("album_tag", 0);
test_equal(album.name(), (std::string)"Hyrule Symphony");
test_equal(album.lowercase(), (std::string)"hyrule symphony");

View File

@ -7,7 +7,7 @@
static void test_generic_tag()
{
GenericTag tag("Generic Tag");
File f("generic_tag", 0);
file f("generic_tag", 0);
test_equal(tag.name(), (std::string)"Generic Tag");
test_equal(tag.lowercase(), (std::string)"generic tag");

View File

@ -7,7 +7,7 @@
static void test_library_tag()
{
Library library("/home/Zelda/Music");
File f("library_tag", 0);
file f("library_tag", 0);
test_equal(library.primary_key(), (std::string)"/home/Zelda/Music");
test_equal(library.size(), (unsigned)0);

View File

@ -57,7 +57,7 @@ static void verify_track_tag(Track *track, unsigned int size)
static void test_track_tag_constructor()
{
File f("track_tag", 0);
file f("track_tag", 0);
album = tags :: get_album("Hyrule Symphony", 1998);
artist = tags :: get_artist("Koji Kondo");