@@ -204,58 +204,82 @@ prepare_input_files(std::vector<std::string> const &input_files,
204204 return files;
205205}
206206
207- static osmium::item_type apply (osmium::OSMObject &object, osmdata_t &osmdata,
208- progress_display_t &progress)
207+ class input_context_t
209208{
210- static osmium::item_type last_type = osmium::item_type::node;
209+ public:
210+ input_context_t (osmdata_t &osmdata, progress_display_t &progress,
211+ bool append)
212+ : m_osmdata(osmdata), m_progress(progress), m_append(append)
213+ {}
211214
212- if (last_type != object.type ()) {
213- if (last_type == osmium::item_type::node) {
214- osmdata.after_nodes ();
215- progress.start_way_counter ();
215+ void apply (osmium::OSMObject &object)
216+ {
217+ if (!m_append && object.deleted ()) {
218+ throw std::runtime_error{" Input file contains deleted objects but "
219+ " you are not in append mode." };
216220 }
217- if (object.type () == osmium::item_type::relation) {
218- osmdata.after_ways ();
219- progress.start_relation_counter ();
221+
222+ if (m_last_type != object.type ()) {
223+ if (m_last_type == osmium::item_type::node) {
224+ m_osmdata.after_nodes ();
225+ m_progress.start_way_counter ();
226+ }
227+ if (object.type () == osmium::item_type::relation) {
228+ m_osmdata.after_ways ();
229+ m_progress.start_relation_counter ();
230+ }
231+ m_last_type = object.type ();
220232 }
221- last_type = object.type ();
233+
234+ osmium::apply_item (object, m_osmdata, m_progress);
222235 }
223236
224- osmium::apply_item (object, osmdata, progress);
237+ void eof ()
238+ {
239+ switch (m_last_type) {
240+ case osmium::item_type::node:
241+ m_osmdata.after_nodes ();
242+ // fallthrough
243+ case osmium::item_type::way:
244+ m_osmdata.after_ways ();
245+ break ;
246+ default :
247+ break ;
248+ }
225249
226- return last_type;
227- }
250+ m_osmdata.after_relations ();
251+ m_progress.print_summary ();
252+ }
228253
229- static osmium::item_type process_single_file (osmium::io::File const &file,
230- osmdata_t &osmdata,
231- progress_display_t &progress,
232- bool append)
254+ private:
255+ osmdata_t &m_osmdata;
256+ progress_display_t &m_progress;
257+ osmium::item_type m_last_type = osmium::item_type::node;
258+ bool m_append;
259+ }; // class input_context_t
260+
261+ static void process_single_file (osmium::io::File const &file,
262+ osmdata_t &osmdata,
263+ progress_display_t &progress, bool append)
233264{
234265 osmium::io::Reader reader{file};
235266 type_id_version last{osmium::item_type::node, 0 , 0 };
236267
237- osmium::item_type last_type = osmium::item_type::node ;
268+ input_context_t ctx{osmdata, progress, append} ;
238269 while (osmium::memory::Buffer buffer = reader.read ()) {
239270 for (auto &object : buffer.select <osmium::OSMObject>()) {
240271 last = check_input (last, object);
241- if (!append && object.deleted ()) {
242- throw std::runtime_error{
243- " Input file contains deleted objects but "
244- " you are not in append mode." };
245- }
246- last_type = apply (object, osmdata, progress);
272+ ctx.apply (object);
247273 }
248274 }
275+ ctx.eof ();
249276
250277 reader.close ();
251-
252- return last_type;
253278}
254279
255- static osmium::item_type
256- process_multiple_files (std::vector<osmium::io::File> const &files,
257- osmdata_t &osmdata, progress_display_t &progress,
258- bool append)
280+ static void process_multiple_files (std::vector<osmium::io::File> const &files,
281+ osmdata_t &osmdata,
282+ progress_display_t &progress, bool append)
259283{
260284 std::vector<data_source_t > data_sources;
261285 data_sources.reserve (files.size ());
@@ -270,53 +294,34 @@ process_multiple_files(std::vector<osmium::io::File> const &files,
270294 }
271295 }
272296
273- osmium::item_type last_type = osmium::item_type::node ;
297+ input_context_t ctx{osmdata, progress, append} ;
274298 while (!queue.empty ()) {
275299 auto element = queue.top ();
276300 queue.pop ();
277301 if (queue.empty () || element != queue.top ()) {
278- if (!append && element.object ().deleted ()) {
279- throw std::runtime_error{
280- " Input file contains deleted objects but "
281- " you are not in append mode." };
282- }
283- last_type = apply (element.object (), osmdata, progress);
302+ ctx.apply (element.object ());
284303 }
285304
286305 auto *source = element.data_source ();
287306 if (source->next ()) {
288307 queue.emplace (source->get (), source);
289308 }
290309 }
310+ ctx.eof ();
291311
292312 for (auto &data_source : data_sources) {
293313 data_source.close ();
294314 }
295-
296- return last_type;
297315}
298316
299317void process_files (std::vector<osmium::io::File> const &files,
300318 osmdata_t &osmdata, bool append, bool show_progress)
301319{
302320 progress_display_t progress{show_progress};
303321
304- auto const last_type =
305- (files.size () == 1 )
306- ? process_single_file (files.front (), osmdata, progress, append)
307- : process_multiple_files (files, osmdata, progress, append);
308-
309- switch (last_type) {
310- case osmium::item_type::node:
311- osmdata.after_nodes ();
312- // fallthrough
313- case osmium::item_type::way:
314- osmdata.after_ways ();
315- break ;
316- default :
317- break ;
322+ if (files.size () == 1 ) {
323+ process_single_file (files.front (), osmdata, progress, append);
324+ } else {
325+ process_multiple_files (files, osmdata, progress, append);
318326 }
319-
320- osmdata.after_relations ();
321- progress.print_summary ();
322327}
0 commit comments