creaImageIOSettings.cpp

Go to the documentation of this file.
00001 #include <creaImageIOSettings.h>
00002 #include <boost/filesystem/fstream.hpp>
00003 #include <boost/algorithm/string/replace.hpp>
00004 #include <boost/filesystem/path.hpp>
00005 #include <boost/filesystem/operations.hpp>
00006 
00007 #include <iostream>
00008 #include <fstream>
00009 
00010 // Memory tracking allocation
00011 #ifdef _DEBUG
00012 #define new DEBUG_NEW
00013 #endif
00014 using namespace boost;
00015 namespace po = boost::program_options;
00016 
00017 namespace creaImageIO
00018 {
00019     Settings::Settings(const std::string i_path)
00020     {
00021         //need to position path in user directory first.
00022         m_SettingsFileName = i_path + "\\.creaImageIO\\share\\creaImageIO\\app.config";
00023         //Test if Settings File exist
00024         if(!boost::filesystem::exists(m_SettingsFileName) )
00025         {
00026             createFile();
00027         }
00028         std::ifstream ifs(m_SettingsFileName.c_str());
00029                 std::string line;
00030                 std::string sets;
00031             if (ifs.is_open())
00032                 {
00033                         while (! ifs.eof() )
00034                         {
00035                                 getline(ifs,line);
00036                                 sets += line;
00037                         }
00038                         ifs.close();
00039                 }
00040         std::vector<std::string> Keys;
00041                 Keys.push_back(SETTINGS_SYNC_EVENT);
00042                 Keys.push_back(SETTINGS_DBPATH);
00043                 Keys.push_back(SETTINGS_SYNC_FREQ);
00044                 Keys.push_back(SETTINGS_COPY_PATH);
00045                 Keys.push_back(SETTINGS_REMOVE_PATIENT_DISPLAY);
00046                 Keys.push_back(SETTINGS_OUTPUT_ASK);
00047                 Keys.push_back(SETTINGS_OUTPUT_DIM);
00048                 readSettings(Keys, sets);
00049 
00050     }
00051 
00052     Settings::~Settings()
00053     {
00054         
00055     }
00056 
00057 
00059     // create the config file                                                                                 //
00060     //@param : -                                                                                               //
00061     // return  : -                                                                                                // 
00063    void Settings::createFile()
00064    {
00065        m_SettingsMap[SETTINGS_SYNC_FREQ] = "12";
00066        m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
00067        m_SettingsMap[SETTINGS_DBPATH] = "";
00068        m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
00069            m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
00070            m_SettingsMap[SETTINGS_REMOVE_PATIENT_DISPLAY] = "0";
00071            m_SettingsMap[SETTINGS_OUTPUT_ASK] ="true";
00072            m_SettingsMap[SETTINGS_OUTPUT_DIM] = "1";
00073        writeSettingsFile();
00074    }
00075 
00077     // read Settings from config file                                                             //
00078     // @param i_keys : list of keys                                                               //
00079     // @param  i_file : text from config file                                                     //
00080         // return : -
00082         void Settings::readSettings(std::vector<std::string> &i_Keys, const std::string &i_file)
00083         {
00084                 std::vector<std::string>::iterator it_key = i_Keys.begin();
00085                 for(;  it_key< i_Keys.end(); ++it_key)
00086                 {
00087                         size_t fpos = i_file.find(it_key->c_str());
00088                         size_t lpos = i_file.rfind(it_key->c_str());
00089                         if(fpos != std::string::npos && lpos != std::string::npos)
00090                         {
00091                                 m_SettingsMap[it_key->c_str()] = i_file.substr(fpos + it_key->size(),lpos-fpos - it_key->size());
00092                         }
00093                 }
00094         }
00095 
00097     // Update settings in config file                                                             //
00098     // @param key : Key to update                                                                 //
00099     // @param value: New value to set                                                             //
00100         // return : -
00102         void Settings::updateSetting(const std::string& key, const std::string &val)
00103         {
00104                 m_SettingsMap[key.c_str()] = val;
00105         }
00106 
00108     // add a path to a DB                                                                                     //
00109     // @param i_path : DB path to add                                                             //
00110     // return : -                                                                                                                                                                 //
00112         void Settings::addDB(const std::string &i_path)
00113         {
00114                 if(m_SettingsMap[SETTINGS_DBPATH].find(i_path) == std::string::npos)
00115                 {
00116                         m_SettingsMap[SETTINGS_DBPATH] += i_path + ";";
00117                 }
00118         }
00119 
00121     // remove a path to a DB                                                                              //
00122     // @param i_path : DB path to delete (don't exist anymore)                                    //
00123     // return : -
00125         
00126         void Settings::removeDB(const std::string &i_path)
00127         {
00128                 boost::algorithm::replace_all(m_SettingsMap[SETTINGS_DBPATH],i_path + ";","");
00129         }
00130 
00132     // write Settings buffer from                                                                //
00133     // @param o_file : settings buffer                                                           //
00134     //                                                                                                                                                                                   //
00135         // return : -                                                                                                                                                            //
00137         void Settings::writeSettings(std::ofstream &o_file)
00138         {
00139                 std::map<std::string, std::string>::iterator it_map = m_SettingsMap.begin();
00140                 std::stringstream st;
00141                 for(; it_map != m_SettingsMap.end(); ++it_map)
00142                 {
00143                         o_file << it_map->first.c_str();
00144                         o_file <<  it_map->second.c_str();
00145                         o_file << it_map->first.c_str();
00146                         o_file << std::endl;
00147                 }
00148         }
00149 
00151     // write Settings file                                                             //
00152     // @param : -                                                               //
00153     // return : -
00155         void Settings::writeSettingsFile()
00156         {       
00157             std::ofstream ofs(m_SettingsFileName.c_str());
00158                 ofs.clear();
00159                 writeSettings(ofs);
00160                 ofs.close();
00161         }  
00162 }
00163