From 3e1cec290e04799d1596e79d4ca467e15a5d065a Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 5 Jun 2020 21:40:36 -0400 Subject: changed base name from :root to : --- nodelink.c | 2 +- read.c | 2 +- template | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/nodelink.c b/nodelink.c index 2838593..8c98690 100644 --- a/nodelink.c +++ b/nodelink.c @@ -31,7 +31,7 @@ static void printeach(strbstnode* loc, char reprint) { if (!reprint) printf(" "); // indent on subnode printlink(loc->ind, loc->data); // prints it // and subnodes if iterating over root tree (except root link) - if (reprint && strcmp(loc->ind,"root")) { + if (reprint && strcmp(loc->ind,"")) { // prints the link's target's link tree printeach( ( (link*)loc->data)->to->links->head, 0); } diff --git a/read.c b/read.c index 37fefb0..b9e26aa 100644 --- a/read.c +++ b/read.c @@ -94,7 +94,7 @@ node* readfile(char* name) { FILE* read = fopen(name, "r"); node *root, *cur; cur = root = newnode(); - link* curl = addbstlink(root->links, "root", root); + link* curl = addbstlink(root->links, "", root); llnode start; start.next = NULL; // empty desc isn't undefined diff --git a/template b/template index bb2aa73..912188a 100644 --- a/template +++ b/template @@ -19,3 +19,6 @@ 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' + +: +should append -- cgit From 6efdaa8ac39129bfd31ec14cae1075075688ab95 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Sun, 14 Jun 2020 12:30:18 -0400 Subject: switched nl handling to optional (=1 or 0) --- nodelink.c | 2 +- read.c | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nodelink.c b/nodelink.c index 8c98690..b8dbb91 100644 --- a/nodelink.c +++ b/nodelink.c @@ -20,7 +20,7 @@ link* newlink(node* to) { static void printlink(char* name, link* conn) { if (conn->desc[0]) - printf("%s: %s\n", name, conn->desc); + printf("%s: %s", name, conn->desc); else printf("%s\n", name); } diff --git a/read.c b/read.c index b9e26aa..5d7602d 100644 --- a/read.c +++ b/read.c @@ -15,13 +15,12 @@ static void consumespaces(FILE* file) { ungetc(c, file); } -static void wsclean(char* txt, size_t sz){ // "string\n \n" -> "string" - size_t i; - for (i = sz; i > 0; i--) { // start at end [i-1] and dec - // break on non-nl, non-space char - if (txt[i-1] != ' ' && txt[i-1] != '\n') break; +static void wsclean(char* txt, size_t* sz){ + // "string\n \n" -> "string\n" (must always end with \n) + for (; *sz > 0; (*sz)--) { // start at end [i-1] and dec + if (txt[*sz-1] != ' ' && txt[*sz-1] != '\n') break; } - txt[i] = 0; // finish string + txt[*sz] = '\n'; txt[*sz+1] = 0;// finish string } static char* empty(void) { @@ -47,16 +46,16 @@ static char* getline(size_t* n, FILE* file) { return realloc(out, sizeof(char) * (*n+1) ); } -static char* line(FILE* file) { // gets a line from file +static char* line(FILE* file, bool nl) { // gets a line from file size_t n; char* link = getline(&n, file); - // getline stores a line of size n in link - wsclean(link, n); // removes trailing whitespace - if (*link == '\n') { // empty line treated as "" + // getline stores a line of len n (size n+1) in link + wsclean(link, &n); // removes trailing whitespace + if (n == 1) { // "\n" => "" free(link); - char* out = empty(); // DEBGUGU - return out; + return empty(); } else { // else return obtained + if (!nl) link[n] = 0; //truncate the newline return link; } } @@ -113,12 +112,13 @@ node* readfile(char* name) { } switch (c) { case ':': - curl = insorget(root->links, line(read)); + curl = insorget(root->links, line(read, false)); cur = curl->to; break; case '-': ; - char* lname = line(read); + char* lname = line(read, false); + printf("%s.\n",lname); node* at = insorget(root->links, lname)->to; curl = addbstlink(cur->links, lname, at); break; @@ -126,7 +126,7 @@ node* readfile(char* name) { default: ungetc(c, read); case '.': - tail = appendll(tail, line(read)); + tail = appendll(tail, line(read, true)); sz += strlen(tail->str); break; } -- cgit From a126523db70b872795c488f947f8a9315f2d5118 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Sun, 14 Jun 2020 12:33:22 -0400 Subject: wsclean sets length to sz-2, so n == 0 --- read.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/read.c b/read.c index 5d7602d..f1ecea9 100644 --- a/read.c +++ b/read.c @@ -42,7 +42,6 @@ static char* getline(size_t* n, FILE* file) { if (c == '\n') break; } out[(*n)] = 0; - //(*n)++; return realloc(out, sizeof(char) * (*n+1) ); } @@ -51,7 +50,7 @@ static char* line(FILE* file, bool nl) { // gets a line from file char* link = getline(&n, file); // getline stores a line of len n (size n+1) in link wsclean(link, &n); // removes trailing whitespace - if (n == 1) { // "\n" => "" + if (n == 0) { // "\n" => "" free(link); return empty(); } else { // else return obtained -- cgit 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