diff --git a/src/ocispec/generate.py b/src/ocispec/generate.py index 0e84a8a9..80f5a80e 100755 --- a/src/ocispec/generate.py +++ b/src/ocispec/generate.py @@ -703,8 +703,12 @@ def reflection(schema_info, gen_ref): print("Failed parse schema") sys.exit(1) structs = expand(tree, [], {}) - headers.header_reflect(structs, schema_info, header_file) - sources.src_reflect(structs, schema_info, source_file, tree.typ) + headers_text = [] + headers.header_reflect(structs, schema_info, headers_text) + source_text = [] + header_file.writelines(headers_text) + sources.src_reflect(structs, schema_info, source_text, tree.typ) + source_file.writelines(source_text) except RuntimeError: traceback.print_exc() print("Failed to parse schema file: %s" % schema_info.name.name) diff --git a/src/ocispec/headers.py b/src/ocispec/headers.py index 606903d9..cbf00de2 100755 --- a/src/ocispec/headers.py +++ b/src/ocispec/headers.py @@ -37,7 +37,7 @@ def append_header_arr(obj, header, prefix): if not obj.subtypobj or obj.subtypname: return - header.write("typedef struct {\n") + header.append("typedef struct {\n") for i in obj.subtypobj: if i.typ == 'array': c_typ = helpers.get_prefixed_pointer(i.name, i.subtyp, prefix) or \ @@ -46,23 +46,21 @@ def append_header_arr(obj, header, prefix): c_typ = helpers.get_name_substr(i.name, prefix) if not helpers.judge_complex(i.subtyp): - header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", \ - i.fixname)) + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}*{i.fixname};\n") else: - header.write(" %s **%s;\n" % (c_typ, i.fixname)) - header.write(" size_t %s;\n\n" % (i.fixname + "_len")) + header.append(f" {c_typ} **{i.fixname};\n") + header.append(f" size_t {i.fixname + '_len'};\n\n") else: c_typ = helpers.get_prefixed_pointer(i.name, i.typ, prefix) or \ helpers.get_map_c_types(i.typ) - header.write(" %s%s%s;\n" % (c_typ, " " if '*' not in c_typ else "", i.fixname)) + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}{i.fixname};\n") for i in obj.subtypobj: if helpers.judge_data_type(i.typ) or i.typ == 'boolean': - header.write(" unsigned int %s_present : 1;\n" % (i.fixname)) + header.append(f" unsigned int {i.fixname}_present : 1;\n") typename = helpers.get_name_substr(obj.name, prefix) - header.write("}\n%s;\n\n" % typename) - header.write("void free_%s (%s *ptr);\n\n" % (typename, typename)) - header.write("%s *make_%s (yajl_val tree, const struct parser_context *ctx, parser_error *err);"\ - "\n\n" % (typename, typename)) + header.append(f"}}\n{typename};\n\n") + header.append(f"void free_{typename} ({typename} *ptr);\n\n") + header.append(f"{typename} *make_{typename} (yajl_val tree, const struct parser_context *ctx, parser_error *err);\n\n") def append_header_map_str_obj(obj, header, prefix): @@ -72,16 +70,16 @@ def append_header_map_str_obj(obj, header, prefix): History: 2019-06-17 ''' child = obj.children[0] - header.write("typedef struct {\n") - header.write(" char **keys;\n") + header.append("typedef struct {\n") + header.append(" char **keys;\n") if helpers.valid_basic_map_name(child.typ): c_typ = helpers.get_prefixed_pointer("", child.typ, "") elif child.subtypname: c_typ = child.subtypname else: c_typ = helpers.get_prefixed_pointer(child.name, child.typ, prefix) - header.write(" %s%s*%s;\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname)) - header.write(" size_t len;\n") + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}*{child.fixname};\n") + header.append(" size_t len;\n") def append_header_child_arr(child, header, prefix): @@ -93,7 +91,7 @@ def append_header_child_arr(child, header, prefix): if helpers.get_map_c_types(child.subtyp) != "": c_typ = helpers.get_map_c_types(child.subtyp) elif helpers.valid_basic_map_name(child.subtyp): - c_typ = '%s *' % helpers.make_basic_map_name(child.subtyp) + c_typ = f'{helpers.make_basic_map_name(child.subtyp)} *' elif child.subtypname is not None: c_typ = child.subtypname elif child.subtypobj is not None: @@ -106,16 +104,16 @@ def append_header_child_arr(child, header, prefix): dflag = "*" if helpers.valid_basic_map_name(child.subtyp): - header.write(" %s **%s;\n" % (helpers.make_basic_map_name(child.subtyp), child.fixname)) + header.append(f" {helpers.make_basic_map_name(child.subtyp)} **{child.fixname};\n") elif not helpers.judge_complex(child.subtyp): - header.write(" %s%s*%s%s;\n" % (c_typ, " " if '*' not in c_typ else "", dflag, child.fixname)) + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}*{dflag}{child.fixname};\n") else: - header.write(" %s%s**%s%s;\n" % (c_typ, " " if '*' not in c_typ else "", dflag, child.fixname)) + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}**{dflag}{child.fixname};\n") if child.doublearray and not helpers.valid_basic_map_name(child.subtyp): - header.write(" size_t *%s;\n" % (child.fixname + "_item_lens")) + header.append(f" size_t *{child.fixname + '_item_lens'};\n") - header.write(" size_t %s;\n\n" % (child.fixname + "_len")) + header.append(f" size_t {child.fixname + '_len'};\n\n") def append_header_child_others(child, header, prefix): ''' @@ -126,12 +124,12 @@ def append_header_child_others(child, header, prefix): if helpers.get_map_c_types(child.typ) != "": c_typ = helpers.get_map_c_types(child.typ) elif helpers.valid_basic_map_name(child.typ): - c_typ = '%s *' % helpers.make_basic_map_name(child.typ) + c_typ = f'{helpers.make_basic_map_name(child.typ)} *' elif child.subtypname: c_typ = helpers.get_prefixed_pointer(child.subtypname, child.typ, "") else: c_typ = helpers.get_prefixed_pointer(child.name, child.typ, prefix) - header.write(" %s%s%s;\n\n" % (c_typ, " " if '*' not in c_typ else "", child.fixname)) + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}{child.fixname};\n\n") def append_type_c_header(obj, header, prefix): @@ -154,30 +152,28 @@ def append_type_c_header(obj, header, prefix): elif obj.typ == 'object': if obj.subtypname is not None: return - header.write("typedef struct {\n") + header.append("typedef struct {\n") if obj.children is None: - header.write(" char unuseful; // unuseful definition to avoid empty struct\n") + header.append(" char unuseful; // unuseful definition to avoid empty struct\n") present_tags = [] for i in obj.children or []: if helpers.judge_data_type(i.typ) or i.typ == 'boolean': - present_tags.append(" unsigned int %s_present : 1;\n" % (i.fixname)) + present_tags.append(f" unsigned int {i.fixname}_present : 1;\n") if i.typ == 'array': append_header_child_arr(i, header, prefix) else: append_header_child_others(i, header, prefix) if obj.children is not None: - header.write(" yajl_val _residual;\n") + header.append(" yajl_val _residual;\n") if len(present_tags) > 0: - header.write("\n") + header.append("\n") for tag in present_tags: - header.write(tag) + header.append(tag) typename = helpers.get_prefixed_name(obj.name, prefix) - header.write("}\n%s;\n\n" % typename) - header.write("void free_%s (%s *ptr);\n\n" % (typename, typename)) - header.write("%s *make_%s (yajl_val tree, const struct parser_context *ctx, parser_error *err)"\ - ";\n\n" % (typename, typename)) - header.write("yajl_gen_status gen_%s (yajl_gen g, const %s *ptr, const struct parser_context "\ - "*ctx, parser_error *err);\n\n" % (typename, typename)) + header.append(f"}}\n{typename};\n\n") + header.append(f"void free_{typename} ({typename} *ptr);\n\n") + header.append(f"{typename} *make_{typename} (yajl_val tree, const struct parser_context *ctx, parser_error *err);\n\n") + header.append(f"yajl_gen_status gen_{typename} (yajl_gen g, const {typename} *ptr, const struct parser_context *ctx, parser_error *err);\n\n") def header_reflect_top_array(obj, prefix, header): c_typ = helpers.get_prefixed_pointer(obj.name, obj.subtyp, prefix) or \ @@ -191,25 +187,25 @@ def header_reflect_top_array(obj, prefix, header): return typename = helpers.get_top_array_type_name(obj.name, prefix) - header.write("typedef struct {\n") + header.append("typedef struct {\n") if obj.doublearray: - header.write(" %s%s**items;\n" % (c_typ, " " if '*' not in c_typ else "")) - header.write(" size_t *subitem_lens;\n\n") + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}**items;\n") + header.append(" size_t *subitem_lens;\n\n") else: - header.write(" %s%s*items;\n" % (c_typ, " " if '*' not in c_typ else "")) - header.write(" size_t len;\n\n") - header.write("}\n%s;\n\n" % (typename)) + header.append(f" {c_typ}{' ' if '*' not in c_typ else ''}*items;\n") + header.append(" size_t len;\n\n") + header.append(f"}}\n{typename};\n\n") - header.write("void free_%s (%s *ptr);\n\n" % (typename, typename)) - header.write("%s *%s_parse_file(const char *filename, const struct "\ - "parser_context *ctx, parser_error *err);\n\n" % (typename, typename)) - header.write("%s *%s_parse_file_stream(FILE *stream, const struct "\ - "parser_context *ctx, parser_error *err);\n\n" % (typename, typename)) - header.write("%s *%s_parse_data(const char *jsondata, const struct "\ - "parser_context *ctx, parser_error *err);\n\n" % (typename, typename)) - header.write("char *%s_generate_json(const %s *ptr, "\ - "const struct parser_context *ctx, parser_error *err);\n\n" % (typename, typename)) + header.append(f"void free_{typename} ({typename} *ptr);\n\n") + header.append(f"{typename} *{typename}_parse_file(const char *filename, const struct "\ + "parser_context *ctx, parser_error *err);\n\n") + header.append(f"{typename} *{typename}_parse_file_stream(FILE *stream, const struct "\ + "parser_context *ctx, parser_error *err);\n\n") + header.append(f"{typename} *{typename}_parse_data(const char *jsondata, const struct "\ + "parser_context *ctx, parser_error *err);\n\n") + header.append(f"char *{typename}_generate_json(const {typename} *ptr, "\ + "const struct parser_context *ctx, parser_error *err);\n\n") def header_reflect(structs, schema_info, header): ''' @@ -218,36 +214,36 @@ def header_reflect(structs, schema_info, header): History: 2019-06-17 ''' prefix = schema_info.prefix - header.write("// Generated from %s. Do not edit!\n" % (schema_info.name.basename)) - header.write("#ifndef %s_SCHEMA_H\n" % prefix.upper()) - header.write("#define %s_SCHEMA_H\n\n" % prefix.upper()) - header.write("#include \n") - header.write("#include \n") - header.write("#include \"ocispec/json_common.h\"\n") + header.append(f"// Generated from {schema_info.name.basename}. Do not edit!\n") + header.append(f"#ifndef {prefix.upper()}_SCHEMA_H\n") + header.append(f"#define {prefix.upper()}_SCHEMA_H\n\n") + header.append("#include \n") + header.append("#include \n") + header.append("#include \"ocispec/json_common.h\"\n") if schema_info.refs: for ref in schema_info.refs.keys(): - header.write("#include \"ocispec/%s\"\n" % (ref)) - header.write("\n#ifdef __cplusplus\n") - header.write("extern \"C\" {\n") - header.write("#endif\n\n") + header.append(f"#include \"ocispec/{ref}\"\n") + header.append("\n#ifdef __cplusplus\n") + header.append("extern \"C\" {\n") + header.append("#endif\n\n") for i in structs: append_type_c_header(i, header, prefix) length = len(structs) toptype = structs[length - 1].typ if length != 0 else "" if toptype == 'object': - header.write("%s *%s_parse_file(const char *filename, const struct parser_context *ctx, "\ - "parser_error *err);\n\n" % (prefix, prefix)) - header.write("%s *%s_parse_file_stream(FILE *stream, const struct parser_context *ctx, "\ - "parser_error *err);\n\n" % (prefix, prefix)) - header.write("%s *%s_parse_data(const char *jsondata, const struct parser_context *ctx, "\ - "parser_error *err);\n\n" % (prefix, prefix)) - header.write("char *%s_generate_json(const %s *ptr, const struct parser_context *ctx, "\ - "parser_error *err);\n\n" % (prefix, prefix)) + header.append(f"{prefix} *{prefix}_parse_file(const char *filename, const struct parser_context *ctx, "\ + "parser_error *err);\n\n") + header.append(f"{prefix} *{prefix}_parse_file_stream(FILE *stream, const struct parser_context *ctx, "\ + "parser_error *err);\n\n") + header.append(f"{prefix} *{prefix}_parse_data(const char *jsondata, const struct parser_context *ctx, "\ + "parser_error *err);\n\n") + header.append(f"char *{prefix}_generate_json(const {prefix} *ptr, const struct parser_context *ctx, "\ + "parser_error *err);\n\n") elif toptype == 'array': header_reflect_top_array(structs[length - 1], prefix, header) - header.write("#ifdef __cplusplus\n") - header.write("}\n") - header.write("#endif\n\n") - header.write("#endif\n\n") + header.append("#ifdef __cplusplus\n") + header.append("}\n") + header.append("#endif\n\n") + header.append("#endif\n\n") diff --git a/src/ocispec/sources.py b/src/ocispec/sources.py index 77b127ef..f1c499d2 100755 --- a/src/ocispec/sources.py +++ b/src/ocispec/sources.py @@ -30,7 +30,6 @@ import helpers - def append_c_code(obj, c_file, prefix): """ Description: append c language code to file @@ -56,34 +55,32 @@ def parse_map_string_obj(obj, c_file, prefix, obj_typename): childname = child.subtypname else: childname = helpers.get_prefixed_name(child.name, prefix) - c_file.write(' if (YAJL_GET_OBJECT (tree) != NULL)\n') - c_file.write(' {\n') - c_file.write(' size_t i;\n') - c_file.write(' size_t len = YAJL_GET_OBJECT_NO_CHECK (tree)->len;\n') - c_file.write(' const char **keys = YAJL_GET_OBJECT_NO_CHECK (tree)->keys;\n') - c_file.write(' yajl_val *values = YAJL_GET_OBJECT_NO_CHECK (tree)->values;\n') - c_file.write(' ret->len = len;\n') - c_file.write(' ret->keys = calloc (len + 1, sizeof (*ret->keys));\n') - c_file.write(' if (ret->keys == NULL)\n') - c_file.write(' return NULL;\n') - c_file.write(' ret->%s = calloc (len + 1, sizeof (*ret->%s));\n' % \ - (child.fixname, child.fixname)) - c_file.write(' if (ret->%s == NULL)\n' % child.fixname) - c_file.write(' return NULL;\n') - c_file.write(' for (i = 0; i < len; i++)\n') - c_file.write(' {\n') - c_file.write(' yajl_val val;\n') - c_file.write(' const char *tmpkey = keys[i];\n') - c_file.write(' ret->keys[i] = strdup (tmpkey ? tmpkey : "");\n') - c_file.write(' if (ret->keys[i] == NULL)\n') - c_file.write(" return NULL;\n") - c_file.write(' val = values[i];\n') - c_file.write(' ret->%s[i] = make_%s (val, ctx, err);\n' \ - % (child.fixname, childname)) - c_file.write(' if (ret->%s[i] == NULL)\n' % (child.fixname)) - c_file.write(" return NULL;\n") - c_file.write(' }\n') - c_file.write(' }\n') + c_file.append(' if (YAJL_GET_OBJECT (tree) != NULL)\n') + c_file.append(' {\n') + c_file.append(' size_t i;\n') + c_file.append(' size_t len = YAJL_GET_OBJECT_NO_CHECK (tree)->len;\n') + c_file.append(' const char **keys = YAJL_GET_OBJECT_NO_CHECK (tree)->keys;\n') + c_file.append(' yajl_val *values = YAJL_GET_OBJECT_NO_CHECK (tree)->values;\n') + c_file.append(' ret->len = len;\n') + c_file.append(' ret->keys = calloc (len + 1, sizeof (*ret->keys));\n') + c_file.append(' if (ret->keys == NULL)\n') + c_file.append(' return NULL;\n') + c_file.append(f' ret->{child.fixname} = calloc (len + 1, sizeof (*ret->{child.fixname}));\n') + c_file.append(f' if (ret->{child.fixname} == NULL)\n') + c_file.append(' return NULL;\n') + c_file.append(' for (i = 0; i < len; i++)\n') + c_file.append(' {\n') + c_file.append(' yajl_val val;\n') + c_file.append(' const char *tmpkey = keys[i];\n') + c_file.append(' ret->keys[i] = strdup (tmpkey ? tmpkey : "");\n') + c_file.append(' if (ret->keys[i] == NULL)\n') + c_file.append(" return NULL;\n") + c_file.append(' val = values[i];\n') + c_file.append(f' ret->{child.fixname}[i] = make_{childname} (val, ctx, err);\n') + c_file.append(f' if (ret->{child.fixname}[i] == NULL)\n') + c_file.append(" return NULL;\n") + c_file.append(' }\n') + c_file.append(' }\n') def parse_obj_type_array(obj, c_file, prefix, obj_typename): @@ -92,127 +89,112 @@ def parse_obj_type_array(obj, c_file, prefix, obj_typename): typename = obj.subtypname else: typename = helpers.get_name_substr(obj.name, prefix) - c_file.write(' do\n') - c_file.write(' {\n') - c_file.write(' yajl_val tmp = get_val (tree, "%s", yajl_t_array);\n' \ - % (obj.origname)) - c_file.write(' if (tmp != NULL && YAJL_GET_ARRAY (tmp) != NULL)\n') - c_file.write(' {\n') - c_file.write(' size_t i;\n') - c_file.write(' size_t len = YAJL_GET_ARRAY_NO_CHECK (tmp)->len;\n') - c_file.write(' yajl_val *values = YAJL_GET_ARRAY_NO_CHECK (tmp)->values;\n') - c_file.write(' ret->%s_len = len;\n' % (obj.fixname)) - c_file.write(' ret->%s = calloc (len + 1, sizeof (*ret->%s));\n' % \ - (obj.fixname, obj.fixname)) - c_file.write(' if (ret->%s == NULL)\n' % obj.fixname) - c_file.write(' return NULL;\n') + c_file.append(' do\n') + c_file.append(' {\n') + c_file.append(f' yajl_val tmp = get_val (tree, "{obj.origname}", yajl_t_array);\n') + c_file.append(' if (tmp != NULL && YAJL_GET_ARRAY (tmp) != NULL)\n') + c_file.append(' {\n') + c_file.append(' size_t i;\n') + c_file.append(' size_t len = YAJL_GET_ARRAY_NO_CHECK (tmp)->len;\n') + c_file.append(' yajl_val *values = YAJL_GET_ARRAY_NO_CHECK (tmp)->values;\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') + c_file.append(' return NULL;\n') if obj.doublearray: - c_file.write(' ret->%s_item_lens = calloc ( len + 1, sizeof (size_t));\n' - % (obj.fixname)) - c_file.write(' if (ret->%s_item_lens == NULL)\n' % (obj.fixname)) - c_file.write(' return NULL;\n') - c_file.write(' for (i = 0; i < len; i++)\n') - c_file.write(' {\n') - c_file.write(' yajl_val val = values[i];\n') + 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(' {\n') + c_file.append(' yajl_val val = values[i];\n') if obj.doublearray: - c_file.write(' size_t j;\n') - c_file.write(' ret->%s[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(val)->len + 1, sizeof (**ret->%s));\n' - % (obj.fixname, obj.fixname)) - c_file.write(' if (ret->%s[i] == NULL)\n' % obj.fixname) - c_file.write(' return NULL;\n') - c_file.write(' yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(val)->values;\n') - c_file.write(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(val)->len; j++)\n') - c_file.write(' {\n') - c_file.write(' ret->%s[i][j] = make_%s (items[j], ctx, err);\n' \ - % (obj.fixname, typename)) - c_file.write(' if (ret->%s[i][j] == NULL)\n' % (obj.fixname)) - c_file.write(" return NULL;\n") - c_file.write(' ret->%s_item_lens[i] += 1;\n' % (obj.fixname)) - c_file.write(' };\n') + c_file.append(' size_t j;\n') + c_file.append(f' ret->{obj.fixname}[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(val)->len + 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(' yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(val)->values;\n') + c_file.append(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(val)->len; j++)\n') + c_file.append(' {\n') + c_file.append(f' ret->{obj.fixname}[i][j] = make_{typename} (items[j], ctx, err);\n') + c_file.append(f' if (ret->{obj.fixname}[i][j] == NULL)\n') + c_file.append(" return NULL;\n") + c_file.append(f' ret->{obj.fixname}_item_lens[i] += 1;\n') + c_file.append(' };\n') else: - c_file.write(' ret->%s[i] = make_%s (val, ctx, err);\n' \ - % (obj.fixname, typename)) - c_file.write(' if (ret->%s[i] == NULL)\n' % (obj.fixname)) - c_file.write(" return NULL;\n") - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(f' ret->{obj.fixname}[i] = make_{typename} (val, ctx, err);\n') + c_file.append(f' if (ret->{obj.fixname}[i] == NULL)\n') + c_file.append(" return NULL;\n") + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' while (0);\n') elif obj.subtyp == 'byte': - c_file.write(' do\n') - c_file.write(' {\n') - c_file.write(' yajl_val tmp = get_val (tree, "%s", yajl_t_string);\n' \ - % (obj.origname)) - c_file.write(' if (tmp != NULL)\n') - c_file.write(' {\n') + c_file.append(' do\n') + c_file.append(' {\n') + c_file.append(f' yajl_val tmp = get_val (tree, "{obj.origname}", yajl_t_string);\n') + c_file.append(' if (tmp != NULL)\n') + c_file.append(' {\n') if obj.doublearray: - c_file.write(' yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(tmp)->values;\n') - c_file.write(' ret->%s = calloc ( YAJL_GET_ARRAY_NO_CHECK(tmp)->len + 1, sizeof (*ret->%s));\n' - % (obj.fixname, obj.fixname)) - c_file.write(' if (ret->%s[i] == NULL)\n' % obj.fixname) - c_file.write(' return NULL;\n') - c_file.write(' size_t j;\n') - c_file.write(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(tmp)->len; j++)\n') - c_file.write(' {\n') - c_file.write(' char *str = YAJL_GET_STRING (itmes[j]);\n') - c_file.write(' ret->%s[j] = (uint8_t *)strdup (str ? str : "");\n' \ - % obj.fixname) - c_file.write(' if (ret->%s[j] == NULL)\n' % (obj.fixname)) - c_file.write(" return NULL;\n") - c_file.write(' };\n') + c_file.append(' yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(tmp)->values;\n') + c_file.append(f' ret->{obj.fixname} = calloc ( YAJL_GET_ARRAY_NO_CHECK(tmp)->len + 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 < YAJL_GET_ARRAY_NO_CHECK(tmp)->len; j++)\n') + c_file.append(' {\n') + c_file.append(' char *str = YAJL_GET_STRING (itmes[j]);\n') + c_file.append(f' ret->{obj.fixname}[j] = (uint8_t *)strdup (str ? str : "");\n') + c_file.append(f' if (ret->{obj.fixname}[j] == NULL)\n') + c_file.append(" return NULL;\n") + c_file.append(' };\n') else: - c_file.write(' char *str = YAJL_GET_STRING (tmp);\n') - c_file.write(' ret->%s = (uint8_t *)strdup (str ? str : "");\n' \ - % obj.fixname) - c_file.write(' if (ret->%s == NULL)\n' % obj.fixname) - c_file.write(' return NULL;\n') - c_file.write(' ret->%s_len = str != NULL ? strlen (str) : 0;\n' \ - % obj.fixname) - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' char *str = YAJL_GET_STRING (tmp);\n') + c_file.append(f' ret->{obj.fixname} = (uint8_t *)strdup (str ? str : "");\n') + c_file.append(f' if (ret->{obj.fixname} == NULL)\n') + c_file.append(' return NULL;\n') + c_file.append(f' ret->{obj.fixname}_len = str != NULL ? strlen (str) : 0;\n') + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' while (0);\n') else: - c_file.write(' do\n') - c_file.write(' {\n') - c_file.write(' yajl_val tmp = get_val (tree, "%s", yajl_t_array);\n' \ - % (obj.origname)) - c_file.write(' if (tmp != NULL && YAJL_GET_ARRAY (tmp) != NULL)\n') - c_file.write(' {\n') - c_file.write(' size_t i;\n') - c_file.write(' size_t len = YAJL_GET_ARRAY_NO_CHECK (tmp)->len;\n') - c_file.write(' yajl_val *values = YAJL_GET_ARRAY_NO_CHECK (tmp)->values;\n') - c_file.write(' ret->%s_len = len;\n' % (obj.fixname)) - c_file.write(' ret->%s = calloc (len + 1, sizeof (*ret->%s));\n' \ - % (obj.fixname, obj.fixname)) - c_file.write(' if (ret->%s == NULL)\n' % obj.fixname) - c_file.write(' return NULL;\n') + c_file.append(' do\n') + c_file.append(' {\n') + c_file.append(f' yajl_val tmp = get_val (tree, "{obj.origname}", yajl_t_array);\n') + c_file.append(' if (tmp != NULL && YAJL_GET_ARRAY (tmp) != NULL)\n') + c_file.append(' {\n') + c_file.append(' size_t i;\n') + c_file.append(' size_t len = YAJL_GET_ARRAY_NO_CHECK (tmp)->len;\n') + c_file.append(' yajl_val *values = YAJL_GET_ARRAY_NO_CHECK (tmp)->values;\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') + c_file.append(' return NULL;\n') if obj.doublearray: - c_file.write(' ret->%s_item_lens = calloc ( len + 1, sizeof (size_t));\n' - % (obj.fixname)) - c_file.write(' if (ret->%s_item_lens == NULL)\n' % (obj.fixname)) - c_file.write(' return NULL;\n') - c_file.write(' for (i = 0; i < len; i++)\n') - c_file.write(' {\n') + 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(' {\n') if obj.doublearray: - c_file.write(' yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(values[i])->values;\n') - c_file.write(' ret->%s[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(values[i])->len + 1, sizeof (**ret->%s));\n' - % (obj.fixname, obj.fixname)) - c_file.write(' if (ret->%s[i] == NULL)\n' % obj.fixname) - c_file.write(' return NULL;\n') - c_file.write(' size_t j;\n') - c_file.write(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(values[i])->len; j++)\n') - c_file.write(' {\n') + c_file.append(' yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(values[i])->values;\n') + c_file.append(f' ret->{obj.fixname}[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(values[i])->len + 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 < YAJL_GET_ARRAY_NO_CHECK(values[i])->len; j++)\n') + c_file.append(' {\n') read_val_generator(c_file, 5, 'items[j]', \ - "ret->%s[i][j]" % obj.fixname, obj.subtyp, obj.origname, obj_typename) - c_file.write(' ret->%s_item_lens[i] += 1;\n' % (obj.fixname)) - c_file.write(' };\n') + 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]', \ - "ret->%s[i]" % obj.fixname, obj.subtyp, obj.origname, obj_typename) - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' while (0);\n') + f"ret->{obj.fixname}[i]", obj.subtyp, obj.origname, obj_typename) + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' while (0);\n') def parse_obj_type(obj, c_file, prefix, obj_typename): """ @@ -221,75 +203,71 @@ def parse_obj_type(obj, c_file, prefix, obj_typename): History: 2019-06-17 """ if obj.typ == 'string': - c_file.write(' do\n') - c_file.write(' {\n') - read_val_generator(c_file, 2, 'get_val (tree, "%s", yajl_t_string)' % obj.origname, \ - "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' do\n') + c_file.append(' {\n') + read_val_generator(c_file, 2, f'get_val (tree, "{obj.origname}", yajl_t_string)', \ + f"ret->{obj.fixname}", obj.typ, obj.origname, obj_typename) + c_file.append(' }\n') + c_file.append(' while (0);\n') elif helpers.judge_data_type(obj.typ): - c_file.write(' do\n') - c_file.write(' {\n') - read_val_generator(c_file, 2, 'get_val (tree, "%s", yajl_t_number)' % obj.origname, \ - "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' do\n') + c_file.append(' {\n') + read_val_generator(c_file, 2, f'get_val (tree, "{obj.origname}", yajl_t_number)', \ + f"ret->{obj.fixname}", obj.typ, obj.origname, obj_typename) + c_file.append(' }\n') + c_file.append(' while (0);\n') elif helpers.judge_data_pointer_type(obj.typ): - c_file.write(' do\n') - c_file.write(' {\n') - read_val_generator(c_file, 2, 'get_val (tree, "%s", yajl_t_number)' % obj.origname, \ - "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' do\n') + c_file.append(' {\n') + read_val_generator(c_file, 2, f'get_val (tree, "{obj.origname}", yajl_t_number)', \ + f"ret->{obj.fixname}", obj.typ, obj.origname, obj_typename) + c_file.append(' }\n') + c_file.append(' while (0);\n') if obj.typ == 'boolean': - c_file.write(' do\n') - c_file.write(' {\n') - read_val_generator(c_file, 2, 'get_val (tree, "%s", yajl_t_true)' % obj.origname, \ - "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' do\n') + c_file.append(' {\n') + read_val_generator(c_file, 2, f'get_val (tree, "{obj.origname}", yajl_t_true)', \ + f"ret->{obj.fixname}", obj.typ, obj.origname, obj_typename) + c_file.append(' }\n') + c_file.append(' while (0);\n') if obj.typ == 'booleanPointer': - c_file.write(' do\n') - c_file.write(' {\n') - read_val_generator(c_file, 2, 'get_val (tree, "%s", yajl_t_true)' % obj.origname, \ - "ret->%s" % obj.fixname, obj.typ, obj.origname, obj_typename) - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' do\n') + c_file.append(' {\n') + read_val_generator(c_file, 2, f'get_val (tree, "{obj.origname}", yajl_t_true)', \ + f"ret->{obj.fixname}", obj.typ, obj.origname, obj_typename) + c_file.append(' }\n') + c_file.append(' while (0);\n') elif obj.typ == 'object' or obj.typ == 'mapStringObject': if obj.subtypname is not None: typename = obj.subtypname else: typename = helpers.get_prefixed_name(obj.name, prefix) - c_file.write( - ' ret->%s = make_%s (get_val (tree, "%s", yajl_t_object), ctx, err);\n' \ - % (obj.fixname, typename, obj.origname)) - c_file.write(" if (ret->%s == NULL && *err != 0)\n" % obj.fixname) - c_file.write(" return NULL;\n") + c_file.append( + f' ret->{obj.fixname} = make_{typename} (get_val (tree, "{obj.origname}", yajl_t_object), ctx, err);\n') + c_file.append(f" if (ret->{obj.fixname} == NULL && *err != 0)\n") + c_file.append(" return NULL;\n") elif obj.typ == 'array': parse_obj_type_array(obj, c_file, prefix, obj_typename) elif helpers.valid_basic_map_name(obj.typ): - c_file.write(' do\n') - c_file.write(' {\n') - c_file.write(' yajl_val tmp = get_val (tree, "%s", yajl_t_object);\n' \ - % (obj.origname)) - c_file.write(' if (tmp != NULL)\n') - c_file.write(' {\n') - c_file.write(' ret->%s = make_%s (tmp, ctx, err);\n' \ - % (obj.fixname, helpers.make_basic_map_name(obj.typ))) - c_file.write(' if (ret->%s == NULL)\n' % (obj.fixname)) - c_file.write(' {\n') - c_file.write(' char *new_error = NULL;\n') - c_file.write(" if (asprintf (&new_error, \"Value error for key" \ - " '%s': %%s\", *err ? *err : \"null\") < 0)\n" % (obj.origname)) - c_file.write(' new_error = strdup (' \ + c_file.append(' do\n') + c_file.append(' {\n') + c_file.append(f' yajl_val tmp = get_val (tree, "{obj.origname}", yajl_t_object);\n') + c_file.append(' if (tmp != NULL)\n') + c_file.append(' {\n') + c_file.append(f' ret->{obj.fixname} = make_{helpers.make_basic_map_name(obj.typ)} (tmp, ctx, err);\n') + c_file.append(f' if (ret->{obj.fixname} == NULL)\n') + c_file.append(' {\n') + c_file.append(' char *new_error = NULL;\n') + c_file.append(f" if (asprintf (&new_error, \"Value error for key '{obj.origname}': %s\", *err ? *err : \"null\") < 0)\n") + c_file.append(' new_error = strdup (' \ '"error allocating memory");\n') - c_file.write(' free (*err);\n') - c_file.write(' *err = new_error;\n') - c_file.write(' return NULL;\n') - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(' while (0);\n') + c_file.append(' free (*err);\n') + c_file.append(' *err = new_error;\n') + c_file.append(' return NULL;\n') + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(' while (0);\n') def parse_obj_arr_obj(obj, c_file, prefix, obj_typename): """ @@ -306,19 +284,18 @@ def parse_obj_arr_obj(obj, c_file, prefix, obj_typename): parse_obj_type(i, c_file, prefix, obj_typename) for i in required_to_check: - c_file.write(' if (ret->%s == NULL)\n' % i.fixname) - c_file.write(' {\n') - c_file.write(' if (asprintf (err, "Required field \'%%s\' not present", ' \ - ' "%s") < 0)\n' % i.origname) - c_file.write(' *err = strdup ("error allocating memory");\n') - c_file.write(" return NULL;\n") - c_file.write(' }\n') + c_file.append(f' if (ret->{i.fixname} == NULL)\n') + c_file.append(' {\n') + c_file.append(f' if (asprintf (err, "Required field \'%s\' not present", "{i.origname}") < 0)\n') + c_file.append(' *err = strdup ("error allocating memory");\n') + c_file.append(" return NULL;\n") + c_file.append(' }\n') if obj.typ == 'object' and obj.children is not None: # O(n^2) complexity, but the objects should not really be big... condition = "\n && ".join( \ - ['strcmp (tree->u.object.keys[i], "%s")' % i.origname for i in obj.children]) - c_file.write(""" + [f'strcmp (tree->u.object.keys[i], "{i.origname}")' for i in obj.children]) + c_file.append(""" if (tree->type == yajl_t_object) { size_t i; @@ -348,9 +325,9 @@ def parse_obj_arr_obj(obj, c_file, prefix, obj_typename): } for (i = 0; i < tree->u.object.len; i++) - { - if (%s) - { + {""" \ + f"if ({condition})" \ + """{ if (ctx->options & OPT_PARSE_FULLKEY) { resi->u.object.keys[j] = tree->u.object.keys[i]; @@ -370,7 +347,7 @@ def parse_obj_arr_obj(obj, c_file, prefix, obj_typename): if (ctx->options & OPT_PARSE_FULLKEY) ret->_residual = resi; } -""" % (condition)) +""") def parse_json_to_c(obj, c_file, prefix): @@ -390,25 +367,24 @@ def parse_json_to_c(obj, c_file, prefix): objs = obj.subtypobj if objs is None or obj.subtypname: return - c_file.write("define_cleaner_function (%s *, free_%s)\n" % (typename, typename)) - c_file.write("%s *\nmake_%s (yajl_val tree, const struct parser_context *ctx, "\ - "parser_error *err)\n" % (typename, typename)) - c_file.write("{\n") - c_file.write(" __auto_cleanup(free_%s) %s *ret = NULL;\n" % (typename, typename)) - c_file.write(" *err = NULL;\n") - c_file.write(" (void) ctx; /* Silence compiler warning. */\n") - c_file.write(" if (tree == NULL)\n") - c_file.write(" return NULL;\n") - c_file.write(" ret = calloc (1, sizeof (*ret));\n") - c_file.write(" if (ret == NULL)\n") - c_file.write(" return NULL;\n") + c_file.append(f"define_cleaner_function ({typename} *, free_{typename})\n") + c_file.append(f"{typename} *\nmake_{typename} (yajl_val tree, const struct parser_context *ctx, parser_error *err)\n") + c_file.append("{\n") + c_file.append(f" __auto_cleanup(free_{typename}) {typename} *ret = NULL;\n") + c_file.append(" *err = NULL;\n") + c_file.append(" (void) ctx; /* Silence compiler warning. */\n") + c_file.append(" if (tree == NULL)\n") + c_file.append(" return NULL;\n") + c_file.append(" ret = calloc (1, sizeof (*ret));\n") + c_file.append(" if (ret == NULL)\n") + c_file.append(" return NULL;\n") if obj.typ == 'mapStringObject': parse_map_string_obj(obj, c_file, prefix, obj_typename) if obj.typ == 'object' or (obj.typ == 'array' and obj.subtypobj): parse_obj_arr_obj(obj, c_file, prefix, obj_typename) - c_file.write(' return move_ptr (ret);\n') - c_file.write("}\n\n") + c_file.append(' return move_ptr (ret);\n') + c_file.append("}\n\n") def get_map_string_obj(obj, c_file, prefix): @@ -425,35 +401,33 @@ def get_map_string_obj(obj, c_file, prefix): childname = child.subtypname else: childname = helpers.get_prefixed_name(child.name, prefix) - c_file.write(' size_t len = 0, i;\n') - c_file.write(" if (ptr != NULL)\n") - c_file.write(" len = ptr->len;\n") - c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.write(" stat = yajl_gen_map_open ((yajl_gen) g);\n") - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' if (len || (ptr != NULL && ptr->keys != NULL && ptr->%s != NULL))\n' \ - % child.fixname) - c_file.write(' {\n') - c_file.write(' for (i = 0; i < len; i++)\n') - c_file.write(' {\n') - c_file.write(' char *str = ptr->keys[i] ? ptr->keys[i] : "";\n') - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ + c_file.append(' size_t len = 0, i;\n') + c_file.append(" if (ptr != NULL)\n") + c_file.append(" len = ptr->len;\n") + c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') + c_file.append(" stat = yajl_gen_map_open ((yajl_gen) g);\n") + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f' if (len || (ptr != NULL && ptr->keys != NULL && ptr->{child.fixname} != NULL))\n') + c_file.append(' {\n') + c_file.append(' for (i = 0; i < len; i++)\n') + c_file.append(' {\n') + c_file.append(' char *str = ptr->keys[i] ? ptr->keys[i] : "";\n') + c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ (const unsigned char *)str, strlen (str));\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' stat = gen_%s (g, ptr->%s[i], ctx, err);\n' \ - % (childname, child.fixname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' }\n') - c_file.write(' }\n') - c_file.write(" stat = yajl_gen_map_close ((yajl_gen) g);\n") - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f' stat = gen_{childname} (g, ptr->{child.fixname}[i], ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' }\n') + c_file.append(' }\n') + c_file.append(" stat = yajl_gen_map_close ((yajl_gen) g);\n") + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') def get_obj_arr_obj_array(obj, c_file, prefix): if obj.subtypobj or obj.subtyp == 'object': @@ -462,129 +436,120 @@ def get_obj_arr_obj_array(obj, c_file, prefix): typename = obj.subtypname else: typename = helpers.get_name_substr(obj.name, prefix) - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) || ' \ - '(ptr != NULL && ptr->%s != NULL))\n' % obj.fixname) - c_file.write(' {\n') - c_file.write(' size_t len = 0, i;\n') - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (ptr != NULL && ptr->%s != NULL)\n" % obj.fixname) - c_file.write(" len = ptr->%s_len;\n" % obj.fixname) - c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' for (i = 0; i < len; i++)\n') - c_file.write(' {\n') + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(' {\n') + c_file.append(' size_t len = 0, i;\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {int(l)} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") + c_file.append(f" len = ptr->{obj.fixname}_len;\n") + c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' for (i = 0; i < len; i++)\n') + c_file.append(' {\n') if obj.doublearray: - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" size_t j;\n") - c_file.write(' for (j = 0; j < ptr->%s_item_lens[i]; j++)\n' %(obj.fixname)) - c_file.write(' {\n') - c_file.write(' stat = gen_%s (g, ptr->%s[i][j], ctx, err);\n' \ - % (typename, obj.fixname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' }\n') - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" size_t j;\n") + c_file.append(f' for (j = 0; j < ptr->{obj.fixname}_item_lens[i]; j++)\n') + c_file.append(' {\n') + c_file.append(f' stat = gen_{typename} (g, ptr->{obj.fixname}[i][j], ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' }\n') + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: - c_file.write(' stat = gen_%s (g, ptr->%s[i], ctx, err);\n' \ - % (typename, obj.fixname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' }\n') - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') - c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' }\n') + c_file.append(f' stat = gen_{typename} (g, ptr->{obj.fixname}[i], ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' }\n') + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' }\n') elif obj.subtyp == 'byte': l = len(obj.origname) - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) ||' \ - ' (ptr != NULL && ptr->%s != NULL && ptr->%s_len))\n' \ - % (obj.fixname, obj.fixname)) - c_file.write(' {\n') - c_file.write(' const char *str = "";\n') - c_file.write(' size_t len = 0;\n') - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL && ptr->{obj.fixname}_len))\n') + c_file.append(' {\n') + c_file.append(' const char *str = "";\n') + c_file.append(' size_t len = 0;\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") if obj.doublearray: - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" {\n") - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->%s_len; i++)\n" % (obj.fixname)) - c_file.write(" {\n") - c_file.write(" if (ptr->%s[i] != NULL)\n" % obj.fixname) - c_file.write(" str = (const char *)ptr->%s[i];\n" % obj.fixname) - c_file.write(" else ()\n") - c_file.write(" str = "";\n") - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" {\n") + c_file.append(" size_t i;\n") + c_file.append(f" for (i = 0; i < ptr->{obj.fixname}_len; i++)\n") + c_file.append(" {\n") + c_file.append(f" if (ptr->{obj.fixname}[i] != NULL)\n") + c_file.append(f" str = (const char *)ptr->{obj.fixname}[i];\n") + c_file.append(" else ()\n") + c_file.append(" str = "";\n") + c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ (const unsigned char *)str, strlen(str));\n') - c_file.write(" }\n") - c_file.write(" }\n") - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(" }\n") + c_file.append(" }\n") + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: - c_file.write(" if (ptr != NULL && ptr->%s != NULL)\n" % obj.fixname) - c_file.write(" {\n") - c_file.write(" str = (const char *)ptr->%s;\n" % obj.fixname) - c_file.write(" len = ptr->%s_len;\n" % obj.fixname) - c_file.write(" }\n") - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") + c_file.append(" {\n") + c_file.append(f" str = (const char *)ptr->{obj.fixname};\n") + c_file.append(f" len = ptr->{obj.fixname}_len;\n") + c_file.append(" }\n") + c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ (const unsigned char *)str, len);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" }\n") + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" }\n") else: l = len(obj.origname) - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) || ' \ - '(ptr != NULL && ptr->%s != NULL))\n' % obj.fixname) - c_file.write(' {\n') - c_file.write(' size_t len = 0, i;\n') - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (ptr != NULL && ptr->%s != NULL)\n" % obj.fixname) - c_file.write(" len = ptr->%s_len;\n" % obj.fixname) - c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' for (i = 0; i < len; i++)\n') - c_file.write(' {\n') + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(' {\n') + c_file.append(' size_t len = 0, i;\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") + c_file.append(f" len = ptr->{obj.fixname}_len;\n") + c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' for (i = 0; i < len; i++)\n') + c_file.append(' {\n') if obj.doublearray: typename = helpers.get_map_c_types(obj.subtyp) - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" size_t j;\n") - c_file.write(' for (j = 0; j < ptr->%s_item_lens[i]; j++)\n' % obj.fixname) - c_file.write(' {\n') - json_value_generator(c_file, 4, "ptr->%s[i][j]" % obj.fixname, 'g', 'ctx', obj.subtyp) - c_file.write(' }\n') - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" size_t j;\n") + c_file.append(f' for (j = 0; j < ptr->{obj.fixname}_item_lens[i]; j++)\n') + c_file.append(' {\n') + json_value_generator(c_file, 4, f"ptr->{obj.fixname}[i][j]", 'g', 'ctx', obj.subtyp) + c_file.append(' }\n') + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: - json_value_generator(c_file, 3, "ptr->%s[i]" % obj.fixname, 'g', 'ctx', obj.subtyp) + json_value_generator(c_file, 3, f"ptr->{obj.fixname}[i]", 'g', 'ctx', obj.subtyp) - c_file.write(' }\n') - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') - c_file.write(' }\n') + c_file.append(' }\n') + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') + c_file.append(' }\n') def get_obj_arr_obj(obj, c_file, prefix): """ @@ -594,22 +559,19 @@ def get_obj_arr_obj(obj, c_file, prefix): """ if obj.typ == 'string': l = len(obj.origname) - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) ||' \ - ' (ptr != NULL && ptr->%s != NULL))\n' % obj.fixname) - c_file.write(' {\n') - c_file.write(' char *str = "";\n') - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (ptr != NULL && ptr->%s != NULL)\n" % obj.fixname) - c_file.write(" str = ptr->%s;\n" % obj.fixname) + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n' ) + c_file.append(' {\n') + c_file.append(' char *str = "";\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") + c_file.append(f" str = ptr->{obj.fixname};\n") json_value_generator(c_file, 2, "str", 'g', 'ctx', obj.typ) - c_file.write(" }\n") + c_file.append(" }\n") elif helpers.judge_data_type(obj.typ): - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) ||' \ - ' (ptr != NULL && ptr->%s_present))\n' % obj.fixname) - c_file.write(' {\n') + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname}_present))\n') + c_file.append(' {\n') if obj.typ == 'double': numtyp = 'double' elif obj.typ.startswith("uint") or obj.typ == 'GID' or obj.typ == 'UID': @@ -617,83 +579,73 @@ def get_obj_arr_obj(obj, c_file, prefix): else: numtyp = 'long long int' l = len(obj.origname) - c_file.write(' %s num = 0;\n' % numtyp) - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (ptr != NULL && ptr->%s)\n" % obj.fixname) - c_file.write(" num = (%s)ptr->%s;\n" % (numtyp, obj.fixname)) + c_file.append(f' {numtyp} num = 0;\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname})\n") + c_file.append(f" num = ({numtyp})ptr->{obj.fixname};\n") json_value_generator(c_file, 2, "num", 'g', 'ctx', obj.typ) - c_file.write(" }\n") + c_file.append(" }\n") elif helpers.judge_data_pointer_type(obj.typ): - c_file.write(' if ((ptr != NULL && ptr->%s != NULL))\n' % obj.fixname) - c_file.write(' {\n') + c_file.append(f' if ((ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(' {\n') numtyp = helpers.obtain_data_pointer_type(obj.typ) if numtyp == "": return l = len(obj.origname) - c_file.write(' %s num = 0;\n' % helpers.get_map_c_types(numtyp)) - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (ptr != NULL && ptr->%s != NULL)\n" % obj.fixname) - c_file.write(" {\n") - c_file.write(" num = (%s)*(ptr->%s);\n" \ - % (helpers.get_map_c_types(numtyp), obj.fixname)) - c_file.write(" }\n") + c_file.append(f' {helpers.get_map_c_types(numtyp)} num = 0;\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, \ +(const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") + c_file.append(" {\n") + c_file.append(f" num = ({helpers.get_map_c_types(numtyp)})*(ptr->{obj.fixname});\n") + c_file.append(" }\n") json_value_generator(c_file, 2, "num", 'g', 'ctx', numtyp) - c_file.write(" }\n") + c_file.append(" }\n") elif obj.typ == 'boolean': - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) ||' \ - ' (ptr != NULL && ptr->%s_present))\n' % obj.fixname) - c_file.write(' {\n') - c_file.write(' bool b = false;\n') + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname}_present))\n') + c_file.append(' {\n') + c_file.append(' bool b = false;\n') l = len(obj.origname) - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" if (ptr != NULL && ptr->%s)\n" % obj.fixname) - c_file.write(" b = ptr->%s;\n" % obj.fixname) - c_file.write(" \n") + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname})\n") + c_file.append(f" b = ptr->{obj.fixname};\n") + c_file.append(" \n") json_value_generator(c_file, 2, "b", 'g', 'ctx', obj.typ) - c_file.write(" }\n") + c_file.append(" }\n") elif obj.typ == 'object' or obj.typ == 'mapStringObject': l = len(obj.origname) if obj.subtypname: typename = obj.subtypname else: typename = helpers.get_prefixed_name(obj.name, prefix) - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) ||' \ - ' (ptr != NULL && ptr->%s != NULL))\n' % obj.fixname) - c_file.write(" {\n") - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' stat = gen_%s (g, ptr != NULL ? ptr->%s : NULL, ctx, err);\n' \ - % (typename, obj.fixname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" }\n") + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(" {\n") + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f' stat = gen_{typename} (g, ptr != NULL ? ptr->{obj.fixname} : NULL, ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" }\n") elif obj.typ == 'array': get_obj_arr_obj_array(obj, c_file, prefix) elif helpers.valid_basic_map_name(obj.typ): l = len(obj.origname) - c_file.write(' if ((ctx->options & OPT_GEN_KEY_VALUE) || ' \ - '(ptr != NULL && ptr->%s != NULL))\n' % obj.fixname) - c_file.write(' {\n') - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("%s"), %d /* strlen ("%s") */);\n' % (obj.origname, l, obj.origname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' stat = gen_%s (g, ptr ? ptr->%s : NULL, ctx, err);\n' \ - % (helpers.make_basic_map_name(obj.typ), obj.fixname)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" }\n") + c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(' {\n') + c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.fixname}"), {l} /* strlen ("{obj.fixname}") */);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f' stat = gen_{helpers.make_basic_map_name(obj.typ)} (g, ptr ? ptr->{obj.fixname} : NULL, ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" }\n") def get_c_json(obj, c_file, prefix): """ @@ -710,42 +662,42 @@ def get_c_json(obj, c_file, prefix): objs = obj.subtypobj if objs is None: return - c_file.write( - "yajl_gen_status\ngen_%s (yajl_gen g, const %s *ptr, const struct parser_context " \ - "*ctx, parser_error *err)\n" % (typename, typename)) - c_file.write("{\n") - c_file.write(" yajl_gen_status stat = yajl_gen_status_ok;\n") - c_file.write(" *err = NULL;\n") - c_file.write(" (void) ptr; /* Silence compiler warning. */\n") + c_file.append( + f"yajl_gen_status\ngen_{typename} (yajl_gen g, const {typename} *ptr, const struct parser_context " \ + "*ctx, parser_error *err)\n") + c_file.append("{\n") + c_file.append(" yajl_gen_status stat = yajl_gen_status_ok;\n") + c_file.append(" *err = NULL;\n") + c_file.append(" (void) ptr; /* Silence compiler warning. */\n") if obj.typ == 'mapStringObject': get_map_string_obj(obj, c_file, prefix) elif obj.typ == 'object' or (obj.typ == 'array' and obj.subtypobj): nodes = obj.children if obj.typ == 'object' else obj.subtypobj if nodes is None: - c_file.write(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') + c_file.append(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.write(" stat = yajl_gen_map_open ((yajl_gen) g);\n") - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" stat = yajl_gen_map_open ((yajl_gen) g);\n") + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") for i in nodes or []: get_obj_arr_obj(i, c_file, prefix) if obj.typ == 'object': if obj.children is not None: - c_file.write(" if (ptr != NULL && ptr->_residual != NULL)\n") - c_file.write(" {\n") - c_file.write(" stat = gen_yajl_object_residual (ptr->_residual, g, err);\n") - c_file.write(" if (yajl_gen_status_ok != stat)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" }\n") - c_file.write(" stat = yajl_gen_map_close ((yajl_gen) g);\n") - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" if (ptr != NULL && ptr->_residual != NULL)\n") + c_file.append(" {\n") + c_file.append(" stat = gen_yajl_object_residual (ptr->_residual, g, err);\n") + c_file.append(" if (yajl_gen_status_ok != stat)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" }\n") + c_file.append(" stat = yajl_gen_map_close ((yajl_gen) g);\n") + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") if nodes is None: - c_file.write(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') - c_file.write(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') - c_file.write(' return yajl_gen_status_ok;\n') - c_file.write("}\n\n") + c_file.append(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') + c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') + c_file.append(' return yajl_gen_status_ok;\n') + c_file.append("}\n\n") def read_val_generator(c_file, level, src, dest, typ, keyname, obj_typename): @@ -755,144 +707,129 @@ def read_val_generator(c_file, level, src, dest, typ, keyname, obj_typename): History: 2019-06-17 """ if helpers.valid_basic_map_name(typ): - c_file.write('%syajl_val val = %s;\n' % (' ' * level, src)) - c_file.write('%sif (val != NULL)\n' % (' ' * level)) - c_file.write('%s {\n' % (' ' * level)) - c_file.write('%s%s = make_%s (val, ctx, err);\n' \ - % (' ' * (level + 1), dest, helpers.make_basic_map_name(typ))) - c_file.write('%sif (%s == NULL)\n' % (' ' * (level + 1), dest)) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s char *new_error = NULL;\n' % (' ' * (level + 1))) - c_file.write("%s if (asprintf (&new_error, \"Value error for key" \ - " '%s': %%s\", *err ? *err : \"null\") < 0)\n" \ - % (' ' * (level + 1), keyname)) - c_file.write('%s new_error = strdup ("error allocating memory");\n' \ - % (' ' * (level + 1))) - c_file.write('%s free (*err);\n' % (' ' * (level + 1))) - c_file.write('%s *err = new_error;\n' % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s }\n' % (' ' * (level + 1))) - c_file.write('%s}\n' % (' ' * (level))) + c_file.append(f"{' ' * level}yajl_val val = {src};\n") + c_file.append(f"{' ' * level}if (val != NULL)\n") + c_file.append(f'{" " * level} {{\n') + c_file.append(f'{" " * (level + 1)}{dest} = make_{helpers.make_basic_map_name(typ)} (val, ctx, err);\n') + c_file.append(f"{' ' * (level + 1)}if ({dest} == NULL)\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f"{' ' * (level + 1)} char *new_error = NULL;\n") + c_file.append(f"{' ' * (level + 1)} if (asprintf (&new_error, \"Value error for key '{keyname}': %s\", *err ? *err : \"null\") < 0)\n") + c_file.append(f'{" " * (level + 1)} new_error = strdup ("error allocating memory");\n') + c_file.append(f"{' ' * (level + 1)} free (*err);\n") + c_file.append(f"{' ' * (level + 1)} *err = new_error;\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)} }}\n') + c_file.append(f'{" " * (level)}}}\n') elif typ == 'string': - c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) - c_file.write('%sif (val != NULL)\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) - c_file.write('%schar *str = YAJL_GET_STRING (val);\n' % (' ' * (level + 1))) - c_file.write('%s%s = strdup (str ? str : "");\n' % (' ' * (level + 1), dest)) - c_file.write('%sif (%s == NULL)\n' % (' ' * (level + 1), dest)) - c_file.write('%s return NULL;\n' % (' ' * (level+1))) - c_file.write('%s }\n' % (' ' * level)) + c_file.append(f"{' ' * level}yajl_val val = {src};\n") + c_file.append(f"{' ' * level}if (val != NULL)\n") + c_file.append(f"{' ' * (level)} {{\n") + c_file.append(f"{' ' * (level + 1)}char *str = YAJL_GET_STRING (val);\n") + c_file.append(f"{' ' * (level + 1)}{dest} = strdup (str ? str : \"\");\n") + c_file.append(f"{' ' * (level + 1)}if ({dest} == NULL)\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * level} }}\n') elif helpers.judge_data_type(typ): - c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) - c_file.write('%sif (val != NULL)\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) + c_file.append(f"{' ' * level}yajl_val val = {src};\n") + c_file.append(f"{' ' * level}if (val != NULL)\n") + c_file.append(f'{" " * (level)} {{\n') if typ.startswith("uint") or \ (typ.startswith("int") and typ != "integer") or typ == "double": - c_file.write('%sint invalid;\n' % (' ' * (level + 1))) - c_file.write('%sif (! YAJL_IS_NUMBER (val))\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s *err = strdup ("invalid type");\n' % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s }\n' % (' ' * (level + 1))) - c_file.write('%sinvalid = common_safe_%s (YAJL_GET_NUMBER (val), &%s);\n' \ - % (' ' * (level + 1), typ, dest)) + c_file.append(f"{' ' * (level + 1)}int invalid;\n") + c_file.append(f"{' ' * (level + 1)}if (! YAJL_IS_NUMBER (val))\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f"{' ' * (level + 1)} *err = strdup (\"invalid type\");\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)} }}\n') + c_file.append(f'{" " * (level + 1)}invalid = common_safe_{typ} (YAJL_GET_NUMBER (val), &{dest});\n') elif typ == "integer": - c_file.write('%sint invalid;\n' % (' ' * (level + 1))) - c_file.write('%sif (! YAJL_IS_NUMBER (val))\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s *err = strdup ("invalid type");\n' % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s }\n' % (' ' * (level + 1))) - c_file.write('%sinvalid = common_safe_int (YAJL_GET_NUMBER (val), (int *)&%s);\n' \ - % (' ' * (level + 1), dest)) + c_file.append(f"{' ' * (level + 1)}int invalid;\n") + c_file.append(f"{' ' * (level + 1)}if (! YAJL_IS_NUMBER (val))\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f"{' ' * (level + 1)} *err = strdup (\"invalid type\");\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)} }}\n') + c_file.append(f'{" " * (level + 1)}invalid = common_safe_int (YAJL_GET_NUMBER (val), (int *)&{dest});\n') elif typ == "UID" or typ == "GID": - c_file.write('%sint invalid;\n' % (' ' * (level + 1))) - c_file.write('%sif (! YAJL_IS_NUMBER (val))\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s *err = strdup ("invalid type");\n' % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s }\n' % (' ' * (level + 1))) - c_file.write('%sinvalid = common_safe_uint (YAJL_GET_NUMBER (val),' \ - ' (unsigned int *)&%s);\n' % (' ' * (level + 1), dest)) - c_file.write('%sif (invalid)\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s if (asprintf (err, "Invalid value \'%%s\' with type \'%s\' ' - 'for key \'%s\': %%s", YAJL_GET_NUMBER (val), strerror (-invalid)) < 0)\n' \ - % (' ' * (level + 1), typ, keyname)) - c_file.write('%s *err = strdup ("error allocating memory");\n' \ - % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s}\n' % (' ' * (level + 1))) + c_file.append(f"{' ' * (level + 1)}int invalid;\n") + c_file.append(f"{' ' * (level + 1)}if (! YAJL_IS_NUMBER (val))\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f"{' ' * (level + 1)} *err = strdup (\"invalid type\");\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)} }}\n') + c_file.append(f'{" " * (level + 1)}invalid = common_safe_uint (YAJL_GET_NUMBER (val), (unsigned int *)&{dest});\n') + c_file.append(f"{' ' * (level + 1)}if (invalid)\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f'{" " * (level + 1)} if (asprintf (err, "Invalid value \'%s\' with type \'{typ}\' for key \'{keyname}\': %s", YAJL_GET_NUMBER (val), strerror (-invalid)) < 0)\n') + c_file.append(f'{" " * (level + 1)} *err = strdup ("error allocating memory");\n') + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)}}}\n') if '[' not in dest: - c_file.write('%s%s_present = 1;\n' % (' ' * (level + 1), dest)) - c_file.write('%s}\n' % (' ' * (level))) + c_file.append(f"{' ' * (level + 1)}{dest}_present = 1;\n") + c_file.append(f'{" " * (level)}}}\n') elif helpers.judge_data_pointer_type(typ): num_type = helpers.obtain_data_pointer_type(typ) if num_type == "": return - c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) - c_file.write('%sif (val != NULL)\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) - c_file.write('%s%s = calloc (1, sizeof (%s));\n' % - (' ' * (level + 1), dest, helpers.get_map_c_types(num_type))) - c_file.write('%sif (%s == NULL)\n' % (' ' * (level + 1), dest)) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%sint invalid;\n' % (' ' * (level + 1))) - c_file.write('%sif (! YAJL_IS_NUMBER (val))\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s *err = strdup ("invalid type");\n' % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s}\n' % (' ' * (level + 1))) - c_file.write('%sinvalid = common_safe_%s (YAJL_GET_NUMBER (val), %s);\n' \ - % (' ' * (level + 1), num_type, dest)) - c_file.write('%sif (invalid)\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s if (asprintf (err, "Invalid value \'%%s\' with type \'%s\' ' \ - 'for key \'%s\': %%s", YAJL_GET_NUMBER (val), strerror (-invalid)) < 0)\n' \ - % (' ' * (level + 1), typ, keyname)) - c_file.write('%s *err = strdup ("error allocating memory");\n' \ - % (' ' * (level + 1))) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s}\n' % (' ' * (level + 1))) - c_file.write('%s}\n' % (' ' * (level))) + c_file.append(f"{' ' * level}yajl_val val = {src};\n") + c_file.append(f"{' ' * level}if (val != NULL)\n") + c_file.append(f'{" " * (level)} {{\n') + c_file.append(f'{" " * (level + 1)}{dest} = calloc (1, sizeof ({helpers.get_map_c_types(num_type)}));\n') + c_file.append(f"{' ' * (level + 1)}if ({dest} == NULL)\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f"{' ' * (level + 1)}int invalid;\n") + c_file.append(f"{' ' * (level + 1)}if (! YAJL_IS_NUMBER (val))\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f"{' ' * (level + 1)} *err = strdup (\"invalid type\");\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)}}}\n') + c_file.append(f'{" " * (level + 1)}sinvalid = common_safe_{num_type} (YAJL_GET_NUMBER (val), {dest});\n') + c_file.append(f"{' ' * (level + 1)}if (invalid)\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f'{" " * (level + 1)} if (asprintf (err, "Invalid value \'%s\' with type \'{typ}\' ' \ + f'for key \'{keyname}\': %s", YAJL_GET_NUMBER (val), strerror (-invalid)) < 0)\n') + c_file.append(f'{" " * (level + 1)} *err = strdup ("error allocating memory");\n') + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f'{" " * (level + 1)}}}\n') + c_file.append(f'{" " * (level)}}}\n') elif typ == 'boolean': - c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) - c_file.write('%sif (val != NULL)\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) - c_file.write('%s%s = YAJL_IS_TRUE(val);\n' % (' ' * (level + 1), dest)) + c_file.append(f"{' ' * level}yajl_val val = {src};\n") + c_file.append(f"{' ' * level}if (val != NULL)\n") + c_file.append(f'{" " * (level)} {{\n') + c_file.append(f"{' ' * (level + 1)}{dest} = YAJL_IS_TRUE(val);\n") if '[' not in dest: - c_file.write('%s%s_present = 1;\n' % (' ' * (level + 1), dest)) - c_file.write('%s }\n' % (' ' * (level))) - c_file.write('%selse\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) - c_file.write('%sval = %s;\n' % (' ' * (level+1), src.replace('yajl_t_true', 'yajl_t_false'))) - c_file.write('%sif (val != NULL)\n' % (' ' * (level+1))) - c_file.write('%s {\n' % (' ' * (level+1))) - c_file.write('%s%s = 0;\n' % (' ' * (level + 2), dest)) - c_file.write('%s%s_present = 1;\n' % (' ' * (level + 2), dest)) - c_file.write('%s }\n' % (' ' * (level+1))) - c_file.write('%s }\n' % (' ' * (level))) + c_file.append(f"{' ' * (level + 1)}{dest}_present = 1;\n") + c_file.append(f'{" " * (level)} }}\n') + c_file.append(f"{' ' * level}else\n") + c_file.append(f'{" " * (level)} {{\n') + c_file.append(f"{' ' * (level + 1)}val = {src.replace('yajl_t_true', 'yajl_t_false')};\n") + c_file.append(f"{' ' * (level + 1)}if (val != NULL)\n") + c_file.append(f'{" " * (level+1)} {{\n') + c_file.append(f"{' ' * (level + 2)}{dest} = 0;\n") + c_file.append(f"{' ' * (level + 2)}{dest}_present = 1;\n") + c_file.append(f'{" " * (level+1)} }}\n') + c_file.append(f'{" " * (level)} }}\n') elif typ == 'booleanPointer': - c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src)) - c_file.write('%sif (val != NULL)\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) - c_file.write('%s%s = calloc (1, sizeof (bool));\n' % (' ' * (level + 1), dest)) - c_file.write('%sif (%s == NULL)\n' % (' ' * (level + 1), dest)) - c_file.write('%s return NULL;\n' % (' ' * (level + 1))) - c_file.write('%s*(%s) = YAJL_IS_TRUE(val);\n' % (' ' * (level + 1), dest)) - c_file.write('%s }\n' % (' ' * (level))) - c_file.write('%selse\n' % (' ' * (level))) - c_file.write('%s {\n' % (' ' * (level))) - c_file.write('%sval = get_val (tree, "%s", yajl_t_false);\n' \ - % (' ' * (level + 1), keyname)) - c_file.write('%sif (val != NULL)\n' % (' ' * (level + 1))) - c_file.write('%s {\n' % (' ' * (level + 1))) - c_file.write('%s%s = calloc (1, sizeof (bool));\n' % (' ' * (level + 2), dest)) - c_file.write('%sif (%s == NULL)\n' % (' ' * (level + 2), dest)) - c_file.write('%s return NULL;\n' % (' ' * (level + 2))) - c_file.write('%s*(%s) = YAJL_IS_TRUE(val);\n' % (' ' * (level + 2), dest)) - c_file.write('%s}\n' % (' ' * (level + 1))) - c_file.write('%s}\n' % (' ' * (level))) + c_file.append(f"{' ' * level}yajl_val val = {src};\n") + c_file.append(f"{' ' * level}if (val != NULL)\n") + c_file.append(f'{" " * (level)} {{\n') + c_file.append(f"{' ' * (level + 1)}{dest} = calloc (1, sizeof (bool));\n") + c_file.append(f"{' ' * (level + 1)}if ({dest} == NULL)\n") + c_file.append(f"{' ' * (level + 1)} return NULL;\n") + c_file.append(f"{' ' * (level + 1)}*({dest}) = YAJL_IS_TRUE(val);\n") + c_file.append(f'{" " * (level)} }}\n') + c_file.append(f"{' ' * level}else\n") + c_file.append(f'{" " * (level)} {{\n') + c_file.append(f'{" " * (level + 1)}val = get_val (tree, "{keyname}", yajl_t_false);\n') + c_file.append(f"{' ' * (level + 1)}if (val != NULL)\n") + c_file.append(f'{" " * (level + 1)} {{\n') + c_file.append(f"{' ' * (level + 2)}{dest} = calloc (1, sizeof (bool));\n") + c_file.append(f"{' ' * (level + 2)}if ({dest} == NULL)\n") + c_file.append(f"{' ' * (level + 2)} return NULL;\n") + c_file.append(f"{' ' * (level + 2)}*({dest}) = YAJL_IS_TRUE(val);\n") + c_file.append(f'{" " * (level + 1)}}}\n') + c_file.append(f'{" " * (level)}}}\n') def json_value_generator(c_file, level, src, dst, ptx, typ): @@ -902,105 +839,100 @@ def json_value_generator(c_file, level, src, dst, ptx, typ): History: 2019-06-17 """ if helpers.valid_basic_map_name(typ): - c_file.write('%sstat = gen_%s (%s, %s, %s, err);\n' \ - % (' ' * (level), helpers.make_basic_map_name(typ), dst, src, ptx)) - c_file.write("%sif (stat != yajl_gen_status_ok)\n" % (' ' * (level))) - c_file.write("%sGEN_SET_ERROR_AND_RETURN (stat, err);\n" % (' ' * (level + 1))) + c_file.append(f'{" " * (level)}stat = gen_{helpers.make_basic_map_name(typ)} ({dst}, {src}, {ptx}, err);\n') + c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") elif typ == 'string': - c_file.write('%sstat = yajl_gen_string ((yajl_gen)%s, \ -(const unsigned char *)(%s), strlen (%s));\n' % (' ' * (level), dst, src, src)) - c_file.write("%sif (stat != yajl_gen_status_ok)\n" % (' ' * (level))) - c_file.write("%sGEN_SET_ERROR_AND_RETURN (stat, err);\n" % (' ' * (level + 1))) + c_file.append(f'{" " * (level)}stat = yajl_gen_string ((yajl_gen){dst}, (const unsigned char *)({src}), strlen ({src}));\n') + c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") elif helpers.judge_data_type(typ): if typ == 'double': - c_file.write('%sstat = yajl_gen_double ((yajl_gen)%s, %s);\n' \ - % (' ' * (level), dst, src)) + c_file.append(f'{" " * (level)}stat = yajl_gen_double ((yajl_gen){dst}, {src});\n') elif typ.startswith("uint") or typ == 'GID' or typ == 'UID': - c_file.write('%sstat = map_uint (%s, %s);\n' % (' ' * (level), dst, src)) + c_file.append(f"{' ' * level}stat = map_uint ({dst}, {src});\n") else: - c_file.write('%sstat = map_int (%s, %s);\n' % (' ' * (level), dst, src)) - c_file.write("%sif (stat != yajl_gen_status_ok)\n" % (' ' * (level))) - c_file.write("%sGEN_SET_ERROR_AND_RETURN (stat, err);\n" \ - % (' ' * (level + 1))) + c_file.append(f"{' ' * level}stat = map_int ({dst}, {src});\n") + c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") elif typ == 'boolean': - c_file.write('%sstat = yajl_gen_bool ((yajl_gen)%s, (int)(%s));\n' \ - % (' ' * (level), dst, src)) - c_file.write("%sif (stat != yajl_gen_status_ok)\n" % (' ' * (level))) - c_file.write("%sGEN_SET_ERROR_AND_RETURN (stat, err);\n" % (' ' * (level + 1))) + c_file.append(f'{" " * (level)}stat = yajl_gen_bool ((yajl_gen){dst}, (int)({src}));\n') + c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") def make_c_array_free (i, c_file, prefix): if helpers.valid_basic_map_name(i.subtyp): free_func = helpers.make_basic_map_name(i.subtyp) - c_file.write(" if (ptr->%s != NULL)\n" % i.fixname) - c_file.write(" {\n" % i.fixname) - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->%s_len; i++)\n" % i.fixname) - c_file.write(" {\n" % i.fixname) - c_file.write(" if (ptr->%s[i] != NULL)\n" % (i.fixname)) - c_file.write(" {\n") - c_file.write(" free_%s (ptr->%s[i]);\n" % (free_func, i.fixname)) - c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) - c_file.write(" }\n") - c_file.write(" }\n") - c_file.write(" free (ptr->%s);\n" % (i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) - c_file.write(" }\n") + c_file.append(f" if (ptr->{i.fixname} != NULL)\n") + c_file.append(" {\n") + c_file.append(" size_t i;\n") + c_file.append(f" for (i = 0; i < ptr->{i.fixname}_len; i++)\n") + c_file.append(" {\n") + c_file.append(f" if (ptr->{i.fixname}[i] != NULL)\n") + c_file.append(" {\n") + c_file.append(f" free_{free_func} (ptr->{i.fixname}[i]);\n") + c_file.append(f" ptr->{i.fixname}[i] = NULL;\n") + c_file.append(" }\n") + c_file.append(" }\n") + c_file.append(f" free (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") + c_file.append(" }\n") elif i.subtyp == 'string': c_file_str(c_file, i) elif not helpers.judge_complex(i.subtyp): - c_file.write(" {\n") + c_file.append(" {\n") if i.doublearray: - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->%s_len; i++)\n" % i.fixname) - c_file.write(" {\n") - c_file.write(" free (ptr->%s[i]);\n" % (i.fixname)) - c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) - c_file.write(" }\n") - c_file.write(" free (ptr->%s_item_lens);\n" % (i.fixname)) - c_file.write(" ptr->%s_item_lens = NULL;\n" % (i.fixname)) - c_file.write(" free (ptr->%s);\n" % (i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) - c_file.write(" }\n") + c_file.append(" size_t i;\n") + c_file.append(f" for (i = 0; i < ptr->{i.fixname}_len; i++)\n") + c_file.append(" {\n") + c_file.append(f" free (ptr->{i.fixname}[i]);\n") + c_file.append(f" ptr->{i.fixname}[i] = NULL;\n") + c_file.append(" }\n") + c_file.append(f" free (ptr->{i.fixname}_item_lens);\n") + c_file.append(f" ptr->{i.fixname}_item_lens = NULL;\n") + c_file.append(f" free (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") + c_file.append(" }\n") elif i.subtyp == 'object' or i.subtypobj is not None: if i.subtypname is not None: free_func = i.subtypname else: free_func = helpers.get_name_substr(i.name, prefix) - c_file.write(" if (ptr->%s != NULL)\n" % i.fixname) - c_file.write(" {\n") - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->%s_len; i++)\n" % i.fixname) - c_file.write(" {\n") + c_file.append(f" if (ptr->{i.fixname} != NULL)") + c_file.append(" {\n") + c_file.append(" size_t i;\n") + c_file.append(f" for (i = 0; i < ptr->{i.fixname}_len; i++)\n") + c_file.append(" {\n") if i.doublearray: - c_file.write(" size_t j;\n"); - c_file.write(" for (j = 0; j < ptr->%s_item_lens[i]; j++)\n" % i.fixname); - c_file.write(" {\n"); - c_file.write(" free_%s (ptr->%s[i][j]);\n" % (free_func, i.fixname)) - c_file.write(" ptr->%s[i][j] = NULL;\n" % i.fixname); - c_file.write(" }\n"); - c_file.write(" free (ptr->%s[i]);\n" % i.fixname) - c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) + c_file.append(" size_t j;\n") + c_file.append(f" for (j = 0; j < ptr->{i.fixname}_item_lens[i]; j++)\n") + c_file.append(" {\n") + c_file.append(f" free_{free_func} (ptr->{i.fixname}[i][j]);\n") + c_file.append(f" ptr->{i.fixname}[i][j] = NULL;\n") + c_file.append(" }\n") + c_file.append(f" free (ptr->{i.fixname}[i]);\n") + c_file.append(f" ptr->{i.fixname}[i] = NULL;\n") else: - c_file.write(" if (ptr->%s[i] != NULL)\n" % (i.fixname)) - c_file.write(" {\n") - c_file.write(" free_%s (ptr->%s[i]);\n" % (free_func, i.fixname)) - c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) - c_file.write(" }\n") - c_file.write(" }\n") + c_file.append(f" if (ptr->{i.fixname}[i] != NULL)\n") + c_file.append(" {\n") + c_file.append(f" free_{free_func} (ptr->{i.fixname}[i]);\n") + c_file.append(f" ptr->{i.fixname}[i] = NULL;\n") + c_file.append(" }\n") + c_file.append(" }\n") if i.doublearray: - c_file.write(" free (ptr->%s_item_lens);\n" % (i.fixname)) - c_file.write(" ptr->%s_item_lens = NULL;\n" % (i.fixname)) + c_file.append(f" free (ptr->{i.fixname}_item_lens);\n") + c_file.append(f" ptr->{i.fixname}_item_lens = NULL;\n") - c_file.write(" free (ptr->%s);\n" % i.fixname) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) - c_file.write(" }\n") + c_file.append(f" free (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") + c_file.append(" }\n") c_typ = helpers.obtain_pointer(i.name, i.subtypobj, prefix) if c_typ == "": return True if i.subobj is not None: c_typ = c_typ + "_element" - c_file.write(" free_%s (ptr->%s);\n" % (c_typ, i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.append(f" free_{c_typ} (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") return False def make_c_free (obj, c_file, prefix): @@ -1021,10 +953,10 @@ def make_c_free (obj, c_file, prefix): return else: typename = helpers.get_name_substr(obj.name, prefix) - c_file.write("void\nfree_%s (%s *ptr)\n" % (typename, typename)) - c_file.write("{\n") - c_file.write(" if (ptr == NULL)\n") - c_file.write(" return;\n") + c_file.append(f"void\nfree_{typename} ({typename} *ptr)\n") + c_file.append("{\n") + c_file.append(" if (ptr == NULL)\n") + c_file.append(" return;\n") if obj.typ == 'mapStringObject': child = obj.children[0] if helpers.valid_basic_map_name(child.typ): @@ -1038,15 +970,15 @@ def make_c_free (obj, c_file, prefix): for i in objs or []: if helpers.valid_basic_map_name(i.typ): free_func = helpers.make_basic_map_name(i.typ) - c_file.write(" free_%s (ptr->%s);\n" % (free_func, i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.append(f" free_{free_func} (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") if i.typ == 'mapStringObject': if i.subtypname: free_func = i.subtypname else: free_func = helpers.get_prefixed_name(i.name, prefix) - c_file.write(" free_%s (ptr->%s);\n" % (free_func, i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.append(f" free_{free_func} (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") elif i.typ == 'array': if make_c_array_free (i, c_file, prefix): continue @@ -1054,22 +986,22 @@ def make_c_free (obj, c_file, prefix): typename = helpers.get_prefixed_name(i.name, prefix) if i.typ == 'string' or i.typ == 'booleanPointer' or \ helpers.judge_data_pointer_type(i.typ): - c_file.write(" free (ptr->%s);\n" % (i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) + c_file.append(f" free (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") elif i.typ == 'object': if i.subtypname is not None: typename = i.subtypname - c_file.write(" if (ptr->%s != NULL)\n" % (i.fixname)) - c_file.write(" {\n") - c_file.write(" free_%s (ptr->%s);\n" % (typename, i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) - c_file.write(" }\n") + c_file.append(f" if (ptr->{i.fixname} != NULL)\n") + c_file.append(" {\n") + c_file.append(f" free_{typename} (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") + c_file.append(" }\n") if obj.typ == 'object': if obj.children is not None: - c_file.write(" yajl_tree_free (ptr->_residual);\n") - c_file.write(" ptr->_residual = NULL;\n") - c_file.write(" free (ptr);\n") - c_file.write("}\n\n") + c_file.append(" yajl_tree_free (ptr->_residual);\n") + c_file.append(" ptr->_residual = NULL;\n") + c_file.append(" free (ptr);\n") + c_file.append("}\n\n") def c_file_map_str(c_file, child, childname): @@ -1078,21 +1010,21 @@ def c_file_map_str(c_file, child, childname): Interface: None History: 2019-10-31 """ - c_file.write(" if (ptr->keys != NULL && ptr->%s != NULL)\n" % child.fixname) - c_file.write(" {\n") - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->len; i++)\n") - c_file.write(" {\n") - c_file.write(" free (ptr->keys[i]);\n") - c_file.write(" ptr->keys[i] = NULL;\n") - c_file.write(" free_%s (ptr->%s[i]);\n" % (childname, child.fixname)) - c_file.write(" ptr->%s[i] = NULL;\n" % (child.fixname)) - c_file.write(" }\n") - c_file.write(" free (ptr->keys);\n") - c_file.write(" ptr->keys = NULL;\n") - c_file.write(" free (ptr->%s);\n" % (child.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (child.fixname)) - c_file.write(" }\n") + c_file.append(f" if (ptr->keys != NULL && ptr->{child.fixname} != NULL)\n") + c_file.append(" {\n") + c_file.append(" size_t i;\n") + c_file.append(" for (i = 0; i < ptr->len; i++)\n") + c_file.append(" {\n") + c_file.append(" free (ptr->keys[i]);\n") + c_file.append(" ptr->keys[i] = NULL;\n") + c_file.append(f" free_{childname} (ptr->{child.fixname}[i]);\n") + c_file.append(f" ptr->{child.fixname}[i] = NULL;\n") + c_file.append(" }\n") + c_file.append(" free (ptr->keys);\n") + c_file.append(" ptr->keys = NULL;\n") + c_file.append(f" free (ptr->{child.fixname});\n") + c_file.append(f" ptr->{child.fixname} = NULL;\n") + c_file.append(" }\n") def c_file_str(c_file, i): """ @@ -1100,30 +1032,30 @@ def c_file_str(c_file, i): Interface: None History: 2019-10-31 """ - c_file.write(" if (ptr->%s != NULL)\n" % i.fixname) - c_file.write(" {\n") - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->%s_len; i++)\n" % i.fixname) - c_file.write(" {\n") + c_file.append(f" if (ptr->{i.fixname} != NULL)\n") + c_file.append(" {\n") + c_file.append(" size_t i;\n") + c_file.append(f" for (i = 0; i < ptr->{i.fixname}_len; i++)\n") + c_file.append(" {\n") if i.doublearray: - c_file.write(" size_t j;\n") - c_file.write(" for (j = 0; j < ptr->%s_item_lens[i]; j++)\n" % (i.fixname)) - c_file.write(" {\n") - c_file.write(" free (ptr->%s[i][j]);\n" % (i.fixname)) - c_file.write(" ptr->%s[i][j] = NULL;\n" % (i.fixname)) - c_file.write(" }\n") - c_file.write(" if (ptr->%s[i] != NULL)\n" % (i.fixname)) - c_file.write(" {\n") - c_file.write(" free (ptr->%s[i]);\n" % (i.fixname)) - c_file.write(" ptr->%s[i] = NULL;\n" % (i.fixname)) - c_file.write(" }\n") - c_file.write(" }\n") + c_file.append(" size_t j;\n") + c_file.append(f" for (j = 0; j < ptr->{i.fixname}_item_lens[i]; j++)\n") + c_file.append(" {\n") + c_file.append(f" free (ptr->{i.fixname}[i][j]);\n") + c_file.append(f" ptr->{i.fixname}[i][j] = NULL;\n") + c_file.append(" }\n") + c_file.append(f" if (ptr->{i.fixname}[i] != NULL)\n") + c_file.append(" {\n") + c_file.append(f" free (ptr->{i.fixname}[i]);\n") + c_file.append(f" ptr->{i.fixname}[i] = NULL;\n") + c_file.append(" }\n") + c_file.append(" }\n") if i.doublearray: - c_file.write(" free (ptr->%s_item_lens);\n" % (i.fixname)) - c_file.write(" ptr->%s_item_lens = NULL;\n" % (i.fixname)) - c_file.write(" free (ptr->%s);\n" % (i.fixname)) - c_file.write(" ptr->%s = NULL;\n" % (i.fixname)) - c_file.write(" }\n") + c_file.append(f" free (ptr->{i.fixname}_item_lens);\n") + c_file.append(f" ptr->{i.fixname}_item_lens = NULL;\n") + c_file.append(f" free (ptr->{i.fixname});\n") + c_file.append(f" ptr->{i.fixname} = NULL;\n") + c_file.append(" }\n") def src_reflect(structs, schema_info, c_file, root_typ): @@ -1132,16 +1064,15 @@ def src_reflect(structs, schema_info, c_file, root_typ): Interface: None History: 2019-06-17 """ - c_file.write("/* Generated from %s. Do not edit! */\n\n" \ - % (schema_info.name.basename)) - c_file.write("#ifndef _GNU_SOURCE\n") - c_file.write("#define _GNU_SOURCE\n") - c_file.write("#endif\n") - c_file.write('#include \n') - c_file.write('#include \n') - c_file.write('#include "ocispec/%s"\n\n' % schema_info.header.basename) - c_file.write('#define YAJL_GET_ARRAY_NO_CHECK(v) (&(v)->u.array)\n') - c_file.write('#define YAJL_GET_OBJECT_NO_CHECK(v) (&(v)->u.object)\n') + c_file.append(f"/* Generated from {schema_info.name.basename}. Do not edit! */\n\n") + c_file.append("#ifndef _GNU_SOURCE\n") + c_file.append("#define _GNU_SOURCE\n") + c_file.append("#endif\n") + c_file.append('#include \n') + c_file.append('#include \n') + c_file.append(f'#include "ocispec/{schema_info.header.basename}"\n\n') + c_file.append('#define YAJL_GET_ARRAY_NO_CHECK(v) (&(v)->u.array)\n') + c_file.append('#define YAJL_GET_OBJECT_NO_CHECK(v) (&(v)->u.object)\n') for i in structs: append_c_code(i, c_file, schema_info.prefix) @@ -1157,37 +1088,36 @@ def get_c_epilog_for_array_make_parse(c_file, prefix, typ, obj): return typename = helpers.get_top_array_type_name(obj.name, prefix) - c_file.write("""\n -define_cleaner_function (%s *, free_%s)\n -%s -*make_%s (yajl_val tree, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup(free_%s) %s *ptr = NULL; - size_t i, alen; - - (void) ctx; - - if (tree == NULL || err == NULL || YAJL_GET_ARRAY (tree) == NULL) - return NULL; - *err = NULL; - alen = YAJL_GET_ARRAY_NO_CHECK (tree)->len; - if (alen == 0) - return NULL; - ptr = calloc (1, sizeof (%s)); - if (ptr == NULL) - return NULL; - ptr->items = calloc (alen + 1, sizeof(*ptr->items)); - if (ptr->items == NULL) - return NULL; - ptr->len = alen; -""" % (typename, typename, typename, typename, typename, typename, typename)) + c_file.append(f"\ndefine_cleaner_function ({typename} *, free_{typename})\n" + + f"{typename}\n" + + f"*make_{typename} (yajl_val tree, const struct parser_context *ctx, parser_error *err)\n" + + "{\n" + + f" __auto_cleanup(free_{typename}) {typename} *ptr = NULL;\n" + + f" size_t i, alen;\n" + + f" "+ + f" (void) ctx;\n" + + f" "+ + f" if (tree == NULL || err == NULL || YAJL_GET_ARRAY (tree) == NULL)\n" + + f" return NULL;\n" + + f" *err = NULL;\n" + + f" alen = YAJL_GET_ARRAY_NO_CHECK (tree)->len;\n" + + f" if (alen == 0)\n" + + f" return NULL;\n" + + f" ptr = calloc (1, sizeof ({typename}));\n" + + f" if (ptr == NULL)\n" + + f" return NULL;\n" + + f" ptr->items = calloc (alen + 1, sizeof(*ptr->items));\n" + + f" if (ptr->items == NULL)\n" + + f" return NULL;\n" + + f" ptr->len = alen;\n" + ) if obj.doublearray: - c_file.write(' ptr->subitem_lens = calloc ( alen + 1, sizeof (size_t));\n') - c_file.write(' if (ptr->subitem_lens == NULL)\n') - c_file.write(' return NULL;') + c_file.append(' ptr->subitem_lens = calloc ( alen + 1, sizeof (size_t));\n') + c_file.append(' if (ptr->subitem_lens == NULL)\n') + c_file.append(' return NULL;') - c_file.write("""\n + c_file.append("""\n for (i = 0; i < alen; i++) { yajl_val work = YAJL_GET_ARRAY_NO_CHECK (tree)->values[i]; @@ -1200,52 +1130,50 @@ def get_c_epilog_for_array_make_parse(c_file, prefix, typ, obj): subtypename = helpers.get_name_substr(obj.name, prefix) if obj.doublearray: - c_file.write(' size_t j;\n') - c_file.write(' ptr->items[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(work)->len + 1, sizeof (**ptr->items));\n') - c_file.write(' if (ptr->items[i] == NULL)\n') - c_file.write(' return NULL;\n') - c_file.write(' yajl_val *tmps = YAJL_GET_ARRAY_NO_CHECK(work)->values;\n') - c_file.write(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(work)->len; j++)\n') - c_file.write(' {\n') - c_file.write(' ptr->items[i][j] = make_%s (tmps[j], ctx, err);\n' \ - % (subtypename)) - c_file.write(' if (ptr->items[i][j] == NULL)\n') - c_file.write(" return NULL;\n") - c_file.write(' ptr->subitem_lens[i] += 1;\n') - c_file.write(' }\n') + c_file.append(' size_t j;\n') + c_file.append(' ptr->items[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(work)->len + 1, sizeof (**ptr->items));\n') + c_file.append(' if (ptr->items[i] == NULL)\n') + c_file.append(' return NULL;\n') + c_file.append(' yajl_val *tmps = YAJL_GET_ARRAY_NO_CHECK(work)->values;\n') + c_file.append(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(work)->len; j++)\n') + c_file.append(' {\n') + c_file.append(f' ptr->items[i][j] = make_{subtypename} (tmps[j], 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') + c_file.append(' }\n') else: - c_file.write(' ptr->items[i] = make_%s (work, ctx, err);\n' \ - % (subtypename)) - c_file.write(' if (ptr->items[i] == NULL)\n') - c_file.write(" return NULL;\n") + c_file.append(f' ptr->items[i] = make_{subtypename} (work, ctx, err);\n') + c_file.append(' if (ptr->items[i] == NULL)\n') + c_file.append(" return NULL;\n") elif obj.subtyp == 'byte': if obj.doublearray: - c_file.write(' char *str = YAJL_GET_STRING (work);\n') - c_file.write(' ptr->items[j] = (uint8_t *)strdup (str ? str : "");\n') - c_file.write(' if (ptr->items[j] == NULL)\n') - c_file.write(" return NULL;\n") + c_file.append(' char *str = YAJL_GET_STRING (work);\n') + c_file.append(' ptr->items[j] = (uint8_t *)strdup (str ? str : "");\n') + c_file.append(' if (ptr->items[j] == NULL)\n') + c_file.append(" return NULL;\n") else: - c_file.write(' char *str = YAJL_GET_STRING (tree);\n') - c_file.write(' memcpy(ptr->items, str ? str : "", strlen(str ? str : ""));\n') - c_file.write(' break;\n') + c_file.append(' char *str = YAJL_GET_STRING (tree);\n') + c_file.append(' memcpy(ptr->items, str ? str : "", strlen(str ? str : ""));\n') + c_file.append(' break;\n') else: if obj.doublearray: - c_file.write(' ptr->items[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(work)->len + 1, sizeof (**ptr->items));\n') - c_file.write(' if (ptr->items[i] == NULL)\n') - c_file.write(' return NULL;\n') - c_file.write(' size_t j;\n') - c_file.write(' yajl_val *tmps = YAJL_GET_ARRAY_NO_CHECK(work)->values;\n') - c_file.write(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(work)->len; j++)\n') - c_file.write(' {\n') + c_file.append(' ptr->items[i] = calloc ( YAJL_GET_ARRAY_NO_CHECK(work)->len + 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(' yajl_val *tmps = YAJL_GET_ARRAY_NO_CHECK(work)->values;\n') + c_file.append(' for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(work)->len; j++)\n') + c_file.append(' {\n') read_val_generator(c_file, 3, 'tmps[j]', \ "ptr->items[i][j]", obj.subtyp, obj.origname, c_typ) - c_file.write(' ptr->subitem_lens[i] += 1;\n') - c_file.write(' }\n') + c_file.append(' ptr->subitem_lens[i] += 1;\n') + c_file.append(' }\n') else: read_val_generator(c_file, 2, 'work', \ "ptr->items[i]", obj.subtyp, obj.origname, c_typ) - c_file.write("""\n + c_file.append("""\n } return move_ptr(ptr); } @@ -1260,8 +1188,7 @@ def get_c_epilog_for_array_make_free(c_file, prefix, typ, obj): return typename = helpers.get_top_array_type_name(obj.name, prefix) - c_file.write("""\n -void free_%s (%s *ptr) + c_file.append(f"\n\nvoid free_{typename} ({typename} *ptr)" + """ { size_t i; @@ -1270,32 +1197,32 @@ def get_c_epilog_for_array_make_free(c_file, prefix, typ, obj): for (i = 0; i < ptr->len; i++) { -""" % (typename, typename)) +""") if helpers.valid_basic_map_name(obj.subtyp): free_func = helpers.make_basic_map_name(obj.subtyp) - c_file.write(" if (ptr->items[i] != NULL)\n") - c_file.write(" {\n") - c_file.write(" free_%s (ptr->items[i]);\n" % (free_func)) - c_file.write(" ptr->items[i] = NULL;\n") - c_file.write(" }\n") + c_file.append(" if (ptr->items[i] != NULL)\n") + c_file.append(" {\n") + c_file.append(f" free_{free_func} (ptr->items[i]);\n") + c_file.append(" ptr->items[i] = NULL;\n") + c_file.append(" }\n") elif obj.subtyp == 'string': if obj.doublearray: - c_file.write(" size_t j;\n") - c_file.write(" for (j = 0; j < ptr->subitem_lens[i]; j++)\n") - c_file.write(" {\n") - c_file.write(" free (ptr->items[i][j]);\n") - c_file.write(" ptr->items[i][j] = NULL;\n") - c_file.write(" }\n") - c_file.write(" free (ptr->items[i]);\n") - c_file.write(" ptr->items[i] = NULL;\n") + c_file.append(" size_t j;\n") + c_file.append(" for (j = 0; j < ptr->subitem_lens[i]; j++)\n") + c_file.append(" {\n") + c_file.append(" free (ptr->items[i][j]);\n") + c_file.append(" ptr->items[i][j] = NULL;\n") + c_file.append(" }\n") + c_file.append(" free (ptr->items[i]);\n") + c_file.append(" ptr->items[i] = NULL;\n") else: - c_file.write(" free (ptr->items[i]);\n") - c_file.write(" ptr->items[i] = NULL;\n") + c_file.append(" free (ptr->items[i]);\n") + c_file.append(" ptr->items[i] = NULL;\n") elif not helpers.judge_complex(obj.subtyp): if obj.doublearray: - c_file.write(" free (ptr->items[i]);\n") - c_file.write(" ptr->items[i] = NULL;\n") + c_file.append(" free (ptr->items[i]);\n") + c_file.append(" ptr->items[i] = NULL;\n") elif obj.subtyp == 'object' or obj.subtypobj is not None: if obj.subtypname is not None: free_func = obj.subtypname @@ -1303,39 +1230,39 @@ def get_c_epilog_for_array_make_free(c_file, prefix, typ, obj): free_func = helpers.get_name_substr(obj.name, prefix) if obj.doublearray: - c_file.write(" size_t j;\n"); - c_file.write(" for (j = 0; j < ptr->subitem_lens[i]; j++)\n"); - c_file.write(" {\n"); - c_file.write(" free_%s (ptr->items[i][j]);\n" % (free_func)) - c_file.write(" ptr->items[i][j] = NULL;\n"); - c_file.write(" }\n"); - c_file.write(" free (ptr->items[i]);\n"); - c_file.write(" ptr->items[i] = NULL;\n"); + c_file.append(" size_t j;\n") + c_file.append(" for (j = 0; j < ptr->subitem_lens[i]; j++)\n") + c_file.append(" {\n") + c_file.append(f" free_{free_func} (ptr->items[i][j]);\n") + c_file.append(" ptr->items[i][j] = NULL;\n") + c_file.append(" }\n") + c_file.append(" free (ptr->items[i]);\n") + c_file.append(" ptr->items[i] = NULL;\n") else: - c_file.write(" free_%s (ptr->items[i]);\n" % (free_func)) - c_file.write(" ptr->items[i] = NULL;\n") + c_file.append(f" free_{free_func} (ptr->items[i]);\n") + c_file.append(" ptr->items[i] = NULL;\n") - c_file.write(""" + c_file.append(""" } """) if obj.doublearray: - c_file.write(" free (ptr->subitem_lens);\n") - c_file.write(" ptr->subitem_lens = NULL;\n") + c_file.append(" free (ptr->subitem_lens);\n") + c_file.append(" ptr->subitem_lens = NULL;\n") c_typ = helpers.obtain_pointer(obj.name, obj.subtypobj, prefix) if c_typ != "": if obj.subobj is not None: c_typ = c_typ + "_element" - c_file.write(" free_%s (ptr->items);\n" % (c_typ)) - c_file.write(" ptr->items = NULL;\n") + c_file.append(f" free_{c_typ} (ptr->items);\n") + c_file.append(" ptr->items = NULL;\n") return else: - c_file.write(""" + c_file.append(""" free (ptr->items); ptr->items = NULL; """) - c_file.write("""\n + c_file.append("""\n free (ptr); } """) @@ -1349,8 +1276,7 @@ def get_c_epilog_for_array_make_gen(c_file, prefix, typ, obj): return typename = helpers.get_top_array_type_name(obj.name, prefix) - c_file.write("""\n -yajl_gen_status gen_%s (yajl_gen g, const %s *ptr, const struct parser_context *ctx, + c_file.append(f"yajl_gen_status gen_{typename} (yajl_gen g, const {typename} *ptr, const struct parser_context *ctx," + """ parser_error *err) { yajl_gen_status stat; @@ -1359,10 +1285,10 @@ def get_c_epilog_for_array_make_gen(c_file, prefix, typ, obj): if (ptr == NULL) return yajl_gen_status_ok; *err = NULL; -""" % (typename, typename)) +""") if obj.subtypobj or obj.subtyp == 'object': - c_file.write("""\n + c_file.append("""\n stat = yajl_gen_array_open ((yajl_gen) g); if (stat != yajl_gen_status_ok) GEN_SET_ERROR_AND_RETURN (stat, err); @@ -1374,88 +1300,86 @@ def get_c_epilog_for_array_make_gen(c_file, prefix, typ, obj): subtypename = obj.subtypname else: subtypename = helpers.get_name_substr(obj.name, prefix) - c_file.write(' {\n') + c_file.append(' {\n') if obj.doublearray: - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" size_t j;\n") - c_file.write(' for (j = 0; j < ptr->subitem_lens[i]; j++)\n') - c_file.write(' {\n') - c_file.write(' stat = gen_%s (g, ptr->items[i][j], ctx, err);\n' \ - % (subtypename)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(' }\n') - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" size_t j;\n") + c_file.append(' for (j = 0; j < ptr->subitem_lens[i]; j++)\n') + c_file.append(' {\n') + c_file.append(f' stat = gen_{subtypename} (g, ptr->items[i][j], ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' }\n') + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: - c_file.write(' stat = gen_%s (g, ptr->items[i], ctx, err);\n' \ - % (subtypename)) - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write("""\n + c_file.append(f' stat = gen_{subtypename} (g, ptr->items[i], ctx, err);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append("""\n } } stat = yajl_gen_array_close ((yajl_gen) g); """) elif obj.subtyp == 'byte': - c_file.write(' {\n') - c_file.write(' const char *str = NULL;\n') + c_file.append(' {\n') + c_file.append(' const char *str = NULL;\n') if obj.doublearray: - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" {\n") - c_file.write(" size_t i;\n") - c_file.write(" for (i = 0; i < ptr->len; i++)\n") - c_file.write(" {\n") - c_file.write(" if (ptr->items[i] != NULL)\n") - c_file.write(" str = (const char *)ptr->items[i];\n") - c_file.write(" else ()\n") - c_file.write(" str = "";\n") - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" {\n") + c_file.append(" size_t i;\n") + c_file.append(" for (i = 0; i < ptr->len; i++)\n") + c_file.append(" {\n") + c_file.append(" if (ptr->items[i] != NULL)\n") + c_file.append(" str = (const char *)ptr->items[i];\n") + c_file.append(" else ()\n") + c_file.append(" str = "";\n") + c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ (const unsigned char *)str, strlen(str));\n') - c_file.write(" }\n") - c_file.write(" }\n") - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(" }\n") + c_file.append(" }\n") + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: - c_file.write(" if (ptr != NULL && ptr->items != NULL)\n") - c_file.write(" {\n") - c_file.write(" str = (const char *)ptr->items;\n") - c_file.write(" }\n") - c_file.write(' stat = yajl_gen_string ((yajl_gen) g, \ + c_file.append(" if (ptr != NULL && ptr->items != NULL)\n") + c_file.append(" {\n") + c_file.append(" str = (const char *)ptr->items;\n") + c_file.append(" }\n") + c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ (const unsigned char *)str, ptr->len);\n') - c_file.write(' }\n') + c_file.append(' }\n') else: - c_file.write("""\n + c_file.append("""\n stat = yajl_gen_array_open ((yajl_gen) g); if (stat != yajl_gen_status_ok) GEN_SET_ERROR_AND_RETURN (stat, err); for (i = 0; i < ptr->len; i++) { """) - c_file.write(' {\n') + c_file.append(' {\n') if obj.doublearray: - c_file.write(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.write(" if (stat != yajl_gen_status_ok)\n") - c_file.write(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.write(" size_t j;\n") - c_file.write(' for (j = 0; j < ptr->subitem_lens[i]; j++)\n') - c_file.write(' {\n') + c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') + c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" size_t j;\n") + c_file.append(' for (j = 0; j < ptr->subitem_lens[i]; j++)\n') + c_file.append(' {\n') json_value_generator(c_file, 4, "ptr->items[i][j]", 'g', 'ctx', obj.subtyp) - c_file.write(' }\n') - c_file.write(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(' }\n') + c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: json_value_generator(c_file, 3, "ptr->items[i]", 'g', 'ctx', obj.subtyp) - c_file.write("""\n + c_file.append("""\n } } stat = yajl_gen_array_close ((yajl_gen) g); """) - c_file.write("""\n + c_file.append("""\n if (ptr->len > 0 && !(ctx->options & OPT_GEN_SIMPLIFY)) yajl_gen_config (g, yajl_gen_beautify, 1); if (stat != yajl_gen_status_ok) @@ -1485,12 +1409,10 @@ def get_c_epilog(c_file, prefix, typ, obj): typename = helpers.get_top_array_type_name(obj.name, prefix) get_c_epilog_for_array(c_file, prefix, typ, obj) - c_file.write(""" -%s * -%s_parse_file (const char *filename, const struct parser_context *ctx, parser_error *err) -{ - %s *ptr = NULL; - size_t filesize; + c_file.append(f"\n{typename} *\n{typename}_parse_file (const char *filename, const struct parser_context *ctx, parser_error *err)" +"\n{" + f"\n{typename} *ptr = NULL;" + + """size_t filesize; __auto_free char *content = NULL; if (filename == NULL || err == NULL) @@ -1500,21 +1422,20 @@ def get_c_epilog(c_file, prefix, typ, obj): content = read_file (filename, &filesize); if (content == NULL) { - if (asprintf (err, "cannot read the file: %%s", filename) < 0) + if (asprintf (err, "cannot read the file: %s", filename) < 0) *err = strdup ("error allocating memory"); return NULL; - } - ptr = %s_parse_data (content, ctx, err); - return ptr; + }""" + + f"ptr = {typename}_parse_data (content, ctx, err);" + + """return ptr; } -""" % (typename, typename, typename, typename)) +""") - c_file.write(""" -%s * -%s_parse_file_stream (FILE *stream, const struct parser_context *ctx, parser_error *err) -{ - %s *ptr = NULL; - size_t filesize; + c_file.append( +f"{typename} * \n" + +f"{typename}_parse_file_stream (FILE *stream, const struct parser_context *ctx, parser_error *err)\n{{" + + f"{typename} *ptr = NULL;"+ + """\nsize_t filesize; __auto_free char *content = NULL; if (stream == NULL || err == NULL) @@ -1526,20 +1447,19 @@ def get_c_epilog(c_file, prefix, typ, obj): { *err = strdup ("cannot read the file"); return NULL; - } - ptr = %s_parse_data (content, ctx, err); - return ptr; + }\n""" + + f"ptr = {typename}_parse_data (content, ctx, err);" + + """return ptr; } -""" % (typename, typename, typename, typename)) +""") - c_file.write(""" + c_file.append(""" define_cleaner_function (yajl_val, yajl_tree_free) - -%s * -%s_parse_data (const char *jsondata, const struct parser_context *ctx, parser_error *err) -{ - %s *ptr = NULL; - __auto_cleanup(yajl_tree_free) yajl_val tree = NULL; +""" + +f"\n {typename} * " + +f"{typename}_parse_data (const char *jsondata, const struct parser_context *ctx, parser_error *err)\n {{ \n" + + f" {typename} *ptr = NULL;" + + """__auto_cleanup(yajl_tree_free) yajl_val tree = NULL; char errbuf[1024]; struct parser_context tmp_ctx = { 0 }; @@ -1553,16 +1473,15 @@ def get_c_epilog(c_file, prefix, typ, obj): tree = yajl_tree_parse (jsondata, errbuf, sizeof (errbuf)); if (tree == NULL) { - if (asprintf (err, "cannot parse the data: %%s", errbuf) < 0) + if (asprintf (err, "cannot parse the data: %s", errbuf) < 0) *err = strdup ("error allocating memory"); return NULL; - } - ptr = make_%s (tree, ctx, err); - return ptr; -} -""" % (typename, typename, typename, typename)) + }\n""" + + f"ptr = make_{typename} (tree, ctx, err);" + + "return ptr; \n}\n" +) - c_file.write("""\nstatic void\ncleanup_yajl_gen (yajl_gen g) + c_file.append("""\nstatic void\ncleanup_yajl_gen (yajl_gen g) { if (!g) return; @@ -1574,10 +1493,9 @@ def get_c_epilog(c_file, prefix, typ, obj): """) - c_file.write(""" -char * -%s_generate_json (const %s *ptr, const struct parser_context *ctx, parser_error *err) -{ + c_file.append("\n char * \n" + +f"{typename}_generate_json (const {typename} *ptr, const struct parser_context *ctx, parser_error *err)" + +"""{ __auto_cleanup(cleanup_yajl_gen) yajl_gen g = NULL; struct parser_context tmp_ctx = { 0 }; const unsigned char *gen_buf = NULL; @@ -1595,10 +1513,10 @@ def get_c_epilog(c_file, prefix, typ, obj): { *err = strdup ("Json_gen init failed"); return json_buf; - } - - if (yajl_gen_status_ok != gen_%s (g, ptr, ctx, err)) - { + } \n +""" + + f"if (yajl_gen_status_ok != gen_{typename} (g, ptr, ctx, err))" + + """ { if (*err == NULL) *err = strdup ("Failed to generate json"); return json_buf; @@ -1622,4 +1540,4 @@ def get_c_epilog(c_file, prefix, typ, obj): return json_buf; } -""" % (typename, typename, typename)) +""")