diff options
author | yenatch <yenatch@gmail.com> | 2017-07-08 14:29:28 -0400 |
---|---|---|
committer | yenatch <yenatch@gmail.com> | 2017-07-08 14:29:28 -0400 |
commit | a6f85dabbe5fad7f4fb7962293f52b6942a364c8 (patch) | |
tree | f603d20aa2e47d0321729eeebc415f9aa9e3bdec /tools/scan_includes.c | |
parent | 56ff80212749f8a4aa7b3af7c30e6642252b4781 (diff) |
Refactor scan_includes and use "rb" (fixes crlf).
Diffstat (limited to 'tools/scan_includes.c')
-rw-r--r-- | tools/scan_includes.c | 80 |
1 files changed, 41 insertions, 39 deletions
diff --git a/tools/scan_includes.c b/tools/scan_includes.c index b6fcca0..02a7810 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -19,16 +19,8 @@ struct Options { struct Options Options = {0}; - void scan_file(char* filename) { - FILE* f; - long size; - char* orig; - char* buffer; - char* include; - int length; - - f = fopen(filename, "r"); + FILE *f = fopen(filename, "rb"); if (!f) { if (Options.strict) { fprintf(stderr, "Could not open file: '%s'\n", filename); @@ -39,46 +31,56 @@ void scan_file(char* filename) { } fseek(f, 0, SEEK_END); - size = ftell(f); + long size = ftell(f); rewind(f); - buffer = malloc(size + 1); - orig = buffer; - fread(buffer, 1, size, f); + char *buffer = malloc(size + 1); + char *orig = buffer; + size = fread(buffer, 1, size, f); buffer[size] = '\0'; fclose(f); 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_incbin = false; - 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; - if (!buffer) { + switch (*buffer) { + case ';': + buffer = strchr(buffer, '\n'); + if (!buffer) { + fprintf(stderr, "%s: no newline at end of file\n", filename); + } break; - } - length = strcspn(buffer, "\""); - include = malloc(length + 1); - strncpy(include, buffer, length); - include[length] = '\0'; - printf("%s ", include); - if (is_include) { - scan_file(include); - } - free(include); + + case 'i': + case 'I': + 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, '"'); + 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); |