diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-03-04 17:09:37 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-03-04 17:09:37 -0800 |
| commit | 482c9971568ab55878b5b4ac3c4add1385b29813 (patch) | |
| tree | 32007c1ff0e8febae8eacceaac566730babd741b /src/precedent.c | |
| parent | 2e9c68c309d004a38ab0c188c05f74291952e465 (diff) | |
added include guard
Diffstat (limited to 'src/precedent.c')
| -rw-r--r-- | src/precedent.c | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/src/precedent.c b/src/precedent.c index 1225a91..8351108 100644 --- a/src/precedent.c +++ b/src/precedent.c @@ -45,7 +45,11 @@ int main(int argc, char** argv) { char* baseDir = malloc((strlen(dirName) + 2) * sizeof(char)); strcpy(baseDir, dirName); strcat(baseDir, "/"); - char* finalContent = preprocess(read_file(path), baseDir); + hashtable_t* includeTable = init_hashtable(500, NULL); + char* baseContent = read_file(path); + char* finalContent = preprocess(baseContent, baseDir, includeTable); + destroy_hashtable(includeTable); + /* TODO: write hashtable information if debug */ sym_t* finalSymbol = parse_symbol_final(finalContent); free(pathCpy); free(baseDir); @@ -82,15 +86,20 @@ char* read_file(char* path) { out[length] = '\0'; return out; } -char* preprocess(char* str, char* baseDir) { +char* preprocess(char* str, char* baseDir, hashtable_t* includeHt) { while (1) { int i = 0; - while (str[i] != '@') { + while (str[i] != '@' && str[i] != '&') { i++; if (str[i] == '\0') { return str; } } + char alwaysInclude = 0; + /* include even if already included */ + if (str[i] == '&') { + alwaysInclude = 1; + } str[i] = ' '; i++; char* includeName = malloc(40 * sizeof(char)); @@ -105,14 +114,26 @@ char* preprocess(char* str, char* baseDir) { char* path = malloc((strlen(baseDir) + strlen(includeName) + 1) * sizeof(char)); strcpy(path, baseDir); strcat(path, includeName); - char* toInclude = preprocess(read_file(path), baseDir); - char* newStr = malloc((strlen(toInclude) + strlen(str) + 1) * sizeof(char)); - strcpy(newStr, toInclude); - strcat(newStr, str); - free(str); + char* fileContent = read_file(path); + /* ensure the file has not already been included */ + char notIncluded = hashtable_get_pair(includeHt, fileContent).key == NULL; + if (notIncluded) { + hashtable_add_pair(includeHt, init_pair(fileContent, NULL)); + } + if (notIncluded || alwaysInclude) { + /* TODO: fix relative paths */ + char* toInclude = preprocess(fileContent, baseDir, includeHt); + char* newStr = malloc((strlen(toInclude) + strlen(str) + 1) * sizeof(char)); + strcpy(newStr, toInclude); + strcat(newStr, str); + free(str); + free(toInclude); + str = newStr; + } + else { + free(fileContent); + } free(includeName); free(path); - free(toInclude); - str = newStr; } } |
