Skip to content

Commit

Permalink
lib: cfl: upgrade to v0.5.0
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Apr 18, 2024
1 parent 7233f9a commit 96a8e4b
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 92 deletions.
2 changes: 1 addition & 1 deletion lib/cfl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

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

Expand Down
11 changes: 7 additions & 4 deletions lib/cfl/include/cfl/cfl_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ static inline struct cfl_variant *cfl_array_fetch_by_index(struct cfl_array *arr
return array->entries[position];
}

int cfl_array_resizable(struct cfl_array *array, int v);
int cfl_array_remove_by_index(struct cfl_array *array, size_t position);
int cfl_array_remove_by_reference(struct cfl_array *array, struct cfl_variant *value);
int cfl_array_append(struct cfl_array *array, struct cfl_variant *value);
int cfl_array_append_string(struct cfl_array *array, char *value);
int cfl_array_append_bytes(struct cfl_array *array, char *value, size_t length);
int cfl_array_append_string_s(struct cfl_array *array, char *str, size_t str_len, int referenced);
int cfl_array_append_bytes(struct cfl_array *array, char *value, size_t length, int referenced);
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);
Expand All @@ -58,6 +56,11 @@ int cfl_array_append_null(struct cfl_array *array);
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_resizable(struct cfl_array *array, int v);
int cfl_array_remove_by_index(struct cfl_array *array, size_t position);
int cfl_array_remove_by_reference(struct cfl_array *array, struct cfl_variant *value);

int cfl_array_print(FILE *fp, struct cfl_array *array);

#endif
9 changes: 6 additions & 3 deletions lib/cfl/include/cfl/cfl_kvlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ int cfl_kvlist_insert_string(struct cfl_kvlist *list,

int cfl_kvlist_insert_bytes(struct cfl_kvlist *list,
char *key, char *value,
size_t value_length);
size_t value_length,
int referenced);

int cfl_kvlist_insert_reference(struct cfl_kvlist *list,
char *key, void *value);
Expand Down Expand Up @@ -78,12 +79,14 @@ int cfl_kvlist_print(FILE *fp, struct cfl_kvlist *list);

int cfl_kvlist_insert_string_s(struct cfl_kvlist *list,
char *key, size_t key_size,
char *value, size_t value_size);
char *value, size_t value_size,
int referenced);

int cfl_kvlist_insert_bytes_s(struct cfl_kvlist *list,
char *key, size_t key_size,
char *value,
size_t value_length);
size_t value_length,
int referenced);

