- fixed global preferences serialization

- global_preferences.??
- fixed key=value configuration class
  - key_file.??
 


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@767 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
paolo 2006-07-15 11:25:57 +00:00
parent 8a43216527
commit 2b31d6d2eb
4 changed files with 331 additions and 12 deletions

View File

@ -20,6 +20,9 @@
#include "config.h" #include "config.h"
#include "global_preferences.hh" #include "global_preferences.hh"
#include "key_file.hh"
#include "string_utils.hh"
#include <sstream>
// Do not include in header file: // Do not include in header file:
#include "singleton.tcc" #include "singleton.tcc"
@ -61,6 +64,13 @@ GlobalPreferences::modules_dir_end() const
} }
const
Glib::ustring
GlobalPreferences::get_config_filename()
{
return Glib::ustring("sgpem.cfg");
}
void void
GlobalPreferences::add_modules_dir(const Glib::ustring& moddir) GlobalPreferences::add_modules_dir(const Glib::ustring& moddir)
{ {
@ -74,3 +84,188 @@ GlobalPreferences::add_policies_dir(const Glib::ustring& poldir)
_pol_dirs.insert(_pol_dirs.begin(), poldir); _pol_dirs.insert(_pol_dirs.begin(), poldir);
} }
std::vector<Glib::ustring>&
GlobalPreferences::get_policy_dirs()
{
return _pol_dirs;
}
std::vector<Glib::ustring>&
GlobalPreferences::get_plugin_dirs()
{
return _mod_dirs;
}
int
GlobalPreferences::get_speed()
{
return _speed;
}
int
GlobalPreferences::set_speed(int new_speed)
{
int old_speed = _speed;
_speed = new_speed;
return old_speed;
}
void
GlobalPreferences::write_configrc()
{
KeyFile kf;
key_file_write(kf);
kf.file_write(get_config_filename());
}
void
GlobalPreferences::load_configrc()
{
KeyFile kf;
kf.file_read(get_config_filename());
key_file_read(kf);
}
void
GlobalPreferences::write_configrc(std::ostream &os)
{
KeyFile kf;
key_file_write(kf);
kf.file_write(os);
}
void
GlobalPreferences::load_configrc(std::istream &is)
{
KeyFile kf;
kf.file_read(is);
key_file_read(kf);
}
void
GlobalPreferences::key_file_read(KeyFile& kf)
{
_mod_dirs.clear();
_pol_dirs.clear();
// read speed
{
int new_speed;
const Glib::ustring* val = kf.search_value(Glib::ustring("speed"));
if(val){
string_to_int(*val, new_speed);
set_speed(new_speed);
}
}
// read modules directories
{
const Glib::ustring* val = kf.search_value(Glib::ustring("modules-dir-number"));
if(val)
{
std::istringstream istr(val->c_str());
int n;
istr >> n;
for(int i=1; i<=n; i++)
{
std::ostringstream ostr;
ostr << "modules-dir-" << i;
Glib::ustring key(ostr.str());
val = kf.search_value(key);
if(val)
{
// add_modules_dir(*val);
_mod_dirs.push_back(*val);
}
}
}
}
// read policies directories
{
const Glib::ustring* val = kf.search_value(Glib::ustring("policies-dir-number"));
if(val)
{
std::istringstream istr(val->c_str());
int n;
istr >> n;
for(int i=1; i<=n; i++)
{
std::ostringstream ostr;
ostr << "policies-dir-" << i;
Glib::ustring key(ostr.str());
val = kf.search_value(key);
if(val)
{
// add_policies_dir(*val);
_pol_dirs.push_back(*val);
}
}
}
}
}
void
GlobalPreferences::key_file_write(KeyFile& kf)
{
// write speed
{
Glib::ustring key("speed");
Glib::ustring value;
int_to_string(_speed, value);
kf.insert_key_value(key, value);
}
// add modules directories
{
/*
GlobalPreferences::dir_iterator iter=_globalPreferences.modules_dir_begin();
*/
dir_iterator iter=_mod_dirs.begin();
dir_iterator end=_mod_dirs.end();
int n=0;
while(iter!=end){
std::ostringstream ostr;
n++;
ostr << "modules-dir-" << n; // << std::ends;
Glib::ustring key(ostr.str());
kf.insert_key_value(key, (*iter));
++iter;
}
Glib::ustring key("modules-dir-number");
std::ostringstream ostr;
ostr << n << std::ends;
Glib::ustring value(ostr.str());
kf.insert_key_value(key, value);
}
// add policies directories
{
/*
GlobalPreferences::dir_iterator iter=_globalPreferences.policies_dir_begin();
*/
dir_iterator iter=_pol_dirs.begin();
dir_iterator end=_pol_dirs.end();
int n=0;
while(iter!=end){
std::ostringstream ostr;
n++;
ostr << "policies-dir-" << n; // << std::ends;
Glib::ustring key(ostr.str());
kf.insert_key_value(key, (*iter));
++iter;
}
Glib::ustring key("policies-dir-number");
std::ostringstream ostr;
ostr << n << std::ends;
Glib::ustring value(ostr.str());
kf.insert_key_value(key, value);
}
}

