From 4d4635162b01eae1acf885b3788fd39ef83ae448 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 22 Jan 2018 21:31:03 +0300 Subject: [PATCH] [A] Modified argument checking --- example/main.c | 16 ++++++++-------- include/fort.h | 48 +++++++++++++++++++++++++++++++--------------- src/fort.c | 26 +++++++++++++++++++++---- tests/test_table.c | 40 +++++++++++++++++++------------------- 4 files changed, 83 insertions(+), 47 deletions(-) diff --git a/example/main.c b/example/main.c index cb94420..d75a9b8 100644 --- a/example/main.c +++ b/example/main.c @@ -7,10 +7,10 @@ int main() FTABLE *table = ft_create_table(); ft_set_column_alignment(table, 0, CenterAligned); ft_set_column_alignment(table, 1, LeftAligned); - FT_HDR_PRINTF_LN(table, "#|Planet|Avg. speed"); - FT_PRINTF_LN(table, "%d|%s|%5.2f km/s", 1, "Mercury", 47.362); - FT_PRINTF_LN(table, "%d|%s|%5.2f km/s", 2, "Venus", 35.02); - FT_PRINTF_LN(table, "%d|%s|%5.2f km/s", 3, "Earth", 29.78); + ft_hdr_printf_ln(table, "#|Planet|Avg. speed"); + ft_printf_ln(table, "%d|%s|%5.2f km/s", 1, "Mercury", 47.362); + ft_printf_ln(table, "%d|%s|%5.2f km/s", 2, "Venus", 35.02); + ft_printf_ln(table, "%d|%s|%5.2f km/s", 3, "Earth", 29.78); printf("Table:\n"); printf("%s\n", ft_to_string(table)); @@ -21,7 +21,7 @@ int main() table = ft_create_table(); ft_set_column_alignment(table, 0, CenterAligned); ft_set_column_alignment(table, 1, LeftAligned); - FT_HDR_PRINTF_LN(table, "Rank|Title|Year|Rating"); + ft_hdr_printf_ln(table, "Rank|Title|Year|Rating"); FT_NWRITE_LN(table, "1", "The Shawshank Redemption", "1994", "9.5"); FT_NWRITE_LN(table, "2", "12 Angry Men", "1957", "8.8"); @@ -38,7 +38,7 @@ int main() table = ft_create_table(); ft_set_column_alignment(table, 0, LeftAligned); ft_set_column_alignment(table, 1, CenterAligned); - FT_HDR_PRINTF_LN(table, "Commodity|Farm price|Avg. spread"); + ft_hdr_printf_ln(table, "Commodity|Farm price|Avg. spread"); const char *row1[] = {"Potatoes", "$1.60", "200.94%"}; const char *row2[] = {"Carrots", "$0.32 ", "190.63%"}; @@ -54,7 +54,7 @@ int main() table = ft_create_table(); ft_set_column_alignment(table, 0, CenterAligned); ft_set_column_alignment(table, 1, LeftAligned); - FT_HDR_PRINTF_LN(table, "No.|Name|Avg. Mark"); + ft_hdr_printf_ln(table, "No.|Name|Avg. Mark"); const char *ctab[2][3] = { {"1", "Joe Public", "3.14"}, @@ -71,7 +71,7 @@ int main() table = ft_create_table(); ft_set_column_alignment(table, 0, CenterAligned); ft_set_column_alignment(table, 1, LeftAligned); - FT_HDR_PRINTF_LN(table, "No.|Name|Avg. Mark"); + ft_hdr_printf_ln(table, "No.|Name|Avg. Mark"); const char **tab[2] = { row1, diff --git a/include/fort.h b/include/fort.h index f4c851c..ba31b97 100644 --- a/include/fort.h +++ b/include/fort.h @@ -59,6 +59,16 @@ SOFTWARE. #define FORT_RESTRICT restrict #endif /* if defined(__cplusplus) */ +/* + * Attribute format for argument checking + */ +#if defined(FORT_CLANG_COMPILER) || defined(FORT_GCC_COMPILER) +#define FORT_PRINTF_ATTRIBUTE_FORMAT(string_index, first_to_check) \ + __attribute__ ((format (printf, string_index, first_to_check))) +#else +#define FORT_PRINTF_ATTRIBUTE_FORMAT(string_index, first_to_check) +#endif /* defined(FORT_CLANG_COMPILER) || defined(FORT_GCC_COMPILER) */ + /* * C++ needs to know that types and declarations are C, not C++. @@ -99,22 +109,29 @@ FORT_EXTERN void ft_destroy_table(FTABLE *FORT_RESTRICT table); FORT_EXTERN void ft_ln(FTABLE *FORT_RESTRICT table); -FORT_EXTERN int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); -FORT_EXTERN int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); +#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); -#define FT_PRINTF(table, ...) \ - (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf(table, __VA_ARGS__)) -#define FT_PRINTF_LN(table, ...) \ - (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_ln(table, __VA_ARGS__)) +#else -FORT_EXTERN int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); -FORT_EXTERN int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); +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_HDR_PRINTF(table, ...) \ - (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf(table, __VA_ARGS__)) -#define FT_HDR_PRINTF_LN(table, ...) \ - (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_hdr_printf_ln(table, __VA_ARGS__)) +#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 @@ -123,9 +140,10 @@ FORT_EXTERN int ft_write_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRI -static inline void fort_check_if_string_helper(const char*str) +static inline int fort_check_if_string_helper(const char*str) { (void)str; + return 0; } #define PP_ARG_N( \ @@ -181,10 +199,10 @@ static inline void fort_check_if_string_helper(const char*str) #define CHECK_IF_ARGS_ARE_STRINGS(...) CHECK_IF_ARGS_ARE_STRINGS_(CHECK_IF_ARG_IS_STRING_,PP_NARG(__VA_ARGS__), __VA_ARGS__) #define FT_NWRITE(table, ...)\ - (CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__),ft_nwrite(table, PP_NARG(__VA_ARGS__), __VA_ARGS__)) + (0 ? CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite(table, PP_NARG(__VA_ARGS__), __VA_ARGS__)) #define FT_NWRITE_LN(table, ...)\ - (CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__),ft_nwrite_ln(table, PP_NARG(__VA_ARGS__), __VA_ARGS__)) + (0 ? CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite_ln(table, PP_NARG(__VA_ARGS__), __VA_ARGS__)) FORT_EXTERN int ft_nwrite(FTABLE *FORT_RESTRICT table, size_t n, const char* FORT_RESTRICT cell_content, ...); FORT_EXTERN int ft_nwrite_ln(FTABLE *FORT_RESTRICT table, size_t n, const char* FORT_RESTRICT cell_content, ...); diff --git a/src/fort.c b/src/fort.c index 5817408..5a61209 100644 --- a/src/fort.c +++ b/src/fort.c @@ -133,7 +133,20 @@ clear: return -1; } -int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) + +#if defined(FORT_CLANG_COMPILER) || defined(FORT_GCC_COMPILER) +#define FT_PRINTF ft_printf +#define FT_PRINTF_LN ft_printf_ln +#define FT_HDR_PRINTF ft_hdr_printf +#define FT_HDR_PRINTF_LN ft_hdr_printf_ln +#else +#define FT_PRINTF ft_printf_impl +#define FT_PRINTF_LN ft_printf_ln_impl +#define FT_HDR_PRINTF ft_hdr_printf_impl +#define FT_HDR_PRINTF_LN ft_hdr_printf_ln_impl +#endif + +int FT_HDR_PRINTF(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) { assert(table); assert(fmt); @@ -151,7 +164,7 @@ int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, .. return result; } -int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) +int FT_HDR_PRINTF_LN(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) { assert(table); assert(fmt); @@ -172,7 +185,7 @@ int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, return result; } -int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) +int FT_PRINTF(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) { assert(table); va_list va; @@ -182,7 +195,7 @@ int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) return result; } -int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) +int FT_PRINTF_LN(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) { assert(table); va_list va; @@ -195,6 +208,11 @@ int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ... return result; } +#undef FT_PRINTF +#undef FT_PRINTF_LN +#undef FT_HDR_PRINTF +#undef FT_HDR_PRINTF_LN + int ft_write(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content) { diff --git a/tests/test_table.c b/tests/test_table.c index 4ebf262..5df42bb 100644 --- a/tests/test_table.c +++ b/tests/test_table.c @@ -56,7 +56,7 @@ void test_table_sizes(void **state) } WHEN("Insert one cell") { - int n = FT_HDR_PRINTF_LN(table, "%c", 'c'); + int n = ft_printf_ln(table, "%c", 'c'); assert_true( n == 1 ); status = get_table_sizes(table, &rows, &cols); assert_true( IS_SUCCESS(status) ); @@ -65,7 +65,7 @@ void test_table_sizes(void **state) } WHEN("Insert two cells in the next row") { - int n = FT_PRINTF_LN(table, "%c|%c", 'c', 'd'); + int n = ft_printf_ln(table, "%c|%c", 'c', 'd'); assert_true( n == 2 ); status = get_table_sizes(table, &rows, &cols); assert_true( IS_SUCCESS(status) ); @@ -74,7 +74,7 @@ void test_table_sizes(void **state) } WHEN("Insert five cells in the next row") { - int n = FT_PRINTF_LN(table, "%d|%d|%d|%d|%d", 1, 2, 3, 4, 5); + int n = ft_printf_ln(table, "%d|%d|%d|%d|%d", 1, 2, 3, 4, 5); assert_true( n == 5 ); status = get_table_sizes(table, &rows, &cols); assert_true( IS_SUCCESS(status) ); @@ -105,7 +105,7 @@ void test_table_geometry(void **state) } WHEN("Table has one cell") { - int n = FT_HDR_PRINTF_LN(table, "%c", 'c'); + int n = ft_printf_ln(table, "%c", 'c'); assert_true( n == 1 ); status = table_geometry(table, &height, &width); assert_true( IS_SUCCESS(status) ); @@ -114,7 +114,7 @@ void test_table_geometry(void **state) } WHEN("Inserting 3 cells in the next row") { - int n = FT_PRINTF_LN(table, "%c|%s|%c", 'c', "as", 'e'); + int n = ft_printf_ln(table, "%c|%s|%c", 'c', "as", 'e'); assert_true( n == 3 ); status = table_geometry(table, &height, &width); assert_true( IS_SUCCESS(status) ); @@ -136,11 +136,11 @@ void test_table_basic(void **state) assert_true( table != NULL ); ft_set_table_options(table, &test_table_opts); - int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + int n = ft_hdr_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -173,11 +173,11 @@ void test_table_basic(void **state) assert_true( table != NULL ); ft_set_table_options(table, &test_table_opts); - int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + int n = ft_hdr_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "%c|%s|%f|%d", 'c', "234", 3.14, 3); + n = ft_printf_ln(table, "%c|%s|%f|%d", 'c', "234", 3.14, 3); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "%s|%f|%d|%c", "234", 3.14, 3, 'c'); + n = ft_printf_ln(table, "%s|%f|%d|%c", "234", 3.14, 3, 'c'); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -208,11 +208,11 @@ void test_table_basic(void **state) assert_true( table != NULL ); ft_set_table_options(table, &test_table_opts); - int n = FT_HDR_PRINTF_LN(table, "||%s|%f", "234", 3.14); + int n = ft_hdr_printf_ln(table, "||%s|%f", "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "%c|%s|%f", 'c', "234", 3.14); + n = ft_printf_ln(table, "%c|%s|%f", 'c', "234", 3.14); assert_true( n == 3 ); - n = FT_PRINTF_LN(table, "%s|%f||", "234", 3.14); + n = ft_printf_ln(table, "%s|%f||", "234", 3.14); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -243,11 +243,11 @@ void test_table_basic(void **state) assert_true( table != NULL ); ft_set_table_options(table, &test_table_opts); - int n = FT_HDR_PRINTF_LN(table, "|||"); + int n = ft_hdr_printf_ln(table, "|||"); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "|||"); + n = ft_printf_ln(table, "|||"); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, "|||"); + n = ft_printf_ln(table, "|||"); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -288,7 +288,7 @@ FTABLE *create_test_int_table(int set_test_opts) assert_true (table != NULL); - int n = FT_HDR_PRINTF_LN(table, "%d|%d|%d|%d", 3, 4, 55, 67); + int n = ft_hdr_printf_ln(table, "%d|%d|%d|%d", 3, 4, 55, 67); assert_true( n == 4 ); assert(ft_write(table, "3") == F_SUCCESS); @@ -663,11 +663,11 @@ void test_table_options(void **state) WHEN("All columns are equal and not empty") { table = ft_create_table(); - int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 4, 'c', "234", 3.14); + int n = ft_hdr_printf_ln(table, "%d|%c|%s|%f", 4, 'c', "234", 3.14); assert_true( n == 4 ); n = FT_NWRITE_LN(table, "5", "c", "234\n12", "3.140000"); assert_true( n == F_SUCCESS ); - n = FT_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); const char *table_str = ft_to_string(table);