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
This commit is contained in:
Werner Almesberger 2010-09-26 06:09:12 -03:00
parent d9ddd52427
commit 34117153cb
2 changed files with 19 additions and 6 deletions

View File

@ -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);
}

View File

@ -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;