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 Keys.push_back(SETTINGS_REMOVE_PATIENT_DISPLAY);
43 readSettings(Keys, sets);
53 ////////////////////////////////////////////////////////////////////////////////////////////////
54 // create the config file //
57 ///////////////////////////////////////////////////////////////////////////////////////////////
58 void Settings::createFile()
60 m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
61 m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
62 m_SettingsMap[SETTINGS_DBPATH] = "";
63 m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
64 m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
65 m_SettingsMap[SETTINGS_REMOVE_PATIENT_DISPLAY] = "0";
69 ////////////////////////////////////////////////////////////////////////////////////////////////
70 // read Settings from config file //
71 // @param i_keys : list of keys //
72 // @param i_file : text from config file //
74 ///////////////////////////////////////////////////////////////////////////////////////////////
75 void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
77 std::vector<std::string>::iterator it_key = i_Keys.begin();
78 for(; it_key< i_Keys.end(); ++it_key)
80 size_t fpos = i_file.find(it_key->c_str());
81 size_t lpos = i_file.rfind(it_key->c_str());
82 if(fpos != std::string::npos && lpos != std::string::npos)
84 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
89 ////////////////////////////////////////////////////////////////////////////////////////////////
90 // Update settings in config file //
91 // @param key : Key to update //
92 // @param value: New value to set //
94 ///////////////////////////////////////////////////////////////////////////////////////////////
95 void Settings::updateSetting(const std::string& key, const std::string &val)
97 m_SettingsMap[key.c_str()] = val;
100 ////////////////////////////////////////////////////////////////////////////////////////////////
101 // add a path to a DB //
102 // @param i_path : DB path to add //
104 ///////////////////////////////////////////////////////////////////////////////////////////////
105 void Settings::addDB(const std::string &i_path)
107 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
109 m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
113 ////////////////////////////////////////////////////////////////////////////////////////////////
114 // remove a path to a DB //
115 // @param i_path : DB path to delete (don't exist anymore) //
117 ///////////////////////////////////////////////////////////////////////////////////////////////
119 void Settings::removeDB(const std::string &i_path)
121 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
124 ///////////////////////////////////////////////////////////////////////////////////////////////
125 // write Settings buffer from //
126 // @param o_file : settings buffer //
129 ///////////////////////////////////////////////////////////////////////////////////////////////
130 void Settings::writeSettings(std::ofstream &o_file)
132 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
133 std::stringstream st;
134 for(; it_map != m_SettingsMap.end(); ++it_map)
136 o_file << it_map->first.c_str();
137 o_file << it_map->second.c_str();
138 o_file << it_map->first.c_str();
143 ////////////////////////////////////////////////////////////////////////////////////////////////
144 // write Settings file //
147 ///////////////////////////////////////////////////////////////////////////////////////////////
148 void Settings::writeSettingsFile()
150 std::ofstream ofs(m_SettingsFileName.c_str());