/*
_______________________________________________________________________________
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 "tgen_internal.hpp"
namespace TableGen
{
///////////////////////////////////////////////////////////////////////////////
// FramedTable strategy
FramedTable::FramedTable( int c,
int s,
const std::string &vcol,
const std::string &fcol,
const std::string &hcol,
const std::string &fg,
const std::string &nl )
: _columns( c < 1 ? 1 : c ),
_space( s < 0 ? 0 : s ),
_vColour( vcol ),
_fColour( fcol ),
_hColour( hcol ),
_frameGfx( fg ),
_newline( nl )
{
}
std::string FramedTable::render( const std::vector< TableRow > &table,
const std::vector< size_t > &fieldWidth )
{
if( _frameGfx.length() != 3 )
_frameGfx = "+-|";
const char cornerChar = _frameGfx[ 0 ];
const char horizChar = _frameGfx[ 1 ];
const char vertChar = _frameGfx[ 2 ];
#ifdef __STORMGCC__
std::ostrstream buf;
#else
std::ostringstream buf;
#endif
int col = 0;
if( !table.empty() )
{
for( int headNum = 0; headNum < _columns; ++headNum )
{
for( size_t colNum = 0; colNum < table[ 0 ].size(); ++colNum )
{
buf << _fColour << cornerChar
<< std::string( _space, horizChar )
<< _hColour << table[ 0 ][ colNum ]
<< _fColour <<
std::string( fieldWidth[ colNum ]
- table[ 0 ][ colNum ].length(), horizChar )
<< std::string( _space, horizChar );
}
}
buf << _fColour << cornerChar << _newline;
}
for( size_t rowNum = 1; rowNum < table.size(); ++rowNum )
{
const TableRow &row = table[ rowNum ];
std::string newLine;
for( size_t colNum = 0; colNum < row.size(); ++colNum )
{
buf << _fColour << vertChar
<< std::string( _space, ' ' );
if( isNumber( row[ colNum ] ) )
#ifdef __STORMGCC__
buf.unsetf( std::ios::left );
else
buf.setf( std::ios::left );
#else
buf << std::right;
else
buf << std::left;
#endif
buf << _vColour << std::setw( fieldWidth[ colNum ] )
<< row[ colNum ]
<< std::string( _space, ' ' );
}
if( !( ++col % _columns ) )
buf << _fColour << vertChar << _newline;
}
// Fill out empty fields
if( !table.empty() && ( ( table.size() - 1 ) % _columns ) )
{
for( int n = ( ( table.size() - 1 ) % _columns );
n < _columns; ++n )
{
for( size_t colNum = 0; colNum < table[ 0 ].size(); ++colNum )
{
buf << _fColour << vertChar
<< std::string( fieldWidth[ colNum ] + _space * 2, ' ' );
}
}
buf << vertChar << _newline;
}
if( !table.empty() )
{
for( int headNum = 0; headNum < _columns; ++headNum )
{
for( size_t colNum = 0; colNum < table[ 0 ].size(); ++colNum )
{
buf << _fColour << cornerChar
<< std::string( fieldWidth[ colNum ] + _space * 2,
horizChar );
}
}
buf << cornerChar << _newline;
}
#ifdef __STORMGCC__
std::string tmp = buf.str();
buf.rdbuf()->freeze( false ); // avoid memory leak
return tmp;
#else
return buf.str();
#endif
}
}