]> Creatis software - creaImageIO.git/commitdiff
added first version of settings management.
authorFrederic Cervenansky <Frederic.Cervenansky@creatis.insa-lyon.fr>
Thu, 30 Apr 2009 09:50:11 +0000 (09:50 +0000)
committerFrederic Cervenansky <Frederic.Cervenansky@creatis.insa-lyon.fr>
Thu, 30 Apr 2009 09:50:11 +0000 (09:50 +0000)
src2/CMakeLists.txt
src2/creaImageIOGimmick.cpp
src2/creaImageIOGimmick.h
src2/creaImageIOSettings.cpp [new file with mode: 0644]
src2/creaImageIOSettings.h [new file with mode: 0644]

index fca3c0e099ba39365febef07993340658bdb238f..a06a64b5e90af836e1f875c39dc5c36cf3a83db0 100644 (file)
@@ -54,7 +54,9 @@ SET( SRCS
   #  Viewer
   creaImageIOWxViewer
   creaImageIOImagePointerHolder.h
-
+  
+  # settings
+  creaImageIOSettings
 
 )
 
index e38db81ff628e90f987f3ef3296925a079665bf7..b30fbc18ba9a4fa5d9704fae24640b158b75cb1b 100644 (file)
@@ -1,7 +1,6 @@
 #include <creaImageIOGimmick.h>
 
 #include <creaImageIOSystem.h>
-
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 
@@ -22,7 +21,9 @@ namespace creaImageIO
   //==============================================================
   Gimmick::~Gimmick()
   {
-    
+         mSettings->writeSettingsFile();
+      delete mSettings;
+         delete mSynchronizer;
   }
   //==============================================================
   
@@ -37,12 +38,18 @@ namespace creaImageIO
     mCurrentDirectory =  GetHomeDirectory();
        mSynchronizer= new Synchronizer(GetUserSettingsDirectory());
 
+       mSettings  = new Settings(mCurrentDirectory);
+       
+
        std::string dbpath = GetLocalDatabasePath();
     // Create or open local database
        mLocalDatabase = createDB(i_nameDB, mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt", dbpath);
     // Add it to the TreeHandlerMap
     mTreeHandlerMap[i_nameDB] = mLocalDatabase;
     
+       //Add additional DB from user Settings
+       addDBSettings();
+
        // Creates files and directories database
     mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
     // Create or open local database
@@ -87,6 +94,7 @@ namespace creaImageIO
                {
                        mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
                        mTreeHandlerMap[i_name]->Open(true);
+                       mSettings->addDB(i_location);
                }
        }
 
@@ -330,6 +338,38 @@ namespace creaImageIO
     GetTreeHandler(d)->GetTree().Print();
   }
   //========================================================================
+  /////////////////////////////////////////////////////////////////////////
+  // add DB from Settings file                                                                              //
+  // @param : -                                                                                                                         //
+  // return : -                                                                                                                         //
+  /////////////////////////////////////////////////////////////////////////
+  void Gimmick::addDBSettings()
+  {
+
+        std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
+        
+        // split to find all paths
+        std::vector<std::string> paths;
+        std::string separator = ";";
+        std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
+        //find first separator
+        std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
+        while(std::string::npos != pos || std::string::npos != last_pos)
+        {
+               paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
+               last_pos = pathSettings.find_first_not_of(separator, pos);
+               pos = pathSettings.find_first_of(separator, last_pos);
+        }
 
+        std::vector<std::string>::iterator it_path = paths.begin();
+        for(; it_path != paths.end(); ++it_path)
+        {
+                pos = it_path->find_last_of("\\");
+                last_pos = it_path->find_last_of(".");
+                std::string name = it_path->substr(pos +1, last_pos -pos-1 );
+                addDB(name, it_path->c_str());
+        }
 
+  }
+       
 }
index dfd176e27e83dde6ae7e2f5b56064a2ee8a7862c..b316b1b5383ba2481ff57b2d593c21600bbf37a9 100644 (file)
@@ -5,6 +5,7 @@
 #include <creaImageIOTreeHandlerImageAdder.h>
 #include <creaImageIOTimestampDatabaseHandler.h>
 #include <creaImageIOSynchron.h>
+#include <creaImageIOSettings.h>
 
 namespace creaImageIO
 {
@@ -121,7 +122,8 @@ namespace creaImageIO
     const SQLiteTreeHandler* GetLocalDatabase() const 
     { return mLocalDatabase; }
 
-    
+       // add DB from Settings file
+    void addDBSettings();
 
     /// 
     const std::string& GetHomeDirectory();
@@ -143,7 +145,7 @@ namespace creaImageIO
     std::string mUserSettingsDirectory;
     std::string mLocalDatabasePath;
        std::string mTimestampDatabasePath;
-
+       Settings        *mSettings;
     TreeHandlerImageAdder mImageAdder;
   };
   // EO class Gimmick
