forked from antirez/smaz
-
Notifications
You must be signed in to change notification settings - Fork 8
/
extract_instance.c
79 lines (70 loc) · 1.55 KB
/
extract_instance.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
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
if (argc<3) {
fprintf(stderr,"You need to indicate the form name and form instance on the command line.\n");
exit(-1);
}
char *form_name=argv[1];
FILE *f=fopen(argv[2],"r");
char tag[1024];
int taglen=0;
char value[1024];
int val_len=0;
int in_instance=0;
int interesting_tag=0;
int state=0;
int c=fgetc(f);
while(c>=-1&&(!feof(f))) {
switch(c) {
case '\n': case '\r': break;
case '<':
state=1;
if (interesting_tag&&val_len>0) {
value[val_len]=0;
printf("%s=%s\n",tag,value);
val_len=0;
}
interesting_tag=0;
break;
case '>':
if (taglen) {
// got a tag name
tag[taglen]=0;
interesting_tag=0;
if (tag[0]!='/'&&in_instance&&tag[taglen-1]!='/') {
interesting_tag=1;
}
if (!strncasecmp(form_name,tag,strlen(form_name)))
{
// if (!in_instance) printf("Found start of instance\n");
in_instance++;
}
if ((!strncasecmp(form_name,&tag[1],strlen(form_name)))
&&tag[0]=='/')
{
in_instance--;
// if (!in_instance) printf("Found end of instance\n");
}
taglen=0;
}
state=0; break; // out of a tag
default:
if (state==1) {
// in a tag specification, so accumulate name of tag
if (taglen<1000) tag[taglen++]=c;
}
if (interesting_tag) {
// exclude leading spaces from values
if (val_len||(c!=' ')) {
if (val_len<1000) value[val_len++]=c;
}
}
}
c= fgetc(f);
}
return 0;
}