Skip to content

Commit

Permalink
lib: cfl: upgrade to v0.1.9
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
  • Loading branch information
edsiper committed Oct 21, 2022
1 parent 1f0d963 commit 7f92f05
Show file tree
Hide file tree
Showing 9 changed files with 594 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/cfl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# C Floppy Version
set(CFL_VERSION_MAJOR 0)
set(CFL_VERSION_MINOR 1)
set(CFL_VERSION_PATCH 8)
set(CFL_VERSION_PATCH 9)
set(CFL_VERSION_STR "${CFL_VERSION_MAJOR}.${CFL_VERSION_MINOR}.${CFL_VERSION_PATCH}")

# Configuration options
Expand Down
2 changes: 2 additions & 0 deletions lib/cfl/include/cfl/cfl_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef CFL_ARRAY_H
#define CFL_ARRAY_H

#include <stdio.h>
#include <cfl/cfl_variant.h>

struct cfl_array {
Expand Down Expand Up @@ -56,5 +57,6 @@ 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);
int cfl_array_append_kvlist(struct cfl_array *array, struct
cfl_kvlist *value);
int cfl_array_print(FILE *fp, struct cfl_array *array);

#endif
2 changes: 2 additions & 0 deletions lib/cfl/include/cfl/cfl_kvlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef CFL_KVLIST_H
#define CFL_KVLIST_H

#include <stdio.h>
#include <cfl/cfl_sds.h>
#include <cfl/cfl_list.h>
#include <cfl/cfl_variant.h>
Expand Down Expand Up @@ -70,5 +71,6 @@ int cfl_kvlist_insert(struct cfl_kvlist *list,

int cfl_kvlist_count(struct cfl_kvlist *list);
struct cfl_variant *cfl_kvlist_fetch(struct cfl_kvlist *list, char *key);
int cfl_kvlist_print(FILE *fp, struct cfl_kvlist *list);

#endif
7 changes: 5 additions & 2 deletions lib/cfl/include/cfl/cfl_variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#ifndef CFL_VARIANT_H
#define CFL_VARIANT_H

#include <stdio.h>
#include <inttypes.h>

#define CFL_VARIANT_STRING 1
#define CFL_VARIANT_BOOL 2
#define CFL_VARIANT_INT 3
Expand All @@ -46,7 +49,7 @@ struct cfl_variant {
struct cfl_kvlist *as_kvlist;
} data;
};

int cfl_variant_print(FILE *fp, struct cfl_variant *val);
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);
Expand All @@ -59,4 +62,4 @@ struct cfl_variant *cfl_variant_create();

void cfl_variant_destroy(struct cfl_variant *instance);

#endif
#endif
29 changes: 29 additions & 0 deletions lib/cfl/src/cfl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <cfl/cfl.h>
#include <cfl/cfl_array.h>
#include <cfl/cfl_variant.h>

struct cfl_array *cfl_array_create(size_t slot_count)
{
Expand Down Expand Up @@ -344,3 +345,31 @@ cfl_kvlist *value)

return 0;
}


int cfl_array_print(FILE *fp, struct cfl_array *array)
{
size_t size;
size_t i;
int ret;

if (fp == NULL || array == NULL) {
return -1;
}

size = array->entry_count;
if (size == 0) {
fputs("[]", fp);
return 0;
}

fputs("[", fp);
for (i=0; i<size-1; i++) {
ret = cfl_variant_print(fp, array->entries[i]);
fputs(",", fp);
}
ret = cfl_variant_print(fp, array->entries[size-1]);
fputs("]", fp);

return ret;
}
34 changes: 34 additions & 0 deletions lib/cfl/src/cfl_kvlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,37 @@ struct cfl_variant *cfl_kvlist_fetch(struct cfl_kvlist *list, char *key)
return NULL;
}

int cfl_kvlist_print(FILE *fp, struct cfl_kvlist *list)
{
size_t size;
size_t i;
int ret = -1;

struct cfl_list *head = NULL;
struct cfl_kvpair *pair = NULL;

if (fp == NULL || list == NULL) {
return -1;
}

size = (size_t)cfl_kvlist_count(list);
i = 0;
fputs("{", fp);
cfl_list_foreach(head, &list->list) {
pair = cfl_list_entry(head, struct cfl_kvpair, _head);
if (pair == NULL || pair->key == NULL || pair->val == NULL) {
continue;
}

fprintf(fp, "\"%s\":", pair->key);
ret = cfl_variant_print(fp, pair->val);

i++;
if (i != size) {
fputs(",", fp);
}
}
fputs("}", fp);

return ret;
}
52 changes: 52 additions & 0 deletions lib/cfl/src/cfl_variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,58 @@
#include <cfl/cfl_array.h>
#include <cfl/cfl_kvlist.h>

int cfl_variant_print(FILE *fp, struct cfl_variant *val)
{
int ret = -1;
size_t size;
size_t i;

if (fp == NULL || val == NULL) {
return -1;
}

switch (val->type) {
case CFL_VARIANT_STRING:
ret = fprintf(fp, "\"%s\"", val->data.as_string);
break;
case CFL_VARIANT_BOOL:
if (val->data.as_bool) {
ret = fputs("true",fp);
}
else {
ret = fputs("false", fp);
}
break;
case CFL_VARIANT_INT:
ret = fprintf(fp, "%" PRId64, val->data.as_int64);
break;
case CFL_VARIANT_DOUBLE:
ret = fprintf(fp, "%lf", val->data.as_double);
break;
case CFL_VARIANT_BYTES:
size = cfl_sds_len(val->data.as_bytes);
for (i=0; i<size; i++) {
ret = fprintf(fp, "%02x", (unsigned char)val->data.as_bytes[i]);
}
break;

case CFL_VARIANT_REFERENCE:
ret = fprintf(fp, "%p", val->data.as_reference);
break;
case CFL_VARIANT_ARRAY:
ret = cfl_array_print(fp, val->data.as_array);
break;

case CFL_VARIANT_KVLIST:
ret = cfl_kvlist_print(fp, val->data.as_kvlist);
break;

default:
ret = fputs("!Unknown Type", fp);
}
return ret;
}

struct cfl_variant *cfl_variant_create_from_string(char *value)
{
struct cfl_variant *instance;
Expand Down
1 change: 1 addition & 0 deletions lib/cfl/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(UNIT_TESTS_FILES
sds.c
hash.c
list.c
variant.c
)

configure_file(
Expand Down
Loading

0 comments on commit 7f92f05

Please sign in to comment.