[C] Refactoring

This commit is contained in:
seleznevae 2018-02-26 21:42:48 +03:00
parent b7666160c4
commit 284cb61321
8 changed files with 53 additions and 40 deletions

View File

@ -41,7 +41,7 @@ int hint_width_cell(const fort_cell_t *cell, const context_t *context)
{
assert(cell);
assert(context);
int result = context->cell_padding_left + context->cell_padding_right;
int result = context->table_options->cell_padding_left + context->table_options->cell_padding_right;
if (cell->str_buffer && cell->str_buffer->str) {
result += buffer_text_width(cell->str_buffer);
}
@ -52,10 +52,10 @@ int hint_height_cell(const fort_cell_t *cell, const context_t *context)
{
assert(cell);
assert(context);
int result = context->cell_padding_top + context->cell_padding_bottom;
int result = context->table_options->cell_padding_top + context->table_options->cell_padding_bottom;
if (cell->str_buffer && cell->str_buffer->str) {
size_t text_height = buffer_text_height(cell->str_buffer);
result += text_height == 0 ? context->cell_empty_string_height : text_height;
result += text_height == 0 ? context->table_options->cell_empty_string_height : text_height;
}
return result;
}
@ -89,19 +89,19 @@ int cell_printf(fort_cell_t *cell, size_t row, size_t column, char *buf, size_t
}
if (row >= hint_height_cell(cell, context)
|| row < context->cell_padding_top
|| row >= (context->cell_padding_top + buffer_text_height(cell->str_buffer))) {
|| row < context->table_options->cell_padding_top
|| row >= (context->table_options->cell_padding_top + buffer_text_height(cell->str_buffer))) {
int k = snprint_n_chars(buf, buf_len, buf_len - 1, ' ');
return k;
} else {
int written = 0;
int left = context->cell_padding_left;
int right = context->cell_padding_right;
int left = context->table_options->cell_padding_left;
int right = context->table_options->cell_padding_right;
written += snprint_n_chars(buf + written, buf_len - written, left, ' ');
if (cell->str_buffer)
written += buffer_printf(cell->str_buffer, row - context->cell_padding_top, column, buf + written, buf_len - written - right, context);
written += buffer_printf(cell->str_buffer, row - context->table_options->cell_padding_top, column, buf + written, buf_len - written - right, context);
else
written += snprint_n_chars(buf + written, buf_len - written, buf_len - written - right, ' ');
written += snprint_n_chars(buf + written, buf_len - written, right, ' ');

View File

@ -549,7 +549,8 @@ const char* ft_to_string(const FTABLE *FORT_RESTRICT table)
int dev = 0;
int k = 0;
context_t *context = (table->options ? table->options : &g_table_options);
context_t context;
context.table_options = (table->options ? table->options : &g_table_options);
fort_row_t *prev_row = NULL;
fort_row_t *cur_row = NULL;
separator_t *cur_sep = NULL;
@ -559,13 +560,14 @@ const char* ft_to_string(const FTABLE *FORT_RESTRICT table)
cur_sep = (i < sep_size) ? (*(separator_t **)vector_at(table->separators, i)) : NULL;
cur_row = *(fort_row_t**)vector_at(table->rows, i);
enum HorSeparatorPos separatorPos = (i == 0) ? TopSeparator : InsideSeparator;
CHECK_RESULT_AND_MOVE_DEV(print_row_separator(buffer + dev, sz - dev, col_width_arr, cols, prev_row, cur_row, separatorPos, cur_sep, context));
CHECK_RESULT_AND_MOVE_DEV(snprintf_row(cur_row, buffer + dev, sz - dev, col_width_arr, cols, row_height_arr[i], context));
CHECK_RESULT_AND_MOVE_DEV(print_row_separator(buffer + dev, sz - dev, col_width_arr, cols, prev_row, cur_row, separatorPos, cur_sep, &context));
context.row = i;
CHECK_RESULT_AND_MOVE_DEV(snprintf_row(cur_row, buffer + dev, sz - dev, col_width_arr, cols, row_height_arr[i], &context));
prev_row = cur_row;
}
cur_row = NULL;
cur_sep = (i < sep_size) ? (*(separator_t **)vector_at(table->separators, i)) : NULL;
CHECK_RESULT_AND_MOVE_DEV(print_row_separator(buffer + dev, sz - dev, col_width_arr, cols, prev_row, cur_row, BottomSeparator, cur_sep, context));
CHECK_RESULT_AND_MOVE_DEV(print_row_separator(buffer + dev, sz - dev, col_width_arr, cols, prev_row, cur_row, BottomSeparator, cur_sep, &context));
F_FREE(col_width_arr);

View File

@ -85,7 +85,13 @@ struct separator
};
typedef struct fort_table_options fort_table_options_t;
typedef fort_table_options_t context_t;
struct fort_context
{
fort_table_options_t *table_options;
size_t row;
size_t column;
};
typedef struct fort_context context_t;
typedef struct fort_column_options fort_column_options_t;
typedef struct vector vector_t;
typedef struct fort_cell fort_cell_t;

View File

