From 2d8e20dd4911e9f65be2f3dd8dc695cbf615760e Mon Sep 17 00:00:00 2001 From: Dmitry Baryshev Date: Wed, 11 Oct 2023 14:14:41 +0300 Subject: [PATCH] COMMON: Added variant printf functions --- examples/c/sail/sail.c | 41 ++++++++++---------------- src/libsail-common/variant.c | 57 ++++++++++++++++++++++++++++++++++++ src/libsail-common/variant.h | 25 ++++++++++++++++ tests/sail-common/variant.c | 27 +++++++++++++++++ 4 files changed, 124 insertions(+), 26 deletions(-) diff --git a/examples/c/sail/sail.c b/examples/c/sail/sail.c index 960b8f1f..1dcb863c 100644 --- a/examples/c/sail/sail.c +++ b/examples/c/sail/sail.c @@ -140,33 +140,22 @@ static void print_aligned_image_info(const struct sail_image *image) { printf("Interlaced : %s\n", image->source_image->interlaced ? "yes" : "no"); printf("Delay : %d ms.\n", image->delay); - for (const struct sail_meta_data_node *meta_data_node = image->meta_data_node; meta_data_node != NULL; meta_data_node = meta_data_node->next) { - const struct sail_meta_data *meta_data = meta_data_node->meta_data; - const char *meta_data_str = NULL; - - if (meta_data->key == SAIL_META_DATA_UNKNOWN) { - meta_data_str = meta_data->key_unknown; - } else { - meta_data_str = sail_meta_data_to_string(meta_data->key); - } + if (image->meta_data_node != NULL) { + printf("Meta data :\n"); + + for (const struct sail_meta_data_node *meta_data_node = image->meta_data_node; meta_data_node != NULL; meta_data_node = meta_data_node->next) { + const struct sail_meta_data *meta_data = meta_data_node->meta_data; + const char *meta_data_str = NULL; + + if (meta_data->key == SAIL_META_DATA_UNKNOWN) { + meta_data_str = meta_data->key_unknown; + } else { + meta_data_str = sail_meta_data_to_string(meta_data->key); + } - printf("%-14s: ", meta_data_str); - - switch (meta_data->value->type) { - case SAIL_VARIANT_TYPE_BOOL: printf("%s\n", sail_variant_to_bool(meta_data->value) ? "" : ""); break; - case SAIL_VARIANT_TYPE_CHAR: printf("%d\n", sail_variant_to_char(meta_data->value)); break; - case SAIL_VARIANT_TYPE_UNSIGNED_CHAR: printf("%u\n", sail_variant_to_unsigned_char(meta_data->value)); break; - case SAIL_VARIANT_TYPE_SHORT: printf("%d\n", sail_variant_to_short(meta_data->value)); break; - case SAIL_VARIANT_TYPE_UNSIGNED_SHORT: printf("%u\n", sail_variant_to_unsigned_short(meta_data->value)); break; - case SAIL_VARIANT_TYPE_INT: printf("%d\n", sail_variant_to_int(meta_data->value)); break; - case SAIL_VARIANT_TYPE_UNSIGNED_INT: printf("%u\n", sail_variant_to_unsigned_int(meta_data->value)); break; - case SAIL_VARIANT_TYPE_LONG: printf("%ld\n", sail_variant_to_long(meta_data->value)); break; - case SAIL_VARIANT_TYPE_UNSIGNED_LONG: printf("%lu\n", sail_variant_to_unsigned_long(meta_data->value)); break; - case SAIL_VARIANT_TYPE_FLOAT: printf("%.1f\n", sail_variant_to_float(meta_data->value)); break; - case SAIL_VARIANT_TYPE_DOUBLE: printf("%.1f\n", sail_variant_to_double(meta_data->value)); break; - case SAIL_VARIANT_TYPE_STRING: printf("%s\n", sail_variant_to_string(meta_data->value)); break; - case SAIL_VARIANT_TYPE_DATA: printf("\n", (unsigned)meta_data->value->size); break; - case SAIL_VARIANT_TYPE_INVALID: printf("\n"); break; + printf(" %-12s: ", meta_data_str); + sail_printf_variant(meta_data->value); + printf("\n"); } } } diff --git a/src/libsail-common/variant.c b/src/libsail-common/variant.c index 5efd32cb..458bf5d8 100644 --- a/src/libsail-common/variant.c +++ b/src/libsail-common/variant.c @@ -313,3 +313,60 @@ bool sail_equal_variants(const struct sail_variant *variant1, const struct sail_ return memcmp(variant1->value, variant2->value, variant1->size) == 0; } } + +int sail_printf_variant(const struct sail_variant *variant) { + + return sail_fprintf_variant(variant, stdout); +} + +int sail_fprintf_variant(const struct sail_variant *variant, FILE *f) { + + if (variant == NULL) { + return -1; + } + + switch (variant->type) { + case SAIL_VARIANT_TYPE_BOOL: return fprintf(f, "%s", sail_variant_to_bool(variant) ? "true" : "false"); + case SAIL_VARIANT_TYPE_CHAR: return fprintf(f, "%d", sail_variant_to_char(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_CHAR: return fprintf(f, "%u", sail_variant_to_unsigned_char(variant)); + case SAIL_VARIANT_TYPE_SHORT: return fprintf(f, "%d", sail_variant_to_short(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_SHORT: return fprintf(f, "%u", sail_variant_to_unsigned_short(variant)); + case SAIL_VARIANT_TYPE_INT: return fprintf(f, "%d", sail_variant_to_int(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_INT: return fprintf(f, "%u", sail_variant_to_unsigned_int(variant)); + case SAIL_VARIANT_TYPE_LONG: return fprintf(f, "%ld", sail_variant_to_long(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_LONG: return fprintf(f, "%lu", sail_variant_to_unsigned_long(variant)); + case SAIL_VARIANT_TYPE_FLOAT: return fprintf(f, "%f", sail_variant_to_float(variant)); + case SAIL_VARIANT_TYPE_DOUBLE: return fprintf(f, "%f", sail_variant_to_double(variant)); + case SAIL_VARIANT_TYPE_STRING: return fprintf(f, "%s", sail_variant_to_string(variant)); + case SAIL_VARIANT_TYPE_DATA: return fprintf(f, "", (unsigned)variant->size); + case SAIL_VARIANT_TYPE_INVALID: return fprintf(f, ""); + } + + return 0; +} + +int sail_snprintf_variant(const struct sail_variant *variant, char *str, size_t str_size) { + + if (variant == NULL) { + return -1; + } + + switch (variant->type) { + case SAIL_VARIANT_TYPE_BOOL: return snprintf(str, str_size, "%s", sail_variant_to_bool(variant) ? "true" : "false"); + case SAIL_VARIANT_TYPE_CHAR: return snprintf(str, str_size, "%d", sail_variant_to_char(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_CHAR: return snprintf(str, str_size, "%u", sail_variant_to_unsigned_char(variant)); + case SAIL_VARIANT_TYPE_SHORT: return snprintf(str, str_size, "%d", sail_variant_to_short(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_SHORT: return snprintf(str, str_size, "%u", sail_variant_to_unsigned_short(variant)); + case SAIL_VARIANT_TYPE_INT: return snprintf(str, str_size, "%d", sail_variant_to_int(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_INT: return snprintf(str, str_size, "%u", sail_variant_to_unsigned_int(variant)); + case SAIL_VARIANT_TYPE_LONG: return snprintf(str, str_size, "%ld", sail_variant_to_long(variant)); + case SAIL_VARIANT_TYPE_UNSIGNED_LONG: return snprintf(str, str_size, "%lu", sail_variant_to_unsigned_long(variant)); + case SAIL_VARIANT_TYPE_FLOAT: return snprintf(str, str_size, "%f", sail_variant_to_float(variant)); + case SAIL_VARIANT_TYPE_DOUBLE: return snprintf(str, str_size, "%f", sail_variant_to_double(variant)); + case SAIL_VARIANT_TYPE_STRING: return snprintf(str, str_size, "%s", sail_variant_to_string(variant)); + case SAIL_VARIANT_TYPE_DATA: return snprintf(str, str_size, "", (unsigned)variant->size); + case SAIL_VARIANT_TYPE_INVALID: return snprintf(str, str_size, ""); + } + + return 0; +} diff --git a/src/libsail-common/variant.h b/src/libsail-common/variant.h index 7e185bd2..3e0ca898 100644 --- a/src/libsail-common/variant.h +++ b/src/libsail-common/variant.h @@ -28,6 +28,7 @@ #include #include /* size_t */ +#include /* FILE */ #ifdef SAIL_BUILD #include "common.h" @@ -312,6 +313,30 @@ SAIL_EXPORT sail_status_t sail_copy_variant(const struct sail_variant *source, s */ SAIL_EXPORT bool sail_equal_variants(const struct sail_variant *variant1, const struct sail_variant *variant2); +/* + * Calls printf() to print the value of the variant. If the variant is NULL, + * doesn't print anything and returns -1. + * + * Returns the result of printf(). + */ +SAIL_EXPORT int sail_printf_variant(const struct sail_variant *variant); + +/* + * Calls fprintf() to print the value of the variant into the file. + * If the variant is NULL, doesn't print anything and returns -1. + * + * Returns the result of fprintf(). + */ +SAIL_EXPORT int sail_fprintf_variant(const struct sail_variant *variant, FILE *f); + +/* + * Calls snprintf() to put the value of the variant into the string. + * If the variant is NULL, doesn't print anything and returns -1. + * + * Returns the result of snprintf(). + */ +SAIL_EXPORT int sail_snprintf_variant(const struct sail_variant *variant, char *str, size_t str_size); + /* extern "C" */ #ifdef __cplusplus } diff --git a/tests/sail-common/variant.c b/tests/sail-common/variant.c index 96d38722..b1b0bc2a 100644 --- a/tests/sail-common/variant.c +++ b/tests/sail-common/variant.c @@ -236,6 +236,32 @@ static MunitResult test_set(const MunitParameter params[], void *user_data) { return MUNIT_OK; } +static MunitResult test_snprintf(const MunitParameter params[], void *user_data) { + (void)params; + (void)user_data; + + char buffer[32]; + + struct sail_variant *variant; + munit_assert(sail_alloc_variant(&variant) == SAIL_OK); + + sail_set_variant_bool(variant, true); + munit_assert(sail_snprintf_variant(variant, buffer, sizeof(buffer)) == 4); + munit_assert_string_equal(buffer, "true"); + + sail_set_variant_bool(variant, false); + munit_assert(sail_snprintf_variant(variant, buffer, sizeof(buffer)) == 5); + munit_assert_string_equal(buffer, "false"); + + sail_set_variant_int(variant, 105); + munit_assert(sail_snprintf_variant(variant, buffer, sizeof(buffer)) == 3); + munit_assert_string_equal(buffer, "105"); + + sail_destroy_variant(variant); + + return MUNIT_OK; +} + static MunitTest test_suite_tests[] = { { (char *)"/alloc", test_alloc, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/copy", test_copy, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, @@ -243,6 +269,7 @@ static MunitTest test_suite_tests[] = { { (char *)"/from-string", test_from_string, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/from-data", test_from_data, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { (char *)"/set", test_set, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { (char *)"/snprintf", test_snprintf, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };