Libosmium  2.10.0
Fast and flexible C++ library for working with OpenStreetMap data
progress_bar.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_PROGRESS_BAR_HPP
2 #define OSMIUM_UTIL_PROGRESS_BAR_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <iostream>
37 
38 namespace osmium {
39 
44  class ProgressBar {
45 
46  static const char* bar() noexcept {
47  return "======================================================================";
48  }
49 
50  static const char* spc() noexcept {
51  return " ";
52  }
53 
54  static constexpr const size_t length = 70;
55 
56  // The max size is the file size if there is a single file and the
57  // sum of all file sizes if there are multiple files. It corresponds
58  // to 100%.
59  size_t m_max_size;
60 
61  // The sum of the file sizes already done.
62  size_t m_done_size = 0;
63 
64  // The currently read size in the current file.
65  size_t m_current_size = 0;
66 
67  // The percentage calculated when it was last displayed. Used to decide
68  // whether we need to update the display. Start setting is one that
69  // will always be different from any legal setting.
70  size_t m_prev_percent = 100 + 1;
71 
72  // Is the progress bar enabled at all?
73  bool m_enable;
74 
75  // Used to make sure we do cleanup in the destructor if it was not
76  // already done.
77  bool m_do_cleanup = true;
78 
79  void display() {
80  const size_t percent = 100 * (m_done_size + m_current_size) / m_max_size;
81  if (m_prev_percent == percent) {
82  return;
83  }
84  m_prev_percent = percent;
85 
86  const size_t num = size_t(percent * (length / 100.0));
87  std::cerr << '[';
88  if (num >= length) {
89  std::cerr << bar();
90  } else {
91  std::cerr << (bar() + length - num) << '>' << (spc() + num);
92  }
93  std::cerr << "] ";
94  if (percent < 10) {
95  std::cerr << ' ';
96  }
97  if (percent < 100) {
98  std::cerr << ' ';
99  }
100  std::cerr << percent << "% \r";
101  }
102 
103  public:
104 
112  ProgressBar(size_t max_size, bool enable) noexcept :
113  m_max_size(max_size),
114  m_enable(max_size > 0 && enable) {
115  }
116 
118  if (m_do_cleanup) {
119  try {
120  done();
121  } catch (...) {
122  // Swallow any exceptions, because a destructor should
123  // not throw.
124  }
125  }
126  }
127 
136  void update(size_t current_size) {
137  if (!m_enable) {
138  return;
139  }
140 
141  m_current_size = current_size;
142 
143  display();
144  }
145 
152  void file_done(size_t file_size) {
153  if (m_enable) {
154  m_done_size += file_size;
155  m_current_size = 0;
156  display();
157  }
158  }
159 
165  void done() {
166  m_do_cleanup = false;
167  if (m_enable) {
168  m_done_size = m_max_size;
169  m_current_size = 0;
170  display();
171  std::cerr << '\n';
172  }
173  }
174 
180  void remove() {
181  if (m_enable) {
182  std::cerr << spc() << " \r";
183  m_prev_percent = 100 + 1;
184  }
185  }
186 
187  }; // class ProgressBar
188 
189 } // namespace osmium
190 
191 #endif // OSMIUM_UTIL_PROGRESS_BAR_HPP
size_t file_size(int fd)
Definition: file.hpp:67
size_t m_prev_percent
Definition: progress_bar.hpp:70
void update(size_t current_size)
Definition: progress_bar.hpp:136
void done()
Definition: progress_bar.hpp:165
size_t m_max_size
Definition: progress_bar.hpp:59
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
~ProgressBar()
Definition: progress_bar.hpp:117
bool m_enable
Definition: progress_bar.hpp:73
size_t m_current_size
Definition: progress_bar.hpp:65
ProgressBar(size_t max_size, bool enable) noexcept
Definition: progress_bar.hpp:112
Definition: progress_bar.hpp:44
static const char * spc() noexcept
Definition: progress_bar.hpp:50
static const char * bar() noexcept
Definition: progress_bar.hpp:46
static constexpr const size_t length
Definition: progress_bar.hpp:54
size_t m_done_size
Definition: progress_bar.hpp:62
bool m_do_cleanup
Definition: progress_bar.hpp:77
void display()
Definition: progress_bar.hpp:79
void file_done(size_t file_size)
Definition: progress_bar.hpp:152