[C] Improved encapsulation

This commit is contained in:
seleznevae 2018-01-17 21:34:15 +03:00
parent d2798da48d
commit 379af00762
6 changed files with 31 additions and 23 deletions

View File

@ -9,11 +9,6 @@
/*****************************************************************************
* CELL
* ***************************************************************************/
//struct fort_cell
//{
// string_buffer_t *str_buffer;
// fort_table_options_t *options;
//};
fort_cell_t * create_cell();

View File

@ -135,7 +135,7 @@ static int ft_row_printf_impl(FTABLE *FORT_RESTRICT table, size_t row, const cha
*cur_row_p = new_row;
table->cur_col = 0;
table->cur_row++;
return vector_size(new_row->cells);
return columns_in_row(new_row);
clear:
destroy_row(new_row);
@ -151,7 +151,7 @@ int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ..
if (result >= 0 && table->rows) {
int sz = vector_size(table->rows);
if (sz != 0) {
(*(fort_row_t**)vector_at(table->rows, sz - 1))->is_header = F_TRUE;
set_row_type(*(fort_row_t**)vector_at(table->rows, sz - 1), Header);
}
}
return result;

View File

@ -5,6 +5,14 @@
#include "vector.h"
#include "ctype.h"
struct fort_row
{
vector_t *cells;
enum RowType type;
};
fort_row_t * create_row()
{
fort_row_t * row = F_CALLOC(sizeof(fort_row_t), 1);
@ -15,7 +23,8 @@ fort_row_t * create_row()
F_FREE(row);
return NULL;
}
row->is_header = F_FALSE;
// row->is_header = F_FALSE;
row->type = Common;
return row;
}
@ -118,7 +127,7 @@ int print_row_separator(char *buffer, size_t buffer_sz,
const fort_row_t *main_row = NULL;
if (upper_row != NULL && lower_row != NULL) {
main_row = lower_row->is_header == F_TRUE ? lower_row : upper_row;
main_row = lower_row->type == Header ? lower_row : upper_row;
} else if (upper_row != NULL && lower_row == NULL) {
main_row = upper_row;
} else if (upper_row == NULL && lower_row != NULL) {
@ -137,7 +146,7 @@ int print_row_separator(char *buffer, size_t buffer_sz,
const char *R = NULL;
const char (*border_chars)[BorderItemPosSize] = NULL;
if (main_row && main_row->is_header == F_TRUE) {
if (main_row && main_row->type == Header) {
border_chars = &context->header_border_chars;
} else {
border_chars = &context->border_chars;
@ -333,7 +342,7 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
* L data IV data IV data R
*/
const char (*bord_chars)[BorderItemPosSize] = (row->is_header)
const char (*bord_chars)[BorderItemPosSize] = (row->type == Header)
? (&context->header_border_chars)
: (&context->border_chars);
const char *L = &(*bord_chars)[LL_bip];
@ -361,3 +370,11 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
}
return dev;
}
void set_row_type(fort_row_t *row, enum RowType type)
{
assert(row);
row->type = type;
}

View File

@ -7,12 +7,12 @@
struct fort_row;
typedef struct fort_row fort_row_t;
struct fort_row
{
vector_t *cells;
enum F_BOOL is_header;
};
enum RowType
{
Common,
Header
};
fort_row_t * create_row();
@ -44,5 +44,6 @@ fort_row_t* create_row_from_fmt_string(const char* FORT_RESTRICT fmt, va_list *v
int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col_width_arr, size_t col_width_arr_sz,
size_t row_height, const context_t *context);
void set_row_type(fort_row_t *row, enum RowType type);
#endif // ROW_H

View File

@ -5,6 +5,7 @@
/*****************************************************************************
* STRING BUFFER
* ***************************************************************************/
string_buffer_t* create_string_buffer(size_t sz)
{
string_buffer_t *result = (string_buffer_t *)F_MALLOC(sizeof(string_buffer_t));
@ -130,3 +131,4 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, size_t table_colum
written += snprint_n_chars(buf + written, buf_len - written, right, ' ');
return written;
}

View File

@ -65,13 +65,6 @@ string_buffer_t * get_cur_str_buffer_and_create_if_not_exists(FTABLE *FORT_RESTR
if (cell == NULL)
return NULL;
// if (cell->str_buffer == NULL) {
// cell->str_buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE);
// if (cell->str_buffer == NULL)
// return NULL;
// }
// return cell->str_buffer;
return cell_get_string_buffer(cell);
}