yat/utility/ColumnStream.cc

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