- added files to write to and read from configuration files
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@724 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
90fc0c6c95
commit
fee643d3f3
|
@ -0,0 +1,108 @@
|
|||
// src/global_preferences.cc - Copyright 2005, 2006, University
|
||||
// of Padova, dept. of Pure and Applied
|
||||
// Mathematics
|
||||
//
|
||||
// This file is part of SGPEMv2.
|
||||
//
|
||||
// This is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// SGPEMv2 is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with SGPEMv2; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#include "config.h"
|
||||
#include "key_file.hh"
|
||||
#include <fstream>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
namespace sgpem
|
||||
{
|
||||
|
||||
KeyFile::KeyFile()
|
||||
{}
|
||||
|
||||
|
||||
KeyFile::elements_iterator
|
||||
KeyFile::elements_begin() const
|
||||
{
|
||||
return _elements.begin();
|
||||
}
|
||||
|
||||
|
||||
KeyFile::elements_iterator
|
||||
KeyFile::elements_end() const
|
||||
{
|
||||
return _elements.end();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
KeyFile::insert_key_value(const Glib::ustring& key, const Glib::ustring& value)
|
||||
{
|
||||
std::map<Glib::ustring, Glib::ustring>::value_type val_pair(key, value);
|
||||
_elements.insert(val_pair);
|
||||
}
|
||||
|
||||
|
||||
const Glib::ustring*
|
||||
KeyFile::search_value(const Glib::ustring& key)
|
||||
{
|
||||
const Glib::ustring* ret = 0;
|
||||
elements_iterator cur = _elements.find(key);
|
||||
if(cur != elements_end())
|
||||
{
|
||||
ret = &((*cur).second);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
KeyFile::file_read(const Glib::ustring& filename)
|
||||
{
|
||||
std::ifstream ifs(filename.c_str());
|
||||
if(ifs)
|
||||
{
|
||||
_elements.clear(); // erase all elements
|
||||
char buff[KEY_FILE_BUF_LEN]; //
|
||||
while(ifs)
|
||||
{
|
||||
ifs.getline(buff, sizeof(buff));
|
||||
|
||||
// if not a comment line...
|
||||
if(*buff!='\0' && *buff!='#')
|
||||
{
|
||||
char* pos = strchr(buff, '=');
|
||||
if(pos!=0)
|
||||
{
|
||||
*pos = '\0';
|
||||
const Glib::ustring key(buff);
|
||||
const Glib::ustring value(pos+1);
|
||||
insert_key_value(key, value);
|
||||
}
|
||||
} // end - if not a comment line...
|
||||
} // end - while(ifs)
|
||||
} // end - if(ifs)
|
||||
}
|
||||
|
||||
void
|
||||
KeyFile::file_write(const Glib::ustring& filename)
|
||||
{
|
||||
std::ofstream ofs(filename.c_str());
|
||||
if(ofs)
|
||||
{
|
||||
elements_iterator iter;
|
||||
for(iter = elements_begin(); iter != elements_end(); iter++){
|
||||
ofs << (*iter).first << "=" << (*iter).second << std::endl;
|
||||
}
|
||||
} // end - if(ofs)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
// src/backend/key_file.hh - Copyright 2005, 2006, University
|
||||
// of Padova, dept. of Pure and Applied
|
||||
// Mathematics
|
||||
//
|
||||
// This file is part of SGPEMv2.
|
||||
//
|
||||
// This is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// SGPEMv2 is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with SGPEMv2; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#ifndef KEY_FILE_HH
|
||||
#define KEY_FILE_HH 1
|
||||
|
||||
#define KEY_FILE_BUF_LEN 1000
|
||||
|
||||
#include <glibmm/ustring.h>
|
||||
#include <map>
|
||||
#include "config.h"
|
||||
|
||||
namespace sgpem
|
||||
{
|
||||
class KeyFile;
|
||||
|
||||
|
||||
|
||||
/** \brief Save and retrieve configuration files formatted as key=value rows.
|
||||
*
|
||||
* This class handles files to store and retrieve configuration preferences
|
||||
* in the form of: key=value rows.
|
||||
*
|
||||
* The methods exposed allow to insert a key/value pair,
|
||||
* get a value by its corresponding key, write all pairs to a file,
|
||||
* read pairs from a file.
|
||||
*
|
||||
* Key values cannot contain the equal to (=) char nor start with pound (#).
|
||||
* Reading files, empty lines and lines starting with # are ignored.
|
||||
*
|
||||
*/
|
||||
class SG_DLLEXPORT KeyFile
|
||||
{
|
||||
|
||||
public:
|
||||
/** \brief Elements iterator type definition. */
|
||||
typedef std::map<Glib::ustring, Glib::ustring>::const_iterator elements_iterator;
|
||||
|
||||
/** \brief Object constructor */
|
||||
KeyFile();
|
||||
|
||||
/** \brief Elements iterator (begin of container) initializer. */
|
||||
elements_iterator elements_begin() const;
|
||||
|
||||
/** \brief Elements iterator (end of container) initializer */
|
||||
elements_iterator elements_end() const;
|
||||
|
||||
/** \brief Insert a new key/value pair.
|
||||
*
|
||||
* \param key The key value to insert.
|
||||
* \param value The value associated to the key inserted.
|
||||
*/
|
||||
void insert_key_value(const Glib::ustring& key, const Glib::ustring& value);
|
||||
|
||||
/** \brief Insert a new key/value pair.
|
||||
*
|
||||
* \param key The key value to insert.
|
||||
* \return A pointer to the value associated to the searched key or 0 if not found.
|
||||
*/
|
||||
const Glib::ustring* search_value(const Glib::ustring& key);
|
||||
|
||||
/** \brief Read a file into this object.
|
||||
*
|
||||
* \param filename The file to read from.
|
||||
*/
|
||||
void file_read(const Glib::ustring& filename);
|
||||
|
||||
/** \brief Write a file from this object.
|
||||
*
|
||||
* \param filename The file to write to.
|
||||
*/
|
||||
void file_write(const Glib::ustring& filename);
|
||||
private:
|
||||
|
||||
std::map<Glib::ustring, Glib::ustring> _elements;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue