From 1dcc56c87e870762452bd6a34da9869525c96389 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 10 Sep 2015 08:00:42 -0400 Subject: [PATCH] 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 --- core/audio.cpp | 2 +- core/deck.cpp | 2 +- core/file.cpp | 46 ++++++++++++++++++------------------- core/index.cpp | 4 ++-- core/library.cpp | 2 +- core/queue.cpp | 4 ++-- core/tags/album.cpp | 4 ++-- core/tags/generic.cpp | 4 ++-- core/tags/library.cpp | 4 ++-- core/tags/track.cpp | 4 ++-- include/core/database.h | 6 ++--- include/core/file.h | 17 ++++++-------- include/core/index.h | 4 ++-- include/core/queue.h | 4 ++-- include/core/tags/album.h | 4 ++-- include/core/tags/generic.h | 4 ++-- include/core/tags/library.h | 4 ++-- include/core/tags/track.h | 4 ++-- tests/core/database.cpp | 4 ++-- tests/core/deck.cpp | 2 +- tests/core/file.cpp | 18 +++++++-------- tests/core/queue.cpp | 2 +- tests/core/tags/album.cpp | 2 +- tests/core/tags/generic.cpp | 2 +- tests/core/tags/library.cpp | 2 +- tests/core/tags/track.cpp | 2 +- 26 files changed, 77 insertions(+), 80 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index 5b541f43..a1912a66 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -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; diff --git a/core/deck.cpp b/core/deck.cpp index 76a78f0c..bd1c7c65 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -22,7 +22,7 @@ public: static std::list queue_deck; static RecentQueue recent_queue; -static File deck_file("deck", 1); +static file deck_file("deck", 1); TempQueue :: TempQueue() {} diff --git a/core/file.cpp b/core/file.cpp index cba7f73e..67085b82 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -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; diff --git a/core/index.cpp b/core/index.cpp index f29b6a23..8d3401a8 100644 --- a/core/index.cpp +++ b/core/index.cpp @@ -44,7 +44,7 @@ typename IndexEntry::iterator IndexEntry :: end() return _values.end(); } -void IndexEntry :: write(File &file) +void IndexEntry :: write(file &file) { std::set::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; diff --git a/core/library.cpp b/core/library.cpp index 57a3a62f..c83f81bf 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -12,7 +12,7 @@ class LibraryQueue : public Queue { private: - File f; + file f; public: diff --git a/core/queue.cpp b/core/queue.cpp index d030c1e8..db178c6a 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -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; diff --git a/core/tags/album.cpp b/core/tags/album.cpp index ca5305fc..c7509c25 100644 --- a/core/tags/album.cpp +++ b/core/tags/album.cpp @@ -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); diff --git a/core/tags/generic.cpp b/core/tags/generic.cpp index 650449b0..58cc5ded 100644 --- a/core/tags/generic.cpp +++ b/core/tags/generic.cpp @@ -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; } diff --git a/core/tags/library.cpp b/core/tags/library.cpp index 2a3a6eb9..b8b63a1c 100644 --- a/core/tags/library.cpp +++ b/core/tags/library.cpp @@ -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; } diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 5b724e86..668c5baf 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -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 << " "; diff --git a/include/core/database.h b/include/core/database.h index 8f58efbd..aa7140b4 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -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 */ diff --git a/include/core/file.h b/include/core/file.h index 968d4fe1..fd30c76a 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -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 diff --git a/include/core/index.h b/include/core/index.h index 6b37c09f..37cad964 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -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 &); }; diff --git a/include/core/queue.h b/include/core/queue.h index 3423a342..8d243302 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -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 &); /** diff --git a/include/core/tags/album.h b/include/core/tags/album.h index 0dea895d..2f184201 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -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. diff --git a/include/core/tags/generic.h b/include/core/tags/generic.h index 7fca9cc7..312eb85b 100644 --- a/include/core/tags/generic.h +++ b/include/core/tags/generic.h @@ -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. diff --git a/include/core/tags/library.h b/include/core/tags/library.h index 867450e0..e723414b 100644 --- a/include/core/tags/library.h +++ b/include/core/tags/library.h @@ -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 &); /** diff --git a/include/core/tags/track.h b/include/core/tags/track.h index d4b336ee..821dbdda 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -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 diff --git a/tests/core/database.cpp b/tests/core/database.cpp index c87cb284..16495b05 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -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; } }; diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index de752d92..fb8f0583 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -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::iterator it; test_equal(deck :: next(), TRACK_NULL); diff --git a/tests/core/file.cpp b/tests/core/file.cpp index 6ab9b320..7a583aa9 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.cpp @@ -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), ); diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 107cd915..0d4acf6c 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -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); diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index 96b3f9f3..dec65abf 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -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"); diff --git a/tests/core/tags/generic.cpp b/tests/core/tags/generic.cpp index a35a4824..e8457aee 100644 --- a/tests/core/tags/generic.cpp +++ b/tests/core/tags/generic.cpp @@ -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"); diff --git a/tests/core/tags/library.cpp b/tests/core/tags/library.cpp index 028150ee..b2e9451f 100644 --- a/tests/core/tags/library.cpp +++ b/tests/core/tags/library.cpp @@ -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); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 19b52640..37625c25 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -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");