Jpp  15.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSingleFileScanner.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JSINGLEFILESCANNER__
2 #define __JSUPPORT__JSINGLEFILESCANNER__
3 
4 #include <string>
5 #include <limits>
6 
8 #include "JLang/JTypeList.hh"
9 #include "JLang/JNullType.hh"
10 #include "JLang/JMultiEquals.hh"
11 #include "JLang/JException.hh"
12 
13 #include "JSupport/JLimit.hh"
14 #include "JSupport/JFileScanner.hh"
15 
16 
17 /**
18  * \file
19  * Scanning of objects from a single file according a format that follows from the extension of each file name.
20  * \author mdejong
21  */
22 namespace JSUPPORT {}
23 namespace JPP { using namespace JSUPPORT; }
24 
25 namespace JSUPPORT {
26 
28  using JLANG::JTypeList;
29  using JLANG::JTYPELIST;
30  using JLANG::JNullType;
31  using JLANG::JMultiEquals;
32  using JLANG::skip_type;
33  using JLANG::JException;
34 
35 
36  /**
37  * Auxiliary base class for file name.
38  */
40  public std::string
41  {
42  /**
43  * Default constructor.
44  */
46  {}
47 
48 
49  /**
50  * Constructor.
51  *
52  * \param file_name file name
53  */
54  JSingleFileScanner_t(const std::string& file_name) :
55  std::string(file_name)
56  {}
57 
58 
59  /**
60  * Get file name.
61  *
62  * \return file name
63  */
65  {
66  return static_cast<const JSingleFileScanner_t&>(*this);
67  }
68  };
69 
70 
71  /**
72  * Object reading from a list of files.
73  */
74  template<class T = JNullType>
76 
77 
78  /**
79  * Template specialisation of JSingleFileScanner for undefined type.\n
80  * This specialisation is used as a virtual base class for all implementations of JSingleFileScanner for defined type(s).
81  *
82  * This class is a simple container for a single file name and has an additional counter limit by extending from JSUPPORT::JLimit.\n
83  * This counter limit will be used to limit the number of calls to the method <tt>next</tt> of the derived class.\n
84  * Note that the counter limit can only be set via the member or friend methods of JSUPPORT::JLimit.
85  */
86  template<>
88  public JSingleFileScanner_t,
89  public JLimit,
90  public JMultiEquals<JSingleFileScanner<JNullType>,
91  JTYPELIST<JSingleFileScanner_t, JLimit>::typelist>
92  {
93  public:
94 
96 
97  /**
98  * Default constructor.
99  */
101  {}
102 
103 
104  /**
105  * Virtual destructor.
106  */
108  {}
109 
110 
111  /**
112  * Configure.
113  *
114  * \param input file name
115  * \param limit limit
116  */
117  void configure(const input_type& input, const JLimit& limit)
118  {
119  static_cast<JSingleFileScanner_t&>(*this) = input;
120 
121  this->setLimit(limit);
122  }
123 
124 
125  /**
126  * Read single file scanner from input.
127  *
128  * \param in input stream
129  * \param file_name single file scanner
130  * \return input stream
131  */
132  friend inline std::istream& operator>>(std::istream& in, JSingleFileScanner& file_name)
133  {
134  return in >> static_cast<std::string&>(file_name);
135  }
136 
137 
138  /**
139  * Write single file scanner to output.
140  *
141  * \param out output stream
142  * \param file_name single file scanner
143  * \return output stream
144  */
145  friend inline std::ostream& operator<<(std::ostream& out, const JSingleFileScanner& file_name)
146  {
147  return out << static_cast<const std::string&>(file_name);
148  }
149  };
150 
151 
152  /**
153  * Object reading from a single file name.
154  *
155  * This class implements the JLANG::JRewindableObjectIterator interface.
156  */
157  template<class T>
158  class JSingleFileScanner :
159  public virtual JSingleFileScanner<>,
160  public JRewindableObjectIterator<T>
161  {
162  public:
163 
166 
167  /**
168  * Default constructor.
169  */
171  counter(0)
172  {}
173 
174 
175  /**
176  * Copy constructor.
177  *
178  * Note that the counter limit is not copied and the counter is set to zero.
179  *
180  * \param input input
181  */
182  template<class JTypelist_t>
184  counter(0)
185  {
186  this->configure(input.getFilename(), JLimit());
187  }
188 
189 
190  /**
191  * Constructor.
192  *
193  * \param file_name file name
194  */
195  JSingleFileScanner(const input_type& file_name) :
196  counter(0)
197  {
198  this->configure(file_name, JLimit());
199  }
200 
201 
202  /**
203  * Constructor.
204  *
205  * \param file_name file name
206  * \param limit limit
207  */
208  JSingleFileScanner(const input_type& file_name, const JLimit& limit) :
209  counter(0)
210  {
211  this->configure(file_name, limit);
212  }
213 
214 
215  /**
216  * Get counter.
217  *
218  * \return counter
219  */
221  {
222  return counter;
223  }
224 
225 
226  /**
227  * Rewind.
228  */
229  virtual void rewind() override
230  {
231  if (scanner.is_open()) {
232  scanner.close();
233  }
234 
235  counter = 0;
236 
237  scanner.reset();
238  }
239 
240 
241  /**
242  * Check availability of next element.
243  *
244  * \return true if the iteration has more elements; else false
245  */
246  virtual bool hasNext() override
247  {
248  if (is_valid()) {
249 
250  if (counter < getUpperLimit()) {
251 
252  // first time around
253 
254  if (!scanner.is_open()) {
255  scanner.open(this->getFilename().c_str());
256  }
257 
258  if (counter < getLowerLimit()) {
259  counter += scanner.skip(getLowerLimit() - counter);
260  }
261 
262  if (!scanner.hasNext()) {
263 
264  scanner.close();
265 
266  return false;
267  }
268 
269  return true;
270 
271  } else {
272 
273  // last time around
274 
275  if (scanner.is_open()) {
276  scanner.close();
277  }
278 
279  scanner.reset();
280  }
281  }
282 
283  return false;
284  }
285 
286 
287  /**
288  * Get next element.
289  *
290  * \return pointer to element
291  */
292  virtual const pointer_type& next() override
293  {
294  ++counter;
295 
296  return scanner.next();
297  }
298 
299 
300  /**
301  * Skip items.
302  *
303  * \param ns number of items to skip
304  * \return number of items skipped
305  */
306  virtual skip_type skip(const skip_type ns) override
307  {
308  const skip_type previous = counter;
309 
310  counter += scanner.skip(ns);
311 
312  return counter - previous;
313  }
314 
315 
316  protected:
319  };
320 
321 
322  /**
323  * Template specialisation of JSingleFileScanner for single data types.
324  *
325  * This class implements the JLANG::JRewindableObjectIterator interface for
326  * each data type.
327  * The method rewind(), rewinds each object iterator.
328  */
329  template<class JHead_t, class JTail_t>
330  class JSingleFileScanner< JTypeList<JHead_t, JTail_t> > :
331  public virtual JSingleFileScanner<>,
332  public JSingleFileScanner<JHead_t>,
333  public JSingleFileScanner<JTail_t>,
334  public JRewindableObjectIterator< JTypeList<JHead_t, JTail_t> >
335  {
336  public:
337 
339 
342 
343 
344  /**
345  * Default constructor.
346  */
349  {}
350 
351 
352  /**
353  * Copy constructor.
354  *
355  * Note that the counter limit is not copied and the counter is set to zero.
356  *
357  * \param input input
358  */
359  template<class JTypelist_t>
361  {
362  this->configure(input.getFilename(), JLimit());
363  }
364 
365 
366  /**
367  * Constructor.
368  *
369  * \param file_name file name
370  */
371  JSingleFileScanner(const input_type& file_name)
372  {
373  this->configure(file_name, JLimit());
374  }
375 
376 
377  /**
378  * Constructor.
379  *
380  * \param file_name file name
381  * \param limit limit
382  */
383  JSingleFileScanner(const input_type& file_name, const JLimit& limit)
384  {
385  this->configure(file_name, limit);
386  }
387 
388 
389  /**
390  * Rewind.\n
391  * This method rewinds the JSingleFileScanner for each data type.
392  */
393  virtual void rewind() override
394  {
397  }
398 
399 
400  /**
401  * Skip items.
402  *
403  * \param ns number of items to skip
404  * \return number of items skipped
405  */
406  virtual skip_type skip(const skip_type ns) override
407  {
408  if (JSingleFileScanner<JHead_t>::skip(ns) == ns &&
410  return ns;
411  else
412  throw JException("JSingleFileScanner::skip(): inconsistent number of items skipped.");
413  }
414  };
415 
416 
417  /**
418  * Terminator class of recursive JSingleFileScanner class.
419  */
420  template<class JHead_t>
422  public JSingleFileScanner<JHead_t>
423  {
424  public:
425 
427 
428  /**
429  * Default constructor.
430  */
432  JSingleFileScanner<JHead_t>()
433  {}
434 
435 
436  /**
437  * Copy constructor.
438  *
439  * Note that the counter limit is not copied and the counter is set to zero.
440  *
441  * \param input input
442  */
443  template<class JTypelist_t>
445  {
446  this->configure(input.getFilename(), JLimit());
447  }
448 
449 
450  /**
451  * Constructor.
452  *
453  * \param file_name file name
454  */
455  JSingleFileScanner(const input_type& file_name) :
456  JSingleFileScanner<JHead_t>()
457  {
458  this->configure(file_name, JLimit());
459  }
460 
461 
462  /**
463  * Constructor.
464  *
465  * \param file_name file name
466  * \param limit limit
467  */
468  JSingleFileScanner(const input_type& file_name, const JLimit& limit) :
469  JSingleFileScanner<JHead_t>()
470  {
471  this->configure(file_name, limit);
472  }
473  };
474 }
475 
476 #endif
virtual skip_type skip(const skip_type ns) override
Skip items.
General exception.
Definition: JException.hh:23
Exceptions.
JSingleFileScanner(const input_type &file_name)
Constructor.
Interface for object iteration with rewinding.
JRewindableObjectIterator< T >::pointer_type pointer_type
virtual ~JSingleFileScanner()
Virtual destructor.
JSingleFileScanner_t()
Default constructor.
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
unsigned int skip_type
Type definition for number of objects to skip.
JSingleFileScanner(const input_type &file_name)
Constructor.
virtual void rewind() override
Rewind.
virtual const pointer_type & next() override
Get next element.
Long64_t counter_type
Type definition for counter.
JSingleFileScanner(const input_type &file_name)
Constructor.
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
Template definition of auxiliary base class for data structures composed of multiple base classes wit...
Definition: JMultiEquals.hh:32
Auxiliary base class for file name.
Type list.
Definition: JTypeList.hh:22
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
bool is_valid(const json &js)
Check validity of JSon data.
JSingleFileScanner_t(const std::string &file_name)
Constructor.
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JSingleFileScanner(const input_type &file_name, const JLimit &limit)
Constructor.
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
JSingleFileScanner()
Default constructor.
JSingleFileScanner(const input_type &file_name, const JLimit &limit)
Constructor.
counter_type getCounter() const
Get counter.
virtual skip_type skip(const skip_type ns) override
Skip items.
void configure(const input_type &input, const JLimit &limit)
Configure.
const JSingleFileScanner_t & getFilename() const
Get file name.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
virtual bool hasNext() override
Check availability of next element.
JSingleFileScanner(const input_type &file_name, const JLimit &limit)
Constructor.
Object reading from file.
Definition: JFileScanner.hh:36
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:88
Object reading from a list of files.
friend std::istream & operator>>(std::istream &in, JSingleFileScanner &file_name)
Read single file scanner from input.
Auxiliaries for defining the range of iterations of objects.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
JSingleFileScanner::input_type input_type
friend std::ostream & operator<<(std::ostream &out, const JSingleFileScanner &file_name)
Write single file scanner to output.