creaImageIO_lib
creaImageIOSettings.cpp
Go to the documentation of this file.
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # pour la Santé)
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
9 #
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.
16 #
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
21 # liability.
22 #
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 # ------------------------------------------------------------------------
26 */
27 
28 
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>
34 
35 #include <iostream>
36 #include <fstream>
37 
38 // Memory tracking allocation
39 #ifdef _DEBUG
40 #define new DEBUG_NEW
41 #endif
42 using namespace boost;
43 namespace po = boost::program_options;
44 
45 namespace creaImageIO
46 {
47  Settings::Settings(const std::string i_path)
48  {
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) )
53  {
54  createFile();
55  }
56  std::ifstream ifs(m_SettingsFileName.c_str());
57  std::string line;
58  std::string sets;
59  if (ifs.is_open())
60  {
61  while (! ifs.eof() )
62  {
63  getline(ifs,line);
64  sets += line;
65  }
66  ifs.close();
67  }
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);
77 
78  }
79 
80  Settings::~Settings()
81  {
82 
83  }
84 
85 
87  // create the config file //
88  //@param : - //
89  // return : - //
91  void Settings::createFile()
92  {
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";
101  writeSettingsFile();
102  }
103 
105  // read Settings from config file //
106  // @param i_keys : list of keys //
107  // @param i_file : text from config file //
108  // return : -
110  void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
111  {
112  std::vector<std::string>::iterator it_key = i_Keys.begin();
113  for(; it_key< i_Keys.end(); ++it_key)
114  {
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)
118  {
119  m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
120  }
121  }
122  }
123 
125  // Update settings in config file //
126  // @param key : Key to update //
127  // @param value: New value to set //
128  // return : -
130  void Settings::updateSetting(const std::string& key, const std::string &val)
131  {
132  m_SettingsMap[key.c_str()] = val;
133  }
134 
136  // add a path to a DB //
137  // @param i_path : DB path to add //
138  // return : - //
140  void Settings::addDB(const std::string &i_path)
141  {
142  if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
143  {
144  m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
145  }
146  }
147 
149  // remove a path to a DB //
150  // @param i_path : DB path to delete (don't exist anymore) //
151  // return : -
153 
154  void Settings::removeDB(const std::string &i_path)
155  {
156  boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
157  }
158 
160  // write Settings buffer from //
161  // @param o_file : settings buffer //
162  // //
163  // return : - //
165  void Settings::writeSettings(std::ofstream &o_file)
166  {
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)
170  {
171  o_file << it_map->first.c_str();
172  o_file << it_map->second.c_str();
173  o_file << it_map->first.c_str();
174  o_file << std::endl;
175  }
176  }
177 
179  // write Settings file //
180  // @param : - //
181  // return : -
183  void Settings::writeSettingsFile()
184  {
185  std::ofstream ofs(m_SettingsFileName.c_str());
186  ofs.clear();
187  writeSettings(ofs);
188  ofs.close();
189  }
190 }
191