diff --git a/src2/creaImageIOSettings.cpp b/src2/creaImageIOSettings.cpp
new file mode 100644 (file)
index 0000000..88ca918
--- /dev/null
@@ -0,0 +1,136 @@
+#include <creaImageIOSettings.h>
+#include <boost/filesystem/fstream.hpp>
+#include <boost/algorithm/string/replace.hpp>
+#include <iostream>
+#include <fstream>
+
+using namespace boost;
+namespace po = boost::program_options;
+
+namespace creaImageIO
+{
+    Settings::Settings(const std::string i_path)
+    {
+        //need to position path in user directory first.
+        m_SettingsFileName = i_path + "\\.gimmick\\app.config";
+        //Test if Settings File exist
+        if(!boost::filesystem::exists(m_SettingsFileName) )
+        {
+            createFile();
+        }
+        std::ifstream ifs(m_SettingsFileName.c_str());
+               std::string line;
+               std::string sets;
+           if (ifs.is_open())
+               {
+                       while (! ifs.eof() )
+                       {
+                               getline(ifs,line);
+                               sets += line;
+                       }
+                       ifs.close();
+               }
+       std::vector<std::string> Keys;
+               Keys.push_back(SETTINGS_SYNC_EVENT);
+               Keys.push_back(SETTINGS_DBPATH);
+               Keys.push_back(SETTINGS_SYNC_FREQ);
+               readSettings(Keys, sets);
+
+    }
+
+    Settings::~Settings()
+    {
+        
+    }
+
+
+   ////////////////////////////////////////////////////////////////////////////////////////////////
+    // create the config file                                                                                 //
+    //@param : -                                                                                               //
+    // return  : -                                                                                                // 
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+   void Settings::createFile()
+   {
+       m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
+       m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
+       m_SettingsMap[SETTINGS_DBPATH] = "";
+       m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
+       writeSettingsFile();
+   }
+
+       ////////////////////////////////////////////////////////////////////////////////////////////////
+    // read Settings from config file                                                             //
+    // @param i_keys : list of keys                                                               //
+    // @param  i_file : text from config file                                                     //
+       // return : -
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+       void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
+       {
+               std::vector<std::string>::iterator it_key = i_Keys.begin();
+               for(;  it_key< i_Keys.end(); ++it_key)
+               {
+                       size_t fpos = i_file.find(it_key->c_str());
+                       size_t lpos = i_file.rfind(it_key->c_str());
+                       if(fpos != std::string::npos && lpos != std::string::npos)
+                       {
+                               m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
+                       }
+               }
+       }
+       ////////////////////////////////////////////////////////////////////////////////////////////////
+    // add a path to a DB                                                                                    //
+    // @param i_path : DB path to add                                                             //
+    // return : -                                                                                                                                                                //
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+       void Settings::addDB(const std::string &i_path)
+       {
+               if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
+               {
+                       m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
+               }
+       }
+
+       ////////////////////////////////////////////////////////////////////////////////////////////////
+    // remove a path to a DB                                                                             //
+    // @param i_path : DB path to delete (don't exist anymore)                                    //
+    // return : -
+       ///////////////////////////////////////////////////////////////////////////////////////////////
+       
+       void Settings::removeDB(const std::string &i_path)
+       {
+               boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
+       }
+
+       ///////////////////////////////////////////////////////////////////////////////////////////////
+    // write Settings buffer from                                                                //
+    // @param o_file : settings buffer                                                           //
+    //                                                                                                                                                                                  //
+       // return : -                                                                                                                                                            //
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+       void Settings::writeSettings(std::ofstream &o_file)
+       {
+               std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
+               std::stringstream st;
+               for(; it_map != m_SettingsMap.end(); ++it_map)
+               {
+                       o_file << it_map->first.c_str();
+                       o_file <<  it_map->second.c_str();
+                       o_file << it_map->first.c_str();
+                       o_file << std::endl;
+               }
+       }
+
+       ////////////////////////////////////////////////////////////////////////////////////////////////
+    // write Settings file                                                             //
+    // @param : -                                                               //
+    // return : -
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+       void Settings::writeSettingsFile()
+       {       
+           std::ofstream ofs(m_SettingsFileName.c_str());
+               ofs.clear();
+               writeSettings(ofs);
+               ofs.close();
+       }
+         
+}
\ No newline at end of file
diff --git a/src2/creaImageIOSettings.h b/src2/creaImageIOSettings.h
new file mode 100644 (file)
index 0000000..76f8214
--- /dev/null
@@ -0,0 +1,39 @@
+#include <boost/program_options.hpp>
+#include <map>
+
+#define SETTINGS_DICOM_LIBRARY "<DICOM Library>"
+#define SETTINGS_SYNC_EVENT     "<syncro_event>"
+#define SETTINGS_SYNC_FREQ       "<syncro_frequency>"
+#define SETTINGS_DBPATH       "<dbpath>"
+
+
+namespace creaImageIO
+{
+    class Settings{
+    public :
+        Settings(const std::string i_path);
+        ~Settings();
+
+        //get the value for a given option
+               const std::string getValue(const std::string i_key){return m_SettingsMap[i_key];}
+       
+               void addDB(const std::string &i_path);
+
+               void removeDB(const std::string &i_path);
+               //write configuration file
+               void writeSettingsFile();
+               
+    private :
+               // Settings Key-Value Map
+               std::map<std::string, std::string> m_SettingsMap;
+               
+               //read the configuration file
+               void readSettings(std::vector<std::string> &i_Keys, const std::string &i_file);
+        // create the configuration file
+        void createFile();
+               void writeSettings(std::ofstream &o_filebuf);
+               std::string m_SettingsFileName;
+               
+               
+    };
+}