summaryrefslogtreecommitdiff
path: root/tools/scan_includes.c
diff options
context:
space:
mode:
authoryenatch <yenatch@gmail.com>2017-04-28 01:41:06 -0400
committeryenatch <yenatch@gmail.com>2017-04-28 01:41:06 -0400
commitb78ba89f5f0db937e7cf4e386bd41340634a31c4 (patch)
tree15ec3b649c56ec920604c7e48948143a0d575e23 /tools/scan_includes.c
parenta98538641b7cee5fa90611ec88cbc3406b826aff (diff)
Refactor scan_includes.
Diffstat (limited to 'tools/scan_includes.c')
-rw-r--r--tools/scan_includes.c42
1 files changed, 17 insertions, 25 deletions
diff --git a/tools/scan_includes.c b/tools/scan_includes.c
index cc6eeb1be..4aba83eaa 100644
--- a/tools/scan_includes.c
+++ b/tools/scan_includes.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
void usage(void) {
printf("Usage: scan_includes filename\n");
@@ -25,55 +26,46 @@ void scan_file(char* filename) {
size = ftell(f);
rewind(f);
- //fprintf(stderr, "malloc: %s\n", filename);
buffer = malloc(size + 1);
orig = buffer;
fread(buffer, 1, size, f);
buffer[size] = '\0';
fclose(f);
- //fprintf(stderr, "read: %s\n", filename);
- for (; (buffer != NULL) && (buffer != 0) && (buffer - orig < size); buffer++) {
- //fprintf(stderr, "%c", buffer[0]);
+ for (; buffer && (buffer - orig < size); buffer++) {
if (buffer[0] == ';') {
buffer = strchr(buffer, '\n');
- if (buffer == NULL) {
+ if (!buffer) {
fprintf(stderr, "%s: no newline at end of file\n", filename);
break;
}
continue;
}
- if (
- (strncmp(buffer, "INCBIN", 6) == 0) ||
- (strncmp(buffer, "incbin", 6) == 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);
- free(include);
- } else if (
- (strncmp(buffer, "INCLUDE", 7) == 0) ||
- (strncmp(buffer, "include", 7) == 0)
- ) {
+ 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 == NULL) break;
+ if (!buffer) {
+ break;
+ }
length = strcspn(buffer, "\"");
include = malloc(length + 1);
strncpy(include, buffer, length);
include[length] = '\0';
printf("%s ", include);
- scan_file(include);
+ if (is_include) {
+ scan_file(include);
+ }
free(include);
}
}
- //fprintf(stderr, "free: %s\n", filename);
free(orig);
- //fprintf(stderr, "freed: %s\n", filename);
}
int main(int argc, char* argv[]) {