Jpp  19.1.0-rc.1
the software that should make you happy
JDatalogString.hh
Go to the documentation of this file.
1 #ifndef __JDB_JDATALOGSTRING__
2 #define __JDB_JDATALOGSTRING__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <sstream>
8 #include <fstream>
9 #include <iomanip>
10 #include <limits>
11 #include <vector>
12 #include <set>
13 #include <regex>
14 
15 #include "JLang/JException.hh"
16 #include "JLang/gzstream.h"
17 
18 #include "JSystem/JDateAndTime.hh"
19 
20 #include "Jeep/JeepToolkit.hh"
21 
22 
23 namespace JDB {}
24 namespace JPP { using namespace JDB; }
25 
26 namespace JDB {
27 
29 
30  /**
31  * Auxiliary data structure for datalog strings.
32  */
33  struct JDatalogString {
34 
35  static const char* const getName() { return "datalogstrings"; } //!< Table name
36 
37 
38  /**
39  * Get match.
40  *
41  * \param datalog datalog
42  * \return true if datalog matches; else false
43  */
44  bool match(const JDatalogString& datalog) const
45  {
46  using namespace std;
47 
48  if (!datalog.source .empty() && datalog.source != source) { return false; }
49  if (!datalog.parameter.empty() && datalog.parameter != parameter) { return false; }
50  if (!datalog.data .empty() && !regex_match(data, regex(datalog.data))) { return false; }
51 
52  return true;
53  }
54 
55 
56  /**
57  * Read datalog from input.
58  *
59  * \param in input stream
60  * \param object datalog
61  * \return input stream
62  */
63  friend inline std::istream& operator>>(std::istream& in, JDatalogString& object)
64  {
65  object = JDatalogString();
66 
67  in >> object.run
68  >> object.utc
69  >> object.source
70  >> object.parameter;
71 
72  while (in.peek() != EOF && isspace((char) in.peek())) { in.ignore(1); }
73 
74  return getline(in, object.data);
75  }
76 
77 
78  /**
79  * Write datalog to output.
80  *
81  * \param out output stream
82  * \param object datalog
83  * \return output stream
84  */
85  friend inline std::ostream& operator<<(std::ostream& out, const JDatalogString& object)
86  {
87  using namespace JPP;
88 
89  JDateAndTime utc(object.utc / 1000, 1.0e-3 * (float) (object.utc%1000) , true);
90 
91  //out << object.run << ' ';
92  out << object.utc << ' ';
93  out << utc << ' ';
94  out << object.source << ' ';
95  out << object.parameter << ' ';
96  out << object.data;
97 
98  return out;
99  }
100 
101  int run;
102  long int utc;
103  std::string source;
104  std::string parameter;
105  std::string data;
106  };
107 
108 
109  /**
110  * Auxiliary data structure for selecting data.
111  */
112  struct JKeywords :
113  public std::vector<std::string>
114  {
115  /**
116  * Constructor.
117  *
118  * \param buffer list of valid source names
119  */
121  std::vector<std::string>(buffer)
122  {}
123 
124 
125  /**
126  * Test validity of target.
127  * A target is valid if it contains one of the keywords.
128  *
129  * \param target target
130  * \return true if valid; else false
131  */
132  bool operator()(const std::string& target) const
133  {
134  for (const_iterator i = this->begin(); i != this->end(); ++i) {
135  if (target.find(*i) != std::string::npos) {
136  return true;
137  }
138  }
139 
140  return false;
141  }
142  };
143 
144 
145  /**
146  * Valid source names.
147  */
149  "3.4.3.2",
150  "Born",
151  "Died",
152  "LocalAuthenticationProvider",
153  "MasterControlProgram",
154  "MSG",
155  "RC_LOG",
156  "RC_REPLY",
157  "TriDASManager"
158  };
159 
160 
161  /**
162  * Ignored parameter names.
163  */
165  //"DispatcherPutMessage"
166  "DispatchMessage"
167  };
168 
169 
170  /**
171  * Container for datalog strings.
172  */
173  struct JDatalogs_t :
174  public std::vector<JDatalogString>
175  {
176  /**
177  * Constructor.
178  *
179  * \param buffer list of valid source names
180  */
182  sources(buffer)
183  {}
184 
185 
186  /**
187  * Constructor.
188  *
189  * \param buffer list of valid source names
190  */
192  sources(buffer)
193  {}
194 
195 
196  /**
197  * Load message from input file.
198  *
199  * \param file_name file name
200  */
201  void load(const std::string& file_name)
202  {
203  using namespace std;
204  using namespace JPP;
205 
206  if (getFilenameExtension(file_name) == "log" ||
207  getFilenameExtension(file_name) == "txt") {
208 
209  ifstream in(file_name.c_str());
210 
211  in.ignore(numeric_limits<std::streamsize>::max(), '\n');
212 
213  in >> *this;
214 
215  in.close();
216 
217  } else if (getFilenameExtension(file_name) == "gz") {
218 
219  igzstream in(file_name.c_str());
220 
221  in.ignore(numeric_limits<std::streamsize>::max(), '\n');
222 
223  in >> *this;
224 
225  in.close();
226 
227  } else {
228 
229  THROW(JFileOpenException, "Invalid file name extension " << file_name);
230  }
231  }
232 
233 
234  /**
235  * Read message from input.
236  *
237  * \param in input stream
238  * \param object messages
239  * \return input stream
240  */
241  friend inline std::istream& operator>>(std::istream& in, JDatalogs_t& object)
242  {
243  using namespace std;
244 
245  JDatalogString datalog;
246 
247  bool append = false;
248 
249  for (string buffer; getline(in, buffer); ) {
250 
251  istringstream is(buffer);
252 
253  if (is >> datalog.run >> datalog.utc >> datalog.source >> datalog.parameter) {
254 
255  append = false;
256 
257  if (SOURCE_NAMES(datalog.source) && !IGNORED_PARAMETER_NAMES(datalog.parameter)) {
258 
259  while (is.peek() != EOF && isspace((char) is.peek())) { is.ignore(1); }
260 
261  getline(is, datalog.data);
262 
263  // new datalog
264 
265  if (object.sources.empty() || object.sources.count(datalog.source) != 0) {
266 
267  object.push_back(datalog);
268 
269  append = true;
270  }
271  }
272 
273  } else if (append) {
274 
275  // append traling data to latest datalog
276 
277  if (!object.empty()) {
278  object.rbegin()->data.append(" " + buffer);
279  }
280  }
281  }
282  return in;
283  }
284 
285  protected:
287  };
288 }
289 
290 #endif
Date and time functions.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Auxiliary methods for handling file names, type names and environment.
Exception for opening of file.
Definition: JException.hh:360
void close()
Definition: gzstream.h:185
const JKeywords IGNORED_PARAMETER_NAMES
Ignored parameter names.
const JKeywords SOURCE_NAMES
Valid source names.
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e. part after last JEEP::FILENAME_SEPARATOR if any.
Definition: JeepToolkit.hh:109
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Target.
Definition: JHead.hh:300
Auxiliary data structure for datalog strings.
friend std::istream & operator>>(std::istream &in, JDatalogString &object)
Read datalog from input.
bool match(const JDatalogString &datalog) const
Get match.
static const char *const getName()
Table name.
friend std::ostream & operator<<(std::ostream &out, const JDatalogString &object)
Write datalog to output.
Container for datalog strings.
JDatalogs_t(const std::initializer_list< std::string > &buffer={})
Constructor.
std::set< std::string > sources
friend std::istream & operator>>(std::istream &in, JDatalogs_t &object)
Read message from input.
void load(const std::string &file_name)
Load message from input file.
JDatalogs_t(const std::set< std::string > &buffer)
Constructor.
Auxiliary data structure for selecting data.
JKeywords(const std::initializer_list< std::string > &buffer)
Constructor.
bool operator()(const std::string &target) const
Test validity of target.
Auxiliary class for date and time.
Definition: JDateAndTime.hh:80