From 5e62cd959b743c812e7695ca3447b09fcb5486c9 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 26 Aug 2019 12:57:07 +0300 Subject: [PATCH] [C] Refactoring --- lib/fort.c | 58 +++++++++++++++++++++++++++++++++++++++++++----- src/fort_utils.c | 20 +++++++++++++++++ src/fort_utils.h | 20 +++++++++++++++++ src/row.c | 18 ++++++++++----- 4 files changed, 106 insertions(+), 10 deletions(-) diff --git a/lib/fort.c b/lib/fort.c index 57d09f7..f809470 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -104,6 +104,21 @@ enum str_buf_type { #endif /* FT_HAVE_WCHAR */ }; +struct ft_gen_string { + union { + char *cstr; +#ifdef FT_HAVE_WCHAR + wchar_t *wstr; +#endif +#ifdef FT_HAVE_UTF8 + void *u8str; +#endif + void *data; + } u; + enum str_buf_type type; + +}; + typedef const char **str_arr; @@ -175,7 +190,9 @@ struct conv_context { char *buf_origin; union { char *buf; +#ifdef FT_HAVE_WCHAR wchar_t *wbuf; +#endif } u; size_t raw_avail; struct fort_context *cntx; @@ -202,6 +219,9 @@ char *fort_strdup(const char *str); FT_INTERNAL size_t number_of_columns_in_format_string(const char *fmt); +FT_INTERNAL +size_t number_of_columns_in_format_string2(const struct ft_gen_string *fmt); + #if defined(FT_HAVE_WCHAR) FT_INTERNAL wchar_t *fort_wcsdup(const wchar_t *str); @@ -3640,6 +3660,26 @@ size_t number_of_columns_in_format_u8string(const void *fmt) } #endif +FT_INTERNAL +size_t number_of_columns_in_format_string2(const struct ft_gen_string *fmt) +{ + switch (fmt->type) { + case CHAR_BUF: + return number_of_columns_in_format_string(fmt->u.cstr); +#ifdef FT_HAVE_WCHAR + case W_CHAR_BUF: + return number_of_columns_in_format_wstring(fmt->u.wstr); +#endif /* FT_HAVE_WCHAR */ +#ifdef FT_HAVE_UTF8 + case UTF8_BUF: + return number_of_columns_in_format_u8string(fmt->u.u8str); +#endif /* FT_HAVE_UTF8 */ + default: + assert(0); + } + return 0; +} + FT_INTERNAL size_t number_of_columns_in_format_buffer(const string_buffer_t *fmt) { @@ -5541,14 +5581,18 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) #define VSNPRINTF vsnprintf #define STR_FILED cstr #define CREATE_ROW_FROM_STRING create_row_from_string -#define NUMBER_OF_COLUMNS_IN_FORMAT_STRING number_of_columns_in_format_string #define STR_BUF_TYPE CHAR_BUF string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); if (buffer == NULL) return NULL; - size_t cols_origin = NUMBER_OF_COLUMNS_IN_FORMAT_STRING(fmt); + /* tmp: remove after refactoring */ + struct ft_gen_string fmt_str; + fmt_str.type = CHAR_BUF; + fmt_str.u.cstr = fmt; + + size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols = 0; while (1) { @@ -5613,7 +5657,6 @@ clear: #undef VSNPRINTF #undef STR_FILED #undef CREATE_ROW_FROM_STRING -#undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING #undef STR_BUF_TYPE } @@ -5631,7 +5674,12 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) if (buffer == NULL) return NULL; - size_t cols_origin = NUMBER_OF_COLUMNS_IN_FORMAT_STRING(fmt); + /* tmp: remove after refactoring */ + struct ft_gen_string fmt_str; + fmt_str.type = W_CHAR_BUF; + fmt_str.u.wstr = fmt; + + size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols = 0; while (1) { @@ -5652,7 +5700,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) goto clear; } - cols = NUMBER_OF_COLUMNS_IN_FORMAT_STRING(buffer->str.STR_FILED); + cols = number_of_columns_in_format_buffer(buffer); if (cols == cols_origin) { fort_row_t *row = CREATE_ROW_FROM_STRING(buffer->str.STR_FILED); diff --git a/src/fort_utils.c b/src/fort_utils.c index 66c23b7..b0446dd 100644 --- a/src/fort_utils.c +++ b/src/fort_utils.c @@ -192,6 +192,26 @@ size_t number_of_columns_in_format_u8string(const void *fmt) } #endif +FT_INTERNAL +size_t number_of_columns_in_format_string2(const struct ft_gen_string *fmt) +{ + switch (fmt->type) { + case CHAR_BUF: + return number_of_columns_in_format_string(fmt->u.cstr); +#ifdef FT_HAVE_WCHAR + case W_CHAR_BUF: + return number_of_columns_in_format_wstring(fmt->u.wstr); +#endif /* FT_HAVE_WCHAR */ +#ifdef FT_HAVE_UTF8 + case UTF8_BUF: + return number_of_columns_in_format_u8string(fmt->u.u8str); +#endif /* FT_HAVE_UTF8 */ + default: + assert(0); + } + return 0; +} + FT_INTERNAL size_t number_of_columns_in_format_buffer(const string_buffer_t *fmt) { diff --git a/src/fort_utils.h b/src/fort_utils.h index 37d97a1..84b53b6 100644 --- a/src/fort_utils.h +++ b/src/fort_utils.h @@ -67,6 +67,21 @@ enum str_buf_type { #endif /* FT_HAVE_WCHAR */ }; +struct ft_gen_string { + union { + char *cstr; +#ifdef FT_HAVE_WCHAR + wchar_t *wstr; +#endif +#ifdef FT_HAVE_UTF8 + void *u8str; +#endif + void *data; + } u; + enum str_buf_type type; + +}; + typedef const char **str_arr; @@ -138,7 +153,9 @@ struct conv_context { char *buf_origin; union { char *buf; +#ifdef FT_HAVE_WCHAR wchar_t *wbuf; +#endif } u; size_t raw_avail; struct fort_context *cntx; @@ -165,6 +182,9 @@ char *fort_strdup(const char *str); FT_INTERNAL size_t number_of_columns_in_format_string(const char *fmt); +FT_INTERNAL +size_t number_of_columns_in_format_string2(const struct ft_gen_string *fmt); + #if defined(FT_HAVE_WCHAR) FT_INTERNAL wchar_t *fort_wcsdup(const wchar_t *str); diff --git a/src/row.c b/src/row.c index 93d11f3..1ce31b9 100644 --- a/src/row.c +++ b/src/row.c @@ -601,14 +601,18 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) #define VSNPRINTF vsnprintf #define STR_FILED cstr #define CREATE_ROW_FROM_STRING create_row_from_string -#define NUMBER_OF_COLUMNS_IN_FORMAT_STRING number_of_columns_in_format_string #define STR_BUF_TYPE CHAR_BUF string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); if (buffer == NULL) return NULL; - size_t cols_origin = NUMBER_OF_COLUMNS_IN_FORMAT_STRING(fmt); + /* tmp: remove after refactoring */ + struct ft_gen_string fmt_str; + fmt_str.type = CHAR_BUF; + fmt_str.u.cstr = fmt; + + size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols = 0; while (1) { @@ -673,7 +677,6 @@ clear: #undef VSNPRINTF #undef STR_FILED #undef CREATE_ROW_FROM_STRING -#undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING #undef STR_BUF_TYPE } @@ -691,7 +694,12 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) if (buffer == NULL) return NULL; - size_t cols_origin = NUMBER_OF_COLUMNS_IN_FORMAT_STRING(fmt); + /* tmp: remove after refactoring */ + struct ft_gen_string fmt_str; + fmt_str.type = W_CHAR_BUF; + fmt_str.u.wstr = fmt; + + size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols = 0; while (1) { @@ -712,7 +720,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) goto clear; } - cols = NUMBER_OF_COLUMNS_IN_FORMAT_STRING(buffer->str.STR_FILED); + cols = number_of_columns_in_format_buffer(buffer); if (cols == cols_origin) { fort_row_t *row = CREATE_ROW_FROM_STRING(buffer->str.STR_FILED);