source: branches/0.4-stable/yat/utility/ColumnStream.cc @ 1743

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

updating copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: ColumnStream.cc 1743 2009-01-23 14:20:30Z peter $
2
3/*
4  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest 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 2 of the License, or
12  (at your option) any later version.
13
14  svndigest 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "ColumnStream.h"
26
27#include <cassert>
28#include <iostream>
29
30namespace theplu{
31namespace yat{
32namespace utility{
33
34  ColumnStream::ColumnStream(std::ostream& os, size_t columns)
35    : activated_(0),os_(os)
36  {
37    margins_=std::vector<size_t>(columns); 
38    buffer_.reserve(columns);
39    while (buffer_.size()<columns)
40      buffer_.push_back(new std::stringstream);
41    width_=std::vector<size_t>(columns, 8); 
42  }
43
44
45  ColumnStream::~ColumnStream(void)
46  {
47    for (size_t i=0; i<buffer_.size(); ++i)
48      delete buffer_[i];
49  }
50
51
52  void ColumnStream::fill(size_t column, size_t count)
53  {
54    while(count<width_[column]){
55      os_ << ' ';
56      ++count;
57    }
58  }
59
60
61  void ColumnStream::flush(void)
62  {
63    bool empty=false;
64    while(!empty) {
65      empty=true;
66      for (size_t i=0; i<columns(); ++i){
67        if (writeline(i))
68          empty=false;
69      }
70      os_ << '\n';
71    }
72    for (size_t i=0; i<columns(); ++i)
73      buffer_[i]->clear(std::ios::goodbit);
74  }
75
76
77  size_t& ColumnStream::margin(size_t c) 
78  { 
79    return margins_[c]; 
80  }
81
82
83  void ColumnStream::next_column(void)
84  {
85    ++activated_;
86    if (activated_>=columns()) {
87      flush();
88    }
89  }
90
91  void ColumnStream::print(std::stringstream& ss)
92  {
93    assert(buffer_[activated_]);
94    assert(activated_<buffer_.size());
95    char c;
96    ss.get(c);
97    while(ss.good()) {
98      if (c=='\t'){
99        //*(buffer_[activated_]) << ' ';
100        next_column();
101      }
102      else if (c=='\n'){
103        //*(buffer_[activated_]) << ' ';
104        flush();
105        activated_=0;
106      }
107      else 
108        *(buffer_[activated_]) << c;
109    ss.get(c);
110    }
111  }
112
113
114  size_t& ColumnStream::width(size_t c) 
115  { 
116    return width_[c]; 
117  }
118
119
120  bool ColumnStream::writeline(size_t column)
121  {
122    assert(column<columns());
123    for (size_t i=0; i<margins_[column]; ++i)
124      os_ << ' ';
125    size_t count=0;
126    std::string word;
127    char c;
128    while (buffer_[column]->good()) {
129      buffer_[column]->get(c);
130      assert(c!='\t');
131      assert(c!='\n');
132      if (buffer_[column]->good())
133        word += c;
134      if (c==' ' || !buffer_[column]->good()) {
135        if (count+word.size()<=width_[column]) {
136          os_ << word;
137          count += word.size();
138          word = "";
139        }       
140        else {
141          if (!buffer_[column]->good())
142            buffer_[column]->clear(std::ios::goodbit);
143         
144          // if line is empty and word is longer than column width, we
145          // have to split the word
146          if (!count) {
147            os_ << word.substr(0,width_[column]);
148            for (std::string::reverse_iterator i=word.rbegin();
149                 i!=word.rbegin()+(word.size()-width_[column]); ++i)
150              buffer_[column]->putback(*i);
151          }
152          else {
153            for (std::string::reverse_iterator i=word.rbegin();
154                 i!=word.rend(); ++i)
155              buffer_[column]->putback(*i);
156            fill(column, count);
157          }
158          return true;
159        }
160      }
161
162    } 
163    fill(column, count);
164    return false;
165  }
166
167
168}}} // end of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.