1 // Copyright 2012 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 /// \file utils/text/table.hpp
30 /// Table construction and formatting.
31 
32 #if !defined(UTILS_TEXT_TABLE_HPP)
33 #define UTILS_TEXT_TABLE_HPP
34 
35 #include <cstddef>
36 #include <string>
37 #include <vector>
38 
39 namespace utils {
40 namespace text {
41 
42 
43 /// Values of the cells of a particular table row.
44 typedef std::vector< std::string > table_row;
45 
46 
47 /// Vector of column widths.
48 typedef std::vector< std::size_t > widths_vector;
49 
50 
51 /// Representation of a table.
52 ///
53 /// A table is nothing more than a matrix of rows by columns.  The number of
54 /// columns is hardcoded at construction times, and the rows can be accumulated
55 /// at a later stage.
56 ///
57 /// The only value of this class is a simpler and more natural mechanism of the
58 /// construction of a table, with additional sanity checks.  We could as well
59 /// just expose the internal data representation to our users.
60 class table {
61     /// Widths of the table columns so far.
62     widths_vector _column_widths;
63 
64     /// Type defining the collection of rows in the table.
65     typedef std::vector< table_row > rows_vector;
66 
67     /// The rows of the table.
68     ///
69     /// This is actually the matrix representing the table.  Every element of
70     /// this vector (which are vectors themselves) must have _ncolumns items.
71     rows_vector _rows;
72 
73 public:
74     table(const table_row::size_type);
75 
76     widths_vector::size_type ncolumns(void) const;
77     widths_vector::value_type column_width(const widths_vector::size_type)
78         const;
79     const widths_vector& column_widths(void) const;
80 
81     void add_row(const table_row&);
82 
83     bool empty(void) const;
84 
85     /// Constant iterator on the rows of the table.
86     typedef rows_vector::const_iterator const_iterator;
87 
88     const_iterator begin(void) const;
89     const_iterator end(void) const;
90 };
91 
92 
93 /// Settings to format a table.
94 ///
95 /// This class implements a builder pattern to construct an object that contains
96 /// all the knowledge to format a table.  Once all the settings have been set,
97 /// the format() method provides the algorithm to apply such formatting settings
98 /// to any input table.
99 class table_formatter {
100     /// Text to use as the separator between cells.
101     std::string _separator;
102 
103     /// Colletion of widths of the columns of a table.
104     std::size_t _table_width;
105 
106     /// Widths of the table columns.
107     ///
108     /// Note that this only includes widths for the column widths explicitly
109     /// overriden by the caller.  In other words, this vector can be shorter
110     /// than the table passed to the format() method, which is just fine.  Any
111     /// non-specified column widths are assumed to be width_auto.
112     widths_vector _column_widths;
113 
114 public:
115     table_formatter(void);
116 
117     static const std::size_t width_auto;
118     static const std::size_t width_refill;
119     table_formatter& set_column_width(const table_row::size_type,
120                                       const std::size_t);
121     table_formatter& set_separator(const char*);
122     table_formatter& set_table_width(const std::size_t);
123 
124     std::vector< std::string > format(const table&) const;
125 };
126 
127 
128 }  // namespace text
129 }  // namespace utils
130 
131 #endif  // !defined(UTILS_TEXT_TABLE_HPP)
132