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