Skip to content

Commit

Permalink
Fix the array traversing and residual object generation
Browse files Browse the repository at this point in the history
Signed-off-by: Sourav Moitra <sourav.moitr@gmail.com>
  • Loading branch information
xw19 committed Nov 30, 2024
1 parent 443dc33 commit d59d158
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 70 deletions.
77 changes: 37 additions & 40 deletions src/ocispec/json_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,33 +793,33 @@ append_json_map_string_string (json_map_string_string *map, const char *key, con
* Input: json_t
* Output: jansson_array_values *
*/
jansson_array_values *json_array_to_struct(json_t *array) {
if (!json_is_array(array)) {
// Handle error: Input is not an array
return NULL;
}
// jansson_array_values *json_array_to_struct(json_t *array) {
// if (!json_is_array(array)) {
// // Handle error: Input is not an array
// return NULL;
// }

size_t len = json_array_size(array);
jansson_array_values *result = malloc(sizeof(jansson_array_values));
if (!result) {
return NULL; // Handle allocation failure
}
// size_t len = json_array_size(array);
// jansson_array_values *result = malloc(sizeof(jansson_array_values));
// if (!result) {
// return NULL; // Handle allocation failure
// }

result->values = json_array();
result->len = len;
// result->values = json_array();
// result->len = len;

if (!result->values) {
free(result);
return NULL; // Handle allocation failure
}
// if (!result->values) {
// free(result);
// return NULL; // Handle allocation failure
// }

for (size_t i = 0; i < len; i++) {
json_t *value = json_array_get(array, i);
json_array_append_new(result->values, json_incref(value));
}
// for (size_t i = 0; i < len; i++) {
// json_t *value = json_array_get(array, i);
// json_array_append_new(result->values, json_incref(value));
// }

return result;
}
// return result;
// }


/**
Expand Down Expand Up @@ -867,31 +867,28 @@ jansson_object_keys_values *json_object_to_keys_values(json_t *object) {
/**
* copy_unmatched_fields We extract all the fields and we match them with the supplied keys if they don't match
* we add it to new json_t
* Input: json_t, const char **, size_t
* Input: json_t, const char **
* Ouput: jsont_t
*/
json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys, size_t num_keys) {
json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys) {
json_t *dst = json_object();

int len = sizeof(exclude_keys) / sizeof(exclude_keys[0]);

const char *key;
json_t *value;

json_t *key_iter = json_object_iter(src);
while (key_iter) {
const char *key = json_object_iter_key(key_iter);
value = json_object_iter_value(key_iter);

bool found = false;
for (size_t i = 0; i < num_keys; i++) {
if (strcmp(key, exclude_keys[i]) == 0) {
found = true;
break;
}
json_object_foreach(src, key, value) {
int match = 0;
for (int i = 0; i < len; i++) {
if (strcmp(key, exclude_keys[i]) == 0) {
match = 1;
break;
}
}

if (!found) {
json_object_set_new(dst, key, json_incref(value));
if (match == 0) {
json_object_set(dst, key, value);
}

key_iter = json_object_iter_next(src, key_iter);
}

return dst;
Expand Down
4 changes: 2 additions & 2 deletions src/ocispec/json_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ typedef struct
size_t len;
} jansson_array_values;

jansson_array_values *json_array_to_struct(json_t *array);
// jansson_array_values *json_array_to_struct(json_t *array);

typedef struct
{
Expand All @@ -230,7 +230,7 @@ typedef struct

jansson_object_keys_values *json_object_to_keys_values(json_t *object);

json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys, size_t num_keys);
json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys);

#ifdef __cplusplus
}
Expand Down
49 changes: 21 additions & 28 deletions src/ocispec/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def parse_obj_type_array(obj, c_file, prefix, obj_typename):
c_file.append(' json_array_foreach(tmp, i, value)\n')
c_file.append(' {\n')
if obj.doublearray:
c_file.append(' size_t j;//rarara\n')
c_file.append(' size_t j;\n')
c_file.append(' json_t *rec_value;\n')
c_file.append(' size_t rec_len = json_array_size(value);\n')
c_file.append(f' ret->{obj.fixname}[i] = calloc ( rec_len + 1, sizeof (**ret->{obj.fixname}));\n')
Expand Down Expand Up @@ -137,8 +137,8 @@ def parse_obj_type_array(obj, c_file, prefix, obj_typename):
c_file.append(' if (tmp != NULL && (tmp) != NULL)\n')
c_file.append(' {\n')
c_file.append(' size_t i;\n')
c_file.append(' size_t len = json_array_to_struct (tmp)->len;\n')
c_file.append(' json_t *values = json_array_to_struct(tmp)->values;\n')
c_file.append(' json_t *value;\n')
c_file.append(' size_t len = json_array_size(tmp);\n')
c_file.append(f' ret->{obj.fixname}_len = len;\n')
c_file.append(f' ret->{obj.fixname} = calloc (len + 1, sizeof (*ret->{obj.fixname}));\n')
c_file.append(f' if (ret->{obj.fixname} == NULL)\n')
Expand All @@ -147,22 +147,22 @@ def parse_obj_type_array(obj, c_file, prefix, obj_typename):
c_file.append(f' ret->{obj.fixname}_item_lens = calloc ( len + 1, sizeof (size_t));\n')
c_file.append(f' if (ret->{obj.fixname}_item_lens == NULL)\n')
c_file.append(' return NULL;\n')
c_file.append(' for (i = 0; i < len; i++)\n')
c_file.append(' json_array_foreach(tmp, i, value)\n')
c_file.append(' {\n')
if obj.doublearray:
c_file.append(' json_t *items = json_array_to_struct(&values[i])->values;\n')
c_file.append(f' ret->{obj.fixname}[i] = calloc ( json_array_to_struct(&values[i])->len + 1, sizeof (**ret->{obj.fixname}));\n')
c_file.append(f' ret->{obj.fixname}[i] = calloc ( json_array_size(value) + 1, sizeof (**ret->{obj.fixname}));\n')
c_file.append(f' if (ret->{obj.fixname}[i] == NULL)\n')
c_file.append(' return NULL;\n')
c_file.append(' size_t j;\n')
c_file.append(' for (j = 0; j < json_array_to_struct(&values[i])->len; j++)\n')
c_file.append(' json_t *rec_value;\n')
c_file.append(' json_array_foreach(value, j, rec_value)\n')
c_file.append(' {\n')
read_val_generator(c_file, 5, 'items[j]', \
read_val_generator(c_file, 5, 'rec_value', \
f"ret->{obj.fixname}[i][j]", obj.subtyp, obj.origname, obj_typename)
c_file.append(f' ret->{obj.fixname}_item_lens[i] += 1;\n')
c_file.append(' };\n')
else:
read_val_generator(c_file, 4, 'values[i]', \
read_val_generator(c_file, 4, 'value', \
f"ret->{obj.fixname}[i]", obj.subtyp, obj.origname, obj_typename)
c_file.append(' }\n')
c_file.append(' }\n')
Expand Down Expand Up @@ -278,9 +278,8 @@ def parse_obj_arr_obj(obj, c_file, prefix, obj_typename):
}
"""
f"const char *excluded[] = {'{'}{condition}{'}'};"
"""
size_t len = json_object_size(tree);
json_t *resi = copy_unmatched_fields(tree, excluded, len);
"""
json_t *resi = copy_unmatched_fields(tree, excluded);
size_t resilen = json_object_size(resi);
Expand Down Expand Up @@ -584,8 +583,6 @@ def read_val_generator(c_file, level, src, dest, typ, keyname, obj_typename):
c_file.append(f'{" " * (level + 1)} }}\n')
c_file.append(f'{" " * (level)}}}\n')
elif typ == 'string':
if not (src.startswith("json") or src.startswith("work")):
src = '&' + src
c_file.append(f"{' ' * level}const json_t *val = {src};\n")
c_file.append(f"{' ' * level}if (val != NULL)\n")
c_file.append(f"{' ' * (level)} {{\n")
Expand All @@ -595,8 +592,6 @@ def read_val_generator(c_file, level, src, dest, typ, keyname, obj_typename):
c_file.append(f"{' ' * (level + 1)} return NULL;\n")
c_file.append(f'{" " * level} }}\n')
elif helpers.judge_data_type(typ):
if not (src.startswith("json") or src.startswith("work")):
src = '&' + src
c_file.append(f"{' ' * level}const json_t *val = {src};\n")
c_file.append(f"{' ' * level}if (val != NULL)\n")
c_file.append(f'{" " * (level)} {{\n')
Expand Down Expand Up @@ -658,8 +653,6 @@ def read_val_generator(c_file, level, src, dest, typ, keyname, obj_typename):
c_file.append(f'{" " * (level + 1)}}}\n')
c_file.append(f'{" " * (level)}}}\n')
elif typ == 'boolean':
if src.startswith("items"):
src = '&' + src
c_file.append(f"{' ' * level}json_t *val = {src};\n")
c_file.append(f"{' ' * level}if (val != NULL)\n")
c_file.append(f'{" " * (level)} {{\n')
Expand Down Expand Up @@ -1118,6 +1111,7 @@ def get_c_epilog_for_array_make_parse(c_file, prefix, typ, obj):
f" if (ptr->items == NULL)\n" +
f" return NULL;\n" +
f" ptr->len = alen;\n"
f" json_t *work;"
)

if obj.doublearray:
Expand All @@ -1126,9 +1120,8 @@ def get_c_epilog_for_array_make_parse(c_file, prefix, typ, obj):
c_file.append(' return NULL;')

c_file.append("""\n
for (i = 0; i < alen; i++)
json_array_foreach(tree, i, work)
{
json_t *work = &json_array_to_struct (tree)->values[i];
""")

if obj.subtypobj or obj.subtyp == 'object':
Expand All @@ -1139,13 +1132,13 @@ def get_c_epilog_for_array_make_parse(c_file, prefix, typ, obj):

if obj.doublearray:
c_file.append(' size_t j;\n')
c_file.append(' ptr->items[i] = calloc ( json_array_to_struct(work)->len + 1, sizeof (**ptr->items));\n')
c_file.append(' ptr->items[i] = calloc ( json_array_size(work) + 1, sizeof (**ptr->items));\n')
c_file.append(' if (ptr->items[i] == NULL)\n')
c_file.append(' return NULL;\n')
c_file.append(' json_t *tmps = json_array_to_struct(work)->values;\n')
c_file.append(' for (j = 0; j < json_array_to_struct(work)->len; j++)\n')
c_file.append(' json_t *nested_item;\n')
c_file.append(' json_array_foreach(work, j, nested_item)\n')
c_file.append(' {\n')
c_file.append(f' ptr->items[i][j] = make_{subtypename} (&tmps[j], ctx, err);\n')
c_file.append(f' ptr->items[i][j] = make_{subtypename} (nested_item, ctx, err);\n')
c_file.append(' if (ptr->items[i][j] == NULL)\n')
c_file.append(" return NULL;\n")
c_file.append(' ptr->subitem_lens[i] += 1;\n')
Expand All @@ -1166,14 +1159,14 @@ def get_c_epilog_for_array_make_parse(c_file, prefix, typ, obj):
c_file.append(' break;\n')
else:
if obj.doublearray:
c_file.append(' ptr->items[i] = calloc ( json_array_to_struct(work)->len + 1, sizeof (**ptr->items));\n')
c_file.append(' ptr->items[i] = calloc ( json_array_size(work) + 1, sizeof (**ptr->items));\n')
c_file.append(' if (ptr->items[i] == NULL)\n')
c_file.append(' return NULL;\n')
c_file.append(' size_t j;\n')
c_file.append(' json_t *tmps = json_array_to_struct(work)->values;\n')
c_file.append(' for (j = 0; j < json_array_to_struct(work)->len; j++)\n')
c_file.append(' json_t *nested_item;\n')
c_file.append(' json_array_foreach(work, j, nested_item)\n')
c_file.append(' {\n')
read_val_generator(c_file, 3, 'tmps[j]', \
read_val_generator(c_file, 3, 'nested_item', \
"ptr->items[i][j]", obj.subtyp, obj.origname, c_typ)
c_file.append(' ptr->subitem_lens[i] += 1;\n')
c_file.append(' }\n')
Expand Down

0 comments on commit d59d158

Please sign in to comment.