diff options
-rw-r--r-- | src/regex/regex.c | 87 |
1 files changed, 14 insertions, 73 deletions
diff --git a/src/regex/regex.c b/src/regex/regex.c index b9a104fae..2c8082608 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c | |||
@@ -169,15 +169,22 @@ nfa_create_state (int accepting) | |||
169 | } | 169 | } |
170 | 170 | ||
171 | void | 171 | void |
172 | nfa_destroy_state (struct State *s) | ||
173 | { | ||
174 | if (s->tcnt > 0) | ||
175 | GNUNET_free (s->transitions); | ||
176 | GNUNET_free (s); | ||
177 | } | ||
178 | |||
179 | void | ||
172 | nfa_add_transition (struct State *from_state, const char literal, | 180 | nfa_add_transition (struct State *from_state, const char literal, |
173 | struct State *to_state) | 181 | struct State *to_state) |
174 | { | 182 | { |
175 | struct Transition t; | 183 | struct Transition t; |
176 | 184 | ||
177 | if (NULL == to_state) | 185 | if (NULL == from_state || NULL == to_state) |
178 | { | 186 | { |
179 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 187 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n"); |
180 | "Could not create Transition. to_state was NULL.\n"); | ||
181 | return; | 188 | return; |
182 | } | 189 | } |
183 | 190 | ||
@@ -203,75 +210,6 @@ mark_all_states (struct GNUNET_REGEX_Nfa *n, int visited) | |||
203 | } | 210 | } |
204 | 211 | ||
205 | void | 212 | void |
206 | print_states (struct GNUNET_REGEX_Nfa *n, char **out_str) | ||
207 | { | ||
208 | struct State *s; | ||
209 | int i_s; | ||
210 | int i_t; | ||
211 | char *s_all; | ||
212 | |||
213 | mark_all_states (n, 0); | ||
214 | |||
215 | s_all = GNUNET_malloc (sizeof (char)); | ||
216 | *s_all = '\0'; | ||
217 | |||
218 | for (i_s = 0; i_s < n->statecnt; i_s++) | ||
219 | { | ||
220 | struct Transition *ctran; | ||
221 | char *s_acc = NULL; | ||
222 | char *s_tran = NULL; | ||
223 | |||
224 | s = n->states[i_s]; | ||
225 | |||
226 | if (s->accepting) | ||
227 | { | ||
228 | GNUNET_asprintf (&s_acc, "s%i [shape=doublecircle];\n", s->id); | ||
229 | |||
230 | s_all = GNUNET_realloc (s_all, strlen (s_all) + strlen (s_acc) + 1); | ||
231 | strcat (s_all, s_acc); | ||
232 | GNUNET_free (s_acc); | ||
233 | } | ||
234 | |||
235 | ctran = s->transitions; | ||
236 | s->visited = 1; | ||
237 | |||
238 | for (i_t = 0; i_t < s->tcnt && NULL != s->transitions; i_t++) | ||
239 | { | ||
240 | if (NULL == ctran) | ||
241 | { | ||
242 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "s->transitions was NULL\n"); | ||
243 | } | ||
244 | |||
245 | if (NULL == ctran->state) | ||
246 | { | ||
247 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
248 | "Transition from State %i has has no state for transitioning\n", | ||
249 | s->id); | ||
250 | } | ||
251 | |||
252 | if (ctran->literal == 0) | ||
253 | { | ||
254 | GNUNET_asprintf (&s_tran, "s%i -> s%i [label = \"epsilon\"];\n", s->id, | ||
255 | ctran->state->id); | ||
256 | } | ||
257 | else | ||
258 | { | ||
259 | GNUNET_asprintf (&s_tran, "s%i -> s%i [label = \"%c\"];\n", s->id, | ||
260 | ctran->state->id, ctran->literal); | ||
261 | } | ||
262 | |||
263 | s_all = GNUNET_realloc (s_all, strlen (s_all) + strlen (s_tran) + 1); | ||
264 | strcat (s_all, s_tran); | ||
265 | GNUNET_free (s_tran); | ||
266 | |||
267 | ctran++; | ||
268 | } | ||
269 | } | ||
270 | |||
271 | *out_str = s_all; | ||
272 | } | ||
273 | |||
274 | void | ||
275 | nfa_add_concatenation () | 213 | nfa_add_concatenation () |
276 | { | 214 | { |
277 | struct GNUNET_REGEX_Nfa *A; | 215 | struct GNUNET_REGEX_Nfa *A; |
@@ -527,8 +465,11 @@ GNUNET_REGEX_destroy_nfa (struct GNUNET_REGEX_Nfa *n) | |||
527 | 465 | ||
528 | for (i = 0; i < n->statecnt; i++) | 466 | for (i = 0; i < n->statecnt; i++) |
529 | { | 467 | { |
530 | GNUNET_free (n->states[i]); | 468 | nfa_destroy_state (n->states[i]); |
531 | } | 469 | } |
470 | |||
471 | GNUNET_free (n->states); | ||
472 | GNUNET_free (n); | ||
532 | } | 473 | } |
533 | 474 | ||
534 | void | 475 | void |