@ -332,19 +332,19 @@ int fort_options_column_width(const fort_table_options_t *options, size_t column
// return ((fort_column_options_t*)vector_at(options->col_options, column))->align;
//}
int fort_options_column_alignment(const fort_table_options_t *options, size_t column)
{
assert(options);
enum TextAlignment defaultAlign = g_column_options.align;
//int fort_options_column_alignment(const fort_table_options_t *options, size_t column)
//{
// assert(options);
// enum TextAlignment defaultAlign = g_column_options.align;
if (options->cell_options == NULL)
return defaultAlign;
// if (options->cell_options == NULL)
// return defaultAlign;
const fort_cell_options_t* col_opt = cget_cell_opt(options->cell_options, FT_ANY_ROW, column);
if (col_opt == NULL || ((col_opt->options & FT_OPT_TEXT_ALIGN) == 0))
return defaultAlign;
else {
return col_opt->align;
}
}
// const fort_cell_options_t* col_opt = cget_cell_opt(options->cell_options, FT_ANY_ROW, column);
// if (col_opt == NULL || ((col_opt->options & FT_OPT_TEXT_ALIGN) == 0))
// return defaultAlign;
// else {
// return col_opt->align;
// }
//}

View File

@ -141,7 +141,7 @@ struct fort_table_options
fort_cell_opt_container_t * cell_options;
};
typedef struct fort_table_options fort_table_options_t;
typedef fort_table_options_t context_t;
//typedef fort_table_options_t context_t;
extern fort_table_options_t g_table_options;
@ -152,7 +152,7 @@ void destroy_table_options(fort_table_options_t* options);
//fort_status_t fort_options_set_column_min_width(fort_table_options_t *options, size_t column, size_t width);
//fort_status_t fort_options_set_column_alignment(fort_table_options_t *options, size_t column, enum TextAlignment al);
int fort_options_column_width(const fort_table_options_t *options, size_t column);
int fort_options_column_alignment(const fort_table_options_t *options, size_t column);
//int fort_options_column_alignment(const fort_table_options_t *options, size_t column);
#endif // OPTIONS_H

View File

@ -148,16 +148,16 @@ int print_row_separator(char *buffer, size_t buffer_sz,
const char (*border_chars)[BorderItemPosSize] = NULL;
if (main_row && main_row->type == Header) {
border_chars = &context->header_border_chars;
border_chars = &context->table_options->header_border_chars;
} else {
border_chars = &context->border_chars;
border_chars = &context->table_options->border_chars;
}
if (sep && sep->enabled) {
L = &(context->separator_chars[LH_sip]);
I = &(context->separator_chars[IH_sip]);
IV = &(context->separator_chars[II_sip]);
R = &(context->separator_chars[RH_sip]);
L = &(context->table_options->separator_chars[LH_sip]);
I = &(context->table_options->separator_chars[IH_sip]);
IV = &(context->table_options->separator_chars[II_sip]);
R = &(context->table_options->separator_chars[RH_sip]);
} else {
switch (separatorPos) {
case TopSeparator:
@ -351,8 +351,8 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
*/
const char (*bord_chars)[BorderItemPosSize] = (row->type == Header)
? (&context->header_border_chars)
: (&context->border_chars);
? (&context->table_options->header_border_chars)
: (&context->table_options->border_chars);
const char *L = &(*bord_chars)[LL_bip];
const char *IV = &(*bord_chars)[IV_bip];
const char *R = &(*bord_chars)[RR_bip];
@ -362,6 +362,7 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
for (size_t i = 0; i < row_height; ++i) {
dev += snprint_n_chars(buffer + dev, buf_sz - dev, 1, *L);
for (size_t j = 0; j < col_width_arr_sz; ++j) {
((context_t *)context)->column = j;
if (j < cols_in_row) {
fort_cell_t *cell = *(fort_cell_t**)vector_at(row->cells, j);
dev += cell_printf(cell, i, j, buffer + dev, col_width_arr[j] + 1, context);

View File

@ -161,7 +161,8 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, size_t table_colum
int left = 0;
int right = 0;
switch (fort_options_column_alignment(context, table_column)) {
// switch (fort_options_column_alignment(context, table_column)) {
switch (get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_TEXT_ALIGN)) {
case LeftAligned:
left = 0;
right = (buf_len - 1) - content_width;

View File

@ -112,15 +112,18 @@ fort_status_t table_rows_and_cols_geometry(const FTABLE *table,
return F_ERROR;
}
context_t *context = (table->options ? table->options : &g_table_options);
context_t context;
context.table_options = (table->options ? table->options : &g_table_options);
for (size_t col = 0; col < cols; ++col) {
col_width_arr[col] = 0;
for (size_t row = 0; row < rows; ++row) {
const fort_row_t *row_p = get_row_c(table, row);
const fort_cell_t *cell = get_cell_c(row_p, col);
context.column = col;
context.row = row;
if (cell) {
col_width_arr[col] = MAX(col_width_arr[col], hint_width_cell(cell, context));
row_height_arr[row] = MAX(row_height_arr[row], hint_height_cell(cell, context));
col_width_arr[col] = MAX(col_width_arr[col], hint_width_cell(cell, &context));
row_height_arr[row] = MAX(row_height_arr[row], hint_height_cell(cell, &context));
}
}
}