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