diff --git a/eeshow/main.c b/eeshow/main.c index 4187ae8..02e9526 100644 --- a/eeshow/main.c +++ b/eeshow/main.c @@ -211,5 +211,7 @@ found: } gfx_end(); + sch_free(&sch_ctx); + return 0; } diff --git a/eeshow/sch-parse.c b/eeshow/sch-parse.c index 7fbdfdd..45e298e 100644 --- a/eeshow/sch-parse.c +++ b/eeshow/sch-parse.c @@ -488,6 +488,8 @@ static bool parse_line(const struct file *file, void *user, const char *line) if (ctx->recurse) sheet_obj->u.sheet.sheet = recurse_sheet(ctx, file); + else + sheet_obj->u.sheet.sheet = NULL; ctx->state = sch_basic; return 1; } @@ -553,3 +555,73 @@ void sch_init(struct sch_ctx *ctx, bool recurse) ctx->next_sheet = &ctx->sheets; new_sheet(ctx); } + + +static void free_comp_fields(struct comp_field *fields) +{ + struct comp_field *next; + + while (fields) { + next = fields->next; + free((char *) fields->txt.s); + free(fields); + fields = next; + } +} + + +static void free_sheet_fields(struct sheet_field *fields) +{ + struct sheet_field *next; + + while (fields) { + next = fields->next; + free((char *) fields->s); + free(fields); + fields = next; + } +} + + +static void free_sheet(struct sheet *sch) +{ + struct sch_obj *obj, *next; + + if (!sch) + return; + free((char *) sch->title); + for (obj = sch->objs; obj; obj = next) { + next = obj->next; + switch (obj->type) { + case sch_obj_text: + free((char *) obj->u.text.s); + break; + case sch_obj_comp: + free_comp_fields(obj->u.comp.fields); + break; + case sch_obj_sheet: + free((char *) obj->u.sheet.name); + free((char *) obj->u.sheet.file); + free_sheet((struct sheet *) obj->u.sheet.sheet); + free_sheet_fields(obj->u.sheet.fields); + break; + default: + break; + } + free(obj); + } + free(sch); +} + + +void sch_free(struct sch_ctx *ctx) +{ + struct sheet *next; + + while (ctx->sheets) { + next = ctx->sheets->next; + free_sheet(ctx->sheets); + ctx->sheets = next; + } +} + diff --git a/eeshow/sch.h b/eeshow/sch.h index ff4f731..ddfc323 100644 --- a/eeshow/sch.h +++ b/eeshow/sch.h @@ -74,7 +74,8 @@ struct sch_obj { const char *file; unsigned file_dim; bool rotated; - const struct sheet *sheet; /* pointer to sub-sheet */ + const struct sheet *sheet; + /* pointer to sub-sheet; NULL if absent */ struct sheet_field { char *s; @@ -91,7 +92,7 @@ struct sch_obj { }; struct sheet { - const char *title; + const char *title; /* malloced, unless delta */ struct sch_obj *objs; struct sch_obj **next_obj; struct sheet *parent; @@ -118,5 +119,6 @@ void decode_alignment(struct text *txt, char hor, char vert); void sch_render(const struct sheet *sheet); void sch_parse(struct sch_ctx *ctx, struct file *file, const struct lib *lib); void sch_init(struct sch_ctx *ctx, bool recurse); +void sch_free(struct sch_ctx *ctx); #endif /* !SCH_H */