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