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

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

Addresses #436.

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