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
|
/**
Copyright (c) 2014 Alex Tsui
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 "ColumnFormatter.h"
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstdlib>
bool ColumnFormatter::load(const std::string& fn)
{
items.clear();
std::ifstream ifs(fn.c_str());
if (!ifs.is_open())
return false;
std::string str;
while (!ifs.eof())
{
std::getline( ifs, str );
if (!ifs.eof())
items.push_back(str);
}
return true;
}
int ColumnFormatter::solve(int width)
{
bool fits = true;
int i = 1;
while (fits)
{
++i;
std::vector<size_t> widths = divideItems(i);
size_t columnWidth = width / i;
for (size_t j = 0; j < widths.size(); ++j)
{
fits &= (widths[j] < columnWidth);
}
if (!fits)
{
--i;
}
}
return i;
}
std::vector<size_t> ColumnFormatter::divideItems(int numColumns)
{
columns.clear();
for (int i = 0; i < numColumns; ++i)
columns.push_back(std::list<std::string>());
for (size_t i = 0; i < items.size(); ++i)
{
columns[i % numColumns].push_back(items[i]);
}
// count the fattest item in each column
std::vector<size_t> res(numColumns);
for (int i = 0; i < numColumns; ++i)
{
for (std::list<std::string>::const_iterator it =
columns[i].begin(); it != columns[i].end(); ++it)
{
if (res[i] < it->size())
res[i] = it->size();
}
}
return res;
}
void ColumnFormatter::format(int width)
{
m_formattedOutput.clear();
int cols = solve(width);
std::vector<int> colWidths(cols, width / cols);
int rem = width % cols;
for (int i = 0; i < rem; ++i)
{
colWidths[i]++;
}
divideItems(cols);
std::vector< std::list<std::string>::const_iterator > its;
std::vector< std::list<std::string>::const_iterator > it_ends;
for (size_t i = 0; i < columns.size(); ++i)
{
its.push_back(columns[i].begin());
it_ends.push_back(columns[i].end());
}
bool done = false;
while (!done)
{
std::stringstream row_ss;
for (size_t i = 0; i < columns.size(); ++i)
{
std::stringstream item_ss;
std::string item;
if (its[i] != it_ends[i])
{
item = *its[i];
++its[i];
}
item_ss << std::left << std::setw(colWidths[i]) << item;
row_ss << item_ss.str();
}
m_formattedOutput.push_back(row_ss.str());
done = true;
for (size_t i = 0; i < columns.size(); ++i)
{
done &= (its[i] == it_ends[i]);
}
}
}
const std::list<std::string>& ColumnFormatter::formattedOutput() const
{
return m_formattedOutput;
}
|