rendered paste body#include <map>
#include <iostream>
#include <cstdlib>
#include <string>
#define TYPE_INT 1
#define TYPE_STRING 2
#define TYPE_SUBBLOC 3
class configBloc {
public:
string name;
map<string, unsigned short int> type;
map<string, map<unsigned int, string> > data;
map<string, configBloc*> subBloc;
configBloc();
configBloc(const configBloc&);
configBloc(char *name);
void setName(char *name) { this->name = name; };
bool add(char *var, char *value, unsigned int type);
string get(char *var, unsigned int index = 0);
configBloc s(char *name);
};
bool configBloc::add(char *var, char *value, unsigned int type)
{
cout << "Ajout de " << var << " dans " << this->name << "[" << this << "]" << endl;
string v(var);
string vv("");
if (value != NULL)
vv = value;
switch (type)
{
case TYPE_SUBBLOC:
this->subBloc[v] = new configBloc(value);
this->type[v] = type;
break;
case TYPE_INT:
case TYPE_STRING:
cout << "String/int" << endl;
map<string, map<unsigned int, string> >::iterator it = this->data.find(v);
if (it != this->data.end())
this->data[v][this->data[v].size()] = vv;
else
this->data[v][0] = vv;
this->type[v] = type;
cout << "Test : " << this->data[v][this->data[v].size() - 1] << ", total actuel : " << this->data.size() << endl;
break;
}
return true;
}
string configBloc::get(char *var, unsigned int index)
{
string v(var);
string empty(" ");
cout << "Demande de " << var << " dans " << this->name << "[" << this << "]" << endl;
map<string, map<unsigned int, string> >::iterator it = this->data.find(v);
cout << "Total : " << this->data.size() << endl;
if (it != this->data.end())
{
map<unsigned int, string>::iterator it2 = this->data[v].find(index);
if (it2 != this->data[v].end())
return this->data[v][index];
cout << var << " présent mais index #" << index << " non présent." << endl;
return empty;
}
cout << var << " non présent" << endl;
return empty;
}
configBloc configBloc::s(char *name)
{
string n(name);
return *(this->subBloc[n]);
}
configBloc::configBloc(const configBloc& old)
{
cout << "Copie de " << old.name << " depuis [" << &old << "] vers [" << this << "]" << endl;
}
configBloc::configBloc(char *name)
{
this->name = name;
cout << "Création ! [" << this << "]" << endl;
}
configBloc::configBloc()
{
cout << "Création ! [" << this << "]" << endl;
}
int main(int argc, char **argv)
{
configBloc prp("root");
char teststr[] = "youpi";
char testint[] = "45";
prp.add("var_int", testint, TYPE_INT);
prp.add("var_int", "44", TYPE_INT);
prp.add("var_str", teststr, TYPE_STRING);
prp.add("sub", "test", TYPE_SUBBLOC);
prp.s("sub").add("var_test", teststr, TYPE_STRING);
cout << prp.get("var_int") << " - " << prp.get("var_int", 1) << " - " << prp.s("sub").get("var_test") << endl;
// prp.s("sub").get("var_test") ne renvoie rien :/
}
/*
exemple de sortie :
Création ! [0xb74e8e28]
Ajout de var_int dans root[0xb74e8e28]
String/int
Test : 45, total actuel : 1
Ajout de var_int dans root[0xb74e8e28]
String/int
Test : 44, total actuel : 1
Ajout de var_str dans root[0xb74e8e28]
String/int
Test : youpi, total actuel : 2
Ajout de sub dans root[0xb74e8e28]
Création ! [0x808c098]
Copie de test depuis [0x808c098] vers [0xb74e8e80]
Ajout de var_test dans [0xb74e8e80]
String/int
Test : youpi, total actuel : 1
Copie de test depuis [0x808c098] vers [0xb74e8ed0]
Demande de var_test dans [0xb74e8ed0]
Total : 0
var_test non présent
Demande de var_int dans root[0xb74e8e28]
Total : 2
Demande de var_int dans root[0xb74e8e28]
Total : 2
45 - 44 -
*/