From f181c93527a3fb6f72a2aae1aa162eaf00e80d0c Mon Sep 17 00:00:00 2001 From: paolo Date: Wed, 5 Jul 2006 16:01:44 +0000 Subject: [PATCH] - added global_preferences_serielizer - TO DO - test-program not finished git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@730 3ecf2c5c-341e-0410-92b4-d18e462d057c --- src/backend/global_preferences.hh | 1 + src/backend/global_preferences_serializer.cc | 128 ++++++++++++++++++ src/backend/global_preferences_serializer.hh | 62 +++++++++ .../test-global_preferences_serializer.cc | 75 ++++++++++ 4 files changed, 266 insertions(+) create mode 100644 src/backend/global_preferences_serializer.cc create mode 100644 src/backend/global_preferences_serializer.hh create mode 100644 src/testsuite/test-global_preferences_serializer.cc diff --git a/src/backend/global_preferences.hh b/src/backend/global_preferences.hh index 975dfeb..16083ab 100644 --- a/src/backend/global_preferences.hh +++ b/src/backend/global_preferences.hh @@ -41,6 +41,7 @@ namespace sgpem class SG_DLLEXPORT GlobalPreferences : public Singleton { friend class Singleton; + friend class GlobalPreferencesSerializer; public: typedef std::vector::const_iterator dir_iterator; diff --git a/src/backend/global_preferences_serializer.cc b/src/backend/global_preferences_serializer.cc new file mode 100644 index 0000000..3c9f97b --- /dev/null +++ b/src/backend/global_preferences_serializer.cc @@ -0,0 +1,128 @@ +// 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 "global_preferences_serializer.hh" +#include + +namespace sgpem +{ + + GlobalPreferencesSerializer::GlobalPreferencesSerializer(GlobalPreferences &gp) + : _globalPreferences(gp) + {} + + + + void + GlobalPreferencesSerializer::file_read(const Glib::ustring& filename) + { + _globalPreferences._mod_dirs.clear(); + _globalPreferences._pol_dirs.clear(); + KeyFile::file_read(filename); + // read modules directories + { + const Glib::ustring* val = 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 << std::ends; + Glib::ustring key(ostr.str()); + val = search_value(key); + if(val) + { + _globalPreferences.add_modules_dir(*val); + } + } + } + } + + // read policies directories + { + const Glib::ustring* val = 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 << std::ends; + Glib::ustring key(ostr.str()); + val = search_value(key); + if(val) + { + _globalPreferences.add_policies_dir(*val); + } + } + } + } + } + + void + GlobalPreferencesSerializer::file_write(const Glib::ustring& filename) + { + // add modules directories + { + GlobalPreferences::dir_iterator iter=_globalPreferences.modules_dir_begin(); + int n=0; + while(iter!=_globalPreferences.modules_dir_end()){ + std::ostringstream ostr; + n++; + ostr << "modules-dir-" << n << std::ends; + Glib::ustring key(ostr.str()); + insert_key_value(key, (*iter)); + ++iter; + } + Glib::ustring key("modules-dir-number"); + std::ostringstream ostr; + ostr << n << std::ends; + Glib::ustring value(ostr.str()); + insert_key_value(key, value); + } + // add policies directories + { + GlobalPreferences::dir_iterator iter=_globalPreferences.policies_dir_begin(); + int n=0; + while(iter!=_globalPreferences.policies_dir_end()){ + std::ostringstream ostr; + n++; + ostr << "policies-dir-" << n << std::ends; + Glib::ustring key(ostr.str()); + insert_key_value(key, (*iter)); + ++iter; + } + Glib::ustring key("policies-dir-number"); + std::ostringstream ostr; + ostr << n << std::ends; + Glib::ustring value(ostr.str()); + insert_key_value(key, value); + } + + KeyFile::file_write(filename); + } +} diff --git a/src/backend/global_preferences_serializer.hh b/src/backend/global_preferences_serializer.hh new file mode 100644 index 0000000..7581c2f --- /dev/null +++ b/src/backend/global_preferences_serializer.hh @@ -0,0 +1,62 @@ +// 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 GLOBAL_PREFERENCES_SERIALIZER_HH +#define GLOBAL_PREFERENCES_SERIALIZER_HH 1 + +#include "global_preferences.hh" +#include "key_file.hh" + +namespace sgpem +{ + class GlobalPreferencesSerializer; + + + + /** \brief Save and retrieve global preferences formatted as key=value rows. + * + * This class handles files to store and retrieve global preferences + * in the form of: key=value rows. + * + */ + class SG_DLLEXPORT GlobalPreferencesSerializer : public KeyFile + { + + public: + /** \brief Object constructor */ + GlobalPreferencesSerializer(GlobalPreferences &gp); + + /** \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: + GlobalPreferences& _globalPreferences; + }; +} + +#endif diff --git a/src/testsuite/test-global_preferences_serializer.cc b/src/testsuite/test-global_preferences_serializer.cc new file mode 100644 index 0000000..049903b --- /dev/null +++ b/src/testsuite/test-global_preferences_serializer.cc @@ -0,0 +1,75 @@ +// src/testsuite/test-stepforward.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 + +/* This executable tests for workingness of the save preferences method, + * and nothing else. */ + +#include "config.h" +#include "glibmm/ustring.h" +#include "backend/global_preferences_serializer.hh" +#include +#include + + + +// from here and further until the bottom, all to throw away I suppose + +int +main(int argc, char** argv) { + + using namespace sgpem; + using Glib::ustring; + using std::cout; + using std::endl; + + if(argc<2) + { + cout << "GlobalPreferencesSerializer class test program" << endl; + cout << "Syntax: test-global_preferences_serializer filename [mod1 mod2 ...][% pol1 pol2 ...]" << endl << endl; + cout << "This test add modules directories (mod1 mod2 ...) and modules directories (mod1 mod2 ...)" + << endl + << "to GlobalPreferences, write it to the file \"filename\", read it and compare." << endl; + cout << "NOTE: 1) Test fails with invalid filenames." << endl; + cout << " 2) File \"filename\" remains in current directory after test execution." + << endl << endl; + return 2; + } + + GlobalPreferences& gp = GlobalPreferences::get_instance(); + int i=2; + while(i