Skip to content

Commit

Permalink
lib: cfl: upgrade to v0.3.0
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Dec 20, 2023
1 parent bcd3281 commit fc7606f
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/cfl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# C Floppy Version
set(CFL_VERSION_MAJOR 0)
set(CFL_VERSION_MINOR 2)
set(CFL_VERSION_PATCH 2)
set(CFL_VERSION_MINOR 3)
set(CFL_VERSION_PATCH 0)
set(CFL_VERSION_STR "${CFL_VERSION_MAJOR}.${CFL_VERSION_MINOR}.${CFL_VERSION_PATCH}")

# Configuration options
Expand Down
1 change: 1 addition & 0 deletions lib/cfl/include/cfl/cfl_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int cfl_array_append_bytes(struct cfl_array *array, char *value, size_t length);
int cfl_array_append_reference(struct cfl_array *array, void *value);
int cfl_array_append_bool(struct cfl_array *array, int value);
int cfl_array_append_int64(struct cfl_array *array, int64_t value);
int cfl_array_append_uint64(struct cfl_array *array, uint64_t value);
int cfl_array_append_double(struct cfl_array *array, double value);
int cfl_array_append_array(struct cfl_array *array, struct cfl_array *value);
int cfl_array_append_new_array(struct cfl_array *array, size_t size);
Expand Down
3 changes: 3 additions & 0 deletions lib/cfl/include/cfl/cfl_kvlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ int cfl_kvlist_insert_bool(struct cfl_kvlist *list,
int cfl_kvlist_insert_int64(struct cfl_kvlist *list,
char *key, int64_t value);

int cfl_kvlist_insert_uint64(struct cfl_kvlist *list,
char *key, uint64_t value);

int cfl_kvlist_insert_double(struct cfl_kvlist *list,
char *key, double value);

Expand Down
3 changes: 3 additions & 0 deletions lib/cfl/include/cfl/cfl_variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define CFL_VARIANT_KVLIST 6
#define CFL_VARIANT_BYTES 7
#define CFL_VARIANT_REFERENCE 8
#define CFL_VARIANT_UINT 9

struct cfl_array;
struct cfl_kvlist;
Expand All @@ -43,6 +44,7 @@ struct cfl_variant {
cfl_sds_t as_bytes;
unsigned int as_bool;
int64_t as_int64;
uint64_t as_uint64;
double as_double;
void *as_reference;
struct cfl_array *as_array;
Expand All @@ -54,6 +56,7 @@ struct cfl_variant *cfl_variant_create_from_string(char *value);
struct cfl_variant *cfl_variant_create_from_bytes(char *value, size_t length);
struct cfl_variant *cfl_variant_create_from_bool(int value);
struct cfl_variant *cfl_variant_create_from_int64(int64_t value);
struct cfl_variant *cfl_variant_create_from_uint64(uint64_t value);
struct cfl_variant *cfl_variant_create_from_double(double value);
struct cfl_variant *cfl_variant_create_from_array(struct cfl_array *value);
struct cfl_variant *cfl_variant_create_from_kvlist(struct cfl_kvlist *value);
Expand Down
21 changes: 21 additions & 0 deletions lib/cfl/src/cfl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,27 @@ int cfl_array_append_int64(struct cfl_array *array, int64_t value)
return 0;
}

int cfl_array_append_uint64(struct cfl_array *array, uint64_t value)
{
struct cfl_variant *value_instance;
int result;

value_instance = cfl_variant_create_from_uint64(value);

if (value_instance == NULL) {
return -1;
}

result = cfl_array_append(array, value_instance);

if (result) {
cfl_variant_destroy(value_instance);
return -2;
}

return 0;
}


int cfl_array_append_double(struct cfl_array *array, double value)
{
Expand Down
23 changes: 23 additions & 0 deletions lib/cfl/src/cfl_kvlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,29 @@ int cfl_kvlist_insert_int64(struct cfl_kvlist *list,
return 0;
}

int cfl_kvlist_insert_uint64(struct cfl_kvlist *list,
char *key, uint64_t value)
{
struct cfl_variant *value_instance;
int result;

value_instance = cfl_variant_create_from_uint64(value);

if (value_instance == NULL) {
return -1;
}

result = cfl_kvlist_insert(list, key, value_instance);

if (result) {
cfl_variant_destroy(value_instance);

return -2;
}

return 0;
}

int cfl_kvlist_insert_double(struct cfl_kvlist *list,
char *key, double value)
{
Expand Down
16 changes: 16 additions & 0 deletions lib/cfl/src/cfl_variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ int cfl_variant_print(FILE *fp, struct cfl_variant *val)
case CFL_VARIANT_INT:
ret = fprintf(fp, "%" PRId64, val->data.as_int64);
break;
case CFL_VARIANT_UINT:
ret = fprintf(fp, "%" PRIu64, val->data.as_uint64);
break;
case CFL_VARIANT_DOUBLE:
ret = fprintf(fp, "%lf", val->data.as_double);
break;
Expand Down Expand Up @@ -146,6 +149,19 @@ struct cfl_variant *cfl_variant_create_from_int64(int64_t value)
return instance;
}

struct cfl_variant *cfl_variant_create_from_uint64(uint64_t value)
{
struct cfl_variant *instance;

instance = cfl_variant_create();
if (instance != NULL) {
instance->data.as_uint64 = value;
instance->type = CFL_VARIANT_UINT;
}

return instance;
}

struct cfl_variant *cfl_variant_create_from_double(double value)
{
struct cfl_variant *instance;
Expand Down
40 changes: 40 additions & 0 deletions lib/cfl/tests/variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,45 @@ static void test_variant_print_int64()
}
}

static void test_variant_print_uint64()
{
int ret;
int i;
uint64_t inputs[] = {1, 0, 18446744073709551615ULL};
char *expects[] = {"1", "0", "18446744073709551615" /*UINT64_MAX*/};

FILE *fp = NULL;
struct cfl_variant *val = NULL;

for (i=0; i<sizeof(inputs)/sizeof(uint64_t); i++) {
fp = tmpfile();
if (!TEST_CHECK(fp != NULL)) {
TEST_MSG("%d: fp is NULL", i);
continue;
}
val = cfl_variant_create_from_uint64(inputs[i]);
if (!TEST_CHECK(val != NULL)) {
TEST_MSG("%d: cfl_variant_create_from_uint64 failed", i);
fclose(fp);
continue;
}

ret = cfl_variant_print(fp, val);
if (!TEST_CHECK(ret > 0)) {
TEST_MSG("%d:cfl_variant_print failed", i);
cfl_variant_destroy(val);
fclose(fp);
continue;
}
ret = compare(fp, expects[i], 0);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("%d:compare failed", i);
}
cfl_variant_destroy(val);
fclose(fp);
}
}

static void test_variant_print_array()
{
int ret;
Expand Down Expand Up @@ -459,6 +498,7 @@ static void test_variant_print_unknown()
TEST_LIST = {
{"variant_print_bool", test_variant_print_bool},
{"variant_print_int64", test_variant_print_int64},
{"variant_print_uint64", test_variant_print_uint64},
{"variant_print_double", test_variant_print_double},
{"variant_print_string", test_variant_print_string},
{"variant_print_bytes", test_variant_print_bytes},
Expand Down

0 comments on commit fc7606f

Please sign in to comment.