aboutsummaryrefslogtreecommitdiff
path: root/read.c
blob: e8968639b647865b46daa88e588fa94da21962b6 (plain)
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
#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;
}