diff --git a/libasn1compiler/asn1c_save.c b/libasn1compiler/asn1c_save.c index 79f3b244c..cf6ea0f4d 100644 --- a/libasn1compiler/asn1c_save.c +++ b/libasn1compiler/asn1c_save.c @@ -54,6 +54,7 @@ static int pdu_collection_has_unused_types(arg_t *arg); static const char *generate_pdu_C_definition(void); static void asn1c__cleanup_pdu_type(void); static int asn1c__pdu_type_lookup(const char *typename); +static int generate_constant_file(arg_t *arg, const char *destdir); static int asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps, @@ -436,6 +437,8 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir, asn1c_dep_chainset_free(deps); asn1c__cleanup_pdu_type(); + generate_constant_file(arg, destdir); + return ret; } @@ -944,3 +947,49 @@ include_type_to_pdu_collection(arg_t *arg) { return 0; } + +static abuf * +generate_constant_collection(arg_t *arg) { + asn1p_module_t *mod; + abuf *buf = abuf_new(); + + abuf_printf(buf, "/*\n * Generated by asn1c-" VERSION + " (http://lionet.info/asn1c)\n */\n\n"); + abuf_printf(buf, "#ifndef _ASN_CONSTANT_H\n#define _ASN_CONSTANT_H\n\n"); + + abuf_printf(buf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); + + TQ_FOR(mod, &(arg->asn->modules), mod_next) { + TQ_FOR(arg->expr, &(mod->members), next) { + if(arg->expr->meta_type != AMT_VALUE) + continue; + + if(arg->expr->expr_type == ASN_BASIC_INTEGER) { + abuf_printf(buf, "#define %s (%s)\n", + asn1c_make_identifier(0, arg->expr, 0), + asn1p_itoa(arg->expr->value->value.v_integer)); + } + } + } + + abuf_printf(buf, "\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _ASN_CONSTANT_H */\n"); + return buf; +} + +static int +generate_constant_file(arg_t *arg, const char *destdir) { + abuf *buf = generate_constant_collection(arg); + assert(buf); + + FILE *fp = asn1c_open_file(destdir, "asn_constant", ".h", 0); + if(fp == NULL) { + perror("asn_constant.h"); + return -1; + } + safe_fwrite(buf->buffer, buf->length, 1, fp); + fclose(fp); + abuf_free(buf); + + safe_fprintf(stderr, "Generated asn_constant.h\n"); + return 0; +}