ITAASCIITable.cpp 9.19 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
#include <ITAASCIITable.h>

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>

#include <ITAStringUtils.h>

ITAASCIITable::ITAASCIITable() {}

ITAASCIITable::ITAASCIITable(unsigned int uiRows, unsigned int uiColumns) {
	m_vColumns.resize(uiColumns);
	for (unsigned int i=0; i<uiColumns; i++)
		m_vColumns[i].vsCells.resize(uiRows);
}

ITAASCIITable::~ITAASCIITable() {}

unsigned int ITAASCIITable::rows() const {
	return (m_vColumns.empty() ? 0 : (unsigned int) m_vColumns[0].vsCells.size());
}

unsigned int ITAASCIITable::columns() const {
	return (unsigned int) m_vColumns.size();
}

void ITAASCIITable::addRow(unsigned int uiPosition) {
	// Jeweils eine Zelle in alle Spalten einf�gen
	uiPosition = (std::min)(uiPosition, rows());
	for (unsigned int i=0; i<(unsigned int) m_vColumns.size(); i++)
		m_vColumns[i].vsCells.insert(m_vColumns[i].vsCells.begin()+uiPosition, "");
}
 
void ITAASCIITable::removeRow(unsigned int uiRow) {
	// Jeweils eine Zelle in alle Spalten entfernen
	if (uiRow > rows()) return;
	for (unsigned int i=0; i<(unsigned int) m_vColumns.size(); i++)
		m_vColumns[i].vsCells.erase(m_vColumns[i].vsCells.begin()+uiRow);
}

void ITAASCIITable::addColumn(unsigned int uiPosition) {
	addColumn("", uiPosition);
}

void ITAASCIITable::addColumn(const std::string& sColumnTitle, unsigned int uiPosition) {
	uiPosition = std::min(uiPosition, (unsigned int) m_vColumns.size());
	ITAASCIITableColumn c;
	c.sTitle = sColumnTitle;
	m_vColumns.insert(m_vColumns.begin() + uiPosition, c);
}

void ITAASCIITable::removeColumn(unsigned int uiColumn) {
	uiColumn = std::min(uiColumn, (unsigned int) m_vColumns.size());
	m_vColumns.erase(m_vColumns.begin() + uiColumn);
}

unsigned int ITAASCIITable::getColumnJustify(unsigned int uiColumn) {
	if (uiColumn >= m_vColumns.size()) return LEFT;
	return m_vColumns[uiColumn].uiJustify;
}

void ITAASCIITable::setColumnJustify(unsigned int uiColumn, unsigned int uiJustify) {
	if (uiColumn < m_vColumns.size()) m_vColumns[uiColumn].uiJustify = uiJustify;
}

std::string ITAASCIITable::getColumnTitle(unsigned int uiColumn) {
	if (uiColumn >= m_vColumns.size()) return "";
	return m_vColumns[uiColumn].sTitle;
}

void ITAASCIITable::setColumnTitle(unsigned int uiColumn, const std::string& sTitle) {
	if (uiColumn < m_vColumns.size()) m_vColumns[uiColumn].sTitle = sTitle;
}

unsigned int ITAASCIITable::getColumnTitleJustify(unsigned int uiColumn) {
	if (uiColumn >= m_vColumns.size()) return LEFT;
	return m_vColumns[uiColumn].uiTitleJustify;
}

void ITAASCIITable::setColumnTitleJustify(unsigned int uiColumn, unsigned int uiJustify) {
	if (uiColumn < m_vColumns.size()) m_vColumns[uiColumn].uiTitleJustify = uiJustify;
}

std::string ITAASCIITable::getContent(unsigned int uiRow, unsigned int uiColumn) {
	if (uiColumn >= m_vColumns.size()) return "";
	if (uiRow >= m_vColumns[0].vsCells.size()) return "";
	return m_vColumns[uiColumn].vsCells[uiRow];
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const std::string& sContent) {
	if (uiColumn >= m_vColumns.size()) return;
	if (uiRow >= m_vColumns[0].vsCells.size()) return;
	m_vColumns[uiColumn].vsCells[uiRow] = sContent;
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const char* pszContent) {
	setContent(uiRow, uiColumn, std::string(pszContent));
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const bool& bContent) {
	setContent(uiRow, uiColumn, BoolToString(bContent));
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const unsigned int& uiContent) {
	setContent(uiRow, uiColumn, UIntToString(uiContent));
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const int& iContent) {
	setContent(uiRow, uiColumn, IntToString(iContent));
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const float& fContent, int iPrecision) {
	setContent(uiRow, uiColumn, FloatToString(fContent, iPrecision));
}

void ITAASCIITable::setContent(unsigned int uiRow, unsigned int uiColumn, const double& dContent, int iPrecision) {
	setContent(uiRow, uiColumn, DoubleToString(dContent, iPrecision));
}

//! Die ASCII-Tabelle rendern
std::ostream& ITAASCIITable::render(std::ostream& os, int iFormat) {
	switch (iFormat) {
	case DAT: return renderDAT(os);
	case CSV: return renderCSV(os);
	default:
		return renderText(os);
	}
}

