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