Libosmium  2.13.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-2017 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 <cstddef>
37 #include <iostream>
38 
39 namespace osmium {
40 
45  class ProgressBar {
46 
47  static const char* bar() noexcept {
48  return "======================================================================";
49  }
50 
51  static const char* spc() noexcept {
52  return " ";
53  }
54 
55  static constexpr const std::size_t length = 70;
56 
57  // The max size is the file size if there is a single file and the
58  // sum of all file sizes if there are multiple files. It corresponds
59  // to 100%.
60  std::size_t m_max_size;
61 
62  // The sum of the file sizes already done.
63  std::size_t m_done_size = 0;
64 
65  // The currently read size in the current file.
66  std::size_t m_current_size = 0;
67 
68  // The percentage calculated when it was last displayed. Used to decide
69  // whether we need to update the display. Start setting is one that
70  // will always be different from any legal setting.
71  std::size_t m_prev_percent = 100 + 1;
72 
73  // Is the progress bar enabled at all?
74  bool m_enable;
75 
76  // Used to make sure we do cleanup in the destructor if it was not
77  // already done.
78  bool m_do_cleanup = true;
79 
80  void display() {
81  const std::size_t percent = 100 * (m_done_size + m_current_size) / m_max_size;
82  if (m_prev_percent == percent) {
83  return;
84  }
85  m_prev_percent = percent;
86 
87  const auto num = static_cast<std::size_t>(percent * (length / 100.0));
88  std::cerr << '[';
89  if (num >= length) {
90  std::cerr << bar();
91  } else {
92  std::cerr << (bar() + length - num) << '>' << (spc() + num);
93  }
94  std::cerr << "] ";
95  if (percent < 10) {
96  std::cerr << ' ';
97  }
98  if (percent < 100) {
99  std::cerr << ' ';
100  }
101  std::cerr << percent << "% \r";
102  }
103 
104  public:
105 
113  ProgressBar(std::size_t max_size, bool enable) noexcept :
114  m_max_size(max_size),
115  m_enable(max_size > 0 && enable) {
116  }
117 
119  if (m_do_cleanup) {
120  try {
121  done();
122  } catch (...) {
123  // Swallow any exceptions, because a destructor should
124  // not throw.
125  }
126  }
127  }
128 
137  void update(std::size_t current_size) {
138  if (!m_enable) {
139  return;
140  }
141 
142  m_current_size = current_size;
143 
144  display();
145  }
146 
153  void file_done(std::size_t file_size) {
154  if (m_enable) {
155  m_done_size += file_size;
156  m_current_size = 0;
157  display();
158  }
159  }
160 
166  void done() {
167  m_do_cleanup = false;
168  if (m_enable) {
169  m_done_size = m_max_size;
170  m_current_size = 0;
171  display();
172  std::cerr << '\n';
173  }
174  }
175 
181  void remove() {
182  if (m_enable) {
183  std::cerr << spc() << " \r";
184  m_prev_percent = 100 + 1;
185  }
186  }
187 
188  }; // class ProgressBar
189 
190 } // namespace osmium
191 
192 #endif // OSMIUM_UTIL_PROGRESS_BAR_HPP
ProgressBar(std::size_t max_size, bool enable) noexcept
Definition: progress_bar.hpp:113
std::size_t file_size(int fd)
Definition: file.hpp:70
void update(std::size_t current_size)
Definition: progress_bar.hpp:137
void done()
Definition: progress_bar.hpp:166
std::size_t m_max_size
Definition: progress_bar.hpp:60
std::size_t m_done_size
Definition: progress_bar.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
~ProgressBar()
Definition: progress_bar.hpp:118
bool m_enable
Definition: progress_bar.hpp:74
Definition: progress_bar.hpp:45
static constexpr const std::size_t length
Definition: progress_bar.hpp:55
void file_done(std::size_t file_size)
Definition: progress_bar.hpp:153
static const char * spc() noexcept
Definition: progress_bar.hpp:51
static const char * bar() noexcept
Definition: progress_bar.hpp:47
bool m_do_cleanup
Definition: progress_bar.hpp:78
std::size_t m_prev_percent
Definition: progress_bar.hpp:71
void display()
Definition: progress_bar.hpp:80
std::size_t m_current_size
Definition: progress_bar.hpp:66