Skip to content

Commit

Permalink
COMMON: Added variant printf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Oct 11, 2023
1 parent 504db04 commit 2d8e20d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 26 deletions.
41 changes: 15 additions & 26 deletions examples/c/sail/sail.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) ? "<set>" : "<unset>"); 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("<binary data, %u byte(s)>\n", (unsigned)meta_data->value->size); break;
case SAIL_VARIANT_TYPE_INVALID: printf("<invalid value>\n"); break;
printf(" %-12s: ", meta_data_str);
sail_printf_variant(meta_data->value);
printf("\n");
}
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/libsail-common/variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<binary data, %u byte(s)>", (unsigned)variant->size);
case SAIL_VARIANT_TYPE_INVALID: return fprintf(f, "<invalid value>");
}

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, "<binary data, %u byte(s)>", (unsigned)variant->size);
case SAIL_VARIANT_TYPE_INVALID: return snprintf(str, str_size, "<invalid value>");
}

return 0;
}
25 changes: 25 additions & 0 deletions src/libsail-common/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <stdbool.h>
#include <stddef.h> /* size_t */
#include <stdio.h> /* FILE */

#ifdef SAIL_BUILD
#include "common.h"
Expand Down Expand Up @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions tests/sail-common/variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,40 @@ 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 },
{ (char *)"/from-value", test_from_value, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
{ (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 }
};
Expand Down

0 comments on commit 2d8e20d

Please sign in to comment.