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