source: trunk/lib/ColumnStream.cc @ 234

Last change on this file since 234 was 234, checked in by Peter Johansson, 14 years ago

fixes #87 and #95 and #103 and #104 and #80 and #108, and refs #69

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