Frobby  0.9.5
ColumnPrinter.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2009 Bjarke Hammersholt Roune (www.broune.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "ColumnPrinter.h"
19 
20 #include "FrobbyStringStream.h"
21 
22 namespace {
23  size_t getLineWidth(const string& str, size_t pos) {
24  size_t startPos = pos;
25  while (pos < str.size() && str[pos] != '\n')
26  ++pos;
27  return pos - startPos;
28  }
29 
30  void printSpaces(ostream& out, size_t howMany) {
31  while (howMany > 0) {
32  out << ' ';
33  --howMany;
34  }
35  }
36 }
37 
38 ColumnPrinter::ColumnPrinter(size_t columnCount):
39  _cols(),
40  _colsDeleter(_cols) {
41  while (columnCount > 0) {
42  addColumn();
43  --columnCount;
44  }
45 }
46 
47 void ColumnPrinter::setPrefix(const string& prefix) {
48  _prefix = prefix;
49 }
50 
51 void ColumnPrinter::addColumn(bool flushLeft,
52  const string& prefix, const string& suffix) {
53  auto_ptr<Col> col(new Col());
54  col->prefix = prefix;
55  col->suffix = suffix;
56  col->flushLeft = flushLeft;
57 
59 }
60 
62  return _cols.size();
63 }
64 
65 ostream& ColumnPrinter::operator[](size_t col) {
66  ASSERT(col < getColumnCount());
67  return _cols[col]->text;
68 }
69 
70 void ColumnPrinter::print(ostream& out) const {
71  // Calculate the width of each column.
72  vector<size_t> widths(getColumnCount());
73  for (size_t col = 0; col < getColumnCount(); ++col) {
74  const string& text = _cols[col]->text.str();
75  size_t maxWidth = 0;
76 
77  size_t pos = 0;
78  while (pos < text.size()) {
79  size_t width = getLineWidth(text, pos);
80  if (width > maxWidth)
81  maxWidth = width;
82 
83  // We can't just increment pos unconditionally by width + 1, as
84  // that could result in an overflow.
85  pos += width;
86  if (text[pos] == '\n')
87  ++pos;
88  }
89  widths[col] = maxWidth;
90  }
91 
92  // Print each row
93  vector<size_t> poses(getColumnCount());
94  while (true) {
95  bool done = true;
96  for (size_t col = 0; col < getColumnCount(); ++col) {
97  if (poses[col] < _cols[col]->text.str().size()) {
98  done = false;
99  break;
100  }
101  }
102  if (done)
103  break;
104 
105  out << _prefix;
106  for (size_t col = 0; col < getColumnCount(); ++col) {
107  out << _cols[col]->prefix;
108 
109  const string& text = _cols[col]->text.str();
110  size_t& pos = poses[col];
111  size_t width = getLineWidth(text, pos);
112 
113  if (!_cols[col]->flushLeft)
114  printSpaces(out, widths[col] - width);
115 
116  while (pos < text.size()) {
117  if (text[pos] == '\n') {
118  ++pos;
119  break;
120  }
121  out << text[pos];
122  ++pos;
123  }
124 
125  if (_cols[col]->flushLeft)
126  printSpaces(out, widths[col] - width);
127 
128  out << _cols[col]->suffix;
129  }
130  out << '\n';
131  }
132 }
133 
135  ostringstream tmp;
136  print(tmp);
137  out << tmp.str().c_str();
138 }
139 
140 ostream& operator<<(ostream& out, const ColumnPrinter& printer) {
141  printer.print(out);
142  return out;
143 }
144 
145 void print(FILE* out, const ColumnPrinter& pr) {
146  ostringstream str;
147  str << pr;
148  fputs(str.str().c_str(), out);
149 }
void print(FILE *out, const ColumnPrinter &pr)
ostream & operator<<(ostream &out, const ColumnPrinter &printer)
void exceptionSafePushBack(Container &container, auto_ptr< Element > pointer)
ostream & operator[](size_t col)
void print(ostream &out) const
size_t getColumnCount() const
ColumnPrinter(size_t columnCount=0)
void addColumn(bool flushLeft=true, const string &prefix=" ", const string &suffix="")
vector< Col * > _cols
Definition: ColumnPrinter.h:48
void setPrefix(const string &prefix)
A replacement for stringstream.
#define ASSERT(X)
Definition: stdinc.h:86