Simplify string construction processes

This commit is contained in:
Nat 2023-01-20 10:37:11 -08:00
parent 046e31f91e
commit 1bd489cdc5
Signed by: nat
GPG Key ID: B53AB05285D710D6
1 changed files with 49 additions and 42 deletions

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <dirent.h>
#include <math.h>
@ -133,6 +134,32 @@ void page_list_free(struct PageList *list) {
}
/*** Templating ***/
char * concat_strings(int n, ...) {
va_list list;
va_start(list, n);
int length = 0;
char **strings = malloc(n * sizeof(char *));
memset(strings, 0, n * sizeof(char *));
for (int i = 0; i < n; i++) {
strings[i] = va_arg(list, char *);
length += strlen(strings[i]);
}
char * joinedString = malloc((length + 1) * sizeof(char *));
memset(joinedString, 0, (length + 1) * sizeof(char *));
strcpy(joinedString, strings[0]);
for (int i = 1; i < n; i++) {
strcat(joinedString, strings[i]);
}
free(strings);
va_end(list);
return joinedString;
}
char * substitute_string(char dest[], char sub[], char *start, char *end) {
int startIndex = start - dest;
@ -242,22 +269,14 @@ int main(int argc, char *argv[]) {
currentPage->incoming = page_list(initialInboundCapacity);
// Build the page's permalink
currentPage->permalink = malloc(strlen(fileBasename) + 6);
strcpy(currentPage->permalink, fileBasename);
strcat(currentPage->permalink, ".html");
currentPage->permalink = concat_strings(2, fileBasename, ".html");
// Construct the relative path
// The two accounts for the slash and the terminal zero
char relativePath[strlen(pagesLocation) + filenameLength + 2];
memset(relativePath, 0, strlen(pagesLocation) + filenameLength + 2);
strcpy(relativePath, pagesLocation);
strcat(relativePath, "/");
strcat(relativePath,fileEntry->d_name);
relativePath[strlen(pagesLocation) + filenameLength + 1] = 0;
char *relativePath = concat_strings(3, pagesLocation, "/", fileEntry->d_name);
char *buffer = read_file(relativePath);
free(relativePath);
if (buffer == NULL) {
fileEntry = readdir(pagesDir);
@ -310,15 +329,8 @@ int main(int argc, char *argv[]) {
}
// Create the directory if it doesn't exist
char *createOutputDir = malloc(
(7 + strlen(outputDirectoryName)) * sizeof(char)
);
strcpy(createOutputDir, "mkdir \0");
strcat(createOutputDir, outputDirectoryName);
char *createOutputDir = concat_strings(2, "mkdir ", outputDirectoryName);
system(createOutputDir);
free(createOutputDir);
/*** Link Processing ***/
@ -346,22 +358,18 @@ int main(int argc, char *argv[]) {
struct Page *linkedPage = map_get(pageMap, mapSize, title);
char *compiledLink;
char *compiledLink = NULL;
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");
compiledLink = concat_strings(3,
"<a class=\"calathea-404\" href=\"#\">",
title,
"</a>"
);
} else {
page_list_insert(linkedPage->incoming, currentPage);
compiledLink = malloc(strlen(title) + strlen(linkedPage->permalink) + 5);
strcpy(compiledLink, "[");
strcat(compiledLink, title);
strcat(compiledLink, "](");
strcat(compiledLink, linkedPage->permalink);
strcat(compiledLink, ")\0");
compiledLink = concat_strings(5, "[", title, "](", linkedPage->permalink, ")");
}
char *newContent = substitute_string(
@ -421,11 +429,15 @@ int main(int argc, char *argv[]) {
strcpy(incomingLinksList, "<ul class=\"calathea-incoming\">\n");
for (int i = 0; i < currentPage->incoming->length; i++) {
struct Page *page = currentPage->incoming->pages[i];
strcat(incomingLinksList, " <li><a href=\"");
strcat(incomingLinksList, page->permalink);
strcat(incomingLinksList, "\">");
strcat(incomingLinksList, page->title);
strcat(incomingLinksList, "</a></li>\n");
char *link = concat_strings(5,
" <li><a href=\"",
page->permalink,
"\">",
page->title,
"</a></li>\n"
);
strcat(incomingLinksList, link);
free(link);
}
strcat(incomingLinksList, "</ul>\n");
@ -441,13 +453,8 @@ int main(int argc, char *argv[]) {
}
// Output the page to the file
char *outputFileName = malloc(
(strlen(outputDirectoryName)
+ strlen(currentPage->permalink)
+ 2));
strcpy(outputFileName, outputDirectoryName);
strcat(outputFileName, "/\0");
strcat(outputFileName, currentPage->permalink);
char *outputFileName = concat_strings(3,
outputDirectoryName, "/", currentPage->permalink);
FILE *outputFile = fopen(outputFileName, "w");