aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-05-24 01:36:32 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-05-24 01:36:48 -0400
commit3263fcbb58566fc1fe1ec3891fccd858cb4e6894 (patch)
tree6cc6d1c1e9d4a5ae021e7e20d0ff2c90da1627b2
parentb9ecb91843036216323d66896e01be7a79e6f362 (diff)
added the framework for reading
-rw-r--r--read.c114
-rw-r--r--read.h9
2 files changed, 123 insertions, 0 deletions
diff --git a/read.c b/read.c
new file mode 100644
index 0000000..e896863
--- /dev/null
+++ b/read.c
@@ -0,0 +1,114 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "read.h"
+#include "strbst.h"
+#include "nodelink.h"
+#include "ll.h"
+
+void consumespaces(FILE* file) {
+ int c;
+ while ( ( c = fgetc(file) ) != EOF && c != ' ' );
+ ungetc(c, file);
+}
+
+void wsclean(char* str, size_t sz) { //
+ bool nl = false;
+ if (str[sz] == '\n') {
+ sz--;
+ nl = true;
+ }
+ size_t i;
+ for (i = sz-1; i >= 0 && str[i] == ' '; i--);
+ if (nl) {
+ str[i+1] = '\n';
+ str[i+2] = 0;
+ } else {
+ str[i+1] = 0;
+ }
+
+}
+
+char* empty(void) {
+ char* str = malloc(sizeof(char));
+ *str = 0;
+ return str;
+}
+
+char* line(FILE* file) {
+ char* link = NULL;
+ size_t n;
+ getline(&link, &n, file);
+ wsclean(link, n);
+ if (*link == '\n') {
+ free(link);
+ link = empty();
+ }
+ return link;
+}
+
+link* insorget(strbst* tgt, char* name) {
+ link* get = query(tgt, name);
+ if (!(*get)) {
+ get = newlink(newnode());
+ insbst(tgt, name, get);
+ }
+ return get;
+}
+
+char* getdesc(FILE* file) {
+ int c;
+ size_t sz;
+ llnode *head, *tail;
+ head = tail = appendll(NULL, NULL);
+ while ( (c = getc()) != EOF && c != ':' && c != '-' ) {
+ tail = tail->next = appendll(tail, line());
+ sz += strlen(tail->str); // this should be traced but I haven't
+ }
+ if (c != EOF) ungetc(c, file);
+ tail = head->next; free(head);
+ 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); free(tmp->str);
+ }
+ return pos;
+}
+
+node* readfile(char* name) {
+ FILE* read = fopen(name, "r");
+ node *root, *cur;
+ cur = root = newnode();
+ link* curl = insbst(root->links, name, root);
+ // getline
+ while ( ( int c = fgetc(read) ) != EOF ) {
+ switch (c) {
+ case ':':
+ consumespaces(file);
+ curl = insorget(root->links, line());
+ cur = curl->to;
+ break;
+
+ case '-':
+ consumespaces(file);
+ char* lname = line();
+ curl = insbst(cur->links, lname,
+ insorget(root->links, lname)->to);
+ break;
+
+ default:
+ ungetc(c, read);
+ curl->desc = getdesc();
+ break;
+ }
+ }
+ fclose(read);
+ return root;
+}
diff --git a/read.h b/read.h
new file mode 100644
index 0000000..c01d4e6
--- /dev/null
+++ b/read.h
@@ -0,0 +1,9 @@
+#ifndef __READ_H__
+#define __READ_H__
+
+#include "strbst.h"
+#include "nodelink.h"
+
+node* readfile(char* name); // returns a root node with all as children
+
+#endif