1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "read.h"
#include "strbst.h"
#include "nodelink.h"
#include "ll.h"
static void consumespaces(FILE* file) {
// used to consume spaces in an item ("- list item" -> "list item")
int c;
while ( ( c = fgetc(file) ) != EOF && c == ' ' );
ungetc(c, file);
}
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[*sz] = '\n'; txt[*sz+1] = 0;// finish string
}
static char* empty(void) {
// "" with a freeable memory address
char* str = malloc(sizeof(char));
*str = 0;
return str;
}
static char* getline(size_t* n, FILE* file) {
size_t sz = 100;
*n = 0;
int c;
char* out = malloc(sizeof(char)*100);
while ( (c = fgetc(file)) != EOF ) {
if (*n == sz) out = realloc(out, sizeof(char) * (sz *= 2));
out[*n] = c;
(*n)++;
if (c == '\n') break;
}
out[(*n)] = 0;
return realloc(out, sizeof(char) * (*n+1) );
}
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 len n (size n+1) in link
wsclean(link, &n); // removes trailing whitespace
if (n == 0) { // "\n" => ""
free(link);
return empty();
} else { // else return obtained
if (!nl) link[n] = 0; //truncate the newline
return link;
}
}
static link* insorget(strbst* tgt, char* name) {
link* get = query(tgt, name);
if (!get) {
get = newlink(newnode());
insbst(tgt, name, get);
} else {
free(name);
}
return get;
}
static link* addbstlink(strbst* tree, char* name, node* to) {
link* l = newlink(to);
insbst(tree, name, l);
return l;
}
static char* resolvell(llnode* tail, size_t sz) {
char *out, *pos;
pos = out = malloc(sizeof(char)*(sz+1)); out[sz] = 0;
while (tail != NULL) {
size_t len = strlen(tail->str);
memcpy(pos, tail->str, len);
pos += len;
llnode* tmp = tail;
tail = tail->next;
free(tmp->str); free(tmp);
}
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);
llnode start;
start.next = NULL; // empty desc isn't undefined
llnode *head, *tail;
head = tail = &start;
size_t sz = 0;
int c;
while ( ( c = fgetc(read) ) != EOF ) {
if (c == ':' || c == '-') {
stradd(&(curl->desc),resolvell(head->next, sz));
head->next = NULL;
tail = head;
sz = 0;
consumespaces(read);
}
switch (c) {
case ':':
curl = insorget(root->links, line(read, false));
cur = curl->to;
break;
case '-': ;
char* lname = line(read, false);
node* at = insorget(root->links, lname)->to;
curl = addbstlink(cur->links, lname, at);
break;
default:
ungetc(c, read);
case '.':
tail = appendll(tail, line(read, true));
sz += strlen(tail->str);
break;
}
}
stradd(&(curl->desc),resolvell(head->next, sz));
fclose(read);
return root;
}
|