libdballe  7.29
processor.h
1 #ifndef DBALLE_CMDLINE_PROCESSOR_H
2 #define DBALLE_CMDLINE_PROCESSOR_H
3 
4 #include <dballe/msg/codec.h>
5 #include <stdexcept>
6 #include <list>
7 #include <string>
8 
9 #define DBALLE_JSON_VERSION "0.1"
10 
11 namespace wreport {
12 struct Bulletin;
13 }
14 
15 namespace dballe {
16 struct Query;
17 struct BinaryMessage;
18 struct Matcher;
19 
20 namespace cmdline {
21 
29 struct ProcessingException : public std::exception
30 {
31  std::string filename;
32  unsigned index;
33  std::string msg;
34 
43  const std::string& filename,
44  unsigned index,
45  const std::string& msg)
46  : filename(filename), index(index)
47  {
48  initmsg(filename, index, msg.c_str());
49  }
50 
60  const std::string& filename,
61  unsigned index,
62  const std::exception& original)
63  : filename(filename), index(index)
64  {
65  initmsg(filename, index, original.what());
66  }
67 
78  const std::string& filename,
79  unsigned index,
80  const std::string& msg,
81  const std::exception& original)
82  : filename(filename), index(index)
83  {
84  initmsg(filename, index, msg.c_str());
85  this->msg += ": ";
86  this->msg += original.what();
87  }
88 
89  virtual ~ProcessingException() throw() {}
90 
91  virtual const char* what() const throw ()
92  {
93  return msg.c_str();
94  }
95 
96 protected:
97  void initmsg(const std::string& fname, unsigned index, const char* msg);
98 };
99 
100 struct Item
101 {
102  unsigned idx;
103  BinaryMessage* rmsg;
104  wreport::Bulletin* bulletin;
105  Messages* msgs;
106 
107  Item();
108  ~Item();
109 
111  void decode(msg::Importer& imp, bool print_errors=false);
112 
114  void set_msgs(Messages* new_msgs);
115 
117  void processing_failed(std::exception& e) const __attribute__ ((noreturn));
118 };
119 
120 struct Action
121 {
122  virtual ~Action() {}
123  virtual bool operator()(const Item& item) = 0;
124 };
125 
127 {
128  std::vector<std::pair<int, int>> ranges;
129 
130  void parse(const std::string& str);
131 
132  bool match(int val) const;
133 };
134 
136 {
137  int category = -1;
138  int subcategory = -1;
139  int checkdigit = -1;
140  int unparsable = 0;
141  int parsable = 0;
142  const char* index_filter = nullptr;
143  const char* input_type = "auto";
144  const char* fail_file_name = nullptr;
145 };
146 
147 struct Filter
148 {
149  msg::Exporter::Options export_opts;
150  int category = -1;
151  int subcategory = -1;
152  int checkdigit = -1;
153  int unparsable = 0;
154  int parsable = 0;
155  IndexMatcher imatcher;
156  Matcher* matcher = nullptr;
157 
158  Filter();
159  Filter(const ReaderOptions& opts);
160  ~Filter();
161 
162  void set_index_filter(const std::string& val);
163 
165  void matcher_reset();
166 
168  void matcher_from_record(const Query& query);
169 
170  bool match_index(int idx) const;
171  bool match_common(const BinaryMessage& rmsg, const Messages* msgs) const;
172  bool match_msgs(const Messages& msgs) const;
173  bool match_bufrex(const BinaryMessage& rmsg, const wreport::Bulletin* rm, const Messages* msgs) const;
174  bool match_bufr(const BinaryMessage& rmsg, const wreport::Bulletin* rm, const Messages* msgs) const;
175  bool match_crex(const BinaryMessage& rmsg, const wreport::Bulletin* rm, const Messages* msgs) const;
176  bool match_aof(const BinaryMessage& rmsg, const Messages* msgs) const;
177  bool match_item(const Item& item) const;
178 };
179 
180 class Reader
181 {
182 protected:
183  std::string input_type;
184  const char* fail_file_name;
185 
186  void read_csv(const std::list<std::string>& fnames, Action& action);
187  void read_json(const std::list<std::string>& fnames, Action& action);
188  void read_file(const std::list<std::string>& fnames, Action& action);
189 
190 public:
191  msg::Importer::Options import_opts;
192  Filter filter;
193  bool verbose = false;
194  unsigned count_successes = 0;
195  unsigned count_failures = 0;
196 
197  Reader(const ReaderOptions& opts);
198 
199  bool has_fail_file() const;
200 
201  void read(const std::list<std::string>& fnames, Action& action);
202 };
203 
204 }
205 }
206 #endif
Definition: codec.h:108
Definition: processor.h:126
Definition: processor.h:120
void processing_failed(std::exception &e) const __attribute__((noreturn))
Throw a ProcessingException based on e.
Definition: codec.h:36
Definition: processor.h:180
General codec options.
Message importer.
Definition: codec.h:33
void matcher_reset()
Reset to the empty matcher.
ProcessingException(const std::string &filename, unsigned index, const std::string &msg, const std::exception &original)
Create a new exception.
Definition: processor.h:77
Definition: processor.h:147
Definition: processor.h:100
Match DB-All.e objects using the same queries that can be made on DB-All.e databases.
Definition: matcher.h:92
void decode(msg::Importer &imp, bool print_errors=false)
Decode all that can be decoded.
Binary message.
Definition: file.h:132
ProcessingException(const std::string &filename, unsigned index, const std::exception &original)
Create a new exception.
Definition: processor.h:59
Ordered collection of messages.
Definition: message.h:67
Query used to filter DB-All.e data.
Definition: query.h:14
Definition: processor.h:135
void matcher_from_record(const Query &query)
Initialise the matcher from a record.
Exception used to embed processing issues that mean that processing of the current element can safely...
Definition: processor.h:29
void set_msgs(Messages *new_msgs)
Set the value of msgs, possibly replacing the previous one.
ProcessingException(const std::string &filename, unsigned index, const std::string &msg)
Create a new exception.
Definition: processor.h:42