GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DotConf.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2010 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef ADF_DotConf
24 #include "DotConf.h"
25 #endif
26 #ifndef ADF_ConfigurationFrame
27 #include "ConfigurationFrame.h"
28 #endif
29 
30 #include <ctime>
31 #include <fstream>
32 #include <cstdlib>
33 
34 using namespace ADF;
35 using namespace std;
36 
38  fLog("ADF::DotConf"),
39  fHeaderDef('#',false,"ADF::DotConf",""),
40  fHeader(-1,-1,"Init"),
41  fListOfDotConf(),
42  fLastModif(""),
43  fComment("Default")
44 {
45  // to remove end of line character
46  if (fComment.size() > 0)
47  if ( fComment.at(fComment.length()-1) == '\n' )
48  fComment.erase(fComment.size()-1);
49 
50  SetLastModif();
51 }
52 
53 DotConf::DotConf(const Char_t *rid, const Char_t *srid, Bool_t is_record) :
54  fLog("ADF::DotConf"),
55  fHeaderDef('#',is_record,rid,srid),
56  fHeader(-1,-1,"Init"),
57  fListOfDotConf(),
58  fLastModif(""),
59  fComment("Default")
60 {
61  // to remove end of line character
62  if (fComment.size() > 0)
63  if ( fComment.at(fComment.length()-1) == '\n' )
64  fComment.erase(fComment.size()-1);
65 
66  SetLastModif();
67 }
68 
70 {
71 }
72 
73 /*
74 DotConf *DotConf::Clone(const char *comment)
75 {
76  // first creats a new agent
77  DotConf *aconf = new DotConf(comment);
78  if ( aconf == NULL )
79  return NULL;
80 
81  // now copy the content of the global agent to it using Configure
82  std::ostringstream o;
83  this->DoConfigure(o);
84  std::string tmp;
85  tmp = o.str();
86  std::istringstream i(tmp);
87  aconf->DoConfigure(i);
88 
89  return aconf;
90 }
91  */
92 
94 {
95  std::string bor(&fHeaderDef.fSkipLine,1); bor += "////"; bor += fHeaderDef.fRID; bor += "_beg////";
96  return bor;
97 }
98 
99 std::string DotConf::EndOfRecord()
100 {
101  std::string eor(&fHeaderDef.fSkipLine,1); eor += "////"; eor += fHeaderDef.fRID; eor += "_end////";
102  return eor;
103 }
104 
106 {
107  time_t thetime; ::time(&thetime); fLastModif = ::ctime(&thetime);
108 
109  // to remove end of line character
110  if (fLastModif.size() > 0)
111  if ( fLastModif.at(fLastModif.length()-1) == '\n' )
112  fLastModif.erase(fLastModif.size()-1);
113 }
114 
115 void DotConf::Reset(const Char_t * /*opt*/)
116 {
117 
118 }
119 
120 Bool_t DotConf::IsBeginOfRecord(const std::string &st, Bool_t allow_init)
121 {
122  std::string bor = BeginOfRecord(), tmp, tmp_srid, tmp_option; Short_t version;
123  Bool_t ok = false;
124 
125  if ( st.find(bor) == 0 ) {
126  // look for the SRID
127  tmp = st.substr(bor.length()+1);
128  tmp += " ";
129  if ( tmp[0] != ' ' )
130  tmp.insert(0," ");
131  // the sub record id has not been found ...
132  if ( tmp.find(fHeaderDef.fSRID) == string::npos ) {
133  return false; // ... and required ! so return false
134  }
135  else ok = true; // yes SRID has been found
136 
137  // now decode version and option
138  istringstream in( tmp );
139  in.clear();
140 
141  // check the number of values in tmp
142  Bool_t aval = false;
143  Int_t nb_val = 0;
144  for (size_t i = 0; i < tmp.size(); i++) {
145  if ( tmp[i] == ' ' && aval )
146  aval = false;
147  if ( tmp[i] != ' ' && aval == false ) {
148  aval = true;
149  nb_val++;
150  }
151  }
152  // according to the number of value, decode tmp
153  version = -1;
154  switch (nb_val) {
155  case 1:
156  in >> tmp_option; // only option
157  break;
158  case 2:
159  if ( fHeaderDef.fSRID == "" ) {
160  in >> version >> tmp_option; // version and option
161  }
162  else {
163  in >> tmp_srid >> tmp_option; // SRID and option
164  }
165  break;
166  default:
167  in >> tmp_srid >> version >> tmp_option; // all
168  break;
169  }
170  if ( !in.good() )
171  return false; // something wrong @ reading
172 
173  // Option is init so call reset if set possible by the user and if everything has been read correctly
174  if ( tmp_option.find("Init") != string::npos ) {
175  if ( allow_init )
176  Reset();
177  else
178  tmp_option = "+,==";
179  }
180  // change dotconf attributes
181  fHeader.fPreviousVersion = fHeader.fVersion; fHeader.fVersion = version; fHeader.fStreamOption = tmp_option;
182  SetLastModif();
183  }
184  return ok;
185 }
186 
187 Bool_t DotConf::IsEndOfRecord(const std::string &st)
188 {
189  std::string eor = EndOfRecord();
190  Bool_t ok = false;
191 
192  if ( st.find(eor) == 0 )
193  ok = true;
194 
195  return ok;
196 }
197 
198 Bool_t DotConf::AddRecord(std::ostream &out, char t)
199 {
200  // nothing to be added ... so ok !
201  if ( !fHeaderDef.fWithRecord )
202  return true;
203 
204  if ( t == 'b' ) {
205  out << BeginOfRecord()
206  << " "
207  << GetSRID()
208  << " "
209  << GetVersion()
210  << " "
211  << GetStreamOption() << " \n";
212 
213  out << "# Last modified by : " << GetLastModif() << " \n";
214  out << "# Modified by : " << GetComment() << " \n";
215  }
216  else {
217  out << EndOfRecord() << " \n";
218  }
219  return out.good();
220 }
221 
222 void DotConf::AddConf ( DotConf *aconf )
223 {
224  fListOfDotConf.push_back(aconf);
225 }
226 
227 Bool_t DotConf::DoConfigure (istream &in, Bool_t allow_init)
228 {
229  // local required
230  string tmp; Bool_t is_a_record = false, do_process = false, ok_decode = true;
231 
232  // to locate the log message
233  fLog.SetProcessMethod("DoConfigure(istream)");
234 
235  // process all non commented lines if the conf file does not contain begin of records
236  if ( !fHeaderDef.fWithRecord )
237  is_a_record = do_process = true;
238 
239  // read the file line by line
240  getline(in,tmp);
241  tmp += " ";
242  while ( in.good() && !in.eof() ) { // read input stream line by line
243  if ( tmp[0] == fHeaderDef.fSkipLine ) {
244  // look in the comment if there is a begin of record
245  if ( fHeaderDef.fWithRecord ) {
246  // this is a start of record statement
247  if ( IsBeginOfRecord(tmp,allow_init) )
248  do_process = is_a_record = true;
249 
250  // this is a end of record statement for that class
251  if ( IsEndOfRecord(tmp) )
252  do_process = false;
253  }
254  getline(in,tmp);
255  tmp += " ";
256  continue;
257  } // this line is a comment
258 
259  if (do_process)
260  ok_decode = ok_decode && ProcessLine(tmp);
261 
262  // add null character at the end since getline
263  // does not store \n and it may give troubles when decoding
264  getline(in,tmp);
265  tmp += " ";
266  }
267 
268  // call it for the list of DotConf ... in case the order is not good as it is, overwrite this method
269 
270  std::list< DotConf * >::iterator
271  listed_item;
272  for ( listed_item = fListOfDotConf.begin() ; listed_item != fListOfDotConf.end() ; listed_item++ ) {
273  in.clear();
274  in.seekg (0, ios::beg);
275  ok_decode = ok_decode && (*listed_item)->DoConfigure(in, allow_init);
276  }
277 
278  // the LogCollector may have been changed ... so wait util the end to apply collection
279  fLog << dolog;
280  //
281  return ok_decode;
282 }
283 
284 
285 Bool_t DotConf::DoConfigure (ostream & out)
286 {
287  Bool_t ok_decode = true;
288 
289  /* uncomment and add information in case this method is overwritten
290  // add a start record
291  AddRecord(out,'b');
292 
293  //
294  // here put what would like to add
295  //
296 
297  // add an end of record
298  AddRecord(out,'e');
299  */
300 
301  // now call it for the list of DotConf
302  std::list< DotConf * >::iterator
303  listed_item;
304  for ( listed_item = fListOfDotConf.begin() ; listed_item != fListOfDotConf.end() ; listed_item++ ) {
305  ok_decode = ok_decode && (*listed_item)->DoConfigure(out);
306  }
307 
308  return ok_decode;
309 }
310 
311 Bool_t DotConf::Configure(const char *name, const char *option, Bool_t allow_init)
312 {
313  // set reference to this method for log system
314  fLog.SetProcessMethod("Configure(const char *name)");
315 
316  Bool_t ok; std::string opt = option;
317  if ( opt == "in" ) {
318  std::ifstream filein(name);
319  if ( filein.is_open() == true ) {
320  ok = DoConfigure(filein,allow_init);
321  }
322  else {
323  fLog << error << "Cannot open file (read mode) " << name << nline;
324  ok = false;
325  }
326  filein.close();
327  }
328  else {
329  std::ofstream fileout(name);
330  if ( fileout.is_open() == true ) {
331  ok = DoConfigure(fileout);
332  }
333  else {
334  fLog << error << " Cannot open file (writting mode) " << name << nline;
335  ok = false;
336  }
337  fileout.close();
338  }
339 
340  fLog << dolog; return ok;
341 }
342 
343 Bool_t DotConf::Configure(ConfigurationFrame *frame, const char *option, Bool_t allow_init)
344 {
345  // set reference to this method for log system
346  fLog.SetProcessMethod("Configure(ConfigurationFrame *name)");
347 
348  Bool_t ok; std::string opt = option;
349 
350  if ( opt == "in" ) {
351  // load the content of the frame in the string
352  frame->Read();
353  // use stream facility
354  std::istringstream in;
355  in.clear();
356  in.str((frame->String()));
357 
358  ok = DoConfigure(in,allow_init);
359 
360  }
361  else {
362  // use stream facility
363  std::ostringstream out;
364  ok = DoConfigure(out);
365  // write the result in the conf frame
366  frame->String() = out.str();
367  frame->Write();
368  }
369 
370  fLog << dolog; return ok;
371 }
372 
DotConf : Utility for class configuration from ascii file or Configuration frames.
Definition: DotConf.h:96
const Char_t * GetComment() const
Definition: DotConf.h:235
std::string BeginOfRecord()
string that gives a start of record for this object
Definition: DotConf.cpp:93
virtual Short_t GetVersion() const
to get the curent version of this object
Definition: DotConf.h:258
virtual const Char_t * GetSRID() const
to get the record id
Definition: DotConf.h:255
void SetLastModif()
set in last modif string the current date.
Definition: DotConf.cpp:105
A configuration frame is just an encapsulation of a string.
Bool_t IsEndOfRecord(const std::string &)
Return true if this string is an end of record.
Definition: DotConf.cpp:187
LogMessage & error(LogMessage &)
virtual void Reset(const Char_t *opt="")
Reset : all values are set to default.
Definition: DotConf.cpp:115
std::string EndOfRecord()
string that gives an end of record for this object
Definition: DotConf.cpp:99
virtual UInt_t Read()
It reads the content of the string from the Frame.
virtual ~DotConf()
Definition: DotConf.cpp:69
virtual Bool_t Configure(ConfigurationFrame *, const char *option="in", Bool_t allow_init=false)
configuration from/to a configuration frame : extact string from the frame and call DoConfigure ...
Definition: DotConf.cpp:343
virtual UInt_t Write()
It writes to the Frame the content of the string.
const Char_t * GetLastModif() const
Definition: DotConf.h:238
virtual void SetProcessMethod(const char *)
To set the current method.
LogMessage & nline(LogMessage &)
LogMessage fLog
the log messenger ... to know the object hae been properly intitialised/modified
Definition: DotConf.h:100
virtual Bool_t DoConfigure(std::istream &, Bool_t allow_init)
configure this from an input stream
Bool_t IsBeginOfRecord(const std::string &, Bool_t allow_init)
Return true if this string is a begin of record and call Reset if Init is found.
Definition: DotConf.cpp:120
header file for DotConf.cpp
virtual Bool_t ProcessLine(const std::string &)
a helper template to do clones : it requires a default constructor. then it uses the Configure facili...
Definition: DotConf.h:216
virtual const Char_t * GetStreamOption() const
get/set option
Definition: DotConf.h:262
virtual void AddConf(DotConf *)
add a conf object to this. The added objects are not owned by this so not deleted.
Definition: DotConf.cpp:222
LogMessage & dolog(LogMessage &)
Bool_t AddRecord(std::ostream &, char t= 'b')
Add a begin or end of record, return true if done.
Definition: DotConf.cpp:198