mirror of
https://gitlab.com/xCrystal/pokecrystal-board.git
synced 2025-01-23 09:16:20 -08:00
Refactor scan_includes.
This commit is contained in:
parent
a98538641b
commit
b78ba89f5f
@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
void usage(void) {
|
void usage(void) {
|
||||||
printf("Usage: scan_includes filename\n");
|
printf("Usage: scan_includes filename\n");
|
||||||
@ -25,55 +26,46 @@ void scan_file(char* filename) {
|
|||||||
size = ftell(f);
|
size = ftell(f);
|
||||||
rewind(f);
|
rewind(f);
|
||||||
|
|
||||||
//fprintf(stderr, "malloc: %s\n", filename);
|
|
||||||
buffer = malloc(size + 1);
|
buffer = malloc(size + 1);
|
||||||
orig = buffer;
|
orig = buffer;
|
||||||
fread(buffer, 1, size, f);
|
fread(buffer, 1, size, f);
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
fclose(f);
|
fclose(f);
|
||||||
//fprintf(stderr, "read: %s\n", filename);
|
|
||||||
|
|
||||||
for (; (buffer != NULL) && (buffer != 0) && (buffer - orig < size); buffer++) {
|
for (; buffer && (buffer - orig < size); buffer++) {
|
||||||
//fprintf(stderr, "%c", buffer[0]);
|
|
||||||
if (buffer[0] == ';') {
|
if (buffer[0] == ';') {
|
||||||
buffer = strchr(buffer, '\n');
|
buffer = strchr(buffer, '\n');
|
||||||
if (buffer == NULL) {
|
if (!buffer) {
|
||||||
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
fprintf(stderr, "%s: no newline at end of file\n", filename);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (
|
bool is_include = false;
|
||||||
(strncmp(buffer, "INCBIN", 6) == 0) ||
|
bool is_incbin = false;
|
||||||
(strncmp(buffer, "incbin", 6) == 0)
|
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
|
||||||
) {
|
is_incbin = true;
|
||||||
|
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
|
||||||
|
is_include = true;
|
||||||
|
}
|
||||||
|
if (is_incbin || is_include) {
|
||||||
buffer = strchr(buffer, '"') + 1;
|
buffer = strchr(buffer, '"') + 1;
|
||||||
if (buffer == NULL) break;
|
if (!buffer) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
length = strcspn(buffer, "\"");
|
length = strcspn(buffer, "\"");
|
||||||
include = malloc(length + 1);
|
include = malloc(length + 1);
|
||||||
strncpy(include, buffer, length);
|
strncpy(include, buffer, length);
|
||||||
include[length] = '\0';
|
include[length] = '\0';
|
||||||
printf("%s ", include);
|
printf("%s ", include);
|
||||||
free(include);
|
if (is_include) {
|
||||||
} else if (
|
scan_file(include);
|
||||||
(strncmp(buffer, "INCLUDE", 7) == 0) ||
|
}
|
||||||
(strncmp(buffer, "include", 7) == 0)
|
|
||||||
) {
|
|
||||||
buffer = strchr(buffer, '"') + 1;
|
|
||||||
if (buffer == NULL) break;
|
|
||||||
length = strcspn(buffer, "\"");
|
|
||||||
include = malloc(length + 1);
|
|
||||||
strncpy(include, buffer, length);
|
|
||||||
include[length] = '\0';
|
|
||||||
printf("%s ", include);
|
|
||||||
scan_file(include);
|
|
||||||
free(include);
|
free(include);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fprintf(stderr, "free: %s\n", filename);
|
|
||||||
free(orig);
|
free(orig);
|
||||||
//fprintf(stderr, "freed: %s\n", filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user