-
Notifications
You must be signed in to change notification settings - Fork 0
/
heron.c
194 lines (183 loc) · 4.47 KB
/
heron.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#define isnode(x, s) (0==xmlStrcmp((x)->name, (xmlChar*)(s)))
#define isconcept(x) (0==xmlStrcmp((x)->name, (xmlChar*)"Concept"))
xmlNodePtr root;
typedef struct concept{
char* notation;
char* uri;
char* broaderuri;
//xmlNodePtr broader;
char *caption;
char *including;
char *application;
char *scope;
char *example;
//char *relateduri;
//xmlNodePtr *related;
} *con;
void checklang(char** dest, xmlNodePtr n)
{
xmlChar *lang;
lang = xmlGetProp(n, "lang");
if(xmlStrcmp(lang, (xmlChar*)"pt")==0) *dest = n->children->content;
else if(xmlStrcmp(lang, (xmlChar*)"en")==0 && *dest==NULL) *dest = n->children->content;
xmlFree(lang);
}
con xml2con(xmlNodePtr n)
{
con ret;
if(!isconcept(n)) return NULL;
ret = malloc(sizeof(struct concept));
memset((void*)ret, 0, sizeof(struct concept));
ret->uri = (char*) xmlGetProp(n, "about");
for(n = n->children; n !=NULL; n = n->next) {
if(isnode(n, "broader")) ret->broaderuri = (char*)xmlGetProp(n, "resource");
if(n->children == NULL) continue;
if(isnode(n, "notation")) ret->notation = (char*)n->children->content;
else if(isnode(n, "prefLabel")) checklang(&(ret->caption), n);
else if(isnode(n, "includingNote")) checklang(&(ret->including), n);
else if(isnode(n, "scopeNote")) checklang(&(ret->scope), n);
else if(isnode(n, "applicationNote")) checklang(&(ret->application), n);
else if(isnode(n, "example")) checklang(&(ret->example), n);
//else if(isnode(n, "related")) ret->relateduri = (char*)n->children->content;
}
return ret;
}
void freecon(con c)
{
if(c == NULL) return;
xmlFree(c->broaderuri);
free((void*)c);
}
con getfromuri(char *uri)
{
int count=0;
xmlNodePtr n = root;
con cc;
for(n = n->children; n!=NULL; n = n->next) {
if(!isconcept(n)) continue;
cc = xml2con(n);
if(0==strcmp(uri, cc->uri)) return cc;
if(cc!=NULL) freecon(cc);
}
}
void shortcon(con c)
{
assert(c!=NULL);
printf("\x1b[32m%s\x1b[0m %s\n", c->notation, c->caption);
}
void recursiveparent(con c)
{
con parent;
if(c->broaderuri==NULL) return;
parent = getfromuri(c->broaderuri);
if(parent==NULL) return;
recursiveparent(parent);
printf("\x1b[1m");
shortcon(parent);
freecon(parent);
}
#define field(s,ca) if(c->ca) printf("\x1b[1m" s ":\x1b[0m \t%s\n", c->ca)
con displaycon(con c)
{
xmlNodePtr n;
con child;
if(c==NULL) return c;
recursiveparent(c);
printf("\x1b[1mNotação: \t\x1b[32m%s\x1b[0m\n\x1b[1mURI:\x1b[0m \t\t%s\n", c->notation, c->uri);
//field("Broader",broaderuri);
if(c->caption) printf("\x1b[1mDescrição:\x1b[33m \t%s\x1b[0m\n", c->caption);
field("Incluindo", including);
field("Aplicação", application);
field("Escopo", scope);
field("Exemplo", example);
//field("Veja também", relateduri);
n = root;
for(n = n->children; n!=NULL; n = n->next) {
if(!isconcept(n)) continue;
child = xml2con(n);
if(child == NULL) continue;
if(child->broaderuri!=NULL) {
if(0==strcmp(child->broaderuri, c->uri)) {
printf("\x1b[1mVer também: \t");
shortcon(child);
}
}
freecon(child);
}
return c;
}
#undef field
con getfromnotation(char* notation)
{
int count=0;
xmlNodePtr n = root;
con cc;
assert(n!=NULL);
for(n = n->children; n!=NULL; n = n->next) {
if(!isconcept(n)) continue;
cc = xml2con(n);
if(0==strcmp(notation, cc->notation)) return cc;
if(cc!=NULL) freecon(cc);
}
return NULL;
}
/* Basic procedure template
void proc(xmlNodePtr n)
{
int count=0;
con cc;
assert(n!=NULL);
for(n = n->children; n!=NULL; n = n->next) {
if(!isconcept(n)) continue;
cc = xml2con(n);
if(count++ == 1240) displaycon(cc);
if(cc!=NULL) freecon(cc);
}
} */
int main(int argn, char*argv[])
{
xmlDocPtr doc;
doc = xmlParseFile("udcsummary-skos.rdf");
assert(doc!=NULL);
root = xmlDocGetRootElement(doc);
assert(root!=NULL);
if(argn>1) {
con res;
int i;
for(i=1; i<argn; i++) {
res = getfromnotation(argv[i]);
if(res == NULL) printf("Não encontrei %s\n", argv[i]);
else freecon(displaycon(res));
putchar('\n');
}
xmlFreeDoc(doc);
return 0;
}
while(1) {
char *entra;
con res;
entra = readline("> ");
if(entra==NULL) break;
res = getfromnotation(entra);
if(entra[0] == '\0') {
free(entra);
continue;
}
if(res == NULL) printf("Não encontrei %s\n", entra);
else {
freecon(displaycon(res));
add_history(entra);
}
free(entra);
}
printf("\nTchau\n");
xmlFreeDoc(doc);
return 0;
}