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_compound.c | 112 +++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 55 deletions(-) (limited to 'src/output/amgc_output_compound.c') diff --git a/src/output/amgc_output_compound.c b/src/output/amgc_output_compound.c index 312d475..1736a06 100644 --- a/src/output/amgc_output_compound.c +++ b/src/output/amgc_output_compound.c @@ -16,14 +16,14 @@ #include "apimagic_driver_impl.h" static int output_string( - FILE * fout, + int * fdout, const char * fmt, const char * string, const char * brace, int * len) { - int ret = fout - ? fprintf(fout,fmt,string,brace) + int ret = fdout + ? amgc_dprintf(*fdout,fmt,string,brace) : snprintf(0,0,fmt,string,brace); if (len && (ret > 0)) @@ -36,7 +36,7 @@ static int output_compound( union entity_t const * entity, int depth, const struct amgc_layout * layout, - FILE * fout) + int * fdout) { struct compound_t const * compound; type_qualifiers_t tquals; @@ -104,14 +104,14 @@ static int output_compound( } - if (depth && fout && (fputc('\n',fout) < 0)) + if (depth && fdout && (amgc_dprintf(*fdout,"\n") < 0)) return -1; for (i=0; ikind); break; @@ -180,14 +182,14 @@ static int output_compound( if (ftabs) for (i=0; ibase.qualifiers & TYPE_QUALIFIER_CONST) if (type->kind == TYPE_POINTER) - if (output_string(fout," const ", + if (output_string(fdout," const ", "","",&len) < 0) return -1; - if (output_string(fout,"*","","",&len) < 0) + if (output_string(fdout,"*","","",&len) < 0) return -1; } if (fspace) len++; - if (fout) { + if (fdout) { symwidth = layout->symwidth; symwidth += layout->tabwidth; @@ -221,7 +223,7 @@ static int output_compound( len &= (~(layout->tabwidth-1)); while (len < symwidth) { - if (fputc('\t',fout) < 0) + if (amgc_dprintf(*fdout,"\t") < 0) return -1; else len += layout->tabwidth; @@ -229,15 +231,16 @@ static int output_compound( } else if (len > width) width = len; - if (output_string(fout,"%s", + if (output_string(fdout,"%s", entity->base.symbol->string, "",0) < 0) return -1; type = entity->declaration.type; - while (fout && type->kind == TYPE_ARRAY) { - if (fprintf(fout, + while (fdout && type->kind == TYPE_ARRAY) { + if (amgc_dprintf( + *fdout, type->array.size ? "[%zu]" : "[]", type->array.size) < 0) return -1; @@ -245,26 +248,26 @@ static int output_compound( type = type->array.element_type; } - if (fout && fputs(";\n",fout) < 0) + if (fdout && amgc_dprintf(*fdout,";\n") < 0) return -1; - if (!ftabs && fout && entity->base.next) - if (fputc('\n',fout) < 0) + if (!ftabs && fdout && entity->base.next) + if (amgc_dprintf(*fdout,"\n") < 0) return -1; } - if (!fout) + if (!fdout) return width; if (--depth) { for (i=0; isymwidth) - return output_compound(aentity->entity,0,layout,fout); + return output_compound(aentity->entity,0,layout,&fdout); if (layout) memcpy(&elayout,layout,sizeof(elayout)); @@ -295,83 +299,81 @@ static int output_compound_entity( if (elayout.tabwidth == 0) elayout.tabwidth = AMGC_TAB_WIDTH; - if (output_compound(aentity->entity,0,&elayout,fout) < 0) + if (output_compound(aentity->entity,0,&elayout,&fdout) < 0) return -1; return 0; } int amgc_output_compound( + const struct amgc_driver_ctx * dctx, const struct amgc_unit_ctx * uctx, const struct amgc_entity * aentity, - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { union entity_t const * entity; entity = aentity->entity; - if ((entity->kind == ENTITY_STRUCT) || (entity->kind == ENTITY_UNION)) - return output_compound_entity(uctx,aentity,layout,fout); - else - return -1; + return ((entity->kind == ENTITY_STRUCT) || (entity->kind == ENTITY_UNION)) + ? output_compound_entity(dctx,uctx,aentity,layout) + : -1; } int amgc_output_struct( + const struct amgc_driver_ctx * dctx, const struct amgc_unit_ctx * uctx, const struct amgc_entity * aentity, - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { union entity_t const * entity; entity = aentity->entity; - if (entity->kind == ENTITY_STRUCT) - return output_compound_entity(uctx,aentity,layout,fout); - else - return -1; + return (entity->kind == ENTITY_STRUCT) + ? output_compound_entity(dctx,uctx,aentity,layout) + : -1; } int amgc_output_union( + const struct amgc_driver_ctx * dctx, const struct amgc_unit_ctx * uctx, const struct amgc_entity * aentity, - const struct amgc_layout * layout, - FILE * fout) + const struct amgc_layout * layout) { union entity_t const * entity; entity = aentity->entity; - if (entity->kind == ENTITY_UNION) - return output_compound_entity(uctx,aentity,layout,fout); - else - return -1; + return (entity->kind == ENTITY_UNION) + ? output_compound_entity(dctx,uctx,aentity,layout) + : -1; + } int amgc_output_unit_structs( + 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->structs; aentity->entity; aentity++) - if (output_compound_entity(uctx,aentity,layout,fout)) + if (output_compound_entity(dctx,uctx,aentity,layout) < 0) return -1; return 0; } int amgc_output_unit_unions( + 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->unions; aentity->entity; aentity++) - if (output_compound_entity(uctx,aentity,layout,fout)) + if (output_compound_entity(dctx,uctx,aentity,layout) < 0) return -1; return 0; -- cgit v1.2.3