View File

@ -24,6 +24,7 @@
#include "config.h" #include "config.h"
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <iostream>
#include <vector> #include <vector>
// Do not include complete template definition here: // Do not include complete template definition here:
@ -32,18 +33,21 @@
namespace sgpem namespace sgpem
{ {
class GlobalPreferences; class GlobalPreferences;
class KeyFile;
} }
#include "config.h"
namespace sgpem namespace sgpem
{ {
/**
\brief Contains all global application preferences
*/
class SG_DLLEXPORT GlobalPreferences : public Singleton<GlobalPreferences> class SG_DLLEXPORT GlobalPreferences : public Singleton<GlobalPreferences>
{ {
friend class Singleton<GlobalPreferences>; friend class Singleton<GlobalPreferences>;
friend class GlobalPreferencesSerializer;
public: public:
typedef std::vector<Glib::ustring>::const_iterator dir_iterator; typedef std::vector<Glib::ustring>::const_iterator dir_iterator;
dir_iterator modules_dir_begin() const; dir_iterator modules_dir_begin() const;
@ -52,16 +56,100 @@ namespace sgpem
dir_iterator policies_dir_begin() const; dir_iterator policies_dir_begin() const;
dir_iterator policies_dir_end() const; dir_iterator policies_dir_end() const;
/**
\return GlobalPreferences configuration filename
*/
const Glib::ustring get_config_filename();
/**
\brief Adds a directory search entry to modules dirs
*/
void add_modules_dir(const Glib::ustring& moddir); void add_modules_dir(const Glib::ustring& moddir);
/**
\brief Adds a directory search entry to policies dirs
*/
void add_policies_dir(const Glib::ustring& poldir); void add_policies_dir(const Glib::ustring& poldir);
/**
\brief Returns configured Policy directories
\return ::_policy_dirs
*/
std::vector<Glib::ustring>& get_policy_dirs();
/**
\brief Returns configured Plugin directories
\return ::_plugin_dirs
*/
std::vector<Glib::ustring>& get_plugin_dirs();
/**
\brief Returns the simumulation speed
\return _speed
*/
int get_speed();
/**
\brief Returns the simumulation speed and return old speed
\return _speed
*/
int set_speed(int new_speed);
/**
\brief Writes preferences to disk
In order, this method should:
#- Call get_preferences_dir()
#- Open the file "config" inside this directory, in write mode
#- Serialize options to disk with the format described in the associated ProductDefinition document.
That is: simulation speed and configured directories.
We advice using a key=value text format.
#- Close the configuration file.
*/
void write_configrc();
/**
\brief Load global preferences from disk
This should be done once during program startup.
Attempt opening the get_preferences_dir() + "/config" file.
\throw std::io_error
*/
void load_configrc();
void write_configrc(std::ostream &os);
void load_configrc(std::istream &is);
private: private:
GlobalPreferences(); GlobalPreferences();
GlobalPreferences(const GlobalPreferences&); GlobalPreferences(const GlobalPreferences&);
GlobalPreferences& operator=(const GlobalPreferences&); GlobalPreferences& operator=(const GlobalPreferences&);
void key_file_read(KeyFile& kf);
void key_file_write(KeyFile& kf);
/**
\brief Returns directories to search for plugins
*/
std::vector<Glib::ustring> _mod_dirs; std::vector<Glib::ustring> _mod_dirs;
/**
\brief Returns directories to search for policies
*/
std::vector<Glib::ustring> _pol_dirs; std::vector<Glib::ustring> _pol_dirs;
/*
These directories can be added to SGPEM in the following ways:
-# Hardcoded (usually done for the default installation directory on *n?x-systems, but NOT for Microsoft Windows)
-# Retrieved from a Windows registry key (only on Micro$oft platforms) set at installation time
-# Via command line arguments
-# By a backend::Plugin::on_init() method called when loading an external DSO. This is perfectly normal and permitted.
*/
int _speed;
}; };
} }

