1 #include <creaImageIOSettings.h>
2 #include <boost/filesystem/fstream.hpp>
3 #include <boost/algorithm/string/replace.hpp>
7 // Memory tracking allocation
11 using namespace boost;
12 namespace po = boost::program_options;
16 Settings::Settings(const std::string i_path)
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) )
25 std::ifstream ifs(m_SettingsFileName.c_str());
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);
52 ////////////////////////////////////////////////////////////////////////////////////////////////
53 // create the config file //
56 ///////////////////////////////////////////////////////////////////////////////////////////////
57 void Settings::createFile()
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";
67 ////////////////////////////////////////////////////////////////////////////////////////////////
68 // read Settings from config file //
69 // @param i_keys : list of keys //
70 // @param i_file : text from config file //
72 ///////////////////////////////////////////////////////////////////////////////////////////////
73 void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
75 std::vector<std::string>::iterator it_key = i_Keys.begin();
76 for(; it_key< i_Keys.end(); ++it_key)
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)
82 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
87 ////////////////////////////////////////////////////////////////////////////////////////////////
88 // Update settings in config file //
89 // @param key : Key to update //
90 // @param value: New value to set //
92 ///////////////////////////////////////////////////////////////////////////////////////////////
93 void Settings::updateSetting(const std::string& key, const std::string &val)
95 m_SettingsMap[key.c_str()] = val;
98 ////////////////////////////////////////////////////////////////////////////////////////////////
99 // add a path to a DB //
100 // @param i_path : DB path to add //
102 ///////////////////////////////////////////////////////////////////////////////////////////////
103 void Settings::addDB(const std::string &i_path)
105 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
107 m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
111 ////////////////////////////////////////////////////////////////////////////////////////////////
112 // remove a path to a DB //
113 // @param i_path : DB path to delete (don't exist anymore) //
115 ///////////////////////////////////////////////////////////////////////////////////////////////
117 void Settings::removeDB(const std::string &i_path)
119 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
122 ///////////////////////////////////////////////////////////////////////////////////////////////
123 // write Settings buffer from //
124 // @param o_file : settings buffer //
127 ///////////////////////////////////////////////////////////////////////////////////////////////
128 void Settings::writeSettings(std::ofstream &o_file)
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)
134 o_file << it_map->first.c_str();
135 o_file << it_map->second.c_str();
136 o_file << it_map->first.c_str();
141 ////////////////////////////////////////////////////////////////////////////////////////////////
142 // write Settings file //
145 ///////////////////////////////////////////////////////////////////////////////////////////////
146 void Settings::writeSettingsFile()
148 std::ofstream ofs(m_SettingsFileName.c_str());