From de0e8322a5166d932b2b211be64022598971abde Mon Sep 17 00:00:00 2001
From: Holden Rohrer
Date: Sun, 14 Jun 2020 12:54:19 -0400
Subject: stradd allows multiple node use to retain data
---
nodelink.c | 8 +++++++-
read.c | 12 ++++++++++--
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/nodelink.c b/nodelink.c
index b8dbb91..aaf8c18 100644
--- a/nodelink.c
+++ b/nodelink.c
@@ -11,9 +11,15 @@ node* newnode(void) {
return new;
}
+static char* empty(void) {
+ char* out = malloc(sizeof(char));
+ *out = 0;
+ return out;
+}
+
link* newlink(node* to) {
link* new = malloc(sizeof(link));
- new->desc = ""; // preferred to NULL because it can be printed
+ new->desc = empty(); // preferred to NULL because it can be printed
new->to = to;
return new;
}
diff --git a/read.c b/read.c
index f1ecea9..fee60ea 100644
--- a/read.c
+++ b/read.c
@@ -88,6 +88,14 @@ static char* resolvell(llnode* tail, size_t sz) {
return out;
}
+static void stradd(char** orig, char* new) { // new is freed
+ size_t nsz = strlen(new)+1;
+ size_t olen = strlen(*orig);
+ *orig = realloc(*orig,olen+nsz);
+ memcpy(*orig+olen,new,nsz);
+ free(new);
+}
+
node* readfile(char* name) {
FILE* read = fopen(name, "r");
node *root, *cur;
@@ -103,7 +111,7 @@ node* readfile(char* name) {
int c;
while ( ( c = fgetc(read) ) != EOF ) {
if (c == ':' || c == '-') {
- curl->desc = resolvell(head->next, sz);
+ stradd(&(curl->desc),resolvell(head->next, sz));
head->next = NULL;
tail = head;
sz = 0;
@@ -117,7 +125,6 @@ node* readfile(char* name) {
case '-': ;
char* lname = line(read, false);
- printf("%s.\n",lname);
node* at = insorget(root->links, lname)->to;
curl = addbstlink(cur->links, lname, at);
break;
@@ -130,6 +137,7 @@ node* readfile(char* name) {
break;
}
}
+ stradd(&(curl->desc),resolvell(head->next, sz));
fclose(read);
return root;
}
--
cgit