2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
29 #include <creaImageIOSettings.h>
30 #include <boost/filesystem/fstream.hpp>
31 #include <boost/algorithm/string/replace.hpp>
32 #include <boost/filesystem/path.hpp>
33 #include <boost/filesystem/operations.hpp>
38 // Memory tracking allocation
42 using namespace boost;
43 namespace po = boost::program_options;
47 Settings::Settings(const std::string i_path)
49 //need to position path in user directory first.
50 m_SettingsFileName = i_path + "\\.creaImageIO\\share\\creaImageIO\\app.config";
51 //Test if Settings File exist
52 if(!boost::filesystem::exists(m_SettingsFileName) )
56 std::ifstream ifs(m_SettingsFileName.c_str());
68 std::vector<std::string> Keys;
69 Keys.push_back(SETTINGS_SYNC_EVENT);
70 Keys.push_back(SETTINGS_DBPATH);
71 Keys.push_back(SETTINGS_SYNC_FREQ);
72 Keys.push_back(SETTINGS_COPY_PATH);
73 Keys.push_back(SETTINGS_REMOVE_PATIENT_DISPLAY);
74 Keys.push_back(SETTINGS_OUTPUT_ASK);
75 Keys.push_back(SETTINGS_OUTPUT_DIM);
76 readSettings(Keys, sets);
86 ////////////////////////////////////////////////////////////////////////////////////////////////
87 // create the config file //
90 ///////////////////////////////////////////////////////////////////////////////////////////////
91 void Settings::createFile()
93 m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
94 m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
95 m_SettingsMap[SETTINGS_DBPATH] = "";
96 m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
97 m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
98 m_SettingsMap[SETTINGS_REMOVE_PATIENT_DISPLAY] = "0";
99 m_SettingsMap[SETTINGS_OUTPUT_ASK] ="true";
100 m_SettingsMap[SETTINGS_OUTPUT_DIM] = "1";
104 ////////////////////////////////////////////////////////////////////////////////////////////////
105 // read Settings from config file //
106 // @param i_keys : list of keys //
107 // @param i_file : text from config file //
109 ///////////////////////////////////////////////////////////////////////////////////////////////
110 void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
112 std::vector<std::string>::iterator it_key = i_Keys.begin();
113 for(; it_key< i_Keys.end(); ++it_key)
115 size_t fpos = i_file.find(it_key->c_str());
116 size_t lpos = i_file.rfind(it_key->c_str());
117 if(fpos != std::string::npos && lpos != std::string::npos)
119 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
124 ////////////////////////////////////////////////////////////////////////////////////////////////
125 // Update settings in config file //
126 // @param key : Key to update //
127 // @param value: New value to set //
129 ///////////////////////////////////////////////////////////////////////////////////////////////
130 void Settings::updateSetting(const std::string& key, const std::string &val)
132 m_SettingsMap[key.c_str()] = val;
135 ////////////////////////////////////////////////////////////////////////////////////////////////
136 // add a path to a DB //
137 // @param i_path : DB path to add //
139 ///////////////////////////////////////////////////////////////////////////////////////////////
140 void Settings::addDB(const std::string &i_path)
142 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
144 m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
148 ////////////////////////////////////////////////////////////////////////////////////////////////
149 // remove a path to a DB //
150 // @param i_path : DB path to delete (don't exist anymore) //
152 ///////////////////////////////////////////////////////////////////////////////////////////////
154 void Settings::removeDB(const std::string &i_path)
156 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
159 ///////////////////////////////////////////////////////////////////////////////////////////////
160 // write Settings buffer from //
161 // @param o_file : settings buffer //
164 ///////////////////////////////////////////////////////////////////////////////////////////////
165 void Settings::writeSettings(std::ofstream &o_file)
167 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
168 std::stringstream st;
169 for(; it_map != m_SettingsMap.end(); ++it_map)
171 o_file << it_map->first.c_str();
172 o_file << it_map->second.c_str();
173 o_file << it_map->first.c_str();
178 ////////////////////////////////////////////////////////////////////////////////////////////////
179 // write Settings file //
182 ///////////////////////////////////////////////////////////////////////////////////////////////
183 void Settings::writeSettingsFile()
185 std::ofstream ofs(m_SettingsFileName.c_str());