libsaria: Switch to the new OutFile() class

The new class is more convenient to work with since it's the output
stream with my save protocol built in.  I should have done this earlier!
This commit is contained in:
Bryan Schumaker 2011-12-25 22:34:12 -05:00
parent 5877d3ac4a
commit da118b2281
5 changed files with 26 additions and 118 deletions

View File

@ -19,36 +19,15 @@ class SaveTask : public IdleTask
void run_task();
};
class OutFile
class OutFile : public ofstream
{
private:
ofstream out;
bool new_line;
void begin_write();
void end_write(bool);
public:
OutFile(string);
~OutFile();
void write_str(string, bool);
void write_int(int, bool);
void write_float(float, bool);
void write_ui(unsigned int, bool);
void write_lui(long unsigned int, bool);
void write_ino(ino_t, bool);
};
class OutFile2 : public ofstream
{
public:
OutFile2(string);
~OutFile2();
OutFile2& operator<<(const char *);
OutFile2& operator<<(string);
OutFile& operator<<(const char *);
OutFile& operator<<(string);
template <class T>
OutFile2& operator<<(T);
OutFile& operator<<(T);
};
class InFile : public ifstream
@ -62,7 +41,7 @@ class InFile : public ifstream
};
template <class T>
OutFile2& OutFile2::operator<<(T item)
OutFile& OutFile::operator<<(T item)
{
ofstream *out = (ofstream *)this;
*out << item << " ";

View File

@ -22,8 +22,7 @@ void LibraryPath::save(OutFile &out)
{
list<TrackTag>::iterator it;
out.write_str(path, false);
out.write_lui(file_list.size(), true);
out << path << file_list.size() << "\n";
for (it = file_list.begin(); it != file_list.end(); it++)
it->save(out);
}
@ -70,7 +69,7 @@ namespace libsaria
list<LibraryPath> *path_list = get_path_list();
list<LibraryPath>::iterator it;
out.write_lui(path_list->size(), true);
out << path_list->size() << "\n";
for (it = path_list->begin(); it != path_list->end(); it++)
it->save(out);
}

View File

@ -20,92 +20,22 @@ OutFile::OutFile(string path)
{
string out_file = get_saria_dir() + "/" + path;
println("Opening save file: " + out_file);
out.open(out_file.c_str());
new_line = true;
open(out_file.c_str());
}
OutFile::~OutFile()
{
out.close();
}
void OutFile::begin_write()
{
if (new_line == false)
out << " ";
}
void OutFile::end_write(bool end)
{
if (end == true) {
out << endl;
new_line = true;
} else
new_line = false;
}
void OutFile::write_str(string s, bool end)
{
begin_write();
out << s.size() << " " << s;
end_write(end);
}
void OutFile::write_int(int i, bool end)
{
begin_write();
out << i;
end_write(end);
}
void OutFile::write_float(float f, bool end)
{
begin_write();
out << f;
end_write(end);
}
void OutFile::write_ui(unsigned int i, bool end)
{
begin_write();
out << i;
end_write(end);
}
void OutFile::write_lui(long unsigned int i, bool end)
{
begin_write();
out << i;
end_write(end);
}
void OutFile::write_ino(ino_t inode, bool end)
{
begin_write();
out << inode;
end_write(end);
}
OutFile2::OutFile2(string path)
{
string out_file = get_saria_dir() + "/" + path;
println("Opening save file: " + out_file);
open(out_file.c_str());
}
OutFile2::~OutFile2()
{
close();
}
OutFile2& OutFile2::operator<<(string s)
OutFile& OutFile::operator<<(string s)
{
ofstream *out = (ofstream *)this;
*out << s.size() << " " << s << " ";
return *this;
}
OutFile2& OutFile2::operator<<(const char*s)
OutFile& OutFile::operator<<(const char*s)
{
ofstream *out = (ofstream *)this;
*out << s;

View File

@ -93,20 +93,20 @@ TrackTag::~TrackTag()
void TrackTag::save(OutFile &out)
{
out.write_ino(inode, false);
out.write_str(filepath, false);
out.write_str(title, false);
out.write_str(artist, false);
out.write_str(album, false);
out.write_str(comment, false);
out.write_str(genre, false);
out.write_str(lenstr, false);
out.write_ui(year, false);
out.write_ui(track, false);
out.write_int(length, false);
out.write_int(bitrate, false);
out.write_int(sample, false);
out.write_int(channels, true);
out << inode;
out << filepath;
out << title;
out << artist;
out << album;
out << comment;
out << genre;
out << lenstr;
out << year;
out << track;
out << length;
out << bitrate;
out << sample;
out << channels << "\n";
}
void TrackTag::make_lenstr()

View File

@ -41,7 +41,7 @@ static void load_pref(InFile &in)
preferences[key] = p;
}
static void save_pref(string key, struct Preference &p, OutFile2 &out)
static void save_pref(string key, struct Preference &p, OutFile &out)
{
out << key << p.type;
switch (p.type) {
@ -58,7 +58,7 @@ static void save_pref(string key, struct Preference &p, OutFile2 &out)
static void save()
{
map<string, Preference>::iterator it;
OutFile2 out("preferences");
OutFile out("preferences");
out << preferences.size() << "\n";
for (it = preferences.begin(); it != preferences.end(); it++)
save_pref(it->first, it->second, out);