source: trunk/yat/ColumnStream.cc @ 1556

Last change on this file since 1556 was 1556, checked in by Peter Johansson, 9 years ago

update to latest yat

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// $Id: ColumnStream.cc 2919 2012-12-19 06:54:23Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010, 2011, 2012 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    while(count<width_[column]){
59      os_ << ' ';
60      ++count;
61    }
62  }
63
64
65  void ColumnStream::flush(void)
66  {
67    bool empty=false;
68    while(!empty) {
69      empty=true;
70      for (size_t i=0; i<columns(); ++i){
71        if (writeline(i))
72          empty=false;
73      }
74      os_ << '\n';
75    }
76    for (size_t i=0; i<columns(); ++i)
77      buffer_[i]->clear(std::ios::goodbit);
78  }
79
80
81  size_t& ColumnStream::margin(size_t c) 
82  { 
83    return margins_[c]; 
84  }
85
86
87  void ColumnStream::next_column(void)
88  {
89    ++activated_;
90    if (activated_>=columns()) {
91      flush();
92    }
93  }
94
95  void ColumnStream::print(std::stringstream& ss)
96  {
97    assert(buffer_[activated_]);
98    assert(activated_<buffer_.size());
99    char c;
100    ss.get(c);
101    while(ss.good()) {
102      if (c=='\t'){
103        //*(buffer_[activated_]) << ' ';
104        next_column();
105      }
106      else if (c=='\n'){
107        //*(buffer_[activated_]) << ' ';
108        flush();
109        activated_=0;
110      }
111      else 
112        *(buffer_[activated_]) << c;
113    ss.get(c);
114    }
115  }
116
117
118  size_t& ColumnStream::width(size_t c) 
119  { 
120    return width_[c]; 
121  }
122
123
124  bool ColumnStream::writeline(size_t column)
125  {
126    assert(column<columns());
127    for (size_t i=0; i<margins_[column]; ++i)
128      os_ << ' ';
129    size_t count=0;
130    std::string word;
131    char c;
132    while (buffer_[column]->good()) {
133      buffer_[column]->get(c);
134      assert(c!='\t');
135      assert(c!='\n');
136      if (buffer_[column]->good())
137        word += c;
138      if (c==' ' || !buffer_[column]->good()) {
139        if (count+word.size()<=width_[column]) {
140          os_ << word;
141          count += word.size();
142          word = "";
143        }       
144        else {
145          if (!buffer_[column]->good())
146            buffer_[column]->clear(std::ios::goodbit);
147         
148          // if line is empty and word is longer than column width, we
149          // have to split the word
150          if (!count) {
151            os_ << word.substr(0,width_[column]);
152            for (std::string::reverse_iterator i=word.rbegin();
153                 i!=word.rbegin()+(word.size()-width_[column]); ++i)
154              buffer_[column]->putback(*i);
155          }
156          else {
157            for (std::string::reverse_iterator i=word.rbegin();
158                 i!=word.rend(); ++i)
159              buffer_[column]->putback(*i);
160            fill(column, count);
161          }
162          return true;
163        }
164      }
165
166    } 
167    fill(column, count);
168    return false;
169  }
170
171
172}}} // end of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.