aboutsummaryrefslogtreecommitdiff
path: root/read.c
diff options
context:
space:
mode:
Diffstat (limited to 'read.c')
-rw-r--r--read.c43
1 files changed, 25 insertions, 18 deletions
diff --git a/read.c b/read.c
index 37fefb0..fee60ea 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) {
@@ -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;
}