creaImageIO_lib
creaImageIOSynchronizer.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 <creaImageIOSystem.h>
31 #include "boost/filesystem.hpp"
32 
33 namespace fs = boost::filesystem;
34 
35 namespace creaImageIO
36 {
37 
38  //==============================================================
40  : mHandler(th)
41  {
42 
43  }
44  //==============================================================
45 
46  //==============================================================
48  {
49 
50  }
51  //==============================================================
52 
53  //==============================================================
54  std::string Synchronizer::Synchronize(bool update)
55  {
56  GimmickMessage(1,"Synchronizing "<<std::endl);
57  int id=1;
58  std::stringstream mess;
59  std::string file;
60  mHandler->GetAttribute("Image","","","FullFileName",file);
61  size_t ini=0;
62  size_t fin=0;
63  while(fin<file.size()-1)
64  {
65  fin=file.find('#',ini);
66  SynchronizeFile(update,file.substr(ini,fin-ini),mess);
67  ini=fin+1;
68  }
69  if(mess.str()=="")
70  {
71  mess<<"Database up to date"<<std::endl;
72  }
73  GimmickMessage(1,mess.str());
74  return mess.str();
75  }
76  //==============================================================
77 
78  //==============================================================
79  void Synchronizer::SynchronizeFile(bool update, std::string file, std::stringstream& mess)
80  {
81  if(!FileExists(file))
82  {
83  if(update)
84  {
85  mHandler->DeleteTuple("Image","FullFileName",file);
86  mess<<file<<" has been removed from the DB"<<std::endl;
87  }
88  else
89  {
90  mess<<file<<" State: Non existant"<<std::endl;
91  }
92  }
93  else
94  {
95  AttributesMatch(update,file,mess);
96  }
97  }
98  //==============================================================
99 
100  //==============================================================
101  bool Synchronizer::FileExists(std::string file)
102  {
103  GimmickDebugMessage(4,"Verifying if file "<<file<<" exists"<<std::endl);
104  bool exists=true;
105  if ( !fs::exists( file ) )
106  {
107  exists=false;
108  }
109  return exists;
110  }
111  //==============================================================
112 
113  //==============================================================
114  void Synchronizer::AttributesMatch(bool update, std::string file, std::stringstream& mess)
115  {
116  std::map< std::string, std::string> attr;
118  mReader.ReadAttributes(file,attr);
120  tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
121  for (a = adl.begin();a!=adl.end();++a)
122  {
123  std::string databaseVal;
124  mHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal);
125  std::string fileVal=attr.find(a->GetKey())->second;
126  if ( a->GetFlags()==0 && databaseVal.compare(fileVal)!=0 )
127  {
128  if(update)
129  {
130  mHandler->SetAttribute("Image",a->GetKey(),fileVal,"FullFileName", file);
131  mess<<file<<" has been updated in the DB"<<std::endl;
132  }
133  else
134  {
135  mess<<file<<" State: Attributes differ"<<std::endl;
136  }
137  }
138  }
139  }
140  //==============================================================
141 
142 }