source: trunk/lib/ColumnStream.cc @ 693

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

Fixes #339. Change to GPLv3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: ColumnStream.cc 693 2008-09-11 20:42:56Z jari $
2
3/*
4  Copyright (C) 2007 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 svndigest. 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 svndigest{
29
30  ColumnStream::ColumnStream(std::ostream& os, size_t columns)
31    : activated_(0),os_(os)
32  {
33    margins_=std::vector<size_t>(columns); 
34    buffer_.reserve(columns);
35    while (buffer_.size()<columns)
36      buffer_.push_back(new std::stringstream);
37    width_=std::vector<size_t>(columns, 8); 
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  void ColumnStream::fill(size_t column, size_t count)
49  {
50    while(count<width_[column]){
51      os_ << ' ';
52      ++count;
53    }
54  }
55
56
57  void ColumnStream::flush(void)
58  {
59    bool empty=false;
60    while(!empty) {
61      empty=true;
62      for (size_t i=0; i<columns(); ++i){
63        if (writeline(i))
64          empty=false;
65      }
66      os_ << '\n';
67    }
68    for (size_t i=0; i<columns(); ++i)
69      buffer_[i]->clear(std::ios::goodbit);
70  }
71
72  void ColumnStream::next_column(void)
73  {
74    ++activated_;
75    if (activated_>=columns()) {
76      flush();
77    }
78  }
79
80  void ColumnStream::print(std::stringstream& ss)
81  {
82    assert(buffer_[activated_]);
83    assert(activated_<buffer_.size());
84    char c;
85    ss.get(c);
86    while(ss.good()) {
87      if (c=='\t'){
88        //*(buffer_[activated_]) << ' ';
89        next_column();
90      }
91      else if (c=='\n'){
92        //*(buffer_[activated_]) << ' ';
93        flush();
94        activated_=0;
95      }
96      else 
97        *(buffer_[activated_]) << c;
98    ss.get(c);
99    }
100  }
101
102
103  bool ColumnStream::writeline(size_t column)
104  {
105    assert(column<columns());
106    for (size_t i=0; i<margins_[column]; ++i)
107      os_ << ' ';
108    size_t count=0;
109    std::string word;
110    char c;
111    while (buffer_[column]->good()) {
112      buffer_[column]->get(c);
113      assert(c!='\t');
114      assert(c!='\n');
115      if (buffer_[column]->good())
116        word += c;
117      if (c==' ' || !buffer_[column]->good()) {
118        if (count+word.size()<=width_[column]) {
119          os_ << word;
120          count += word.size();
121          word = "";
122        }       
123        else {
124          if (!buffer_[column]->good())
125            buffer_[column]->clear(std::ios::goodbit);
126         
127          // if line is empty and word is longer than column width, we
128          // have to split the word
129          if (!count) {
130            os_ << word.substr(0,width_[column]);
131            for (std::string::reverse_iterator i=word.rbegin();
132                 i!=word.rbegin()+(word.size()-width_[column]); ++i)
133              buffer_[column]->putback(*i);
134          }
135          else {
136            for (std::string::reverse_iterator i=word.rbegin();
137                 i!=word.rend(); ++i)
138              buffer_[column]->putback(*i);
139            fill(column, count);
140          }
141          return true;
142        }
143      }
144
145    } 
146    fill(column, count);
147    return false;
148  }
149
150
151}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.