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>
10 // Memory tracking allocation
14 using namespace boost;
15 namespace po = boost::program_options;
19 Settings::Settings(const std::string i_path)
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) )
28 std::ifstream ifs(m_SettingsFileName.c_str());
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);
58 ////////////////////////////////////////////////////////////////////////////////////////////////
59 // create the config file //
62 ///////////////////////////////////////////////////////////////////////////////////////////////
63 void Settings::createFile()
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";
76 ////////////////////////////////////////////////////////////////////////////////////////////////
77 // read Settings from config file //
78 // @param i_keys : list of keys //
79 // @param i_file : text from config file //
81 ///////////////////////////////////////////////////////////////////////////////////////////////
82 void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
84 std::vector<std::string>::iterator it_key = i_Keys.begin();
85 for(; it_key< i_Keys.end(); ++it_key)
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)
91 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
96 ////////////////////////////////////////////////////////////////////////////////////////////////
97 // Update settings in config file //
98 // @param key : Key to update //
99 // @param value: New value to set //
101 ///////////////////////////////////////////////////////////////////////////////////////////////
102 void Settings::updateSetting(const std::string& key, const std::string &val)
104 m_SettingsMap[key.c_str()] = val;
107 ////////////////////////////////////////////////////////////////////////////////////////////////
108 // add a path to a DB //
109 // @param i_path : DB path to add //
111 ///////////////////////////////////////////////////////////////////////////////////////////////
112 void Settings::addDB(const std::string &i_path)
114 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
116 m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
120 ////////////////////////////////////////////////////////////////////////////////////////////////
121 // remove a path to a DB //
122 // @param i_path : DB path to delete (don't exist anymore) //
124 ///////////////////////////////////////////////////////////////////////////////////////////////
126 void Settings::removeDB(const std::string &i_path)
128 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
131 ///////////////////////////////////////////////////////////////////////////////////////////////
132 // write Settings buffer from //
133 // @param o_file : settings buffer //
136 ///////////////////////////////////////////////////////////////////////////////////////////////
137 void Settings::writeSettings(std::ofstream &o_file)
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)
143 o_file << it_map->first.c_str();
144 o_file << it_map->second.c_str();
145 o_file << it_map->first.c_str();
150 ////////////////////////////////////////////////////////////////////////////////////////////////
151 // write Settings file //
154 ///////////////////////////////////////////////////////////////////////////////////////////////
155 void Settings::writeSettingsFile()
157 std::ofstream ofs(m_SettingsFileName.c_str());