source: trunk/yat/utility/ColumnStream.cc @ 1703

Last change on this file since 1703 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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