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