Skip to content

Commit

Permalink
Merge pull request #130 from Ilya-source/optimize_strings_handling
Browse files Browse the repository at this point in the history
Update formatting for source and header files
  • Loading branch information
giuseppe authored Jun 22, 2023
2 parents 19c0567 + 66f6f71 commit 3d16826
Show file tree
Hide file tree
Showing 3 changed files with 906 additions and 988 deletions.
8 changes: 6 additions & 2 deletions src/ocispec/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
140 changes: 68 additions & 72 deletions src/ocispec/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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):
'''
Expand All @@ -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):
Expand All @@ -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 \
Expand All @@ -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):
'''
Expand All @@ -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 <sys/types.h>\n")
header.write("#include <stdint.h>\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 <sys/types.h>\n")
header.append("#include <stdint.h>\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")
Loading

0 comments on commit 3d16826

Please sign in to comment.