file: Set version number on a per-file basis

I'm about to bump the version number for the deck layer, so it makes
senes that different files need different version numbers.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-05-26 12:10:36 -04:00
parent ddfdc7d6f6
commit a96e6f6f2d
8 changed files with 29 additions and 24 deletions

16
DESIGN
View File

@ -95,9 +95,6 @@ On-disk files:
- Notation:
"File << aaaaa << bbbbb << endl" is translated into "aaaaa bbbbb\n"
- File version:
#define FILE_VERSION 0
- Open mode:
enum OpenMode {
OPEN_READ,
@ -109,11 +106,12 @@ On-disk files:
class File : public std::fstream {
private:
unsigned int version;
unsigned int prev_version;
OpenMode mode;
string filename;
public:
File(const std::string &);
File(const std::string &, unsigned int);
~File();
const std::string get_filepath();
const unsigned int get_version();
@ -128,8 +126,8 @@ On-disk files:
File << <OTHER_DATA>;
- API:
File :: File(const std::string &filename);
Store the name of the file.
File :: File(const std::string &filename, unsigned int version);
Store the name of the file and the file version.
File :: ~File();
Close the file stream if it is open.
@ -146,7 +144,9 @@ On-disk files:
and return the result.
const unsigned int File :: get_version();
Return the file version number.
if mode == OPEN_READ:
return prev_version
return version
bool File :: exists();
Return true if the file exists in the filesystem.
@ -161,7 +161,7 @@ On-disk files:
When opening a file for reading (mode == OPEN_READ),
- Return false if the file does not exist
- Open the file
- Read in version from the start of the file
- Read in prev_version from the start of the file
When opening a file for writing (mode == OPEN_WRITE),
- Create missing directories as needed

View File

@ -9,7 +9,7 @@
template <class T>
Database<T> :: Database(std::string filepath, bool autosave)
: _size(0), _autosave(autosave), _file(filepath)
: _size(0), _autosave(autosave), _file(filepath, 0)
{
}

View File

@ -7,7 +7,6 @@
#include <fstream>
#include <string>
#define FILE_VERSION 0
enum OpenMode {
OPEN_READ,
@ -15,18 +14,21 @@ enum OpenMode {
NOT_OPEN,
};
class File : public std::fstream {
private:
OpenMode mode;
std::string filename;
unsigned int version;
unsigned int prev_version;
const std::string find_dir();
bool open_read();
bool open_write();
public:
File(const std::string &);
File(const std::string &, unsigned int);
~File();
const std::string get_filepath();
const unsigned int get_version();

View File

@ -19,7 +19,7 @@ static bool o_pause_enabled = false;
static unsigned int o_pause_count = 0;
static bool o_should_pause = false;
static File f_cur_track("cur_track");
static File f_cur_track("cur_track", 0);
class RecentQueue : public Queue

View File

@ -10,7 +10,7 @@
#include <list>
static std::list<Queue> playqueue_deck;
static File deck_file("deck");
static File deck_file("deck", 0);
void deck :: init()
{

View File

@ -14,8 +14,8 @@ const std::string OCARINA_DIR = "ocarina-debug";
const std::string OCARINA_DIR = "ocarina";
#endif
File :: File(const std::string &name)
: mode(NOT_OPEN), filename(name), version(FILE_VERSION)
File :: File(const std::string &name, unsigned int vers)
: mode(NOT_OPEN), filename(name), version(vers), prev_version(0)
{
}
@ -44,6 +44,8 @@ const std::string File :: get_filepath()
const unsigned int File :: get_version()
{
if (mode == OPEN_READ)
return prev_version;
return version;
}
@ -62,7 +64,7 @@ bool File :: open_read()
return false;
mode = OPEN_READ;
std::fstream::operator>>(version);
std::fstream::operator>>(prev_version);
getline();
return true;
}
@ -77,7 +79,7 @@ bool File :: open_write()
return false;
mode = OPEN_WRITE;
std::fstream::operator<<(FILE_VERSION) << std::endl;
std::fstream::operator<<(version) << std::endl;
return true;
}

View File

@ -13,7 +13,7 @@ private:
public:
LibraryQueue() : Queue(Q_ENABLED | Q_REPEAT), f("library.q")
LibraryQueue() : Queue(Q_ENABLED | Q_REPEAT), f("library.q", 0)
{
Queue :: sort(SORT_ARTIST, true);
Queue :: sort(SORT_YEAR, false);

View File

@ -7,8 +7,8 @@
static void test_filepath()
{
File a("");
File b("file.txt");
File a("", 0);
File b("file.txt", 0);
test_equal(a.get_version(), (unsigned)0);
test_equal((std::string)a.get_filepath(), (std::string)"");
@ -23,12 +23,12 @@ static void test_filepath()
static void test_open()
{
File a("");
File a("", 0);
test_equal(a.open(OPEN_READ), false);
test_equal(a.open(OPEN_WRITE), false);
File b("file.txt");
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);
@ -40,17 +40,18 @@ static void test_open()
static void test_io()
{
File a("file.txt");
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");
File b("file.txt", 0);
std::string res;
test_equal(b.open(OPEN_READ), true);
test_equal(b.get_version(), (unsigned)1);
b >> res;
test_equal(res, (std::string)"ABCDE");
b >> res;