]> Creatis software - creaImageIO.git/blob - src/creaImageIOSettings.cpp
fd7cdd5d080aa86de375a0db6512f5d626a87aaa
[creaImageIO.git] / src / 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 + "\\.creaImageIO\\share\\creaImageIO\\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                 Keys.push_back(SETTINGS_REMOVE_PATIENT_DISPLAY);
43                 Keys.push_back(SETTINGS_OUTPUT_ASK);
44                 Keys.push_back(SETTINGS_OUTPUT_DIM);
45                 readSettings(Keys, sets);
46
47     }
48
49     Settings::~Settings()
50     {
51         
52     }
53
54
55    ////////////////////////////////////////////////////////////////////////////////////////////////
56     // create the config file                                                                                 //
57     //@param : -                                                                                               //
58     // return  : -                                                                                                // 
59     ///////////////////////////////////////////////////////////////////////////////////////////////
60    void Settings::createFile()
61    {
62        m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
63        m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
64        m_SettingsMap[SETTINGS_DBPATH] = "";
65        m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
66            m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
67            m_SettingsMap[SETTINGS_REMOVE_PATIENT_DISPLAY] = "0";
68            m_SettingsMap[SETTINGS_OUTPUT_ASK] ="true";
69            m_SettingsMap[SETTINGS_OUTPUT_DIM] = "1";
70        writeSettingsFile();
71    }
72
73         ////////////////////////////////////////////////////////////////////////////////////////////////
74     // read Settings from config file                                                             //
75     // @param i_keys : list of keys                                                               //
76     // @param  i_file : text from config file                                                     //
77         // return : -
78     ///////////////////////////////////////////////////////////////////////////////////////////////
79         void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
80         {
81                 std::vector<std::string>::iterator it_key = i_Keys.begin();
82                 for(;  it_key< i_Keys.end(); ++it_key)
83                 {
84                         size_t fpos = i_file.find(it_key->c_str());
85                         size_t lpos = i_file.rfind(it_key->c_str());
86                         if(fpos != std::string::npos && lpos != std::string::npos)
87                         {
88                                 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
89                         }
90                 }
91         }
92
93         ////////////////////////////////////////////////////////////////////////////////////////////////
94     // Update settings in config file                                                             //
95     // @param key : Key to update                                                                 //
96     // @param value: New value to set                                                             //
97         // return : -
98     ///////////////////////////////////////////////////////////////////////////////////////////////
99         void Settings::updateSetting(const std::string& key, const std::string &val)
100         {
101                 m_SettingsMap[key.c_str()] = val;
102         }
103
104         ////////////////////////////////////////////////////////////////////////////////////////////////
105     // add a path to a DB                                                                                     //
106     // @param i_path : DB path to add                                                             //
107     // return : -                                                                                                                                                                 //
108     ///////////////////////////////////////////////////////////////////////////////////////////////
109         void Settings::addDB(const std::string &i_path)
110         {
111                 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
112                 {
113                         m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
114                 }
115         }
116
117         ////////////////////////////////////////////////////////////////////////////////////////////////
118     // remove a path to a DB                                                                              //
119     // @param i_path : DB path to delete (don't exist anymore)                                    //
120     // return : -
121         ///////////////////////////////////////////////////////////////////////////////////////////////
122         
123         void Settings::removeDB(const std::string &i_path)
124         {
125                 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
126         }
127
128         ///////////////////////////////////////////////////////////////////////////////////////////////
129     // write Settings buffer from                                                                //
130     // @param o_file : settings buffer                                                           //
131     //                                                                                                                                                                                   //
132         // return : -                                                                                                                                                            //
133     ///////////////////////////////////////////////////////////////////////////////////////////////
134         void Settings::writeSettings(std::ofstream &o_file)
135         {
136                 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
137                 std::stringstream st;
138                 for(; it_map != m_SettingsMap.end(); ++it_map)
139                 {
140                         o_file << it_map->first.c_str();
141                         o_file <<  it_map->second.c_str();
142                         o_file << it_map->first.c_str();
143                         o_file << std::endl;
144                 }
145         }
146
147         ////////////////////////////////////////////////////////////////////////////////////////////////
148     // write Settings file                                                             //
149     // @param : -                                                               //
150     // return : -
151     ///////////////////////////////////////////////////////////////////////////////////////////////
152         void Settings::writeSettingsFile()
153         {       
154             std::ofstream ofs(m_SettingsFileName.c_str());
155                 ofs.clear();
156                 writeSettings(ofs);
157                 ofs.close();
158         }  
159 }
160