source: trunk/yat/ColumnStream.cc @ 1397

Last change on this file since 1397 was 1397, checked in by Peter Johansson, 12 years ago

latest yat

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// $Id: ColumnStream.cc 2526 2011-07-25 02:03:35Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010, 2011 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 "ColumnStream.h"
24
25#include <cassert>
26#include <ostream>
27
28namespace theplu{
29namespace yat{
30namespace utility{
31
32  ColumnStream::ColumnStream(std::ostream& os, size_t columns)
33    : activated_(0), margins_(columns), os_(os), width_(columns, 8)
34  {
35    buffer_.reserve(columns);
36    while (buffer_.size()<columns)
37      buffer_.push_back(new std::stringstream);
38  }
39
40
41  ColumnStream::~ColumnStream(void)
42  {
43    for (size_t i=0; i<buffer_.size(); ++i)
44      delete buffer_[i];
45  }
46
47
48  size_t ColumnStream::columns(void) const
49  {
50    return buffer_.size();
51  }
52
53
54  void ColumnStream::fill(size_t column, size_t count)
55  {
56    while(count<width_[column]){
57      os_ << ' ';
58      ++count;
59    }
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    } 
165    fill(column, count);
166    return false;
167  }
168
169
170}}} // end of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.