[C] Monor code decorations

This commit is contained in:
seleznevae 2018-02-27 21:53:00 +03:00
parent 441442fa9e
commit ff3ba95d85
2 changed files with 55 additions and 178 deletions

View File

@ -87,59 +87,16 @@ SOFTWARE.
#endif
/*
* Helper macros
*/
#define STR_2_CAT_(arg1, arg2) \
arg1##arg2
#define STR_2_CAT(arg1, arg2) \
STR_2_CAT_(arg1, arg2)
/*
* libfort structures and functions declarations
*/
FORT_BEGIN_DECLS
struct fort_table;
typedef struct fort_table FTABLE;
FORT_EXTERN FTABLE * ft_create_table(void);
FORT_EXTERN void ft_destroy_table(FTABLE *FORT_RESTRICT table);
FORT_EXTERN void ft_ln(FTABLE *FORT_RESTRICT table);
#if defined(FORT_CLANG_COMPILER) || defined(FORT_GCC_COMPILER)
FORT_EXTERN int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
#else
FORT_EXTERN int ft_printf_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_printf_ln_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf_ln_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
#define ft_printf(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_impl(table, __VA_ARGS__))
#define ft_printf_ln(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_ln_impl(table, __VA_ARGS__))
#define ft_hdr_printf(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf_impl(table, __VA_ARGS__))
#define ft_hdr_printf_ln(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf_ln_impl(table, __VA_ARGS__))
#endif
FORT_EXTERN int ft_write(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content);
FORT_EXTERN int ft_write_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content);
static inline int fort_check_if_string_helper(const char*str)
{
(void)str;
@ -198,6 +155,56 @@ static inline int fort_check_if_string_helper(const char*str)
#define CHECK_IF_ARGS_ARE_STRINGS_(basis, n, ...) CHECK_IF_ARGS_ARE_STRINGS__(STR_2_CAT_(basis, n), __VA_ARGS__)
#define CHECK_IF_ARGS_ARE_STRINGS(...) CHECK_IF_ARGS_ARE_STRINGS_(CHECK_IF_ARG_IS_STRING_,PP_NARG(__VA_ARGS__), __VA_ARGS__)
/*
* libfort structures and functions declarations
*/
FORT_BEGIN_DECLS
struct fort_table;
typedef struct fort_table FTABLE;
FORT_EXTERN FTABLE * ft_create_table(void);
FORT_EXTERN void ft_destroy_table(FTABLE *FORT_RESTRICT table);
FORT_EXTERN void ft_ln(FTABLE *FORT_RESTRICT table);
#if defined(FORT_CLANG_COMPILER) || defined(FORT_GCC_COMPILER)
FORT_EXTERN int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
#else
FORT_EXTERN int ft_printf_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_printf_ln_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
FORT_EXTERN int ft_hdr_printf_ln_impl(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) FORT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
#define ft_printf(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_impl(table, __VA_ARGS__))
#define ft_printf_ln(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_ln_impl(table, __VA_ARGS__))
#define ft_hdr_printf(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf_impl(table, __VA_ARGS__))
#define ft_hdr_printf_ln(table, ...) \
(( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf_ln_impl(table, __VA_ARGS__))
#endif
FORT_EXTERN int ft_write(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content);
FORT_EXTERN int ft_write_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content);
#define FT_NWRITE(table, ...)\
(0 ? CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite(table, PP_NARG(__VA_ARGS__), __VA_ARGS__))
@ -208,10 +215,6 @@ FORT_EXTERN int ft_nwrite(FTABLE *FORT_RESTRICT table, size_t n, const char* FOR
FORT_EXTERN int ft_nwrite_ln(FTABLE *FORT_RESTRICT table, size_t n, const char* FORT_RESTRICT cell_content, ...);
FORT_EXTERN int ft_write_status(FTABLE *FORT_RESTRICT table, int status, const char* FORT_RESTRICT cell_content);
FORT_EXTERN int ft_row_write(FTABLE *FORT_RESTRICT table, size_t cols, const char* FORT_RESTRICT row_cells[]);
FORT_EXTERN int ft_row_write_ln(FTABLE *FORT_RESTRICT table, size_t cols, const char* FORT_RESTRICT row_cells[]);
@ -237,22 +240,12 @@ FORT_EXTERN int ft_add_separator(FTABLE *FORT_RESTRICT table);
FORT_EXTERN const char* ft_to_string(const FTABLE *FORT_RESTRICT table);
//FORT_EXTERN ssize_t ft_n_to_string(const FTABLE *FORT_RESTRICT table, char *FORT_RESTRICT dst, size_t dst_len);
//FORT_EXTERN char* ft_header_to_string(const FTABLE *FORT_RESTRICT table);
//FORT_EXTERN ssize_t ft_n_header_to_string(const FTABLE *FORT_RESTRICT table, char *FORT_RESTRICT dst, size_t dst_len);
//FORT_EXTERN char* ft_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row);
//FORT_EXTERN ssize_t ft_n_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row, char *FORT_RESTRICT dst, size_t dst_len);
//FORT_EXTERN int ft_set_default_options(const fort_table_options_t *options);
//FORT_EXTERN int ft_get_default_options(fort_table_options_t *options);
//FORT_EXTERN int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options);
struct border_chars
@ -267,11 +260,9 @@ struct border_chars
FORT_EXTERN int ft_set_default_borders(struct border_chars *border_chs, struct border_chars *header_border_chs);
FORT_EXTERN int ft_set_table_borders(FTABLE * FORT_RESTRICT table, struct border_chars *border_chs, struct border_chars *header_border_chs);
//FORT_EXTERN int ft_set_default_option(uint32_t option, int value);
//FORT_EXTERN int ft_set_table_option(FTABLE * FORT_RESTRICT table, uint32_t option, int value);
FORT_EXTERN int ft_set_cell_option(FTABLE * FORT_RESTRICT table, unsigned row, unsigned col, uint32_t option, int value);
FORT_EXTERN int ft_set_default_cell_option(uint32_t option, int value);
FORT_EXTERN int ft_set_cell_option(FTABLE * FORT_RESTRICT table, unsigned row, unsigned col, uint32_t option, int value);
FORT_END_DECLS

View File

@ -296,17 +296,6 @@ FORT_EXTERN int ft_nwrite_ln(FTABLE *FORT_RESTRICT table, size_t n, const char*
}
FORT_EXTERN int ft_write_status(FTABLE *FORT_RESTRICT table, int status, const char* FORT_RESTRICT cell_content)
{
assert(table);
if (IS_ERROR(status))
return status;
return ft_write(table, cell_content);
}
FORT_EXTERN int ft_row_write(FTABLE *FORT_RESTRICT table, size_t cols, const char* FORT_RESTRICT cells[])
{
assert(table);
@ -332,12 +321,6 @@ FORT_EXTERN int ft_row_write_ln(FTABLE *FORT_RESTRICT table, size_t cols, const
//FORT_EXTERN int ft_table_write(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, const char** FORT_RESTRICT table_cells[]);
//FORT_EXTERN int ft_table_write_ln(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, const char** FORT_RESTRICT table_cells[]);
//int ft_table_write(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, const char* FORT_RESTRICT table_cells[rows][cols]);
//int ft_table_write_ln(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, const char** FORT_RESTRICT table_cells[rows][cols]);
int ft_s_table_write(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, const char* FORT_RESTRICT table_cells[rows][cols])
{
@ -394,47 +377,6 @@ int ft_table_write_ln(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, con
//int ft_set_default_options(const fort_table_options_t *options)
//{
// memcpy(&g_table_options, options, sizeof(fort_table_options_t));
// return 0;
//}
//int ft_get_default_options(fort_table_options_t *options)
//{
// memcpy(options, &g_table_options, sizeof(fort_table_options_t));
// return 0;
//}
//int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options)
//{
// assert(table);
// if (options == NULL) {
// destroy_table_options(table->options);
// table->options = NULL;
// return 0;
// }
// fort_table_options_t *new_options = copy_table_options(options);
// if (new_options == NULL) {
// return -1;
// }
// destroy_table_options(table->options);
// table->options = new_options;
// return 0;
//// fort_table_options_t *new_options = F_CALLOC(sizeof(fort_table_options_t), 1);
//// if (new_options == NULL) {
//// return -1;
//// }
//// memcpy(new_options, options, sizeof(fort_table_options_t));
//// F_FREE(table->options);
//// table->options = new_options;
//// return 0;
//}
@ -575,30 +517,6 @@ int ft_add_separator(FTABLE *table)
return F_SUCCESS;
}
//int ft_set_default_option(uint32_t option, int value)
//{
// switch (option) {
// case FT_OPT_TOP_PADDING:
// g_table_options.cell_padding_top = value;
// break;
// case FT_OPT_BOTTOM_PADDING:
// g_table_options.cell_padding_bottom = value;
// break;
// case FT_OPT_LEFT_PADDING:
// g_table_options.cell_padding_left = value;
// break;
// case FT_OPT_RIGHT_PADDING:
// g_table_options.cell_padding_right = value;
// break;
// case FT_OPT_EMPTY_STR_HEIGHT:
// g_table_options.cell_empty_string_height = value;
// break;
// default:
// // todo
// exit(22);
// }
// return F_SUCCESS;
//}
static void set_border_options_for_options(fort_table_options_t *options, struct border_chars *border_chs, struct border_chars *header_border_chs)
@ -667,38 +585,6 @@ int ft_set_table_borders(FTABLE *table, struct border_chars *border_chs, struct
return F_SUCCESS;
}
//int ft_set_table_option(FTABLE *table, uint32_t option, int value)
//{
// assert(table);
// if (table->options == NULL) {
// table->options = create_table_options();
// if (table->options == NULL)
// return F_MEMORY_ERROR;
// }
// switch (option) {
// case FT_OPT_TOP_PADDING:
// table->options->cell_padding_top = value;
// break;
// case FT_OPT_BOTTOM_PADDING:
// table->options->cell_padding_bottom = value;
// break;
// case FT_OPT_LEFT_PADDING:
// table->options->cell_padding_left = value;
// break;
// case FT_OPT_RIGHT_PADDING:
// table->options->cell_padding_right = value;
// break;
// case FT_OPT_EMPTY_STR_HEIGHT:
// table->options->cell_empty_string_height = value;
// break;
// default:
// // todo
// exit(22);
// }
// return F_SUCCESS;
//}
int ft_set_cell_option(FTABLE *table, unsigned row, unsigned col, uint32_t option, int value)