tablestrategy.cpp
tablegenerator/tablestrategy.cpp · 10.7 KB
From the archive tablegenerator-1.1.1.tar.gz
Download the archive « Back to the archive contents
/*
_______________________________________________________________________________
TableGenerator v1.1.1
-------------------------------------------------------------------------------
Copyright (c) 2008 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 "tablestrategy.hpp"
#include "tablegenerator.hpp"
#include <sstream>
#include <iomanip>
// Helpers
static bool isNumber( const std::string &str )
{
for( std::string::const_iterator i = str.begin(); i != str.end(); ++i )
{
if( !isdigit( *i )
&& *i != '-' && *i != '+'
&& *i != '.' && *i != ',' )
{
return false;
}
}
return true;
}
static std::string strlower( const std::string &str )
{
std::ostringstream strlow;
for( std::string::const_iterator i = str.begin(); i != str.end(); ++i )
strlow << static_cast< char >( tolower( *i ) );
return strlow.str();
}
///////////////////////////////////////////////////////////////////////////////
// SimpleTable strategy
SimpleTable::SimpleTable( const int fs, const int c, const int cs )
: _fieldspace( fs < 0 ? 0 : fs ),
_columns( c < 1 ? 1 : c ),
_columnspace( cs < 0 ? 0 : cs )
{
}
std::string SimpleTable::render( const std::vector< TableRow > &table,
const std::vector< size_t > &fieldWidth )
{
std::ostringstream buf;
int col = 0;
for( size_t rowNum = 0; rowNum < table.size(); ++rowNum )
{
const TableRow &row = table[ rowNum ];
for( size_t colNum = 0; colNum < row.size(); ++colNum )
{
if( isNumber( row[ colNum ] ) )
buf << std::right;
else
buf << std::left;
buf << std::setw( fieldWidth[ colNum ] )
<< row[ colNum ];
if( colNum == row.size() - 1 )
buf << std::string( _columnspace, ' ' );
else
buf << std::string( _fieldspace, ' ' );
}
if( !( ++col % _columns ) )
buf << "\r\n";
}
return buf.str();
}
///////////////////////////////////////////////////////////////////////////////
// FramedTable strategy
FramedTable::FramedTable( int c,
int s,
const std::string &vcol,
const std::string &fcol,
const std::string &hcol,
const std::string &fg )
: _columns( c < 1 ? 1 : c ),
_space( s < 0 ? 0 : s ),
_vColour( vcol ),
_fColour( fcol ),
_hColour( hcol ),
_frameGfx( fg )
{
}
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 ];
std::ostringstream buf;
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 << "\r\n";
}
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 ] ) )
buf << std::right;
else
buf << std::left;
buf << _vColour << std::setw( fieldWidth[ colNum ] )
<< row[ colNum ]
<< std::string( _space, ' ' );
}
if( !( ++col % _columns ) )
buf << _fColour << vertChar << "\r\n";
}
// 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 << "\r\n";
}
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 << "\r\n";
}
return buf.str();
}
///////////////////////////////////////////////////////////////////////////////
// Space3Table strategy
Space3Table::Space3Table( const std::string &h,
const std::string &hc,
const std::string &vc )
: _head( h ),
_hColour( hc ),
_vColour( vc )
{
}
std::string Space3Table::render( const std::vector< TableRow > &table,
const std::vector< size_t > &fieldWidth )
{
std::ostringstream buf;
size_t tableWidth = 0;
for( size_t rowNum = 0; rowNum < table.size(); ++rowNum )
{
const TableRow &row = table[ rowNum ];
size_t rowWidth = 0;
buf << _vColour << std::string( 2, ' ' );
for( size_t colNum = 0; colNum < row.size(); ++colNum )
{
if( isNumber( row[ colNum ] ) )
buf << std::right;
else
buf << std::left;
buf << std::setw( fieldWidth[ colNum ] )
<< row[ colNum ];
rowWidth += fieldWidth[ colNum ];
if( colNum != row.size() - 1 )
{
buf << ' ';
rowWidth += 1;
}
}
buf << "\r\n";
if( rowWidth > tableWidth )
tableWidth = rowWidth;
}
std::string out = buf.str();
// Header
const std::string head = strlower( _head );
std::string headFiller;
if( head.length() < tableWidth + 1 )
headFiller = " " + std::string( tableWidth - head.length() - 1, '_' );
const std::string finalHead = head + headFiller;
out.insert( 0, " " + _hColour + finalHead + "\r\n\r\n" );
// Footer
if( finalHead.length() > tableWidth )
tableWidth = finalHead.length();
out += " " + _hColour + std::string( tableWidth, '_' ) + "\r\n";
return out;
}
///////////////////////////////////////////////////////////////////////////////
// ShadowTable strategy
ShadowTable::ShadowTable( const std::string &ht, // Header text
const std::string &tc, // Text colour
const std::string &fc, // Background fill colour
const std::string &sc, // Shadow colour
const std::string &st, // Stop colour
size_t fp, // side padding
size_t cp, // coloumn padding
size_t ind ) // indentation
: _header( ht ),
_textColour( tc ),
_fillColour( fc ),
_shadowColour( sc ),
_stopColour( st ),
_padding( fp ),
_columnPadding( cp ),
_indentation( ind )
{
}
std::string ShadowTable::render( const std::vector< TableRow > &table,
const std::vector< size_t > &fieldWidth )
{
std::ostringstream buf;
size_t tableWidth = 0;
bool hasHeader = !_header.empty();
for( size_t rowNum = 0; rowNum < table.size(); ++rowNum )
{
const TableRow &row = table[ rowNum ];
size_t rowWidth = 0;
if( _padding > 0 )
buf << _stopColour << std::string( _indentation, ' ' )
<< _fillColour << _textColour
<< std::string( _padding, ' ' );
for( size_t colNum = 0; colNum < row.size(); ++colNum )
{
buf << _fillColour << _textColour;
if( isNumber( row[ colNum ] ) )
buf << std::right;
else
buf << std::left;
buf << std::setw( fieldWidth[ colNum ] )
<< row[ colNum ];
rowWidth += fieldWidth[ colNum ];
if( colNum != row.size() - 1 )
{
buf << std::string( _columnPadding, ' ' );
rowWidth += _columnPadding;
}
}
if( _padding > 0 )
buf << std::string( _padding, ' ' );
buf << _shadowColour << ' ' << _stopColour << "\r\n";
if( rowWidth > tableWidth )
tableWidth = rowWidth;
}
std::string out = buf.str();
// Header
if( hasHeader )
{
if( _header.length() > tableWidth )
_header.erase( tableWidth - tableWidth % 2 );
std::string headBuf = _stopColour + std::string( _indentation, ' ' )
+ _fillColour + std::string( _padding, ' ' );
// Calculate center justification
size_t headPad = ( tableWidth / 2 - _header.length() / 2 );
size_t compensate = ( _header.length() % 2 ) ? 1 : 0;
headBuf += std::string( headPad - compensate, ' ' )
+ _textColour + _header;
compensate = ( tableWidth % 2 );
headBuf += std::string( headPad + _padding + compensate, ' ' )
+ _shadowColour + ' ' + _stopColour + "\r\n";
// At an extra blank line below header
headBuf += _stopColour + std::string( _indentation, ' ' ) + _fillColour
+ std::string( tableWidth + _padding * 2, ' ' )
+ _shadowColour + ' ' + _stopColour + "\r\n";
out.insert( 0, headBuf );
}
// Insert a blank line at the very top.
out.insert( 0, _stopColour + std::string( _indentation, ' ' ) + _fillColour
+ std::string( tableWidth + _padding * 2, ' ' )
+ _stopColour + "\r\n" );
// Add a blank line at the bottom
out += _stopColour + std::string( _indentation, ' ' ) + _fillColour
+ std::string( tableWidth + _padding * 2, ' ' )
+ _shadowColour + ' ' + _stopColour + "\r\n";
// And finally the bottom shadow
out += _stopColour + std::string( _indentation + 1, ' ' )
+ _shadowColour + std::string( tableWidth + _padding * 2, ' ' )
+ _stopColour + "\r\n";
return out;
}