1 #include <creaImageIOSettings.h>
2 #include <boost/filesystem/fstream.hpp>
3 #include <boost/algorithm/string/replace.hpp>
8 namespace po = boost::program_options;
12 Settings::Settings(const std::string i_path)
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) )
21 std::ifstream ifs(m_SettingsFileName.c_str());
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 Keys.push_back(SETTINGS_COPY_PATH);
38 readSettings(Keys, sets);
48 ////////////////////////////////////////////////////////////////////////////////////////////////
49 // create the config file //
52 ///////////////////////////////////////////////////////////////////////////////////////////////
53 void Settings::createFile()
55 m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
56 m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
57 m_SettingsMap[SETTINGS_DBPATH] = "";
58 m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
59 m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
63 ////////////////////////////////////////////////////////////////////////////////////////////////
64 // read Settings from config file //
65 // @param i_keys : list of keys //
66 // @param i_file : text from config file //
68 ///////////////////////////////////////////////////////////////////////////////////////////////
69 void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
71 std::vector<std::string>::iterator it_key = i_Keys.begin();
72 for(; it_key< i_Keys.end(); ++it_key)
74 size_t fpos = i_file.find(it_key->c_str());
75 size_t lpos = i_file.rfind(it_key->c_str());
76 if(fpos != std::string::npos && lpos != std::string::npos)
78 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
83 ////////////////////////////////////////////////////////////////////////////////////////////////
84 // Update settings in config file //
85 // @param key : Key to update //
86 // @param value: New value to set //
88 ///////////////////////////////////////////////////////////////////////////////////////////////
89 void Settings::updateSetting(const std::string& key, const std::string &val)
91 m_SettingsMap[key.c_str()] = val;
94 ////////////////////////////////////////////////////////////////////////////////////////////////
95 // add a path to a DB //
96 // @param i_path : DB path to add //
98 ///////////////////////////////////////////////////////////////////////////////////////////////
99 void Settings::addDB(const std::string &i_path)
101 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
103 m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
107 ////////////////////////////////////////////////////////////////////////////////////////////////
108 // remove a path to a DB //
109 // @param i_path : DB path to delete (don't exist anymore) //
111 ///////////////////////////////////////////////////////////////////////////////////////////////
113 void Settings::removeDB(const std::string &i_path)
115 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
118 ///////////////////////////////////////////////////////////////////////////////////////////////
119 // write Settings buffer from //
120 // @param o_file : settings buffer //
123 ///////////////////////////////////////////////////////////////////////////////////////////////
124 void Settings::writeSettings(std::ofstream &o_file)
126 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
127 std::stringstream st;
128 for(; it_map != m_SettingsMap.end(); ++it_map)
130 o_file << it_map->first.c_str();
131 o_file << it_map->second.c_str();
132 o_file << it_map->first.c_str();
137 ////////////////////////////////////////////////////////////////////////////////////////////////
138 // write Settings file //
141 ///////////////////////////////////////////////////////////////////////////////////////////////
142 void Settings::writeSettingsFile()
144 std::ofstream ofs(m_SettingsFileName.c_str());