diff options
| author | Holden Rohrer <hr@hrhr.dev> | 2020-07-09 20:34:40 -0400 | 
|---|---|---|
| committer | Holden Rohrer <hr@hrhr.dev> | 2020-07-09 20:34:40 -0400 | 
| commit | 47ff6a368e1925cdc6d9fa197252c62855ec2bab (patch) | |
| tree | 968600533820514e0551e440864a820ca6cefad8 | |
| parent | 991a37f06b096ded9720502f5b7ba78f42aa4dbd (diff) | |
added !<file> include syntax
improves file structures by including literally instead of parsing, and
example included in `template`
| -rw-r--r-- | read.c | 31 | ||||
| -rw-r--r-- | template | 1 | 
2 files changed, 28 insertions, 4 deletions
| @@ -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;          } @@ -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 | 
