From b8225b67f6e452b4751150e52b3dfee54744f4bf Mon Sep 17 00:00:00 2001 From: midipix Date: Sun, 5 Aug 2018 00:48:15 -0400 Subject: output interfaces: revised API and implementation to use fdctx and pure fdio. --- src/output/amgc_output_enum.c | 83 ++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 37 deletions(-) (limited to 'src/output/amgc_output_enum.c') diff --git a/src/output/amgc_output_enum.c b/src/output/amgc_output_enum.c index 9498774..764ecb1 100644 --- a/src/output/amgc_output_enum.c +++ b/src/output/amgc_output_enum.c @@ -14,14 +14,17 @@ #include "apimagic_driver_impl.h" static int output_enum( + const struct amgc_driver_ctx * dctx, const char * symbol, - const struct amgc_entity enumvals[], - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_entity * enumvals, + const struct amgc_layout * layout) { const struct amgc_entity * enumval; struct amgc_layout elayout; size_t len; + int fdout; + + fdout = amgc_driver_fdout(dctx); if (!layout || !layout->symwidth) { if (!layout) @@ -45,10 +48,11 @@ static int output_enum( } } - if (layout->header && (fputs(layout->header,fout) < 0)) - return -1; + if (layout->header) + if (amgc_dprintf(fdout,layout->header) < 0) + return -1; - if (fprintf(fout,"enum %s {\n",symbol) < 0) + if (amgc_dprintf(fdout,"enum %s {\n",symbol) < 0) return -1; for (enumval=enumvals; enumval->entity || enumval->altname; enumval++) { @@ -56,69 +60,71 @@ static int output_enum( ? enumval->altname : enumval->entity->base.symbol->string; - if (fprintf(fout,"\t%s",symbol) < 0) + if (amgc_dprintf(fdout,"\t%s",symbol) < 0) return -1; - if (amgc_output_pad_symbol(symbol,layout,fout) < 0) + if (amgc_output_pad_symbol(dctx,symbol,layout) < 0) return -1; if ((enumval->enumval < 0) && (enumval->enumval > -128)) - fprintf(fout,"= (%d)",enumval->enumval); + amgc_dprintf(fdout,"= (%d)",enumval->enumval); else if ((enumval->enumval >= 0) && (enumval->enumval < 2048)) - fprintf(fout,"= %d",enumval->enumval); + amgc_dprintf(fdout,"= %d",enumval->enumval); else - fprintf(fout,"= 0x%08x",(unsigned)enumval->enumval); + amgc_dprintf(fdout,"= 0x%08x",(unsigned)enumval->enumval); - if (fputs(",\n",fout) < 0) + if (amgc_dprintf(fdout,",\n") < 0) return -1; } - if (fputs("};\n",fout) < 0) + if (amgc_dprintf(fdout,"};\n") < 0) return -1; - if (layout->footer && (fputs(layout->footer,fout) < 0)) - return -1; + if (layout->footer) + if (amgc_dprintf(fdout,layout->footer) < 0) + return -1; return 0; } int amgc_output_unit_enum( + const struct amgc_driver_ctx * dctx, const struct amgc_unit_ctx * uctx, const union entity_t * entity, - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { - struct amgc_entity * enumvals; - const char * symbol; int ret; + const char * symbol; + struct amgc_entity * enumvals; if (entity->base.kind != ENTITY_ENUM) return -1; + else if (amgc_get_enum_members(uctx,entity,&enumvals)) return -1; - if (entity->base.symbol) - symbol = entity->base.symbol->string; - else - symbol = ""; + symbol = (entity->base.symbol) + ? entity->base.symbol->string + : ""; - ret = output_enum(symbol,enumvals,layout,fout); + ret = output_enum(dctx,symbol,enumvals,layout); amgc_free_enum_members(enumvals); return ret; } int amgc_output_custom_enum( + const struct amgc_driver_ctx * dctx, const struct amgc_entity * penum, const struct amgc_entity enumvals[], - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { const struct amgc_entity * aentity; const char * symbol; if (penum->entity && penum->entity->base.kind != ENTITY_ENUM) return -1; + else if (!penum->entity && !penum->altname) return -1; @@ -133,42 +139,45 @@ int amgc_output_custom_enum( else symbol = ""; - return output_enum(symbol,enumvals,layout,fout); + return output_enum(dctx,symbol,enumvals,layout); } int amgc_output_unit_enums( + const struct amgc_driver_ctx * dctx, const struct amgc_unit_ctx * uctx, - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { const struct amgc_entity * aentity; for (aentity=uctx->entities->enums; aentity->entity; aentity++) - if (amgc_output_unit_enum(uctx,aentity->entity,layout,fout)) + if (amgc_output_unit_enum(dctx,uctx,aentity->entity,layout)) return -1; return 0; } int amgc_list_unit_enums( + const struct amgc_driver_ctx * dctx, const struct amgc_unit_ctx * uctx, - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { const struct amgc_entity * aentity; + int fdout = amgc_driver_fdout(dctx); - if (layout && layout->header && (fputs(layout->header,fout) < 0)) - return -1; + if (layout && layout->header) + if (amgc_dprintf(fdout,layout->header,fdout) < 0) + return -1; for (aentity=uctx->entities->enums; aentity->entity; aentity++) - if ((fprintf(fout,"enum %s;\n", + if ((amgc_dprintf(fdout,"enum %s;\n", aentity->entity && aentity->entity->base.symbol ? aentity->entity->base.symbol->string : aentity->altname) < 0)) return -1; - if (layout && layout->footer && (fputs(layout->footer,fout) < 0)) - return -1; + if (layout && layout->footer) + if (amgc_dprintf(fdout,layout->footer) < 0) + return -1; return 0; } -- cgit v1.2.3