std::ostream& ITAASCIITable::renderText(std::ostream& os) {

	std::stringstream tmp;

	unsigned int n = (unsigned int) m_vColumns.size();
	unsigned int m = rows();

	std::vector< std::vector< std::string > > vvsLines(n);

	// Calculate the column widths
	for (unsigned int i=0; i<n; i++) {
		m_vColumns[i].uiMaxWidth = 0;
		for (unsigned int j=0; j<(unsigned int) m_vColumns[i].vsCells.size(); j++)
			m_vColumns[i].uiMaxWidth = std::max(m_vColumns[i].uiMaxWidth, (unsigned int) m_vColumns[i].vsCells[j].length());
	}

	// Generate the table header

	unsigned int uiLines=0;
	unsigned int tw=0;	// Total width

	// Generate a vector of vectors of strings containing each cells lines
	for (unsigned int i=0; i<n; i++) {
		splitLines(m_vColumns[i].sTitle, vvsLines[i]);
		uiLines = std::max(uiLines, (unsigned int) vvsLines[i].size());
		for (unsigned int j=0; j<uiLines; j++)
			if (vvsLines[i].size() > j)
				m_vColumns[i].uiMaxWidth = std::max(m_vColumns[i].uiMaxWidth, (unsigned int) vvsLines[i][j].length());
	}

	for (unsigned int j=0; j<uiLines; j++) {
		tmp.str("");
		tmp.clear();

		for (unsigned int i=0; i<n; i++) {
			// Left margin
			if (i == 0) tmp << " ";

			unsigned int w = m_vColumns[i].uiMaxWidth;

			switch (m_vColumns[i].uiTitleJustify) {
			case RIGHT:		tmp << std::right;
				break;
			default:		tmp << std::left;
			}

			if (j < (unsigned int) vvsLines[i].size())
				tmp << std::setw(w) << vvsLines[i][j];
			else
				tmp << std::setw(w) << "";

			// Column spacer
			if (i < n-1) tmp << "  ";

			// Right margin
			if (i == n-1) tmp << " " << std::endl;
		}

		tw = std::max(tw, (unsigned int) tmp.str().length()-1);	// -1 because of endl
		os << tmp.str();
	}

	// Generate a horizontal line
	tmp.str("");
	tmp.clear();
	tmp << std::setfill('-') << std::setw(tw) << '-';
	std::string sLine = tmp.str();
	os << sLine << std::endl;

	// Render the table lines
	tmp.fill(' ');
	for (unsigned int r=0; r<m; r++) {
		uiLines=0;

		// Generate a vector of vectors of strings containing each cells lines
		for (unsigned int i=0; i<n; i++) {
			splitLines(m_vColumns[i].vsCells[r], vvsLines[i]);
			uiLines = std::max(uiLines, (unsigned int) vvsLines[i].size());
		}

		for (unsigned int j=0; j<uiLines; j++) {
			tmp.str("");
			tmp.clear();

			for (unsigned int i=0; i<n; i++) {
				// Left margin
				if (i == 0) tmp << " ";
				unsigned int w = m_vColumns[i].uiMaxWidth;

				switch (m_vColumns[i].uiJustify) {
				case RIGHT:		tmp << std::right;
					break;
				default:		tmp << std::left;
				}

				if (j < (unsigned int) vvsLines[i].size())
					tmp << std::setw(w) << vvsLines[i][j];
				else
					tmp << std::setw(w) << "";

				// Column spacer
				if (i < n-1) tmp << "  ";

				// Right margin
				if (i == n-1) tmp << " " << std::endl;
			}

			os << tmp.str();
		}
	}

	// Final line
	os << sLine << std::endl;

	return os;
}

std::ostream& ITAASCIITable::renderDAT(std::ostream& os) {
	std::stringstream tmp;

	unsigned int n = (unsigned int) m_vColumns.size();
	unsigned int m = rows();

	// Calculate the column widths
	for (unsigned int i=0; i<n; i++) {
		m_vColumns[i].uiMaxWidth = 0;
		for (unsigned int j=0; j<(unsigned int) m_vColumns[i].vsCells.size(); j++)
			m_vColumns[i].uiMaxWidth = std::max(m_vColumns[i].uiMaxWidth, (unsigned int) m_vColumns[i].vsCells[j].length());
	}

	// Output the column titles
	for (unsigned int i=0; i<n; i++)
		os << "# [" << i << "] " << m_vColumns[i].sTitle << std::endl;
	os << std::endl;

	// Render the table cells line by line
	for (unsigned int r=0; r<m; r++) {
		for (unsigned int i=0; i<n; i++) {
			unsigned int w = m_vColumns[i].uiMaxWidth;
			os << std::left << std::setw(w) << m_vColumns[i].vsCells[r];
			if (i < (n-1))
				os << "  \t";
			else 
				os << std::endl;
		}
	}

	return os;
}

std::ostream& ITAASCIITable::renderCSV(std::ostream& os) {
	std::stringstream tmp;

	unsigned int n = (unsigned int) m_vColumns.size();
	unsigned int m = rows();

	/*
	// Output the column titles
	for (unsigned int i=0; i<n; i++)
		os << "# [" << i << "] " << m_vColumns[i].sTitle << std::endl;
	os << std::endl;
	*/

	// Render the table cells line by line
	for (unsigned int r=0; r<m; r++) {
		for (unsigned int i=0; i<n; i++) {
			os << std::left << m_vColumns[i].vsCells[r];
			if (i < (n-1))
				os << ";";
			else 
				os << std::endl;
		}
	}

	return os;
}

std::string ITAASCIITable::toString() {
	std::stringstream ss;
	render(ss);
	return ss.str();
}

void ITAASCIITable::splitLines(const std::string& sText, std::vector< std::string >& vsDest) {
	vsDest.clear();
	if (sText.empty()) return;

	int i=0, j;
	do {
		j = (int) sText.find('\n', i);
		if (j!=-1) {
			vsDest.push_back(sText.substr(i, j-i));
			i = j+1;
		}
	} while (j!=-1);
	
	if (i == 0)
		vsDest.push_back(sText);
	else
		if (i != (int) sText.length()-1) vsDest.push_back(sText.substr(i, (int) sText.length()-i+1));

	if (sText[sText.length()-1] == '\n') vsDest.erase(vsDest.end()-1);
}