added scene file validation

pull/14/head
Sara 2023-07-16 13:33:47 +02:00
parent 25c5f83fe4
commit 19401f1f47
1 changed files with 61 additions and 9 deletions

View File

@ -57,6 +57,14 @@ int fpeekc(FILE* file) {
return c; return c;
} }
static
void freadto(FILE* file, char c) {
char read;
do {
read = fgetc(file);
} while(read != c);
}
static static
int nextnw(FILE* file) { int nextnw(FILE* file) {
int next; int next;
@ -75,6 +83,8 @@ void _parse_key(FILE* file, char* out) {
c = fgetc(file); c = fgetc(file);
if(c == ':') { if(c == ':') {
*out = '\0'; *out = '\0';
} else if(c == '#') {
freadto(file, '\n');
} else if(!isspace(c)) { } else if(!isspace(c)) {
*out = c; *out = c;
++out; ++out;
@ -91,6 +101,9 @@ void _parse_value(FILE* file, char* out, int* _argc, char** argv) {
do { do {
c = fgetc(file); c = fgetc(file);
switch(c) { switch(c) {
case '#':
freadto(file, '\n');
break;
case ';': case ';':
*out = '\0'; *out = '\0';
break; break;
@ -138,8 +151,7 @@ void _parse_config(FILE* file) {
} }
static static
void _parse_scene(const char* filename) { void _parse_scene(FILE* file) {
FILE* file = fopen(filename, "r");
int next; int next;
do { do {
_parse_config(file); _parse_config(file);
@ -148,13 +160,53 @@ void _parse_scene(const char* filename) {
} while(next != EOF); } while(next != EOF);
} }
void load_scene(const char* file) { static
world_clear(); int _validate_config(FILE* file) {
_parse_scene(file); char c;
int colon_count = 0;
do {
c = nextnw(file);
switch(c) {
default: break;
case ':':
colon_count++;
break;
}
} while(c != ';');
return colon_count == 1;
} }
void load_scene_additive(const char* file) { static
printf("parsing scene\n"); int _validate_scene(FILE* file) {
_parse_scene(file); if(file == NULL) return 0;
printf("parsed scene\n");
int next;
int validated;
do {
validated = _validate_config(file);
next = nextnw(file);
ungetc(next, file);
} while(validated == 1 && next != EOF);
rewind(file);
return validated;
}
void load_scene(const char* filename) {
FILE* file = fopen(filename, "r");
if(_validate_scene(file)) {
world_clear();
_parse_scene(file);
fclose(file);
}
}
void load_scene_additive(const char* filename) {
FILE* file = fopen(filename, "r");
if(_validate_scene(file)) {
_parse_scene(file);
fclose(file);
}
} }