Replace titleMap with pageMap
Now, our hashmap holds pointers to the Page struct. This will be more useful for implementing incoming links and whatnot
This commit is contained in:
parent
d6ce11f3ce
commit
c7e413c6e5
|
@ -2,3 +2,7 @@ build
|
|||
out
|
||||
pages
|
||||
template.html
|
||||
|
||||
# Vim
|
||||
~*
|
||||
.*.sw*
|
||||
|
|
42
calathea.c
42
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, "<a class=\"calathea-404\" href=\"#\">");
|
||||
strcat(compiledLink, title);
|
||||
strcat(compiledLink, "</a>\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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue