mirror of https://github.com/pret/pokecrystal.git
Refactor scan_includes
This commit is contained in:
parent
80888726b9
commit
885af5e543
|
@ -19,16 +19,8 @@ struct Options {
|
||||||
|
|
||||||
struct Options Options = {0};
|
struct Options Options = {0};
|
||||||
|
|
||||||
|
|
||||||
void scan_file(char* filename) {
|
void scan_file(char* filename) {
|
||||||
FILE* f;
|
FILE *f = fopen(filename, "rb");
|
||||||
long size;
|
|
||||||
char* orig;
|
|
||||||
char* buffer;
|
|
||||||
char* include;
|
|
||||||
int length;
|
|
||||||
|
|
||||||
f = fopen(filename, "r");
|
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (Options.strict) {
|
if (Options.strict) {
|
||||||
fprintf(stderr, "Could not open file: '%s'\n", filename);
|
fprintf(stderr, "Could not open file: '%s'\n", filename);
|
||||||
|
@ -39,65 +31,70 @@ void scan_file(char* filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
size = ftell(f);
|
long size = ftell(f);
|
||||||
rewind(f);
|
rewind(f);
|
||||||
|
|
||||||
buffer = malloc(size + 1);
|
char *buffer = malloc(size + 1);
|
||||||
orig = buffer;
|
char *orig = buffer;
|
||||||
fread(buffer, 1, size, f);
|
size = fread(buffer, 1, size, f);
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
for (; buffer && (buffer - orig < size); buffer++) {
|
for (; buffer && (buffer - orig < size); buffer++) {
|
||||||
if (buffer[0] == ';') {
|
|
||||||
buffer = strchr(buffer, '\n');
|
|
||||||
if (!buffer) {
|
|
||||||
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
bool is_include = false;
|
bool is_include = false;
|
||||||
bool is_incbin = false;
|
bool is_incbin = false;
|
||||||
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
|
switch (*buffer) {
|
||||||
is_incbin = true;
|
case ';':
|
||||||
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
|
buffer = strchr(buffer, '\n');
|
||||||
is_include = true;
|
if (!buffer) {
|
||||||
}
|
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
||||||
if (is_incbin || is_include) {
|
}
|
||||||
buffer = strchr(buffer, '"') + 1;
|
|
||||||
if (!buffer) {
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
length = strcspn(buffer, "\"");
|
case 'i':
|
||||||
include = malloc(length + 1);
|
case 'I':
|
||||||
strncpy(include, buffer, length);
|
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
|
||||||
include[length] = '\0';
|
is_incbin = true;
|
||||||
printf("%s ", include);
|
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
|
||||||
if (is_include) {
|
is_include = true;
|
||||||
scan_file(include);
|
}
|
||||||
}
|
if (is_incbin || is_include) {
|
||||||
free(include);
|
buffer = strchr(buffer, '"');
|
||||||
|
if (!buffer++) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int length = strcspn(buffer, "\"");
|
||||||
|
char *include = malloc(length + 1);
|
||||||
|
strncpy(include, buffer, length);
|
||||||
|
include[length] = '\0';
|
||||||
|
printf("%s ", include);
|
||||||
|
if (is_include) {
|
||||||
|
scan_file(include);
|
||||||
|
}
|
||||||
|
free(include);
|
||||||
|
buffer = strchr(buffer, '"');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if (!buffer) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(orig);
|
free(orig);
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_args(int argc, char *argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
while (1) {
|
int i = 0;
|
||||||
struct option long_options[] = {
|
struct option long_options[] = {
|
||||||
{"strict", no_argument, 0, 's'},
|
{"strict", no_argument, 0, 's'},
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
int i = 0;
|
int opt = -1;
|
||||||
int opt = getopt_long(argc, argv, "sh", long_options, &i);
|
while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
|
||||||
|
|
||||||
if (opt == -1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 's':
|
case 's':
|
||||||
Options.strict = true;
|
Options.strict = true;
|
||||||
|
@ -111,10 +108,6 @@ void get_args(int argc, char *argv[]) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
get_args(argc, argv);
|
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
if (Options.help) {
|
if (Options.help) {
|
||||||
|
|
Loading…
Reference in New Issue