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