]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSettings.cpp
added first version of settings management.
[creaImageIO.git] / src2 / creaImageIOSettings.cpp
1 #include <creaImageIOSettings.h>
2 #include <boost/filesystem/fstream.hpp>
3 #include <boost/algorithm/string/replace.hpp>
4 #include <iostream>
5 #include <fstream>
6
7 using namespace boost;
8 namespace po = boost::program_options;
9
10 namespace creaImageIO
11 {
12     Settings::Settings(const std::string i_path)
13     {
14         //need to position path in user directory first.
15         m_SettingsFileName = i_path + "\\.gimmick\\app.config";
16         //Test if Settings File exist
17         if(!boost::filesystem::exists(m_SettingsFileName) )
18         {
19             createFile();
20         }
21         std::ifstream ifs(m_SettingsFileName.c_str());
22                 std::string line;
23                 std::string sets;
24             if (ifs.is_open())
25                 {
26                         while (! ifs.eof() )
27                         {
28                                 getline(ifs,line);
29                                 sets += line;
30                         }
31                         ifs.close();
32                 }
33         std::vector<std::string> Keys;
34                 Keys.push_back(SETTINGS_SYNC_EVENT);
35                 Keys.push_back(SETTINGS_DBPATH);
36                 Keys.push_back(SETTINGS_SYNC_FREQ);
37                 readSettings(Keys, sets);
38
39     }
40
41     Settings::~Settings()
42     {
43         
44     }
45
46
47    ////////////////////////////////////////////////////////////////////////////////////////////////
48     // create the config file                                                                                 //
49     //@param : -                                                                                               //
50     // return  : -                                                                                                // 
51     ///////////////////////////////////////////////////////////////////////////////////////////////
52    void Settings::createFile()
53    {
54        m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
55        m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
56        m_SettingsMap[SETTINGS_DBPATH] = "";
57        m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
58        writeSettingsFile();
59    }
60
61         ////////////////////////////////////////////////////////////////////////////////////////////////
62     // read Settings from config file                                                             //
63     // @param i_keys : list of keys                                                               //
64     // @param  i_file : text from config file                                                     //
65         // return : -
66     ///////////////////////////////////////////////////////////////////////////////////////////////
67         void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
68         {
69                 std::vector<std::string>::iterator it_key = i_Keys.begin();
70                 for(;  it_key< i_Keys.end(); ++it_key)
71                 {
72                         size_t fpos = i_file.find(it_key->c_str());
73                         size_t lpos = i_file.rfind(it_key->c_str());
74                         if(fpos != std::string::npos && lpos != std::string::npos)
75                         {
76                                 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
77                         }
78                 }
79         }
80         ////////////////////////////////////////////////////////////////////////////////////////////////
81     // add a path to a DB                                                                                     //
82     // @param i_path : DB path to add                                                             //
83     // return : -                                                                                                                                                                 //
84     ///////////////////////////////////////////////////////////////////////////////////////////////
85         void Settings::addDB(const std::string &i_path)
86         {
87                 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
88                 {
89                         m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
90                 }
91         }
92
93         ////////////////////////////////////////////////////////////////////////////////////////////////
94     // remove a path to a DB                                                                              //
95     // @param i_path : DB path to delete (don't exist anymore)                                    //
96     // return : -
97         ///////////////////////////////////////////////////////////////////////////////////////////////
98         
99         void Settings::removeDB(const std::string &i_path)
100         {
101                 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
102         }
103
104         ///////////////////////////////////////////////////////////////////////////////////////////////
105     // write Settings buffer from                                                                //
106     // @param o_file : settings buffer                                                           //
107     //                                                                                                                                                                                   //
108         // return : -                                                                                                                                                            //
109     ///////////////////////////////////////////////////////////////////////////////////////////////
110         void Settings::writeSettings(std::ofstream &o_file)
111         {
112                 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
113                 std::stringstream st;
114                 for(; it_map != m_SettingsMap.end(); ++it_map)
115                 {
116                         o_file << it_map->first.c_str();
117                         o_file <<  it_map->second.c_str();
118                         o_file << it_map->first.c_str();
119                         o_file << std::endl;
120                 }
121         }
122
123         ////////////////////////////////////////////////////////////////////////////////////////////////
124     // write Settings file                                                             //
125     // @param : -                                                               //
126     // return : -
127     ///////////////////////////////////////////////////////////////////////////////////////////////
128         void Settings::writeSettingsFile()
129         {       
130             std::ofstream ofs(m_SettingsFileName.c_str());
131                 ofs.clear();
132                 writeSettings(ofs);
133                 ofs.close();
134         }
135           
136 }