1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include "conf.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std ;
conf * conf::m_Instance;
conf* conf::GetInstance()
{
if(!m_Instance)
{
m_Instance = new conf();
}
return m_Instance;
}
conf::conf()
{
fichier_conf = "../config/conf.ini";
std::cout<<"Creation"<<std::endl;
init();
}
conf::~conf()
{
std::cout<<"Destruction"<<std::endl;
}
string conf::GetStringValue(string name)
{
for(int i = 0 ; i < m_ConfValues.size(); i++)
{
cout<<m_ConfValues[i][0]<<endl;
if(m_ConfValues[i][0] == name)
{
return m_ConfValues[i][1];
}
}
return "";
}
int conf::GetIntValue(string name)
{
[...]
}
double conf::GetDoubleValue(string name)
{
[...]
}
bool conf::GetBoolValue(string name)
{
[...]
}
void conf::init()
{
// lire le fichier
std::ifstream fichier(fichier_conf.c_str(), ios::in); // on ouvre le fichier en lecture
int count = 0;
if(fichier) // si l'ouverture a réussi
{
// recuperer les couples nom/valeur
std::string line;
while(getline(fichier, line))
{
count++;
if(line != "")
{
// stocker les valeurs
//std::cout<<line<<endl;
vector<std::string> result;
if(Split(result , line, '=') == 2)
{
m_ConfValues.push_back(result);
}
else
{
std::ostringstream err;
err << "Format de ligne inattendu [" << count << "]" << endl;
cerr << err.str();
}
}
else
{
cerr << "Ligne sans donnée" << endl;
}
}
// instructions
fichier.close();
}
else // sinon
cerr << "Impossible d'ouvrir le fichier !" << endl;
std::cout<<"FIN INIT"<<endl;
}
int conf::Split(vector<std::string>& result, std::string chaine, char separateur)
{
result.clear();
std::string::size_type stTemp = chaine.find(separateur);
while(stTemp != std::string::npos)
{
result.push_back(chaine.substr(0, stTemp));
chaine = chaine.substr(stTemp + 1);
stTemp = chaine.find(separateur);
}
result.push_back(chaine);
return result.size();
} |