Skip to content

Commit

Permalink
array: adjust prototypes for string and bytes handling
Browse files Browse the repository at this point in the history
This patch adjust the following functions to manipulate the new 'referenced' field
from variants:

 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)

It also adds an extra check for the array size when it's resizable.

Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Apr 12, 2024
1 parent 494bc26 commit 71f3c49
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
11 changes: 7 additions & 4 deletions 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
43 changes: 37 additions & 6 deletions 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

0 comments on commit 71f3c49

Please sign in to comment.