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