diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-06-14 12:54:19 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-06-14 12:54:19 -0400 |
commit | de0e8322a5166d932b2b211be64022598971abde (patch) | |
tree | 052b435fb647310490453e93afde26eda00a3b40 | |
parent | a126523db70b872795c488f947f8a9315f2d5118 (diff) |
stradd allows multiple node use to retain data
-rw-r--r-- | nodelink.c | 8 | ||||
-rw-r--r-- | read.c | 12 |
2 files changed, 17 insertions, 3 deletions
@@ -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; } @@ -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; } |