// $Id: ColumnStream.cc 2919 2012-12-19 06:54:23Z peter $ /* Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson Copyright (C) 2010, 2011, 2012 Peter Johansson This file is part of yat, http://dev.thep.lu.se/yat yat is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. yat is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with yat. If not, see . */ #include #include "ColumnStream.h" #include #include namespace theplu{ namespace yat{ namespace utility{ ColumnStream::ColumnStream(std::ostream& os, size_t columns) : activated_(0), margins_(columns), os_(os), width_(columns, 8) { buffer_.reserve(columns); while (buffer_.size()clear(std::ios::goodbit); } size_t& ColumnStream::margin(size_t c) { return margins_[c]; } void ColumnStream::next_column(void) { ++activated_; if (activated_>=columns()) { flush(); } } void ColumnStream::print(std::stringstream& ss) { assert(buffer_[activated_]); assert(activated_good()) { buffer_[column]->get(c); assert(c!='\t'); assert(c!='\n'); if (buffer_[column]->good()) word += c; if (c==' ' || !buffer_[column]->good()) { if (count+word.size()<=width_[column]) { os_ << word; count += word.size(); word = ""; } else { if (!buffer_[column]->good()) buffer_[column]->clear(std::ios::goodbit); // if line is empty and word is longer than column width, we // have to split the word if (!count) { os_ << word.substr(0,width_[column]); for (std::string::reverse_iterator i=word.rbegin(); i!=word.rbegin()+(word.size()-width_[column]); ++i) buffer_[column]->putback(*i); } else { for (std::string::reverse_iterator i=word.rbegin(); i!=word.rend(); ++i) buffer_[column]->putback(*i); fill(column, count); } return true; } } } fill(column, count); return false; } }}} // end of namespace utility, yat, and theplu