-- internal declarations, part 1 TYPE __ghdl_size_type IS UNSIGNED (32); TYPE __ghdl_index_type IS UNSIGNED (32); TYPE __ghdl_i32 IS SIGNED (32); TYPE __ghdl_real IS FLOAT; TYPE __ghdl_i64 IS SIGNED (64); TYPE __ghdl_file_index IS UNSIGNED (32); TYPE __ghdl_file_index_ptr IS ACCESS __ghdl_file_index; TYPE __ghdl_char IS UNSIGNED (8); TYPE __ghdl_chararray IS ARRAY [__ghdl_index_type] OF __ghdl_char; TYPE __ghdl_char_ptr IS ACCESS __ghdl_chararray; TYPE __ghdl_char_ptr_array IS ARRAY [__ghdl_index_type] OF __ghdl_char_ptr; TYPE __ghdl_char_ptr_array_ptr IS ACCESS __ghdl_char_ptr_array; TYPE __ghdl_ptr IS ACCESS __ghdl_char; TYPE __ghdl_str_len IS RECORD len: __ghdl_index_type; str: __ghdl_char_ptr; END RECORD; TYPE __ghdl_str_len_array IS ARRAY [__ghdl_index_type] OF __ghdl_str_len; TYPE __ghdl_str_len_ptr IS ACCESS __ghdl_str_len; TYPE __ghdl_bool_type IS BOOLEAN {false, true}; TYPE __ghdl_bool_array_type IS ARRAY [__ghdl_index_type] OF __ghdl_bool_type; TYPE __ghdl_bool_array_ptr IS ACCESS __ghdl_bool_array_type; TYPE __ghdl_compare_type IS ENUM {lt, eq, gt}; TYPE __ghdl_location IS RECORD filename: __ghdl_char_ptr; line: __ghdl_i32; col: __ghdl_i32; END RECORD; TYPE __ghdl_location_ptr IS ACCESS __ghdl_location; TYPE __ghdl_dir_type IS ENUM {dir_to, dir_downto}; TYPE __ghdl_signal IS RECORD; TYPE __ghdl_signal_ptr IS ACCESS __ghdl_signal; EXTERNAL FUNCTION __ghdl_alloc ( size: __ghdl_size_type) RETURN __ghdl_ptr; EXTERNAL PROCEDURE __ghdl_program_error ( filename: __ghdl_char_ptr; line: __ghdl_i32; code: __ghdl_index_type); EXTERNAL PROCEDURE __ghdl_bound_check_failed_l1 ( filename: __ghdl_char_ptr; line: __ghdl_i32); EXTERNAL FUNCTION __ghdl_stack2_allocate ( size: __ghdl_index_type) RETURN __ghdl_ptr; EXTERNAL FUNCTION __ghdl_stack2_mark ( ) RETURN __ghdl_ptr; EXTERNAL PROCEDURE __ghdl_stack2_release ( mark: __ghdl_ptr); EXTERNAL PROCEDURE __ghdl_memcpy ( dest: __ghdl_ptr; src: __ghdl_ptr; length: __ghdl_index_type); EXTERNAL PROCEDURE __ghdl_deallocate ( OBJ: __ghdl_ptr); EXTERNAL FUNCTION __ghdl_malloc ( length: __ghdl_index_type) RETURN __ghdl_ptr; EXTERNAL FUNCTION __ghdl_malloc0 ( length: __ghdl_index_type) RETURN __ghdl_ptr; EXTERNAL FUNCTION __ghdl_text_file_elaborate ( ) RETURN __ghdl_file_index; EXTERNAL FUNCTION __ghdl_file_elaborate ( NAME: __ghdl_char_ptr) RETURN __ghdl_file_index; EXTERNAL PROCEDURE __ghdl_file_finalize ( file: __ghdl_file_index); EXTERNAL PROCEDURE __ghdl_text_file_finalize ( file: __ghdl_file_index); EXTERNAL PROCEDURE __ghdl_protected_enter ( OBJ: __ghdl_ptr); EXTERNAL PROCEDURE __ghdl_protected_leave ( OBJ: __ghdl_ptr); EXTERNAL PROCEDURE __ghdl_protected_init ( OBJ: __ghdl_ptr); EXTERNAL PROCEDURE __ghdl_protected_fini ( OBJ: __ghdl_ptr); TYPE __ghdl_rtik IS ENUM {__ghdl_rtik_top, __ghdl_rtik_library, __ghdl_rtik_package, __ghdl_rtik_package_body, __ghdl_rtik_entity, __ghdl_rtik_architecture, __ghdl_rtik_process, __ghdl_rtik_block, __ghdl_rtik_if_generate, __ghdl_rtik_case_generate, __ghdl_rtik_for_generate, __ghdl_rtik_generate_body, __ghdl_rtik_instance, __ghdl_rtik_constant, __ghdl_rtik_iterator, __ghdl_rtik_variable, __ghdl_rtik_signal, __ghdl_rtik_file, __ghdl_rtik_port, __ghdl_rtik_generic, __ghdl_rtik_alias, __ghdl_rtik_guard, __ghdl_rtik_component, __ghdl_rtik_attribute, __ghdl_rtik_type_b1, __ghdl_rtik_type_e8, __ghdl_rtik_type_e32, __ghdl_rtik_type_i32, __ghdl_rtik_type_i64, __ghdl_rtik_type_f64, __ghdl_rtik_type_p32, __ghdl_rtik_type_p64, __ghdl_rtik_type_access, __ghdl_rtik_type_array, __ghdl_rtik_type_record, __ghdl_rtik_type_file, __ghdl_rtik_subtype_scalar, __ghdl_rtik_subtype_array, __ghdl_rtik_subtype_unconstrained_array, __ghdl_rtik_subtype_record, __ghdl_rtik_subtype_access, __ghdl_rtik_type_protected, __ghdl_rtik_element, __ghdl_rtik_unit64, __ghdl_rtik_unitptr, __ghdl_rtik_attribute_transaction, __ghdl_rtik_attribute_quiet, __ghdl_rtik_attribute_stable, __ghdl_rtik_psl_assert, __ghdl_rtik_psl_cover, __ghdl_rtik_psl_endpoint, __ghdl_rtik_error}; TYPE __ghdl_rti_depth IS UNSIGNED (8); TYPE __ghdl_rti_u8 IS UNSIGNED (8); TYPE __ghdl_rti_common IS RECORD kind: __ghdl_rtik; depth: __ghdl_rti_depth; mode: __ghdl_rti_u8; max_depth: __ghdl_rti_depth; END RECORD; TYPE __ghdl_rti_access IS ACCESS __ghdl_rti_common; TYPE __ghdl_rti_array IS ARRAY [__ghdl_index_type] OF __ghdl_rti_access; TYPE __ghdl_rti_arr_acc IS ACCESS __ghdl_rti_array; TYPE __ghdl_component_link_type IS RECORD; TYPE __ghdl_component_link_acc IS ACCESS __ghdl_component_link_type; TYPE __ghdl_entity_link_type IS RECORD rti: __ghdl_rti_access; parent: __ghdl_component_link_acc; END RECORD; TYPE __ghdl_entity_link_acc IS ACCESS __ghdl_entity_link_type; TYPE __ghdl_component_link_type IS RECORD INSTANCE: __ghdl_entity_link_acc; stmt: __ghdl_rti_access; END RECORD; TYPE __ghdl_rtin_block IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; loc: __ghdl_ptr; linecol: __ghdl_index_type; parent: __ghdl_rti_access; nbr_child: __ghdl_index_type; children: __ghdl_rti_arr_acc; END RECORD; TYPE __ghdl_rtin_generate IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; loc: __ghdl_ptr; linecol: __ghdl_index_type; parent: __ghdl_rti_access; size: __ghdl_index_type; child: __ghdl_rti_access; END RECORD; TYPE __ghdl_rtin_block_file IS RECORD block: __ghdl_rtin_block; filename: __ghdl_char_ptr; END RECORD; TYPE __ghdl_rtin_type_scalar IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; END RECORD; TYPE __ghdl_rtin_type_enum IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; nbr: __ghdl_index_type; lits: __ghdl_char_ptr_array_ptr; END RECORD; TYPE __ghdl_rtin_subtype_scalar IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; base: __ghdl_rti_access; range: __ghdl_ptr; END RECORD; TYPE __ghdl_rtin_unit64 IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; val: __ghdl_i64; END RECORD; TYPE __ghdl_rtin_unitptr IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; addr: __ghdl_ptr; END RECORD; TYPE __ghdl_rtin_type_physical IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; nbr: __ghdl_index_type; units: __ghdl_rti_arr_acc; END RECORD; TYPE __ghdl_rtin_type_fileacc IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; base: __ghdl_rti_access; END RECORD; TYPE __ghdl_rtin_type_array IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; element: __ghdl_rti_access; nbr_dim: __ghdl_index_type; indexes: __ghdl_rti_arr_acc; END RECORD; TYPE __ghdl_rtin_subtype_array IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; basetype: __ghdl_rti_access; bounds: __ghdl_ptr; val_size: __ghdl_ptr; sig_size: __ghdl_ptr; END RECORD; TYPE __ghdl_rtin_type_record IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; nbrel: __ghdl_index_type; elements: __ghdl_rti_arr_acc; END RECORD; TYPE __ghdl_rtin_element IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; eltype: __ghdl_rti_access; val_off: __ghdl_index_type; sig_off: __ghdl_index_type; END RECORD; TYPE __ghdl_rtin_object IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; loc: __ghdl_ptr; obj_type: __ghdl_rti_access; linecol: __ghdl_index_type; END RECORD; TYPE __ghdl_rtin_instance IS RECORD common: __ghdl_rti_common; name: __ghdl_char_ptr; linecol: __ghdl_index_type; loc: __ghdl_ptr; parent: __ghdl_rti_access; instance: __ghdl_rti_access; END RECORD; TYPE __ghdl_rtin_compon
/**
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;
}