diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-06-14 21:03:36 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-06-14 21:03:36 -0400 |
commit | 80e87bb217dfa234e86ff747346a211b2484de88 (patch) | |
tree | 7b3444979f0a0dd637602e6ae6fe0e98bd8f1f60 | |
parent | 3ef0e73864d21e2cb96c1c03b09324a665de070e (diff) |
testing duplicate links and avoid freeing reused char*s
-rw-r--r-- | read.c | 21 | ||||
-rw-r--r-- | template | 2 |
2 files changed, 15 insertions, 8 deletions
@@ -59,19 +59,24 @@ static char* line(FILE* file, bool nl) { // gets a line from file } } -static link* insorget(strbst* tgt, char* name) { +static link* insorget(strbst* tgt, char* name, bool tofree) { link* get = query(tgt, name); if (!get) { get = newlink(newnode()); insbst(tgt, name, get); - } else { + } else if (tofree) { free(name); } return get; } -static link* addbstlink(strbst* tree, char* name, node* to) { - link* l = newlink(to); +static link* addbstlink(strbst* tree, char* name, node* to, bool f) { + link* l = query(tree, name); + if (l && f) { + free(name); + return l; + } + l = newlink(to); insbst(tree, name, l); return l; } @@ -102,7 +107,7 @@ node* readfile(char* name) { FILE* read = fopen(name, "r"); node *root, *cur; cur = root = newnode(); - link* curl = addbstlink(root->links, "", root); + link* curl = addbstlink(root->links, "", root, false); llnode start; start.next = NULL; // empty desc isn't undefined @@ -121,14 +126,14 @@ node* readfile(char* name) { } switch (c) { case ':': - curl = insorget(root->links, line(read, false)); + curl = insorget(root->links, line(read, false), true); cur = curl->to; break; case '-': ; char* lname = line(read, false); - node* at = insorget(root->links, lname)->to; - curl = addbstlink(cur->links, lname, at); + node* at = insorget(root->links, lname, false)->to; + curl = addbstlink(cur->links, lname, at, true); break; default: @@ -19,6 +19,8 @@ Civilians are being killed by chemicals outlawed by Geneva Convention. - Chemical Warfare The United Nations has attempted, in ceasefires to get gov't to agree to stop chemical warfare in exchange for enemies' +- Chemical Warfare +Append because I rememmberd more : should append |