Design: Create a File class

This class will manage save files so paths don't need to be resolved in
multiple places.  I'll also use it for reading and writing with version
information.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2013-06-27 23:48:09 -04:00 committed by Anna Schumaker
parent fc40a9f77f
commit 5312853b86
1 changed files with 56 additions and 10 deletions

View File

@ -19,12 +19,14 @@ Files:
ocarina/include/
database.h
database.hpp
file.h
group.h
index.h
library.h
playlist.h
ocarina/lib/
database.cpp
file.cpp
group.cpp
index.cpp
library.cpp
@ -41,7 +43,7 @@ Files:
On-disk files:
On-disk files: (lib/file.cpp)
I use the disk to store data between sessions, this could include
library state and user preferences. In theory, file formats do not
change often so updating between file formats should be possible.
@ -58,6 +60,50 @@ On-disk files:
Notation:
File << aaaaa << bbbbb << endl is translated into "aaaaa bbbbb\n"
File version:
#define FILE_VERSION 4
Open mode:
enum OpenMode {
OPEN_READ,
OPEN_WRITE,
}
File:
class File {
private:
union {
ifstream in;
ofstream out;
} f;
unsigned int version;
OpenMode mode;
string filepath;
public:
File(filepath);
open(OpenMode);
const File &operator<<(File &);
const File &operator>>(File &);
getline(string &);
}
File format:
File << FILE_VERSION << <OTHER_DATA>
API:
File : File(filepath)
Resolve filepath to ~/.ocarina{-debug}/filepath
File : open(mode)
Open a file for either reading or writing.
If reading: Read in version from start of file
Else write version to file
File : operator<<()
Write data to out
File : operator>>()
Read data from in;
File : getline(string);
getline(f.in, string) to read an entire line
Database: (lib/database.cpp)
@ -81,16 +127,13 @@ Database: (lib/database.cpp)
include/database.hpp, which will be included by database.h. Any
function not relying on a template can be written in lib/database.cpp.
File version:
#define FILE_VERSION 4
DatabaseEntry:
class DatabaseEntry { /* let database modify valid flag */
private:
bool valid;
public:
virtual istream &operator>>(istream &) = 0;
virtual ostream &operator<<(ostream &) = 0;
virtual File &operator>>(File &) = 0;
virtual File &operator<<(File &) = 0;
friend class Database;
};
@ -101,7 +144,7 @@ Database:
class Database {
private:
unsigned int _size; /* Number of valid rows */
string filename;
File filename;
vector<T> db;
public:
Database::Database(filename);
@ -113,7 +156,7 @@ Database:
const T &operator[](unsigned int);
};
File << FILE_VERSION << db.size() << endl
File << db.size() << endl
File << INDEX_0 << db[INDEX_0].valid << db[INDEX_0] << endl;
File << Index_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
...
@ -144,7 +187,7 @@ Index:
class Index {
private:
map<string, set<int>>
string filename;
File filename;
public:
Index::Index(filename);
void load();
@ -281,7 +324,8 @@ Updating algorithm:
happen while idle.
- API
/* Path management */
library :: init();
Initialize all databases
library :: add_path(string dir);
Add new row to paths table, update
library :: del_path(unsigned int lib_id);
@ -315,6 +359,8 @@ Default groups:
Tracks with a play count of 0
- API
group :: init();
Initialize the index
group :: list();
return group_idx.keys();
group :: get_tracks(name):