[C] Refactoring

This commit is contained in:
seleznevae 2019-08-26 13:21:11 +03:00
parent d2bed0fdf1
commit ae2f363fd4
2 changed files with 50 additions and 24 deletions

View File

@ -5595,11 +5595,30 @@ fort_row_t *create_row_from_buffer(const string_buffer_t *buffer)
} }
} }
static int
vsnprintf_buffer(string_buffer_t *buffer, const struct ft_gen_string *fmt,
va_list *va)
{
switch (buffer->type) {
case CHAR_BUF:
return vsnprintf(buffer->str.cstr, string_buffer_width_capacity(buffer), fmt->u.cstr, *va);
#ifdef FT_HAVE_WCHAR
case W_CHAR_BUF:
return vswprintf(buffer->str.wstr, string_buffer_width_capacity(buffer), fmt->u.wstr, *va);
#endif
#ifdef FT_HAVE_UTF8
case UTF8_BUF:
return vsnprintf(buffer->str.cstr, string_buffer_width_capacity(buffer), fmt->u.cstr, *va);
#endif
default:
assert(0);
return 0;
}
}
FT_INTERNAL FT_INTERNAL
fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
{ {
#define VSNPRINTF vsnprintf
#define STR_FILED cstr
#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);
@ -5608,7 +5627,7 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
/* tmp: remove after refactoring */ /* tmp: remove after refactoring */
struct ft_gen_string fmt_str; struct ft_gen_string fmt_str;
fmt_str.type = CHAR_BUF; fmt_str.type = STR_BUF_TYPE;
fmt_str.u.cstr = fmt; fmt_str.u.cstr = fmt;
size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols_origin = number_of_columns_in_format_string2(&fmt_str);
@ -5617,7 +5636,7 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
while (1) { while (1) {
va_list va; va_list va;
va_copy(va, *va_args); va_copy(va, *va_args);
int virtual_sz = VSNPRINTF(buffer->str.STR_FILED, string_buffer_width_capacity(buffer), fmt, va); int virtual_sz = vsnprintf_buffer(buffer, &fmt_str, &va);
va_end(va); va_end(va);
/* If error encountered */ /* If error encountered */
if (virtual_sz < 0) if (virtual_sz < 0)
@ -5673,8 +5692,6 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
clear: clear:
destroy_string_buffer(buffer); destroy_string_buffer(buffer);
return NULL; return NULL;
#undef VSNPRINTF
#undef STR_FILED
#undef STR_BUF_TYPE #undef STR_BUF_TYPE
} }
@ -5682,8 +5699,6 @@ clear:
FT_INTERNAL FT_INTERNAL
fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
{ {
#define VSNPRINTF vswprintf
#define STR_FILED wstr
#define STR_BUF_TYPE W_CHAR_BUF #define STR_BUF_TYPE W_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);
@ -5692,7 +5707,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
/* tmp: remove after refactoring */ /* tmp: remove after refactoring */
struct ft_gen_string fmt_str; struct ft_gen_string fmt_str;
fmt_str.type = W_CHAR_BUF; fmt_str.type = STR_BUF_TYPE;
fmt_str.u.wstr = fmt; fmt_str.u.wstr = fmt;
size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols_origin = number_of_columns_in_format_string2(&fmt_str);
@ -5701,7 +5716,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
while (1) { while (1) {
va_list va; va_list va;
va_copy(va, *va_args); va_copy(va, *va_args);
int virtual_sz = VSNPRINTF(buffer->str.STR_FILED, string_buffer_width_capacity(buffer), fmt, va); int virtual_sz = vsnprintf_buffer(buffer, &fmt_str, &va);
va_end(va); va_end(va);
/* If error encountered */ /* If error encountered */
if (virtual_sz < 0) if (virtual_sz < 0)
@ -5758,8 +5773,6 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
clear: clear:
destroy_string_buffer(buffer); destroy_string_buffer(buffer);
return NULL; return NULL;
#undef VSNPRINTF
#undef STR_FILED
#undef STR_BUF_TYPE #undef STR_BUF_TYPE
} }
#endif #endif

View File

@ -615,11 +615,30 @@ fort_row_t *create_row_from_buffer(const string_buffer_t *buffer)
} }
} }
static int
vsnprintf_buffer(string_buffer_t *buffer, const struct ft_gen_string *fmt,
va_list *va)
{
switch (buffer->type) {
case CHAR_BUF:
return vsnprintf(buffer->str.cstr, string_buffer_width_capacity(buffer), fmt->u.cstr, *va);
#ifdef FT_HAVE_WCHAR
case W_CHAR_BUF:
return vswprintf(buffer->str.wstr, string_buffer_width_capacity(buffer), fmt->u.wstr, *va);
#endif
#ifdef FT_HAVE_UTF8
case UTF8_BUF:
return vsnprintf(buffer->str.cstr, string_buffer_width_capacity(buffer), fmt->u.cstr, *va);
#endif
default:
assert(0);
return 0;
}
}
FT_INTERNAL FT_INTERNAL
fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
{ {
#define VSNPRINTF vsnprintf
#define STR_FILED cstr
#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);
@ -628,7 +647,7 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
/* tmp: remove after refactoring */ /* tmp: remove after refactoring */
struct ft_gen_string fmt_str; struct ft_gen_string fmt_str;
fmt_str.type = CHAR_BUF; fmt_str.type = STR_BUF_TYPE;
fmt_str.u.cstr = fmt; fmt_str.u.cstr = fmt;
size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols_origin = number_of_columns_in_format_string2(&fmt_str);
@ -637,7 +656,7 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
while (1) { while (1) {
va_list va; va_list va;
va_copy(va, *va_args); va_copy(va, *va_args);
int virtual_sz = VSNPRINTF(buffer->str.STR_FILED, string_buffer_width_capacity(buffer), fmt, va); int virtual_sz = vsnprintf_buffer(buffer, &fmt_str, &va);
va_end(va); va_end(va);
/* If error encountered */ /* If error encountered */
if (virtual_sz < 0) if (virtual_sz < 0)
@ -693,8 +712,6 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args)
clear: clear:
destroy_string_buffer(buffer); destroy_string_buffer(buffer);
return NULL; return NULL;
#undef VSNPRINTF
#undef STR_FILED
#undef STR_BUF_TYPE #undef STR_BUF_TYPE
} }
@ -702,8 +719,6 @@ clear:
FT_INTERNAL FT_INTERNAL
fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
{ {
#define VSNPRINTF vswprintf
#define STR_FILED wstr
#define STR_BUF_TYPE W_CHAR_BUF #define STR_BUF_TYPE W_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);
@ -712,7 +727,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
/* tmp: remove after refactoring */ /* tmp: remove after refactoring */
struct ft_gen_string fmt_str; struct ft_gen_string fmt_str;
fmt_str.type = W_CHAR_BUF; fmt_str.type = STR_BUF_TYPE;
fmt_str.u.wstr = fmt; fmt_str.u.wstr = fmt;
size_t cols_origin = number_of_columns_in_format_string2(&fmt_str); size_t cols_origin = number_of_columns_in_format_string2(&fmt_str);
@ -721,7 +736,7 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
while (1) { while (1) {
va_list va; va_list va;
va_copy(va, *va_args); va_copy(va, *va_args);
int virtual_sz = VSNPRINTF(buffer->str.STR_FILED, string_buffer_width_capacity(buffer), fmt, va); int virtual_sz = vsnprintf_buffer(buffer, &fmt_str, &va);
va_end(va); va_end(va);
/* If error encountered */ /* If error encountered */
if (virtual_sz < 0) if (virtual_sz < 0)
@ -778,8 +793,6 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args)
clear: clear:
destroy_string_buffer(buffer); destroy_string_buffer(buffer);
return NULL; return NULL;
#undef VSNPRINTF
#undef STR_FILED
#undef STR_BUF_TYPE #undef STR_BUF_TYPE
} }
#endif #endif