[C] Refactoring

This commit is contained in:
seleznevae 2019-08-26 12:57:07 +03:00
parent 051b3145f7
commit 5e62cd959b
4 changed files with 106 additions and 10 deletions

View File

@ -104,6 +104,21 @@ enum str_buf_type {
#endif /* FT_HAVE_WCHAR */ #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; typedef const char **str_arr;
@ -175,7 +190,9 @@ struct conv_context {
char *buf_origin; char *buf_origin;
union { union {
char *buf; char *buf;
#ifdef FT_HAVE_WCHAR
wchar_t *wbuf; wchar_t *wbuf;
#endif
} u; } u;
size_t raw_avail; size_t raw_avail;
struct fort_context *cntx; struct fort_context *cntx;
@ -202,6 +219,9 @@ char *fort_strdup(const char *str);
FT_INTERNAL FT_INTERNAL
size_t number_of_columns_in_format_string(const char *fmt); 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) #if defined(FT_HAVE_WCHAR)
FT_INTERNAL FT_INTERNAL
wchar_t *fort_wcsdup(const wchar_t *str); wchar_t *fort_wcsdup(const wchar_t *str);
@ -3640,6 +3660,26 @@ size_t number_of_columns_in_format_u8string(const void *fmt)
} }
#endif #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 FT_INTERNAL
size_t number_of_columns_in_format_buffer(const string_buffer_t *fmt) 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 VSNPRINTF vsnprintf
#define STR_FILED cstr #define STR_FILED cstr
#define CREATE_ROW_FROM_STRING create_row_from_string #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 #define STR_BUF_TYPE CHAR_BUF
string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE);
if (buffer == NULL) if (buffer == NULL)
return 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; size_t cols = 0;
while (1) { while (1) {
@ -5613,7 +5657,6 @@ clear:
#undef VSNPRINTF #undef VSNPRINTF
#undef STR_FILED #undef STR_FILED
#undef CREATE_ROW_FROM_STRING #undef CREATE_ROW_FROM_STRING
#undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING
#undef STR_BUF_TYPE #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) if (buffer == NULL)
return 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; size_t cols = 0;
while (1) { while (1) {
@ -5652,7 +5700,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
goto clear; 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) { if (cols == cols_origin) {
fort_row_t *row = CREATE_ROW_FROM_STRING(buffer->str.STR_FILED); fort_row_t *row = CREATE_ROW_FROM_STRING(buffer->str.STR_FILED);

View File

@ -192,6 +192,26 @@ size_t number_of_columns_in_format_u8string(const void *fmt)
} }
#endif #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 FT_INTERNAL
size_t number_of_columns_in_format_buffer(const string_buffer_t *fmt) size_t number_of_columns_in_format_buffer(const string_buffer_t *fmt)
{ {

View File

@ -67,6 +67,21 @@ enum str_buf_type {
#endif /* FT_HAVE_WCHAR */ #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; typedef const char **str_arr;
@ -138,7 +153,9 @@ struct conv_context {
char *buf_origin; char *buf_origin;
union { union {
char *buf; char *buf;
#ifdef FT_HAVE_WCHAR
wchar_t *wbuf; wchar_t *wbuf;
#endif
} u; } u;
size_t raw_avail; size_t raw_avail;
struct fort_context *cntx; struct fort_context *cntx;
@ -165,6 +182,9 @@ char *fort_strdup(const char *str);
FT_INTERNAL FT_INTERNAL
size_t number_of_columns_in_format_string(const char *fmt); 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) #if defined(FT_HAVE_WCHAR)
FT_INTERNAL FT_INTERNAL
wchar_t *fort_wcsdup(const wchar_t *str); wchar_t *fort_wcsdup(const wchar_t *str);

View File

@ -601,14 +601,18 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
#define VSNPRINTF vsnprintf #define VSNPRINTF vsnprintf
#define STR_FILED cstr #define STR_FILED cstr
#define CREATE_ROW_FROM_STRING create_row_from_string #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 #define STR_BUF_TYPE CHAR_BUF
string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE);
if (buffer == NULL) if (buffer == NULL)
return 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; size_t cols = 0;
while (1) { while (1) {
@ -673,7 +677,6 @@ clear:
#undef VSNPRINTF #undef VSNPRINTF
#undef STR_FILED #undef STR_FILED
#undef CREATE_ROW_FROM_STRING #undef CREATE_ROW_FROM_STRING
#undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING
#undef STR_BUF_TYPE #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) if (buffer == NULL)
return 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; size_t cols = 0;
while (1) { while (1) {
@ -712,7 +720,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
goto clear; 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) { if (cols == cols_origin) {
fort_row_t *row = CREATE_ROW_FROM_STRING(buffer->str.STR_FILED); fort_row_t *row = CREATE_ROW_FROM_STRING(buffer->str.STR_FILED);