diff --git a/.gitignore b/.gitignore index cfd3752..1ccac57 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ build out pages template.html + +# Vim +~* +.*.sw* diff --git a/calathea.c b/calathea.c index 4eb1c52..6f7ad7c 100644 --- a/calathea.c +++ b/calathea.c @@ -75,21 +75,22 @@ void to_lower_case(char dest[], char str[]) { dest[i] = 0; } -void map_put(char map[][80], int mapSize, char title[], char permalink[]) { +void map_put(struct Page **map, int mapSize, char title[], struct Page *page) { char lowercased[80]; to_lower_case(lowercased, title); int index = hash_polynomial(mapSize, lowercased); - strncpy(map[index], permalink, 79); - map[index][79] = 0; + map[index] = page; } -char * map_get(char map[][80], int mapSize, char title[]) { +struct Page * map_get(struct Page **map, int mapSize, char title[]) { char lowercased[80]; to_lower_case(lowercased, title); int index = hash_polynomial(mapSize, lowercased); - return map[index]; + struct Page *page = map[index]; + + return page; } /*** Templating ***/ @@ -157,10 +158,8 @@ int main(int argc, char *argv[]) { return 1; } - char titleMap[mapSize][80]; - for (int i = 0; i < mapSize; i++) { - titleMap[i][0] = 0; - } + struct Page ** pageMap = malloc(mapSize * sizeof(struct Page *)); + memset(pageMap, 0, mapSize * sizeof(struct Page *)); // Contains some information about the current file picked from pagesDir struct dirent *fileEntry = readdir(pagesDir); @@ -245,7 +244,7 @@ int main(int argc, char *argv[]) { currentPage->title[min(titleLength, 80)] = 0; // Insert it into the hash map for lookup later - map_put(titleMap, mapSize, currentPage->title, currentPage->permalink); + map_put(pageMap, mapSize, currentPage->title, currentPage); // Get ready to process the next page fileEntry = readdir(pagesDir); @@ -298,21 +297,21 @@ int main(int argc, char *argv[]) { strncpy(title, nextLinkStart + 2, (linkLength - 2)*sizeof(char)); title[linkLength - 2] = 0; - char *permalink = map_get(titleMap, mapSize, title); + struct Page *linkedPage = map_get(pageMap, mapSize, title); char *compiledLink; - if (permalink[0] == 0) { + if (linkedPage == NULL) { // i.e. the page does not exist compiledLink = malloc(strlen(title) + 38); strcpy(compiledLink, ""); strcat(compiledLink, title); strcat(compiledLink, "\0"); } else { - compiledLink = malloc(strlen(title) + strlen(permalink) + 5); + compiledLink = malloc(strlen(title) + strlen(linkedPage->permalink) + 5); strcpy(compiledLink, "["); strcat(compiledLink, title); strcat(compiledLink, "]("); - strcat(compiledLink, permalink); + strcat(compiledLink, linkedPage->permalink); strcat(compiledLink, ")\0"); } @@ -368,15 +367,18 @@ int main(int argc, char *argv[]) { free(outputFileName); free(renderedPageContent); - // We no longer need this page's data so we deallocate it - struct Page *nextPage = currentPage->next; - free(currentPage->content); - free(currentPage->permalink); - free(currentPage); - currentPage = nextPage; + currentPage = currentPage->next; } /*** Deallocation and whatnot ***/ + currentPage = firstPage; + while (currentPage != NULL) { + free(currentPage->content); + free(currentPage->permalink); + struct Page *next = currentPage->next; + free(currentPage); + currentPage = next; + } closedir(pagesDir); free(templateContent);