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 + "\\.creaImageIO\\share\\creaImageIO\\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 Keys.push_back(SETTINGS_OUTPUT_ASK);
44 Keys.push_back(SETTINGS_OUTPUT_DIM);
45 readSettings(Keys, sets);
55 ////////////////////////////////////////////////////////////////////////////////////////////////
56 // create the config file //
59 ///////////////////////////////////////////////////////////////////////////////////////////////
60 void Settings::createFile()
62 m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
63 m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
64 m_SettingsMap[SETTINGS_DBPATH] = "";
65 m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
66 m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
67 m_SettingsMap[SETTINGS_REMOVE_PATIENT_DISPLAY] = "0";
68 m_SettingsMap[SETTINGS_OUTPUT_ASK] ="true";
69 m_SettingsMap[SETTINGS_OUTPUT_DIM] = "1";
73 ////////////////////////////////////////////////////////////////////////////////////////////////
74 // read Settings from config file //
75 // @param i_keys : list of keys //
76 // @param i_file : text from config file //
78 ///////////////////////////////////////////////////////////////////////////////////////////////
79 void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
81 std::vector<std::string>::iterator it_key = i_Keys.begin();
82 for(; it_key< i_Keys.end(); ++it_key)
84 size_t fpos = i_file.find(it_key->c_str());
85 size_t lpos = i_file.rfind(it_key->c_str());
86 if(fpos != std::string::npos && lpos != std::string::npos)
88 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
93 ////////////////////////////////////////////////////////////////////////////////////////////////
94 // Update settings in config file //
95 // @param key : Key to update //
96 // @param value: New value to set //
98 ///////////////////////////////////////////////////////////////////////////////////////////////
99 void Settings::updateSetting(const std::string& key, const std::string &val)
101 m_SettingsMap[key.c_str()] = val;
104 ////////////////////////////////////////////////////////////////////////////////////////////////
105 // add a path to a DB //
106 // @param i_path : DB path to add //
108 ///////////////////////////////////////////////////////////////////////////////////////////////
109 void Settings::addDB(const std::string &i_path)
111 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
113 m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
117 ////////////////////////////////////////////////////////////////////////////////////////////////
118 // remove a path to a DB //
119 // @param i_path : DB path to delete (don't exist anymore) //
121 ///////////////////////////////////////////////////////////////////////////////////////////////
123 void Settings::removeDB(const std::string &i_path)
125 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
128 ///////////////////////////////////////////////////////////////////////////////////////////////
129 // write Settings buffer from //
130 // @param o_file : settings buffer //
133 ///////////////////////////////////////////////////////////////////////////////////////////////
134 void Settings::writeSettings(std::ofstream &o_file)
136 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
137 std::stringstream st;
138 for(; it_map != m_SettingsMap.end(); ++it_map)
140 o_file << it_map->first.c_str();
141 o_file << it_map->second.c_str();
142 o_file << it_map->first.c_str();
147 ////////////////////////////////////////////////////////////////////////////////////////////////
148 // write Settings file //
151 ///////////////////////////////////////////////////////////////////////////////////////////////
152 void Settings::writeSettingsFile()
154 std::ofstream ofs(m_SettingsFileName.c_str());