aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--read.c31
-rw-r--r--template1
2 files changed, 28 insertions, 4 deletions
diff --git a/read.c b/read.c
index ec7c6c8..eeb5a2b 100644
--- a/read.c
+++ b/read.c
@@ -31,17 +31,18 @@ static char* empty(void) {
}
static char* getline(size_t* n, FILE* file) {
+ // Can probably be improved by using posix file apis
size_t sz = 100;
*n = 0;
int c;
- char* out = malloc(sizeof(char)*100);
+ char* out = malloc(sizeof(char)*sz);
while ( (c = fgetc(file)) != EOF ) {
- if (*n == sz) out = realloc(out, sizeof(char) * (sz *= 2));
+ if (*n == sz-1) out = realloc(out, sizeof(char) * (sz *= 2));
out[*n] = c;
(*n)++;
if (c == '\n') break;
}
- out[(*n)] = 0;
+ out[*n] = 0;
return realloc(out, sizeof(char) * (*n+1) );
}
@@ -130,16 +131,38 @@ node* readfile(char* name) {
cur = curl->to;
break;
- case '-': ;
+ case '-': ; // ': ;' (empty expr) needed before 'char*'
char* lname = line(read, false);
node* at = insorget(root->links, lname, false)->to;
curl = addbstlink(cur->links, lname, at, true);
break;
+ case '!': ;
+ char* l = line(read,false);
+ FILE* f = fopen(l,"r");
+ if (!f) {
+ printf("Failed to open %s\n",l);
+ break;
+ }
+ char* ln; size_t n;
+ while ( *(ln = getline(&n,f)) ) {
+ if (*ln=='\n'){
+ free(ln);
+ ln = empty();
+ sz -= n;
+ }
+ tail = appendll(tail, ln);
+ sz += n;
+ }
+ fclose(f);
+ break;
+
default:
ungetc(c, read);
case '.':
tail = appendll(tail, line(read, true));
+ // TODO: appendll should change strlen, and
+ // line() needs to return a size
sz += strlen(tail->str);
break;
}
diff --git a/template b/template
index 381044a..894006a 100644
--- a/template
+++ b/template
@@ -21,6 +21,7 @@ The United Nations has attempted, in ceasefires to get gov't to agree to
stop chemical warfare in exchange for enemies'
- Chemical Warfare
Append because I rememmberd more
+!template
:
should append