diff options
-rw-r--r-- | nodelink.c | 12 | ||||
-rw-r--r-- | read.c | 43 | ||||
-rw-r--r-- | template | 3 |
3 files changed, 37 insertions, 21 deletions
@@ -11,16 +11,22 @@ 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; } 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); } @@ -31,7 +37,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); } @@ -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) { @@ -43,20 +42,19 @@ static char* getline(size_t* n, FILE* file) { if (c == '\n') break; } out[(*n)] = 0; - //(*n)++; 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 == 0) { // "\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; } } @@ -90,11 +88,19 @@ 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; 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 @@ -105,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; @@ -113,12 +119,12 @@ 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); node* at = insorget(root->links, lname)->to; curl = addbstlink(cur->links, lname, at); break; @@ -126,11 +132,12 @@ 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; } } + stradd(&(curl->desc),resolvell(head->next, sz)); fclose(read); return root; } @@ -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 |