source: trunk/yat/utility/ColumnStream.cc

Last change on this file was 3330, checked in by Peter, 7 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: ColumnStream.cc 3330 2014-10-14 08:03:25Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010, 2011, 2012, 2014 Peter Johansson
6
7  This file is part of yat, http://dev.thep.lu.se/yat
8
9  yat is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  yat is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include <config.h>
24
25#include "ColumnStream.h"
26
27#include <cassert>
28#include <ostream>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  ColumnStream::ColumnStream(std::ostream& os, size_t columns)
35    : activated_(0), margins_(columns), os_(os), width_(columns, 8)
36  {
37    buffer_.reserve(columns);
38    while (buffer_.size()<columns)
39      buffer_.push_back(new std::stringstream);
40  }
41
42
43  ColumnStream::~ColumnStream(void)
44  {
45    for (size_t i=0; i<buffer_.size(); ++i)
46      delete buffer_[i];
47  }
48
49
50  size_t ColumnStream::columns(void) const
51  {
52    return buffer_.size();
53  }
54
55
56  void ColumnStream::fill(size_t column, size_t count)
57  {
58    if (count<width_[column])
59      os_ << std::string(width_[column]-count, ' ');
60  }
61
62
63  void ColumnStream::flush(void)
64  {
65    bool empty=false;
66    while(!empty) {
67      empty=true;
68      for (size_t i=0; i<columns(); ++i){
69        if (writeline(i))
70          empty=false;
71      }
72      os_ << '\n';
73    }
74    for (size_t i=0; i<columns(); ++i)
75      buffer_[i]->clear(std::ios::goodbit);
76  }
77
78
79  size_t& ColumnStream::margin(size_t c)
80  {
81    return margins_[c];
82  }
83
84
85  void ColumnStream::next_column(void)
86  {
87    ++activated_;
88    if (activated_>=columns())
89      flush();
90  }
91
92
93  void ColumnStream::print(std::stringstream& ss)
94  {
95    assert(buffer_[activated_]);
96    assert(activated_<buffer_.size());
97    char c;
98    ss.get(c);
99    while(ss.good()) {
100      if (c=='\t'){
101        //*(buffer_[activated_]) << ' ';
102        next_column();
103      }
104      else if (c=='\n'){
105        //*(buffer_[activated_]) << ' ';
106        flush();
107        activated_=0;
108      }
109      else
110        *(buffer_[activated_]) << c;
111      ss.get(c);
112    }
113  }
114
115
116  size_t& ColumnStream::width(size_t c)
117  {
118    return width_[c];
119  }
120
121
122  bool ColumnStream::writeline(size_t column)
123  {
124    assert(column<columns());
125    for (size_t i=0; i<margins_[column]; ++i)
126      os_ << ' ';
127    size_t count=0;
128    std::string word;
129    char c;
130    while (buffer_[column]->good()) {
131      buffer_[column]->get(c);
132      assert(c!='\t');
133      assert(c!='\n');
134      if (buffer_[column]->good())
135        word += c;
136      if (c==' ' || !buffer_[column]->good()) {
137        if (count+word.size()<=width_[column]) {
138          os_ << word;
139          count += word.size();
140          word = "";
141        }
142        else {
143          if (!buffer_[column]->good())
144            buffer_[column]->clear(std::ios::goodbit);
145
146          // if line is empty and word is longer than column width, we
147          // have to split the word
148          if (!count) {
149            os_ << word.substr(0,width_[column]);
150            for (std::string::reverse_iterator i=word.rbegin();
151                 i!=word.rbegin()+(word.size()-width_[column]); ++i)
152              buffer_[column]->putback(*i);
153          }
154          else {
155            for (std::string::reverse_iterator i=word.rbegin();
156                 i!=word.rend(); ++i)
157              buffer_[column]->putback(*i);
158            fill(column, count);
159          }
160          return true;
161        }
162      }
163    }
164    fill(column, count);
165    return false;
166  }
167
168}}} // end of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.