forked from Kevin-P-Kerr/hnreader
/
xmlparser.c
166 lines (135 loc) · 3.1 KB
/
xmlparser.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
// this is a special-case xml parser for xml returned from hacker news
// if you use this for anything else it probably wont' work
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXVAL 10000
#define TRUE 1
#define FALSE 0
#define INVAL 100000
#define L_ARROW 128
#define R_ARROW 129
#define QUOTE 130
#define STRING 131
char input[INVAL];
typedef struct token {
int *type;
char *value;
} Token;
struct Lexer {
char *start;
char *end;
};
struct Lexer lexer;
int issymbol(char c) {
if ((c=='/') || (c == '&') || (c == '(') || (c == ')') || (c == '[') || (c == ']') || (c == '!') || (c == '?'))
return 1;
else
return 0;
}
int junk(char c) {
if (isspace(c))
return 1;
else
return 0;
}
void init_lexer(void) {
lexer.start = input;
if (junk(*lexer.start)) {
while (junk(*lexer.start))
++lexer.start;
}
lexer.end = lexer.start;
}
void GetInput(void) {
int i=0;
char c;
while (((c=getchar())!=EOF) && (i < INVAL)) {
input[i] = c;
++i;
}
if (c!=EOF) { // the input is too long
fprintf(stderr, "INPUT ERROR: INPUT TOO LONG\n");
exit(0);
}
else {
input[i] = '\0'; // this will help us exit gracefully
}
}
int GetDifference(void) {
int diff;
if ((*lexer.end == '<') || (*lexer.end == '>'))
++lexer.end;
else if ((isalnum(*lexer.end)) || (*lexer.end == '"') || (*lexer.end == '/') || (*lexer.end == ']') || (*lexer.end == '[') || (*lexer.end =='!') || (*lexer.end == '(')) {
while (((*lexer.end!='<') && (*lexer.end!='>')) && (*lexer.end!='\0'))
++lexer.end;
}
diff = lexer.end - lexer.start;
return diff;
}
void resetInput(void) {
lexer.start = lexer.end;
if (*lexer.end == '\0')
;
else {
while ((*lexer.start!='\0') && junk(*lexer.start))
++lexer.start;
lexer.end = lexer.start;
}
} // is this necessary anymore?
Token *GetToken(void) {
int diff = GetDifference();
Token *p = malloc(sizeof(Token));
if (*lexer.start == '<') {
p->type = malloc(sizeof(int));
*p->type = L_ARROW;
p->value = malloc((sizeof(char) * diff) + 1);
strncpy(p->value, lexer.start, diff);
p->value[diff + 1] = '\0';
}
else if (*lexer.start == '>') {
p->type = malloc(sizeof(int));
*p->type = R_ARROW;
p->value = malloc((sizeof(char) * diff) +1);
strncpy(p->value, lexer.start, diff);
p->value[diff + 1] = '\0';
}
else if (*lexer.start == '"') {
p->type = malloc(sizeof(int));
*p->type = QUOTE;
p->value = malloc((sizeof(char) * diff) + 1);
strncpy(p->value, lexer.start, diff);
p->value[diff + 1] = '\0';
}
else if ((isalnum(*lexer.start)) || (issymbol(*lexer.start))) {
p->type = malloc(sizeof(int));
*p->type = STRING;
p->value = malloc((sizeof(char) * diff) + 1);
strncpy(p->value, lexer.start, diff);
p->value[diff + 1] = '\0';
}
else {
fprintf(stderr, "GET TOKEN ERROR");
}
resetInput();
return p;
}
void printString(Token *p) {
int i=0;
while (p->value[i]!='\0') {
printf("%c", p->value[i]);
++i;
}
printf("\n");
}
main() {
GetInput();
init_lexer();
while(*lexer.start!='\0') {
Token *p = GetToken();
printf("%d\n", *p->type);
printString(p);
}
printf("END OF STREAM\n");
}