diff --git a/Makefile.in b/Makefile.in index 0b47826..7c02a7f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -31,6 +31,7 @@ SOURCE=\ base/endian_utils.cc \ base/error_state.cc \ base/error_string.cc \ + base/grid_layout.cc \ base/progress_monitor.cc \ base/xml_utils.cc \ block-cache/block_cache.cc \ diff --git a/base/grid_layout.cc b/base/grid_layout.cc new file mode 100644 index 0000000..5ba12b4 --- /dev/null +++ b/base/grid_layout.cc @@ -0,0 +1,82 @@ +#include "base/grid_layout.h" + +#include + +using namespace base; +using namespace std; + +//---------------------------------------------------------------- + +grid_layout::grid_layout() + : nr_fields_(0) +{ + new_row(); +} + +void +grid_layout::render(ostream &out) const +{ + vector widths; + calc_field_widths(widths); + + grid::const_iterator row; + for (row = grid_.begin(); row != grid_.end(); ++row) { + row::const_iterator col; + unsigned i; + for (col = row->begin(), i = 0; col != row->end(); ++col, ++i) + out << justify(widths[i], *col) << " "; + out << "\n"; + } +} + +void +grid_layout::new_row() +{ + grid_.push_back(row()); +} + +grid_layout::row const & +grid_layout::current_row() const +{ + return grid_.back(); +} + +grid_layout::row & +grid_layout::current_row() +{ + return grid_.back(); +} + +void +grid_layout::push_field(string const &s) +{ + current_row().push_back(s); + nr_fields_ = max(nr_fields_, current_row().size()); +} + +void +grid_layout::calc_field_widths(vector &widths) const +{ + widths.resize(nr_fields_, 0); + + grid::const_iterator row; + for (row = grid_.begin(); row != grid_.end(); ++row) { + row::const_iterator col; + unsigned i; + for (col = row->begin(), i = 0; col != row->end(); ++col, ++i) + widths[i] = max(widths[i], col->length()); + } +} + +string +grid_layout::justify(unsigned width, string const &txt) const +{ + if (txt.length() > width) + throw runtime_error("string field too long, internal error"); + + string result(width - txt.length(), ' '); + result += txt; + return result; +} + +//---------------------------------------------------------------- diff --git a/base/grid_layout.h b/base/grid_layout.h new file mode 100644 index 0000000..7e3ad5b --- /dev/null +++ b/base/grid_layout.h @@ -0,0 +1,41 @@ +#ifndef BASE_GRID_LAYOUT_H +#define BASE_GRID_LAYOUT_H + +#include +#include +#include +#include +#include + +//---------------------------------------------------------------- + +namespace base { + class grid_layout { + public: + typedef std::list row; + typedef std::list grid; + + grid_layout(); + void render(std::ostream &out) const; + void new_row(); + + template + void field(T const &t) { + push_field(boost::lexical_cast(t)); + } + + private: + row ¤t_row(); + row const ¤t_row() const; + void push_field(std::string const &s); + void calc_field_widths(std::vector &widths) const; + std::string justify(unsigned width, std::string const &txt) const; + + grid grid_; + unsigned nr_fields_; + }; +} + +//---------------------------------------------------------------- + +#endif diff --git a/thin-provisioning/thin_ls.cc b/thin-provisioning/thin_ls.cc index be34fca..8a104c1 100644 --- a/thin-provisioning/thin_ls.cc +++ b/thin-provisioning/thin_ls.cc @@ -22,6 +22,7 @@ #include #include "base/disk_units.h" +#include "base/grid_layout.h" #include "boost/lexical_cast.hpp" #include "boost/optional.hpp" #include "boost/range.hpp" @@ -43,77 +44,6 @@ using namespace thin_provisioning; //---------------------------------------------------------------- namespace { - // FIXME: move to own file - class grid_layout { - public: - typedef list row; - typedef list grid; - - grid_layout() - : nr_fields_(0) { - new_row(); - } - - void render(ostream &out) { - vector widths; - calc_field_widths(widths); - - grid::const_iterator row; - for (row = grid_.begin(); row != grid_.end(); ++row) { - row::const_iterator col; - unsigned i; - for (col = row->begin(), i = 0; col != row->end(); ++col, ++i) - out << justify(widths[i], *col) << " "; - out << "\n"; - } - } - - void new_row() { - grid_.push_back(row()); - } - - template - void field(T const &t) { - push_field(lexical_cast(t)); - } - - private: - row ¤t_row() { - return grid_.back(); - } - - void push_field(string const &s) { - current_row().push_back(s); - nr_fields_ = max(nr_fields_, current_row().size()); - } - - void calc_field_widths(vector &widths) const { - widths.resize(nr_fields_, 0); - - grid::const_iterator row; - for (row = grid_.begin(); row != grid_.end(); ++row) { - row::const_iterator col; - unsigned i; - for (col = row->begin(), i = 0; col != row->end(); ++col, ++i) - widths[i] = max(widths[i], col->length()); - } - } - - string justify(unsigned width, string const &txt) const { - if (txt.length() > width) - throw runtime_error("string field too long, internal error"); - - string result(width - txt.length(), ' '); - result += txt; - return result; - } - - grid grid_; - unsigned nr_fields_; - }; - - //------------------------------------------------ - class mapping_set { public: mapping_set(block_address nr_blocks)