config.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003-2004  Etienne Lachance
00003 
00004 This library is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU Lesser General Public License as
00006 published by the Free Software Foundation; either version 2.1 of the
00007 License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 Report problems and direct all questions to:
00020 
00021 email: etienne.lachance@polytml.ca or richard.gourdeau@polymtl.ca
00022 
00023 -------------------------------------------------------------------------------
00024 Revision_history:
00025 
00026 2004/06/10: Etienne Lachance
00027     -Added doxygen documentation.
00028 
00029 2004/07/01: Ethan Tira-Thompson
00030     -Added support for newmat's use_namespace #define, using ROBOOP namespace
00031 
00032 2004/07/13: Ethan Tira-Thompson
00033     -Added a select_real and add_real function for type indepence of Real
00034     -Added functions to test for sections and parameters existance
00035 
00036 2004/07/29: Etienne Lachance
00037    -Added clear function. Suggested by Sylvain Marleau.
00038 
00039 2004/08/10: Etienne Lachance
00040    -Removed select_real and add_real functions in order to make config.h/cpp 
00041     independent of ROBOOP.
00042    -Removed using ROBOOP namespace
00043 
00044 2004/08/14: Etienne Lachance
00045    -Merge all select_* and add_* functions into select() and add() functions.
00046 
00047 2004/08/20: Etienne Lachance
00048    -Parameter value can now contain space.
00049    -Fix print member function.
00050 -------------------------------------------------------------------------------
00051 */
00052 
00058 
00059 static const char rcsid[] = "$Id: config.cpp,v 1.20 2006/05/16 19:24:26 gourdeau Exp $";
00060 
00061 
00062 #include "config.h"
00063 
00064 using namespace std;
00065 
00066 Config::Config(const bool bPrintErrorMessages_) 
00067 
00068 {
00069    bPrintErrorMessages = bPrintErrorMessages_;
00070 }
00071 
00072 short Config::read_conf(ifstream & inconffile)
00089 {
00090    //std::ifstream inconffile(filename.c_str(), std::ios::in);
00091 
00092    if(inconffile)
00093    {
00094       string temp;
00095       unsigned int tmpPos;
00096       Data data;
00097       getline(inconffile, temp);
00098 
00099       while( !inconffile.eof() )
00100       {
00101          // Is-it comment line?
00102          if(temp.substr(0,1) != "#")
00103          {
00104             // Is-it a section name?
00105             if(temp.substr(0,1) == "[") // [section]
00106             {
00107                // Search for the end of the section name and ignore the rest of the line.
00108                tmpPos = temp.find("]");
00109                if (tmpPos != string::npos)
00110                  {
00111                    data.section = temp.substr(1, tmpPos-1); // remove []
00112                    // Read the next line, it's should be a parameter.
00113                    getline(inconffile, temp);
00114                    // Be sure that is not another section name.
00115                    while( (temp.substr(0,1) != "[") &&
00116                           (!inconffile.eof()) )
00117                      {
00118                        if(temp.substr(0,1) != "#") // ignore comments
00119                          {
00120                            if(temp.find(":") != string::npos)
00121                              {
00122                                istringstream inputString(temp);
00123                                inputString >> data.parameter >> data.value;
00124                                string tmp_value;
00125                                while(inputString >> tmp_value)
00126                                {
00127                                    data.value.append(" ");
00128                                    data.value.append(tmp_value);
00129                                }
00130                                // Find ":" in parameter.
00131                                tmpPos = data.parameter.find(":");
00132                                if (tmpPos != string::npos)
00133                                  // remove ":" a the end of parameter
00134                                  data.parameter = data.parameter.substr(0, tmpPos);
00135                                else
00136                                  {
00137                                    inputString >> data.value;
00138                                    string tmp_value;
00139                                    while(inputString >> tmp_value)
00140                                    {
00141                                        data.value.append(" ");
00142                                        data.value.append(tmp_value);
00143                                    }
00144                                  }
00145 
00146                                // Add data to the config vector
00147                                conf.push_back(data);
00148                              }
00149 
00150                            // Read the next line.
00151                            getline(inconffile, temp);
00152                          }
00153                        else
00154                          {
00155                            // Ignore comments and read the next line.
00156                            getline(inconffile, temp);
00157                          }
00158                      }
00159                  }
00160             }
00161          }
00162          if(temp.substr(0,1) != "[") {
00163             getline(inconffile, temp);
00164          }
00165       }
00166    }
00167    else
00168    {
00169       if (bPrintErrorMessages)
00170       {
00171           cerr << "Config::read_conf: invalid input ifstream " << endl;
00172       }
00173       return CAN_NOT_OPEN_FILE;
00174    }
00175    return 0;
00176 }
00177 
00178 void Config::clear()
00180 {
00181     conf.clear();
00182 }
00183 
00184 void Config::print()
00186 {
00187   string tmpSection;
00188   for(Conf_data::iterator iter = conf.begin(); iter != conf.end(); ++iter)
00189     {
00190       if (tmpSection != iter->section)
00191         {
00192           //Beginning of a section
00193           tmpSection = iter->section;
00194           cout << "\n[" << tmpSection << "]" << endl;
00195           cout << iter->parameter+":" << " " << iter->value << endl;
00196         }
00197       else
00198           cout << iter->parameter+":" << " " << iter->value << endl;
00199     }
00200 }
00201 
00202 bool Config::section_exists(const string& section) const
00207 {
00208         for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
00209                 if(iter->section == section)
00210                         return true;
00211         return false;
00212 }
00213 
00214 bool Config::parameter_exists(const string& section, const string& parameter) const
00219 {
00220         for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
00221                 if( (iter->section == section) && (iter->parameter == parameter) )
00222                         return true;
00223         return false;
00224 }
00225 
00226 short Config::write_conf(ofstream & outconffile, const string & file_title,
00227                          const int space_between_column)
00234 {
00235    if(outconffile)
00236    {                     // file header
00237       outconffile << "# ---------------------------------------------------" << endl;
00238       outconffile << "# " << file_title << endl;
00239       outconffile << "# ---------------------------------------------------" << endl;
00240       outconffile << endl;
00241 
00242       string section = "";
00243 
00244       for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
00245       {
00246          if(section != iterConf->section)
00247          {
00248             section = iterConf->section;
00249             outconffile << "\n[" << section << "]\n" << endl;
00250          }
00251          outconffile << setw(space_between_column-iterConf->parameter.size()) 
00252                      << iterConf->parameter + ":" << " " << iterConf->value << endl;
00253       }
00254       return 0;
00255    }
00256    else
00257    {
00258       if (bPrintErrorMessages)
00259       {
00260           cerr << "Config::write_conf: invalid input ofstream " << endl;
00261       }
00262       return CAN_NOT_CREATE_FILE;
00263    }
00264 }
00265 


kni
Author(s): Martin Günther
autogenerated on Mon Aug 14 2017 02:44:12