libsaria: Create a new way to save files
I inherit from the ifstream class, so I can override the insertion operator for string handling. I can then use templates to save everything else.
This commit is contained in:
parent
603a76964b
commit
5e308202a0
|
@ -57,4 +57,21 @@ class InFile
|
|||
ino_t read_ino();
|
||||
};
|
||||
|
||||
class InFile2 : public ifstream
|
||||
{
|
||||
public:
|
||||
InFile2(string);
|
||||
~InFile2();
|
||||
void operator>>(string &);
|
||||
template <class T>
|
||||
void operator>>(T &);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void InFile2::operator>>(T &item)
|
||||
{
|
||||
ifstream *in = (ifstream *)this;
|
||||
*in >> item;
|
||||
}
|
||||
|
||||
#endif /* LIBSARIA_FILES_H */
|
||||
|
|
|
@ -153,3 +153,32 @@ ino_t InFile::read_ino()
|
|||
in >> i;
|
||||
return i;
|
||||
}
|
||||
|
||||
InFile2::InFile2(string path)
|
||||
{
|
||||
string in_file = get_saria_dir() + "/" + path;
|
||||
open(in_file.c_str());
|
||||
if (!good())
|
||||
println("Error opening file: " + in_file);
|
||||
}
|
||||
|
||||
InFile2::~InFile2()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void InFile2::operator>>(string &res)
|
||||
{
|
||||
int len;
|
||||
res = "";
|
||||
|
||||
*this >> len;
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
len++;
|
||||
this->get();
|
||||
char c[len];
|
||||
this->get(c, len);
|
||||
res = c;
|
||||
}
|
||||
|
|
|
@ -20,18 +20,20 @@ struct Preference {
|
|||
|
||||
static map<string, Preference> preferences;
|
||||
|
||||
static void load_pref(InFile &in)
|
||||
static void load_pref(InFile2 &in)
|
||||
{
|
||||
Preference p;
|
||||
string key = in.read_str();
|
||||
p.type = (pref_t)in.read_ui();
|
||||
string key;
|
||||
|
||||
in >> key;
|
||||
in >> (unsigned int&)p.type;
|
||||
switch (p.type) {
|
||||
case BOOL:
|
||||
p.value_u.boolean = in.read_int();
|
||||
in >> p.value_u.boolean;
|
||||
println("preferences[" + key + "] = %d", p.value_u.boolean);
|
||||
break;
|
||||
case FLOAT:
|
||||
p.value_u.floating = in.read_float();
|
||||
in >> p.value_u.floating;
|
||||
println("preferences[" + key + "] = %f", p.value_u.floating);
|
||||
break;
|
||||
default:
|
||||
|
@ -76,12 +78,12 @@ namespace libsaria
|
|||
|
||||
void prefs::load()
|
||||
{
|
||||
unsigned int size;
|
||||
InFile in("preferences");
|
||||
unsigned int size = 0;
|
||||
InFile2 in("preferences");
|
||||
if (!in.good())
|
||||
return;
|
||||
|
||||
size = in.read_ui();
|
||||
in >> size;
|
||||
println("Preferences size: %d", size);
|
||||
for (unsigned int i = 0; i < size; i++)
|
||||
load_pref(in);
|
||||
|
|
Loading…
Reference in New Issue