/* _______________________________________________________________________________ TableGenerator v1.3 ------------------------------------------------------------------------------- Copyright (c) 2007-2010 Kai Braaten Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------- */ #include <stdexcept> #include "tablegenerator.hpp" #include "tgen_internal.hpp" namespace TableGen { /////////////////////////////////////////////////////////////////////////////// // TableGenerator class definition std::ostream &operator<<( std::ostream &o, const TableGenerator &table ) { o << table.str(); return o; } TableGenerator::TableGenerator( TableStrategy *s ) : _strategy( s ) { if( !s ) { throw std::invalid_argument( "Can't pass NULL pointer to TableGenerator constructor." ); } } TableGenerator::~TableGenerator() { } std::string TableGenerator::str() const { return _strategy->render( _data, _widest ); } void TableGenerator::operator<<( TableRow &r ) { add( r ); r.clear(); } void TableGenerator::operator<<( TableStrategy *s ) { changeStrategy( s ); } void TableGenerator::operator<<( std::string rowStr ) { if( !rowStr.empty() ) { TableRow row; std::string current; while( !( current = getNextChunk( rowStr, '\t' ) ).empty() ) { row << current; } add( row ); } } void TableGenerator::add( const TableRow &newRow ) { _data.push_back( newRow ); // Make sure _widest is the same size as the longest row. if( _widest.size() < newRow.size() ) _widest.resize( newRow.size() ); // Always keep track of the longest string in each column for( size_t n = 0; n < newRow.size(); ++n ) { if( newRow[ n ].length() > _widest[ n ] ) _widest[ n ] = newRow[ n ].length(); } } void TableGenerator::clear() { _data.erase( _data.begin(), _data.end() ); _widest.erase( _widest.begin(), _widest.end() ); } TableStrategy *TableGenerator::changeStrategy( TableStrategy *s ) { TableStrategy *old = _strategy; _strategy = s; return old; } /////////////////////////////////////////////////////////////////////////////// // TableRow class definition TableRow::TableRow( size_t sz ) : _data( sz ) { } TableRow::TableRow( const TableRow &original ) : _data( original._data ) { } TableRow &TableRow::operator=( const TableRow &original ) { if( &original != this ) _data = original._data; return *this; } void TableRow::add( const std::string &txt ) { _data.push_back( txt ); } const std::string &TableRow::operator[]( size_t pos ) const { return _data[ pos ]; } std::string &TableRow::operator[]( size_t pos ) { return _data[ pos ]; } size_t TableRow::size() const { return _data.size(); } void TableRow::clear() { _data.erase( _data.begin(), _data.end() ); } }