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
|
out
|
||||||
pages
|
pages
|
||||||
template.html
|
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;
|
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];
|
char lowercased[80];
|
||||||
to_lower_case(lowercased, title);
|
to_lower_case(lowercased, title);
|
||||||
|
|
||||||
int index = hash_polynomial(mapSize, lowercased);
|
int index = hash_polynomial(mapSize, lowercased);
|
||||||
strncpy(map[index], permalink, 79);
|
map[index] = page;
|
||||||
map[index][79] = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char * map_get(char map[][80], int mapSize, char title[]) {
|
struct Page * map_get(struct Page **map, int mapSize, char title[]) {
|
||||||
char lowercased[80];
|
char lowercased[80];
|
||||||
to_lower_case(lowercased, title);
|
to_lower_case(lowercased, title);
|
||||||
|
|
||||||
int index = hash_polynomial(mapSize, lowercased);
|
int index = hash_polynomial(mapSize, lowercased);
|
||||||
return map[index];
|
struct Page *page = map[index];
|
||||||
|
|
||||||
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** Templating ***/
|
/*** Templating ***/
|
||||||
|
@ -157,10 +158,8 @@ int main(int argc, char *argv[]) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char titleMap[mapSize][80];
|
struct Page ** pageMap = malloc(mapSize * sizeof(struct Page *));
|
||||||
for (int i = 0; i < mapSize; i++) {
|
memset(pageMap, 0, mapSize * sizeof(struct Page *));
|
||||||
titleMap[i][0] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Contains some information about the current file picked from pagesDir
|
// Contains some information about the current file picked from pagesDir
|
||||||
struct dirent *fileEntry = readdir(pagesDir);
|
struct dirent *fileEntry = readdir(pagesDir);
|
||||||
|
@ -245,7 +244,7 @@ int main(int argc, char *argv[]) {
|
||||||
currentPage->title[min(titleLength, 80)] = 0;
|
currentPage->title[min(titleLength, 80)] = 0;
|
||||||
|
|
||||||
// Insert it into the hash map for lookup later
|
// 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
|
// Get ready to process the next page
|
||||||
fileEntry = readdir(pagesDir);
|
fileEntry = readdir(pagesDir);
|
||||||
|
@ -298,21 +297,21 @@ int main(int argc, char *argv[]) {
|
||||||
strncpy(title, nextLinkStart + 2, (linkLength - 2)*sizeof(char));
|
strncpy(title, nextLinkStart + 2, (linkLength - 2)*sizeof(char));
|
||||||
title[linkLength - 2] = 0;
|
title[linkLength - 2] = 0;
|
||||||
|
|
||||||
char *permalink = map_get(titleMap, mapSize, title);
|
struct Page *linkedPage = map_get(pageMap, mapSize, title);
|
||||||
char *compiledLink;
|
char *compiledLink;
|
||||||
|
|
||||||
if (permalink[0] == 0) {
|
if (linkedPage == NULL) {
|
||||||
// i.e. the page does not exist
|
// i.e. the page does not exist
|
||||||
compiledLink = malloc(strlen(title) + 38);
|
compiledLink = malloc(strlen(title) + 38);
|
||||||
strcpy(compiledLink, "<a class=\"calathea-404\" href=\"#\">");
|
strcpy(compiledLink, "<a class=\"calathea-404\" href=\"#\">");
|
||||||
strcat(compiledLink, title);
|
strcat(compiledLink, title);
|
||||||
strcat(compiledLink, "</a>\0");
|
strcat(compiledLink, "</a>\0");
|
||||||
} else {
|
} else {
|
||||||
compiledLink = malloc(strlen(title) + strlen(permalink) + 5);
|
compiledLink = malloc(strlen(title) + strlen(linkedPage->permalink) + 5);
|
||||||
strcpy(compiledLink, "[");
|
strcpy(compiledLink, "[");
|
||||||
strcat(compiledLink, title);
|
strcat(compiledLink, title);
|
||||||
strcat(compiledLink, "](");
|
strcat(compiledLink, "](");
|
||||||
strcat(compiledLink, permalink);
|
strcat(compiledLink, linkedPage->permalink);
|
||||||
strcat(compiledLink, ")\0");
|
strcat(compiledLink, ")\0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,15 +367,18 @@ int main(int argc, char *argv[]) {
|
||||||
free(outputFileName);
|
free(outputFileName);
|
||||||
free(renderedPageContent);
|
free(renderedPageContent);
|
||||||
|
|
||||||
// We no longer need this page's data so we deallocate it
|
currentPage = currentPage->next;
|
||||||
struct Page *nextPage = currentPage->next;
|
|
||||||
free(currentPage->content);
|
|
||||||
free(currentPage->permalink);
|
|
||||||
free(currentPage);
|
|
||||||
currentPage = nextPage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** Deallocation and whatnot ***/
|
/*** 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);
|
closedir(pagesDir);
|
||||||
free(templateContent);
|
free(templateContent);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue