From 34117153cb0e1b178e1c82afa4af152c1c9f937b Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 26 Sep 2010 06:09:12 -0300 Subject: [PATCH] Project description files can now begin with comment lines. - solidify/project.h (struct project), solidify/project.c (make_project, load_project, save_project): allow project descriptions to begin with any numer of comment lines and preserve them across sessions --- solidify/project.c | 23 +++++++++++++++++------ solidify/project.h | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/solidify/project.c b/solidify/project.c index 10e703d..7d14625 100644 --- a/solidify/project.c +++ b/solidify/project.c @@ -30,6 +30,7 @@ static struct project *make_project(const char *name, prj = alloc_type(struct project); prj->name = stralloc(name); + prj->comment = NULL; prj->top = stralloc(top); prj->bottom = stralloc(bottom); prj->s.a = read_face(top); @@ -112,6 +113,7 @@ struct project *load_project(const char *name) { FILE *file; char top[1000], bottom[1000]; /* @@@ enough */ + char *comment; struct project *prj; file = fopen(name, "r"); @@ -120,9 +122,18 @@ struct project *load_project(const char *name) exit(1); } - if (!fgets(top, sizeof(top), file)) { - fprintf(stderr, "%s: can't read name of top face\n", name); - exit(1); + comment = stralloc(""); + + while (1) { + if (!fgets(top, sizeof(top), file)) { + fprintf(stderr, "%s: can't read name of top face\n", + name); + exit(1); + } + if (*top != '#') + break; + comment = realloc(comment, strlen(comment)+strlen(top)+1); + strcat(comment, top); } if (strchr(top, '\n')) *strchr(top, '\n') = 0; @@ -136,7 +147,7 @@ struct project *load_project(const char *name) *strchr(bottom, '\n') = 0; prj = make_project(name, top, bottom, 0); - + prj->comment = comment; read_optional(file, prj); return prj; @@ -172,8 +183,8 @@ void save_project(const struct project *prj) exit(1); } if (fprintf(file, - "%s\n%s\n%g\n", prj->top, prj->bottom, - prj->s.dist*prj->s.a->z_step) < 0) { + "%s%s\n%s\n%g\n", prj->comment ? prj->comment : "", + prj->top, prj->bottom, prj->s.dist*prj->s.a->z_step) < 0) { perror(tmp); exit(1); } diff --git a/solidify/project.h b/solidify/project.h index cf7b502..bf60a17 100644 --- a/solidify/project.h +++ b/solidify/project.h @@ -13,6 +13,7 @@ /* * Project file structure: * + * Zero or more comment lines beginning with a hash sign (#) * line 1: file name of top face (required) * line 2: file name of bottom face (required) * line 3 and beyond, separated by whitespace: @@ -34,6 +35,7 @@ struct project { const char *name; + const char *comment; const char *top; const char *bottom; struct solid s;