-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcallbacks.c
148 lines (125 loc) · 3.6 KB
/
callbacks.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
/**
SFSEXP: Small, Fast S-Expression Library version 1.3
Written by Matthew Sottile ([email protected])
Copyright (2003-2006). The Regents of the University of California. This
material was produced under U.S. Government contract W-7405-ENG-36 for Los
Alamos National Laboratory, which is operated by the University of
California for the U.S. Department of Energy. The U.S. Government has rights
to use, reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
THE UNIVERSITY MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY
LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to produce
derivative works, such modified software should be clearly marked, so as not
to confuse it with the version available from LANL.
Additionally, this library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, U SA
LA-CC-04-094
**/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "sexp.h"
#include <sys/time.h>
#include <time.h>
#include <assert.h>
#include <string.h>
typedef enum { WAITING, NEXT_EDGE, GET_X1, GET_X2, GET_Y1, GET_Y2 } state_t;
static double x1,x2,y1,y2;
static state_t current_state = WAITING;
static int depth = 0;
void add_edge(double ax, double ay, double bx, double by) {
printf("EDGE DEFINED FROM (%f,%f) to (%f,%f)\n",ax,ay,bx,by);
}
void enter_sexpr() {
depth++;
switch (current_state) {
case WAITING:
current_state = NEXT_EDGE;
break;
case NEXT_EDGE:
case GET_X1:
current_state = GET_X1;
break;
case GET_X2:
current_state = GET_X2;
break;
default:
printf("This is unexpected.\n");
break;
}
}
void exit_sexpr() {
depth--;
switch (current_state) {
case GET_Y1:
current_state = GET_X2;
break;
case GET_Y2:
add_edge(x1,y1,x2,y2);
case NEXT_EDGE:
current_state = NEXT_EDGE;
break;
default:
printf("This is also unexpected.\n");
break;
}
if (depth == 0) {
printf("All edges for room found.\n");
current_state = WAITING;
}
}
void characters(const char *buf, size_t len, atom_t aty) {
switch (current_state) {
case NEXT_EDGE:
printf("%s\n",buf);
break;
case GET_X1:
x1 = strtod(buf,NULL);
current_state = GET_Y1;
break;
case GET_X2:
x2 = strtod(buf,NULL);
current_state = GET_Y2;
break;
case GET_Y1:
y1 = strtod(buf,NULL);
break;
case GET_Y2:
y2 = strtod(buf,NULL);
break;
default:
printf("Also not expected.\n");
break;
}
}
int main(int argc, char **argv) {
parser_event_handlers_t peh_t;
int fd;
sexp_iowrap_t *iow = NULL;
sexp_t *sx = NULL;
current_state = WAITING;
peh_t.start_sexpr = enter_sexpr;
peh_t.end_sexpr = exit_sexpr;
peh_t.characters = characters;
fd = open("edgelist.dat", O_RDONLY);
iow = init_iowrap(fd);
iow->cc = init_continuation(NULL);
iow->cc->event_handlers = &peh_t;
do {
if (sx != NULL)
destroy_sexp(sx);
sx = read_one_sexp(iow);
} while (sx != NULL);
destroy_iowrap(iow);
sexp_cleanup();
close(fd);
exit(EXIT_SUCCESS);
}