int cfl_kvlist_insert_reference_s(struct cfl_kvlist *list,
char *key, size_t key_size,
Expand Down
37 changes: 26 additions & 11 deletions lib/cfl/include/cfl/cfl_variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
#include <string.h>
#include <inttypes.h>

#define CFL_VARIANT_STRING 1
#define CFL_VARIANT_BOOL 2
#define CFL_VARIANT_INT 3
#define CFL_VARIANT_BOOL 1
#define CFL_VARIANT_INT 2
#define CFL_VARIANT_UINT 3
#define CFL_VARIANT_DOUBLE 4
#define CFL_VARIANT_ARRAY 5
#define CFL_VARIANT_KVLIST 6
#define CFL_VARIANT_BYTES 7
#define CFL_VARIANT_REFERENCE 8
#define CFL_VARIANT_UINT 9
#define CFL_VARIANT_NULL 10
#define CFL_VARIANT_NULL 5
#define CFL_VARIANT_REFERENCE 6
#define CFL_VARIANT_STRING 7
#define CFL_VARIANT_BYTES 8
#define CFL_VARIANT_ARRAY 9
#define CFL_VARIANT_KVLIST 10

struct cfl_array;
struct cfl_kvlist;
Expand All @@ -42,6 +42,18 @@ struct cfl_variant {
int type;
size_t size;

/*
* indicate if 'data' is being referenced for 'as_string' and 'as_bytes':
*
* CFL_TRUE: data is being referenced
* CFL_FALSE: data is owned by the variant
*
* Referenced data uses less memory, when the variant is not referenced we use
* a copy of the original data.
*/
uint8_t referenced;

/* the data */
union {
cfl_sds_t as_string;
cfl_sds_t as_bytes;
Expand All @@ -57,8 +69,8 @@ struct cfl_variant {

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_string_s(char *value, size_t value_length);
struct cfl_variant *cfl_variant_create_from_bytes(char *value, size_t length);
struct cfl_variant *cfl_variant_create_from_string_s(char *value, size_t value_length, int referenced);
struct cfl_variant *cfl_variant_create_from_bytes(char *value, size_t length, int referenced);
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);
Expand All @@ -71,4 +83,7 @@ struct cfl_variant *cfl_variant_create();

void cfl_variant_destroy(struct cfl_variant *instance);

void cfl_variant_size_set(struct cfl_variant *var, size_t size);
size_t cfl_variant_size_get(struct cfl_variant *var);

#endif
43 changes: 37 additions & 6 deletions lib/cfl/src/cfl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ int cfl_array_append(struct cfl_array *array,
* it controls the input data.
*/
if (array->resizable) {

/*
* if the array size is zero (created as an array of 0 slots),
* change the size to 1 so the resize can work properly
*/
if (array->slot_count == 0) {
array->slot_count = 1;
}

/* set new number of slots and total size */
new_slot_count = (array->slot_count * 2);
new_size = (new_slot_count * sizeof(void *));
Expand All @@ -145,13 +154,17 @@ int cfl_array_append(struct cfl_array *array,
return -1;
}
}
array->entries[array->entry_count++] = value;

/* this is just a double check to make sure the slot is really available */
if (array->entry_count >= array->slot_count) {
return -1;
}

array->entries[array->entry_count++] = value;
return 0;
}

int cfl_array_append_string(struct cfl_array *array,
char *value)
int cfl_array_append_string(struct cfl_array *array, char *value)
{
struct cfl_variant *value_instance;
int result;
Expand All @@ -165,7 +178,25 @@ int cfl_array_append_string(struct cfl_array *array,
result = cfl_array_append(array, value_instance);
if (result) {
cfl_variant_destroy(value_instance);
return -2;
}

return 0;
}

int cfl_array_append_string_s(struct cfl_array *array, char *str, size_t str_len, int referenced)
{
struct cfl_variant *value_instance;
int result;

value_instance = cfl_variant_create_from_string_s(str, str_len, referenced);
if (value_instance == NULL) {
return -1;
}

result = cfl_array_append(array, value_instance);
if (result) {
cfl_variant_destroy(value_instance);
return -2;
}

Expand All @@ -174,13 +205,13 @@ int cfl_array_append_string(struct cfl_array *array,

int cfl_array_append_bytes(struct cfl_array *array,
char *value,
size_t length)
size_t length,
int referenced)
{
struct cfl_variant *value_instance;
int result;

value_instance = cfl_variant_create_from_bytes(value, length);

value_instance = cfl_variant_create_from_bytes(value, length, referenced);
if (value_instance == NULL) {
return -1;
}
Expand Down
59 changes: 10 additions & 49 deletions lib/cfl/src/cfl_kvlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,52 +60,15 @@ void cfl_kvlist_destroy(struct cfl_kvlist *list)
free(list);
}

/*
int cfl_kvlist_insert(struct cfl_kvlist *list,
char *key, void *value,
size_t value_length,
int value_type)
{
struct cfl_kvpair *pair;
pair = malloc(sizeof(struct cfl_kvpair));
if (pair == NULL) {
cfl_errno();
return -1;
}
pair->key = cfl_sds_create(key);
if (pair->key == NULL) {
free(pair);
return -2;
}
pair->val = cfl_variant_create(value, value_length, value_type);
if (pair->val == NULL) {
cfl_sds_destroy(pair->key);
free(pair);
return -3;
}
cfl_list_add(&pair->_head, &list->list);
return 0;
}
*/

int cfl_kvlist_insert_string_s(struct cfl_kvlist *list,
char *key, size_t key_size, char *value, size_t value_size)
char *key, size_t key_size,
char *value, size_t value_size,
int referenced)
{
struct cfl_variant *value_instance;
int result;

value_instance = cfl_variant_create_from_string_s(value, value_size);
value_instance = cfl_variant_create_from_string_s(value, value_size, referenced);
if (value_instance == NULL) {
return -1;
}
Expand All @@ -123,13 +86,12 @@ int cfl_kvlist_insert_string_s(struct cfl_kvlist *list,
int cfl_kvlist_insert_bytes_s(struct cfl_kvlist *list,
char *key, size_t key_size,
char *value,
size_t length)
size_t length, int referenced)
{
struct cfl_variant *value_instance;
int result;

value_instance = cfl_variant_create_from_bytes(value, length);

value_instance = cfl_variant_create_from_bytes(value, length, referenced);
if (value_instance == NULL) {
return -1;
}
Expand All @@ -138,7 +100,6 @@ int cfl_kvlist_insert_bytes_s(struct cfl_kvlist *list,

if (result) {
cfl_variant_destroy(value_instance);

return -2;
}

Expand Down Expand Up @@ -390,14 +351,14 @@ int cfl_kvlist_insert_string(struct cfl_kvlist *list,
key_len = strlen(key);
val_len = strlen(value);

return cfl_kvlist_insert_string_s(list, key, key_len, value, val_len);
return cfl_kvlist_insert_string_s(list, key, key_len, value, val_len, CFL_FALSE);
}

int cfl_kvlist_insert_bytes(struct cfl_kvlist *list,
char *key, char *value,
size_t length)
char *key, char *value,
size_t length, int referenced)
{
return cfl_kvlist_insert_bytes_s(list, key, strlen(key), value, length);
return cfl_kvlist_insert_bytes_s(list, key, strlen(key), value, length, referenced);
}

int cfl_kvlist_insert_reference(struct cfl_kvlist *list,
Expand Down
Loading

0 comments on commit 96a8e4b

Please sign in to comment.