Le Pont 3R / Mbed 2 deprecated Le_Pont_V10116

Dependencies:   mbed

Fork of Le_Pont_V10116 by SAGNES Christophe

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IniParser.cpp Source File

IniParser.cpp

00001 /*
00002  * INIParser.cpp
00003  *
00004  *  Created on: 5 sept. 2017
00005  *      Author: christophe.sagnes
00006  */
00007 
00008 #include "IniParser.h"
00009 
00010 LocalFileSystem local("local") ;
00011 
00012 INIParser::~INIParser() {
00013     // TODO Auto-generated destructor stub
00014      if(AutoSave)    save();
00015      ini.clear();
00016 }
00017 
00018 // spécialisation de la fonction pour la class std::string
00019 template <> std::string INIParser::GetValue<std::string>(const std::string &Section, const std::string &clef, const std::string &defaultValue)
00020 {
00021     std::map<std::string, std::map<std::string, std::string> >::iterator _it=ini.find(Section);
00022 
00023     if(_it != ini.end())
00024     {
00025         std::map<std::string, std::string>::iterator it=_it->second.find(clef);
00026 
00027         if(it != _it->second.end())
00028             return it->second;
00029         else
00030             return defaultValue;
00031     }
00032     else
00033         return defaultValue;
00034 }
00035 
00036 
00037 template <class T> T INIParser::GetValue(const std::string &Section, const std::string &clef, const T &defaultValue)
00038 {
00039     std::map<std::string, std::map<std::string, std::string> >::iterator section_it=ini.find(Section);
00040 
00041     if(section_it != ini.end())    // si la valeur Section n'est pas trouvé
00042     {
00043         std::map<std::string, std::string>::iterator param_it=section_it->second.find(clef);
00044 
00045         if(param_it != section_it->second.end())  // si la valeur clef n'est pas trouvé
00046         {
00047             // permet la convertion de la valeur en type std::string dans le type demandé
00048             T val;
00049             std::istringstream iss(param_it->second);
00050             iss >> val;
00051             return val;
00052         }
00053         else
00054             return defaultValue;
00055     }
00056     else
00057         return defaultValue;
00058 }
00059 
00060 int INIParser::GetInt(const std::string &Section, const std::string &clef, const int &defaultValue)
00061 {
00062     std::map<std::string, std::map<std::string, std::string> >::iterator section_it=ini.find(Section);
00063 
00064     if(section_it != ini.end())    // si la valeur Section n'est pas trouvé
00065     {
00066         std::map<std::string, std::string>::iterator param_it=section_it->second.find(clef);
00067 
00068         if(param_it != section_it->second.end())  // si la valeur clef n'est pas trouvé
00069         {
00070             // permet la convertion de la valeur en type std::string dans le type int
00071             int val;
00072             std::istringstream iss(param_it->second);
00073             iss >> val;
00074             return val;
00075         }
00076         else
00077             return defaultValue;
00078     }
00079     else
00080         return defaultValue;
00081 }
00082 
00083 template <class T> void INIParser::SetValue(const std::string &Section, const std::string &clef, const T &Value)
00084 {
00085     std::ostringstream oss;
00086     // convertion de la valeur en std::string
00087     oss << Value;
00088     // enregistrement de la valeur dans la map
00089     ini[Section][clef] = oss.str();
00090 }
00091 
00092 INIParser::INIParser(const std::string &filename, bool autoSave)
00093 {
00094     std::string section, line, clef, valeur;
00095     //size_t pos;
00096     int pos;
00097 
00098     AutoSave = autoSave;
00099     FileName = filename;
00100 
00101     //std::cout << "Debut" << std::endl ;
00102 
00103     std::ifstream file(filename.c_str());
00104 
00105     if (!file.good())
00106     {
00107         std::cout << "Pas de fichier " << std::endl ;
00108         return ;
00109     }
00110 
00111 
00112     while (!file.eof())
00113     {
00114         std::getline (file, line);
00115 
00116         // supression des commentaires
00117         pos = line.find_first_of(';');
00118         if(pos != std::string::npos)    line.erase (line.begin() + pos, line.end());
00119 
00120         // continue si la ligne n'est pas vide
00121         if(!line.empty())
00122         {
00123             // teste si la ligne correspond a une section
00124             pos = line.find_first_of('[');
00125             if(pos != std::string::npos)
00126             {
00127                 line.erase(line.begin(), line.begin() + pos+1);
00128                 line.erase(line.begin() + line.find_first_of (']'), line.end());
00129                 section = line;
00130             }
00131             else    // sinon elle correspond a une clef
00132             {
00133                 pos = line.find_first_of('=');
00134                 // si le '=' a bien été trouvé
00135                 if(pos != std::string::npos)
00136                 {
00137                     clef = line.substr (0, pos);
00138                     valeur = line.substr (pos+1);
00139 
00140                     // permet la supression de tous les espaces dans la clef
00141                     while(std::string::npos != (pos = clef.find_first_of(' ')))
00142                         clef.erase(pos);
00143 
00144                     ini[section][clef] = valeur;
00145                 }
00146             }
00147         }
00148     }
00149 //std::cout << "fin de fichier" << std::endl ;
00150 
00151     file.close();
00152 }
00153 
00154 bool INIParser::save(std::string filename)
00155 {
00156     std::map<std::string, std::map<std::string, std::string> >::iterator section_it;
00157     std::map<std::string, std::string>::iterator param_it;
00158 
00159     std::ofstream file;
00160 
00161     if(filename == "")
00162         file.open(FileName.c_str());
00163     else
00164         file.open(filename.c_str());
00165 
00166     if(!file.is_open()) return false;
00167 
00168     // ecriture de la map dans le fichier demandé
00169     for(section_it=ini.begin(); section_it!=ini.end(); ++section_it)
00170     {
00171         file << "[" << section_it->first << "]" << std::endl;
00172 
00173         for(param_it=section_it->second.begin(); param_it!=section_it->second.end(); ++param_it)
00174             file << param_it->first << "=" << param_it->second << std::endl;
00175     }
00176 
00177     file.close();
00178     return true;
00179 }