[A] Added HAVE_WCHAR config option

This commit is contained in:
seleznevae 2018-04-16 21:01:45 +03:00
parent 0c844aa143
commit 551ec6089b
8 changed files with 171 additions and 42 deletions

View File

@ -23,6 +23,11 @@ endif(FORT_CXX_BUILD)
set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_VERBOSE_MAKEFILE ON)
# ------------------------------------------------------------------------------
# Add WChar support for the build
# ------------------------------------------------------------------------------
add_definitions(-DFT_CONGIG_HAVE_WCHAR)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Includes # Includes
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -33,6 +38,7 @@ include_directories(src)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Warnings # Warnings
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -32,10 +32,19 @@ SOFTWARE.
#include <stdint.h> #include <stdint.h>
#include <limits.h> #include <limits.h>
/*****************************************************************************
/* * Configuration
* Determine compiler *****************************************************************************/
/**
* libfort configuration macros
* (to enable/disable some options this macros should be defined/undefined)
*/ */
/** #define FT_CONGIG_HAVE_WCHAR */
/*****************************************************************************
* Determine compiler
*****************************************************************************/
#if defined(__clang__) #if defined(__clang__)
#define FT_CLANG_COMPILER #define FT_CLANG_COMPILER
#elif defined(__GNUC__) #elif defined(__GNUC__)
@ -46,11 +55,9 @@ SOFTWARE.
#define FT_UNDEFINED_COMPILER #define FT_UNDEFINED_COMPILER
#endif #endif
/*****************************************************************************
/*
* Declare restrict * Declare restrict
*/ *****************************************************************************/
/* /*
#if defined(__cplusplus) #if defined(__cplusplus)
#if defined(FT_CLANG_COMPILER) #if defined(FT_CLANG_COMPILER)
@ -67,10 +74,9 @@ SOFTWARE.
#endif // if defined(__cplusplus) #endif // if defined(__cplusplus)
*/ */
/*****************************************************************************
/* * Declare inline
* Declare restrict *****************************************************************************/
*/
#if defined(__cplusplus) #if defined(__cplusplus)
#define FT_INLINE inline #define FT_INLINE inline
#else #else
@ -79,9 +85,9 @@ SOFTWARE.
/* /*****************************************************************************
* Attribute format for argument checking * Attribute format for argument checking
*/ *****************************************************************************/
#if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) #if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER)
#define FT_PRINTF_ATTRIBUTE_FORMAT(string_index, first_to_check) \ #define FT_PRINTF_ATTRIBUTE_FORMAT(string_index, first_to_check) \
__attribute__ ((format (printf, string_index, first_to_check))) __attribute__ ((format (printf, string_index, first_to_check)))
@ -90,9 +96,9 @@ SOFTWARE.
#endif /* defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) */ #endif /* defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) */
/* /*****************************************************************************
* C++ needs to know that types and declarations are C, not C++. * C++ needs to know that types and declarations are C, not C++.
*/ *****************************************************************************/
#ifdef __cplusplus #ifdef __cplusplus
# define FT_BEGIN_DECLS extern "C" { # define FT_BEGIN_DECLS extern "C" {
# define FT_END_DECLS } # define FT_END_DECLS }
@ -109,7 +115,7 @@ SOFTWARE.
/***************************************************************************** /*****************************************************************************
* RETURN CODES * RETURN CODES
* ***************************************************************************/ *****************************************************************************/
typedef int fort_status_t; typedef int fort_status_t;
#define FT_SUCCESS 0 #define FT_SUCCESS 0
#define FT_MEMORY_ERROR -1 #define FT_MEMORY_ERROR -1
@ -118,15 +124,18 @@ typedef int fort_status_t;
#define IS_SUCCESS(arg) ((arg) >= 0) #define IS_SUCCESS(arg) ((arg) >= 0)
#define IS_ERROR(arg) ((arg) < 0) #define IS_ERROR(arg) ((arg) < 0)
/*****************************************************************************
/*
* Wchar support * Wchar support
*/ *****************************************************************************/
#define FT_HAVE_WCHAR
/* #if defined(FT_CONGIG_HAVE_WCHAR)
#define FT_HAVE_WCHAR
#endif
/*****************************************************************************
* Helper macros * Helper macros
*/ *****************************************************************************/
#define STR_2_CAT_(arg1, arg2) \ #define STR_2_CAT_(arg1, arg2) \
arg1##arg2 arg1##arg2
@ -195,9 +204,11 @@ static FT_INLINE int ft_check_if_wstring_helper(const wchar_t *str)
CHECK_IF_ARGS_ARE_STRINGS_(ft_check_if_wstring_helper,CHECK_IF_STRING_,PP_NARG(__VA_ARGS__), __VA_ARGS__) CHECK_IF_ARGS_ARE_STRINGS_(ft_check_if_wstring_helper,CHECK_IF_STRING_,PP_NARG(__VA_ARGS__), __VA_ARGS__)
#endif #endif
/*
* libfort structures and functions declarations /*****************************************************************************
*/ * libfort API
*****************************************************************************/
FT_BEGIN_DECLS FT_BEGIN_DECLS
@ -208,8 +219,7 @@ typedef struct fort_table FTABLE;
* Create formatted table. * Create formatted table.
* *
* @return * @return
* The pointer to the new allocated FTABLE, on success. NULL on error * The pointer to the new allocated FTABLE, on success. NULL on error.
* with ft_errno set appropriately.
*/ */
FT_EXTERN FTABLE *ft_create_table(void); FT_EXTERN FTABLE *ft_create_table(void);
@ -220,7 +230,8 @@ FT_EXTERN FTABLE *ft_create_table(void);
* and work with it. * and work with it.
* *
* @param table * @param table
* Pointer to formatted table previousley created with ft_create_table. * Pointer to formatted table previousley created with ft_create_table. If
* table is a null pointer, the function does nothing.
*/ */
FT_EXTERN void ft_destroy_table(FTABLE *table); FT_EXTERN void ft_destroy_table(FTABLE *table);
@ -231,12 +242,78 @@ FT_EXTERN void ft_destroy_table(FTABLE *table);
* Pointer to formatted table. * Pointer to formatted table.
*/ */
FT_EXTERN void ft_ln(FTABLE *table); FT_EXTERN void ft_ln(FTABLE *table);
/**
* Get row number of the current cell
*
* @param table
* Pointer to formatted table.
* @return
* Row number of the current cell
*/
FT_EXTERN size_t ft_cur_row(FTABLE *table); FT_EXTERN size_t ft_cur_row(FTABLE *table);
/**
* Get column number of the current cell
*
* @param table
* Pointer to formatted table.
* @return
* Column number of the current cell
*/
FT_EXTERN size_t ft_cur_col(FTABLE *table); FT_EXTERN size_t ft_cur_col(FTABLE *table);
#if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) #if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER)
/**
* Writes data formatted acording to the format string to a variety of table
* cells.
*
* @param table
* Pointer to formatted table.
* @param fmt
* Pointer to a null-terminated multibyte string specifying how to interpret
* the data. The format string consists of ordinary characters (except % and |),
* which are copied unchanged into the output stream, and conversion
* specifications. Conversion specifications are the same as for standard
* printf function. Character '|' in the format string is treated as a cell
* separator.
* @param ...
* Arguments specifying data to print. Similarly to standard printf-like
* functions if any argument after default conversions is not the type
* expected by the corresponding conversion specifier, or if there are fewer
* arguments than required by format, the behavior is undefined. If there are
* more arguments than required by format, the extraneous arguments are
* evaluated and ignored.
* @return
* todo ?????
*/
FT_EXTERN int ft_printf(FTABLE *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3); FT_EXTERN int ft_printf(FTABLE *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
/**
* Writes data formatted acording to the format string to a variety of table
* cells and moves current position to the first cell of the next line(row).
*
* @param table
* Pointer to formatted table.
* @param fmt
* Pointer to a null-terminated multibyte string specifying how to interpret
* the data. The format string consists of ordinary characters (except % and |),
* which are copied unchanged into the output stream, and conversion
* specifications. Conversion specifications are the same as for standard
* printf function. Character '|' in the format string is treated as a cell
* separator.
* @param ...
* Arguments specifying data to print. Similarly to standard printf-like
* functions if any argument after default conversions is not the type
* expected by the corresponding conversion specifier, or if there are fewer
* arguments than required by format, the behavior is undefined. If there are
* more arguments than required by format, the extraneous arguments are
* evaluated and ignored.
* @return
* todo ?????
*/
FT_EXTERN int ft_printf_ln(FTABLE *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3); FT_EXTERN int ft_printf_ln(FTABLE *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMAT(2, 3);
#else #else
@ -300,9 +377,11 @@ FT_EXTERN int ft_add_separator(FTABLE *table);
* *
* FTABLE has ownership of the returned pointer. So there is no need to * FTABLE has ownership of the returned pointer. So there is no need to
* free it. To take ownership user should explicitly copy the returned * free it. To take ownership user should explicitly copy the returned
* string with strdup or similar functions. Returned pointer remaines valid * string with strdup or similar functions.
* until table is destroyed with ft_destroy_table or other invocations of *
* ft_to_string. * Returned pointer may be later invalidated by:
* - Calling ft_destroy_table
* - Other invocations of ft_to_string
* *
* @param table * @param table
* Formatted table. * Formatted table.
@ -324,7 +403,9 @@ FT_EXTERN const char *ft_to_string(const FTABLE *table);
/**
* Structure describing border appearance
*/
struct ft_border_chars { struct ft_border_chars {
char top_border_ch; char top_border_ch;
char separator_ch; char separator_ch;
@ -334,6 +415,9 @@ struct ft_border_chars {
char in_intersect_ch; char in_intersect_ch;
}; };
/**
* Structure describing border style
*/
struct ft_border_style { struct ft_border_style {
struct ft_border_chars border_chs; struct ft_border_chars border_chs;
struct ft_border_chars header_border_chs; struct ft_border_chars header_border_chs;
@ -479,13 +563,23 @@ FT_EXTERN int ft_set_default_tbl_option(uint32_t option, int value);
FT_EXTERN int ft_set_tbl_option(FTABLE *table, uint32_t option, int value); FT_EXTERN int ft_set_tbl_option(FTABLE *table, uint32_t option, int value);
/**
* Set functions for memory allocation and deallocation to be used instead of
* standard ones.
*
* @param f_malloc
* Pointer to a function for memory allocation that should be used instead of
* malloc.
* @param f_free
* Pointer to a function for memory deallocation that should be used instead
* of free.
* @note
* To return memory allocation/deallocation functions to their standard values
* set f_malloc and f_free to NULL.
*/
FT_EXTERN void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr)); FT_EXTERN void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr));
/*
* WChar support
*/
#ifdef FT_HAVE_WCHAR #ifdef FT_HAVE_WCHAR
FT_EXTERN int ft_wwrite(FTABLE *table, const wchar_t *cell_content); FT_EXTERN int ft_wwrite(FTABLE *table, const wchar_t *cell_content);

View File

@ -8,6 +8,10 @@
#include <stdio.h> #include <stdio.h>
#include "fort.h" #include "fort.h"
#if defined(FT_MICROSOFT_COMPILER)
#define _CRT_SECURE_NO_WARNINGS /* To disable warnings for unsafe functions */
#endif
#define FORT_COL_SEPARATOR '|' #define FORT_COL_SEPARATOR '|'
#define FORT_UNUSED __attribute__((unused)) #define FORT_UNUSED __attribute__((unused))

View File

@ -7,7 +7,9 @@ struct test_case test_suit [] = {
{"test_table_sizes", test_table_sizes}, {"test_table_sizes", test_table_sizes},
{"test_table_geometry", test_table_geometry}, {"test_table_geometry", test_table_geometry},
{"test_table_basic", test_table_basic}, {"test_table_basic", test_table_basic},
#ifdef FT_HAVE_WCHAR
{"test_wcs_table_boundaries", test_wcs_table_boundaries}, {"test_wcs_table_boundaries", test_wcs_table_boundaries},
#endif
{"test_table_write", test_table_write}, {"test_table_write", test_table_write},
{"test_table_border_style", test_table_border_style}, {"test_table_border_style", test_table_border_style},
{"test_table_cell_options", test_table_cell_options}, {"test_table_cell_options", test_table_cell_options},

View File

@ -35,6 +35,7 @@ void test_table_basic(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
WHEN("All columns are equal and not empty (wide strings)") { WHEN("All columns are equal and not empty (wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -64,6 +65,7 @@ void test_table_basic(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
@ -97,6 +99,7 @@ void test_table_basic(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
WHEN("All columns are not equal and not empty (wide strings)") { WHEN("All columns are not equal and not empty (wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -126,6 +129,7 @@ void test_table_basic(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
WHEN("All columns are not equal and some cells are empty") { WHEN("All columns are not equal and some cells are empty") {
table = ft_create_table(); table = ft_create_table();
@ -157,6 +161,7 @@ void test_table_basic(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
WHEN("All columns are not equal and some cells are empty (wide strings)") { WHEN("All columns are not equal and some cells are empty (wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -186,6 +191,7 @@ void test_table_basic(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
WHEN("All cells are empty") { WHEN("All cells are empty") {
table = ft_create_table(); table = ft_create_table();
@ -217,6 +223,7 @@ void test_table_basic(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
WHEN("All cells are empty (wide strings)") { WHEN("All cells are empty (wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -246,11 +253,12 @@ void test_table_basic(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
} }
#ifdef FT_HAVE_WCHAR
void test_wcs_table_boundaries(void) void test_wcs_table_boundaries(void)
{ {
FTABLE *table = NULL; FTABLE *table = NULL;
@ -285,6 +293,7 @@ void test_wcs_table_boundaries(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
} }
#endif
void test_table_write(void) void test_table_write(void)
@ -332,6 +341,7 @@ void test_table_write(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
SCENARIO("Test wwrite functions(wide strings)") { SCENARIO("Test wwrite functions(wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -372,6 +382,7 @@ void test_table_write(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
SCENARIO("Test nwrite functions") { SCENARIO("Test nwrite functions") {
table = ft_create_table(); table = ft_create_table();
@ -404,6 +415,7 @@ void test_table_write(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
SCENARIO("Test nwwrite functions(wide strings)") { SCENARIO("Test nwwrite functions(wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -434,6 +446,7 @@ void test_table_write(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
SCENARIO("Test row_write functions") { SCENARIO("Test row_write functions") {
@ -470,6 +483,7 @@ void test_table_write(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#ifdef FT_HAVE_WCHAR
SCENARIO("Test row_write functions(wide strings)") { SCENARIO("Test row_write functions(wide strings)") {
table = ft_create_table(); table = ft_create_table();
assert_true(table != NULL); assert_true(table != NULL);
@ -503,6 +517,7 @@ void test_table_write(void)
assert_wcs_equal(table_str, table_str_etalon); assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
SCENARIO("Test printf functions") { SCENARIO("Test printf functions") {

View File

@ -70,6 +70,9 @@ void test_table_tbl_options(void)
} }
#ifdef FT_HAVE_WCHAR
WHEN("Test setting entire table options(wide strings case)") { WHEN("Test setting entire table options(wide strings case)") {
set_test_options_as_default(); set_test_options_as_default();
@ -125,6 +128,7 @@ void test_table_tbl_options(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
#endif
} }

View File

@ -33,7 +33,7 @@ int set_test_options_for_table(FTABLE *table)
return ft_set_border_style(table, &brdr_style); return ft_set_border_style(table, &brdr_style);
} }
int set_test_options_as_default() int set_test_options_as_default(void)
{ {
int status = FT_SUCCESS; int status = FT_SUCCESS;
@ -101,6 +101,7 @@ FTABLE *create_test_int_table(int set_test_opts)
return table; return table;
} }
#ifdef FT_HAVE_WCHAR
FTABLE *create_test_int_wtable(int set_test_opts) FTABLE *create_test_int_wtable(int set_test_opts)
{ {
FTABLE *table = NULL; FTABLE *table = NULL;
@ -130,3 +131,4 @@ FTABLE *create_test_int_wtable(int set_test_opts)
return table; return table;
} }
#endif

View File

@ -19,7 +19,9 @@ void test_string_buffer(void);
void test_table_sizes(void); void test_table_sizes(void);
void test_table_geometry(void); void test_table_geometry(void);
void test_table_basic(void); void test_table_basic(void);
#ifdef FT_HAVE_WCHAR
void test_wcs_table_boundaries(void); void test_wcs_table_boundaries(void);
#endif
void test_table_write(void); void test_table_write(void);
void test_table_border_style(void); void test_table_border_style(void);
void test_table_cell_options(void); void test_table_cell_options(void);
@ -60,7 +62,7 @@ struct test_case
} }
int set_test_options_for_table(FTABLE *table); int set_test_options_for_table(FTABLE *table);
int set_test_options_as_default(); int set_test_options_as_default(void);
FTABLE *create_test_int_table(int set_test_opts); FTABLE *create_test_int_table(int set_test_opts);
FTABLE *create_test_int_wtable(int set_test_opts); FTABLE *create_test_int_wtable(int set_test_opts);