libsaria: Created a new OutFile() class

Similar to how I created a new InFile() class.  The new one can be used
directly as a stream, only it has my save file protocol stuff built in.
This commit is contained in:
Bryan Schumaker 2011-12-25 22:21:11 -05:00
parent 6794ce15e5
commit 5877d3ac4a
3 changed files with 52 additions and 9 deletions

View File

@ -40,6 +40,17 @@ class OutFile
void write_ino(ino_t, bool);
};
class OutFile2 : public ofstream
{
public:
OutFile2(string);
~OutFile2();
OutFile2& operator<<(const char *);
OutFile2& operator<<(string);
template <class T>
OutFile2& operator<<(T);
};
class InFile : public ifstream
{
public:
@ -50,6 +61,14 @@ class InFile : public ifstream
InFile& operator>>(T &);
};
template <class T>
OutFile2& OutFile2::operator<<(T item)
{
ofstream *out = (ofstream *)this;
*out << item << " ";
return *this;
}
template <class T>
InFile& InFile::operator>>(T &item)
{

View File

@ -86,6 +86,32 @@ void OutFile::write_ino(ino_t inode, bool end)
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)
{
ofstream *out = (ofstream *)this;
*out << s.size() << " " << s << " ";
return *this;
}
OutFile2& OutFile2::operator<<(const char*s)
{
ofstream *out = (ofstream *)this;
*out << s;
return *this;
}
InFile::InFile(string path)
{
string in_file = get_saria_dir() + "/" + path;

View File

@ -41,27 +41,25 @@ static void load_pref(InFile &in)
preferences[key] = p;
}
static void save_pref(string key, struct Preference &p, OutFile &out)
static void save_pref(string key, struct Preference &p, OutFile2 &out)
{
out.write_str(key, false);
out.write_ui(p.type, false);
out << key << p.type;
switch (p.type) {
case BOOL:
out.write_int(p.value_u.boolean, true);
out << p.value_u.boolean;
break;
case FLOAT:
out.write_float(p.value_u.floating, true);
out << p.value_u.floating;
break;
default:
out.write_str("", true);
}
out << "\n";
}
static void save()
{
map<string, Preference>::iterator it;
OutFile out("preferences");
out.write_ui(preferences.size(), true);
OutFile2 out("preferences");
out << preferences.size() << "\n";
for (it = preferences.begin(); it != preferences.end(); it++)
save_pref(it->first, it->second, out);
}