View File

@ -23,6 +23,9 @@
#include <fstream> #include <fstream>
#include <string.h> #include <string.h>
#include <iostream>
using namespace std;
namespace sgpem namespace sgpem
{ {
@ -70,14 +73,23 @@ namespace sgpem
{ {
std::ifstream ifs(filename.c_str()); std::ifstream ifs(filename.c_str());
if(ifs) if(ifs)
{
file_read(ifs);
} // end - if(ifs)
}
void
KeyFile::file_read(std::istream& is)
{
if(is)
{ {
_elements.clear(); // erase all elements _elements.clear(); // erase all elements
char buff[KEY_FILE_BUF_LEN]; // char buff[KEY_FILE_BUF_LEN]; //
while(ifs) while(is)
{ {
ifs.getline(buff, sizeof(buff)); is.getline(buff, (KEY_FILE_BUF_LEN), '\n');
// if not a comment line... // if not a comment line...
if(*buff!='\0' && *buff!='#') if(*buff!='\0' && *buff!='#')
{ {
char* pos = strchr(buff, '='); char* pos = strchr(buff, '=');
@ -89,7 +101,8 @@ namespace sgpem
insert_key_value(key, value); insert_key_value(key, value);
} }
} // end - if not a comment line... } // end - if not a comment line...
} // end - while(ifs)
} // end - while(ifs))
} // end - if(ifs) } // end - if(ifs)
} }
@ -98,11 +111,21 @@ namespace sgpem
{ {
std::ofstream ofs(filename.c_str()); std::ofstream ofs(filename.c_str());
if(ofs) if(ofs)
{
file_write(ofs);
} // end - if(ofs)
}
void
KeyFile::file_write(std::ostream& os)
{
if(os)
{ {
elements_iterator iter; elements_iterator iter;
for(iter = elements_begin(); iter != elements_end(); iter++){ for(iter = elements_begin(); iter != elements_end(); iter++){
ofs << (*iter).first << "=" << (*iter).second << std::endl; os << (*iter).first << "=" << (*iter).second << std::endl;
} }
} // end - if(ofs) } // end - if(ofs)
} }
} }

View File

@ -24,6 +24,7 @@
#define KEY_FILE_BUF_LEN 1000 #define KEY_FILE_BUF_LEN 1000
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <iostream>
#include <map> #include <map>
#include "config.h" #include "config.h"
@ -31,8 +32,6 @@ namespace sgpem
{ {
class KeyFile; class KeyFile;
/** \brief Save and retrieve configuration files formatted as key=value rows. /** \brief Save and retrieve configuration files formatted as key=value rows.
* *
* This class handles files to store and retrieve configuration preferences * This class handles files to store and retrieve configuration preferences
@ -76,17 +75,31 @@ namespace sgpem
*/ */
const Glib::ustring* search_value(const Glib::ustring& key); const Glib::ustring* search_value(const Glib::ustring& key);
/** \brief Read a file into this object. /** \brief Read a file into this object.
* *
* \param filename The file to read from. * \param filename The file to read from.
*/ */
void file_read(const Glib::ustring& filename); void file_read(const Glib::ustring& filename);
/** \brief Write a file from this object.
/** \brief Read a stream into this object.
*
* \param is the stream to read from.
*/
void file_read(std::istream& is);
/** \brief Write into a file from this object.
* *
* \param filename The file to write to. * \param filename The file to write to.
*/ */
void file_write(const Glib::ustring& filename); void file_write(const Glib::ustring& filename);
/** \brief Write into a stream from this object.
*
* \param os the stream to write to.
*/
void file_write(std::ostream& os);
private: private:
std::map<Glib::ustring, Glib::ustring> _elements; std::map<Glib::ustring, Glib::ustring> _elements;