diff options
author | Maximilian Szengel <gnunet@maxsz.de> | 2012-03-27 16:37:25 +0000 |
---|---|---|
committer | Maximilian Szengel <gnunet@maxsz.de> | 2012-03-27 16:37:25 +0000 |
commit | 70051c54bdc1a6a9aec011a6ef5372bf5c0f4692 (patch) | |
tree | b551472ff1ea635cdcfeacefbc08d997332a026a /src/regex/regex.c | |
parent | a3825c187b4f6acb08f0e5a0b5b0590dbe8ea9c8 (diff) | |
download | gnunet-70051c54bdc1a6a9aec011a6ef5372bf5c0f4692.tar.gz gnunet-70051c54bdc1a6a9aec011a6ef5372bf5c0f4692.zip |
formatting
Diffstat (limited to 'src/regex/regex.c')
-rw-r--r-- | src/regex/regex.c | 212 |
1 files changed, 122 insertions, 90 deletions
diff --git a/src/regex/regex.c b/src/regex/regex.c index 0300d31d6..bbfc71f1c 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c | |||
@@ -57,7 +57,7 @@ stack_pop (struct GNUNET_CONTAINER_SList *stack, size_t length) | |||
57 | } | 57 | } |
58 | 58 | ||
59 | void * | 59 | void * |
60 | stack_top (struct GNUNET_CONTAINER_SList *stack, size_t *len) | 60 | stack_top (struct GNUNET_CONTAINER_SList *stack, size_t * len) |
61 | { | 61 | { |
62 | struct GNUNET_CONTAINER_SList_Iterator it; | 62 | struct GNUNET_CONTAINER_SList_Iterator it; |
63 | 63 | ||
@@ -108,21 +108,22 @@ GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx) | |||
108 | } | 108 | } |
109 | ctx->state_id = 0; | 109 | ctx->state_id = 0; |
110 | ctx->transition_id = 0; | 110 | ctx->transition_id = 0; |
111 | ctx->stack = GNUNET_CONTAINER_slist_create(); | 111 | ctx->stack = GNUNET_CONTAINER_slist_create (); |
112 | } | 112 | } |
113 | 113 | ||
114 | void | 114 | void |
115 | GNUNET_REGEX_context_destroy (struct GNUNET_REGEX_Context *ctx) | 115 | GNUNET_REGEX_context_destroy (struct GNUNET_REGEX_Context *ctx) |
116 | { | 116 | { |
117 | if (NULL != ctx->stack) | 117 | if (NULL != ctx->stack) |
118 | GNUNET_CONTAINER_slist_destroy(ctx->stack); | 118 | GNUNET_CONTAINER_slist_destroy (ctx->stack); |
119 | } | 119 | } |
120 | 120 | ||
121 | void | 121 | void |
122 | debug_print_state (struct State *s) | 122 | debug_print_state (struct State *s) |
123 | { | 123 | { |
124 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "State %i: %s marked: %i accepting: %i\n", | 124 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
125 | s->id, s->name, s->marked, s->accepting); | 125 | "State %i: %s marked: %i accepting: %i\n", s->id, s->name, |
126 | s->marked, s->accepting); | ||
126 | } | 127 | } |
127 | 128 | ||
128 | void | 129 | void |
@@ -153,7 +154,7 @@ debug_print_transitions (struct State *s) | |||
153 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&it); | 154 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&it); |
154 | GNUNET_CONTAINER_slist_next (&it)) | 155 | GNUNET_CONTAINER_slist_next (&it)) |
155 | { | 156 | { |
156 | t = GNUNET_CONTAINER_slist_get(&it, NULL); | 157 | t = GNUNET_CONTAINER_slist_get (&it, NULL); |
157 | 158 | ||
158 | if (0 == t->literal) | 159 | if (0 == t->literal) |
159 | literal = '0'; | 160 | literal = '0'; |
@@ -165,14 +166,16 @@ debug_print_transitions (struct State *s) | |||
165 | else | 166 | else |
166 | state = t->state->name; | 167 | state = t->state->name; |
167 | 168 | ||
168 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id, literal, state); | 169 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id, |
170 | literal, state); | ||
169 | } | 171 | } |
170 | 172 | ||
171 | GNUNET_CONTAINER_slist_iter_destroy (&it); | 173 | GNUNET_CONTAINER_slist_iter_destroy (&it); |
172 | } | 174 | } |
173 | 175 | ||
174 | int | 176 | int |
175 | set_compare (const void *buf1, const size_t len1, const void *buf2, const size_t len2) | 177 | set_compare (const void *buf1, const size_t len1, const void *buf2, |
178 | const size_t len2) | ||
176 | { | 179 | { |
177 | int c1; | 180 | int c1; |
178 | int c2; | 181 | int c2; |
@@ -189,13 +192,12 @@ set_compare (const void *buf1, const size_t len1, const void *buf2, const size_t | |||
189 | int rslt; | 192 | int rslt; |
190 | int contains; | 193 | int contains; |
191 | 194 | ||
192 | if (len1 != len2 | 195 | if (len1 != len2 && len1 != sizeof (struct State) && |
193 | && len1 != sizeof (struct State) | 196 | len2 != sizeof (struct State)) |
194 | && len2 != sizeof (struct State)) | ||
195 | return 1; | 197 | return 1; |
196 | 198 | ||
197 | s1 = (struct State *)buf1; | 199 | s1 = (struct State *) buf1; |
198 | s2 = (struct State *)buf2; | 200 | s2 = (struct State *) buf2; |
199 | 201 | ||
200 | l1 = s1->nfa_set; | 202 | l1 = s1->nfa_set; |
201 | l2 = s2->nfa_set; | 203 | l2 = s2->nfa_set; |
@@ -203,7 +205,7 @@ set_compare (const void *buf1, const size_t len1, const void *buf2, const size_t | |||
203 | c1 = GNUNET_CONTAINER_slist_count (l1); | 205 | c1 = GNUNET_CONTAINER_slist_count (l1); |
204 | c2 = GNUNET_CONTAINER_slist_count (l2); | 206 | c2 = GNUNET_CONTAINER_slist_count (l2); |
205 | 207 | ||
206 | if (c1 != c2) | 208 | if (c1 != c2) |
207 | return ((c1 > c2) ? 1 : -1); | 209 | return ((c1 > c2) ? 1 : -1); |
208 | 210 | ||
209 | rslt = 0; | 211 | rslt = 0; |
@@ -223,10 +225,11 @@ set_compare (const void *buf1, const size_t len1, const void *buf2, const size_t | |||
223 | 225 | ||
224 | if (length1 != length2) | 226 | if (length1 != length2) |
225 | { | 227 | { |
226 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing lists failed, element size mismatch\n"); | 228 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
229 | "Comparing lists failed, element size mismatch\n"); | ||
227 | break; | 230 | break; |
228 | } | 231 | } |
229 | if (((struct State*)el1)->id == ((struct State*)el2)->id) | 232 | if (((struct State *) el1)->id == ((struct State *) el2)->id) |
230 | { | 233 | { |
231 | contains = 1; | 234 | contains = 1; |
232 | break; | 235 | break; |
@@ -246,32 +249,32 @@ set_compare (const void *buf1, const size_t len1, const void *buf2, const size_t | |||
246 | } | 249 | } |
247 | 250 | ||
248 | int | 251 | int |
249 | transition_literal_compare (const void *buf1, const size_t len1, const void *buf2, const size_t len2) | 252 | transition_literal_compare (const void *buf1, const size_t len1, |
253 | const void *buf2, const size_t len2) | ||
250 | { | 254 | { |
251 | struct Transition *t1; | 255 | struct Transition *t1; |
252 | struct Transition *t2; | 256 | struct Transition *t2; |
253 | 257 | ||
254 | if (len1 != len2 | 258 | if (len1 != len2 && len1 != sizeof (struct Transition) && |
255 | && len1 != sizeof (struct Transition) | 259 | len2 != sizeof (struct Transition)) |
256 | && len2 != sizeof (struct Transition)) | ||
257 | { | 260 | { |
258 | return 1; | 261 | return 1; |
259 | } | 262 | } |
260 | 263 | ||
261 | t1 = (struct Transition *)buf1; | 264 | t1 = (struct Transition *) buf1; |
262 | t2 = (struct Transition *)buf2; | 265 | t2 = (struct Transition *) buf2; |
263 | 266 | ||
264 | if (t1->literal == t2->literal) | 267 | if (t1->literal == t2->literal) |
265 | return 0; | 268 | return 0; |
266 | else if (t1->literal > t2->literal) | 269 | else if (t1->literal > t2->literal) |
267 | return 1; | 270 | return 1; |
268 | else | 271 | else |
269 | return -1; | 272 | return -1; |
270 | } | 273 | } |
271 | 274 | ||
272 | void | 275 | void |
273 | add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state, const char literal, | 276 | add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state, |
274 | struct State *to_state) | 277 | const char literal, struct State *to_state) |
275 | { | 278 | { |
276 | struct Transition t; | 279 | struct Transition t; |
277 | 280 | ||
@@ -285,13 +288,14 @@ add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state, cons | |||
285 | t.literal = literal; | 288 | t.literal = literal; |
286 | t.state = to_state; | 289 | t.state = to_state; |
287 | 290 | ||
288 | GNUNET_CONTAINER_slist_add (from_state->transitions, | 291 | GNUNET_CONTAINER_slist_add (from_state->transitions, |
289 | GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, | 292 | GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, &t, |
290 | &t, sizeof t); | 293 | sizeof t); |
291 | } | 294 | } |
292 | 295 | ||
293 | struct State * | 296 | struct State * |
294 | dfa_create_state (struct GNUNET_REGEX_Context *ctx, struct GNUNET_CONTAINER_SList *states) | 297 | dfa_create_state (struct GNUNET_REGEX_Context *ctx, |
298 | struct GNUNET_CONTAINER_SList *states) | ||
295 | { | 299 | { |
296 | struct State *s; | 300 | struct State *s; |
297 | char *name; | 301 | char *name; |
@@ -304,7 +308,7 @@ dfa_create_state (struct GNUNET_REGEX_Context *ctx, struct GNUNET_CONTAINER_SLis | |||
304 | s = GNUNET_malloc (sizeof (struct State)); | 308 | s = GNUNET_malloc (sizeof (struct State)); |
305 | s->id = ctx->state_id++; | 309 | s->id = ctx->state_id++; |
306 | s->accepting = 0; | 310 | s->accepting = 0; |
307 | s->transitions = GNUNET_CONTAINER_slist_create(); | 311 | s->transitions = GNUNET_CONTAINER_slist_create (); |
308 | s->marked = 0; | 312 | s->marked = 0; |
309 | s->name = NULL; | 313 | s->name = NULL; |
310 | 314 | ||
@@ -322,9 +326,9 @@ dfa_create_state (struct GNUNET_REGEX_Context *ctx, struct GNUNET_CONTAINER_SLis | |||
322 | strcat (s->name, "{"); | 326 | strcat (s->name, "{"); |
323 | name = NULL; | 327 | name = NULL; |
324 | 328 | ||
325 | for (stateit = GNUNET_CONTAINER_slist_begin(states); | 329 | for (stateit = GNUNET_CONTAINER_slist_begin (states); |
326 | GNUNET_NO == GNUNET_CONTAINER_slist_end (&stateit); | 330 | GNUNET_NO == GNUNET_CONTAINER_slist_end (&stateit); |
327 | GNUNET_CONTAINER_slist_next (&stateit)) | 331 | GNUNET_CONTAINER_slist_next (&stateit)) |
328 | { | 332 | { |
329 | cstate = GNUNET_CONTAINER_slist_get (&stateit, NULL); | 333 | cstate = GNUNET_CONTAINER_slist_get (&stateit, NULL); |
330 | GNUNET_CONTAINER_slist_iter_destroy (&tranit); | 334 | GNUNET_CONTAINER_slist_iter_destroy (&tranit); |
@@ -340,13 +344,15 @@ dfa_create_state (struct GNUNET_REGEX_Context *ctx, struct GNUNET_CONTAINER_SLis | |||
340 | } | 344 | } |
341 | 345 | ||
342 | // Add a transition for each distinct literal to NULL state | 346 | // Add a transition for each distinct literal to NULL state |
343 | for (tranit = GNUNET_CONTAINER_slist_begin(cstate->transitions); | 347 | for (tranit = GNUNET_CONTAINER_slist_begin (cstate->transitions); |
344 | GNUNET_NO == GNUNET_CONTAINER_slist_end (&tranit); | 348 | GNUNET_NO == GNUNET_CONTAINER_slist_end (&tranit); |
345 | GNUNET_CONTAINER_slist_next (&tranit)) | 349 | GNUNET_CONTAINER_slist_next (&tranit)) |
346 | { | 350 | { |
347 | ctran = GNUNET_CONTAINER_slist_get(&tranit, NULL); | 351 | ctran = GNUNET_CONTAINER_slist_get (&tranit, NULL); |
348 | if (0 != ctran->literal | 352 | if (0 != ctran->literal && |
349 | && NULL == GNUNET_CONTAINER_slist_contains2 (s->transitions, ctran, sizeof *ctran, &transition_literal_compare)) | 353 | NULL == GNUNET_CONTAINER_slist_contains2 (s->transitions, ctran, |
354 | sizeof *ctran, | ||
355 | &transition_literal_compare)) | ||
350 | { | 356 | { |
351 | add_transition (ctx, s, ctran->literal, NULL); | 357 | add_transition (ctx, s, ctran->literal, NULL); |
352 | } | 358 | } |
@@ -357,11 +363,32 @@ dfa_create_state (struct GNUNET_REGEX_Context *ctx, struct GNUNET_CONTAINER_SLis | |||
357 | } | 363 | } |
358 | GNUNET_CONTAINER_slist_iter_destroy (&stateit); | 364 | GNUNET_CONTAINER_slist_iter_destroy (&stateit); |
359 | 365 | ||
360 | s->name[strlen (s->name)-1] = '}'; | 366 | s->name[strlen (s->name) - 1] = '}'; |
361 | 367 | ||
362 | return s; | 368 | return s; |
363 | } | 369 | } |
364 | 370 | ||
371 | void | ||
372 | dfa_clear_nfa_set (struct GNUNET_CONTAINER_SList *states) | ||
373 | { | ||
374 | struct GNUNET_CONTAINER_SList_Iterator it; | ||
375 | struct State *s; | ||
376 | |||
377 | for (it = GNUNET_CONTAINER_slist_begin (states); | ||
378 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&it); | ||
379 | GNUNET_CONTAINER_slist_next (&it)) | ||
380 | { | ||
381 | s = GNUNET_CONTAINER_slist_get (&it, NULL); | ||
382 | if (NULL != s->nfa_set) | ||
383 | { | ||
384 | GNUNET_CONTAINER_slist_destroy (s->nfa_set); | ||
385 | s->nfa_set = NULL; | ||
386 | } | ||
387 | } | ||
388 | |||
389 | GNUNET_CONTAINER_slist_iter_destroy (&it); | ||
390 | } | ||
391 | |||
365 | struct GNUNET_REGEX_Automaton * | 392 | struct GNUNET_REGEX_Automaton * |
366 | nfa_create (struct State *start, struct State *end) | 393 | nfa_create (struct State *start, struct State *end) |
367 | { | 394 | { |
@@ -377,13 +404,11 @@ nfa_create (struct State *start, struct State *end) | |||
377 | return n; | 404 | return n; |
378 | 405 | ||
379 | GNUNET_CONTAINER_slist_add (n->states, | 406 | GNUNET_CONTAINER_slist_add (n->states, |
380 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | 407 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, end, |
381 | end, | ||
382 | sizeof *end); | 408 | sizeof *end); |
383 | 409 | ||
384 | GNUNET_CONTAINER_slist_add (n->states, | 410 | GNUNET_CONTAINER_slist_add (n->states, |
385 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | 411 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, start, |
386 | start, | ||
387 | sizeof *start); | 412 | sizeof *start); |
388 | 413 | ||
389 | n->start = start; | 414 | n->start = start; |
@@ -393,7 +418,8 @@ nfa_create (struct State *start, struct State *end) | |||
393 | } | 418 | } |
394 | 419 | ||
395 | void | 420 | void |
396 | nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct GNUNET_CONTAINER_SList *states) | 421 | nfa_add_states (struct GNUNET_REGEX_Automaton *n, |
422 | struct GNUNET_CONTAINER_SList *states) | ||
397 | { | 423 | { |
398 | // This isn't very pretty. Would be better to use GNUNET_CONTAINER_slist_append, but | 424 | // This isn't very pretty. Would be better to use GNUNET_CONTAINER_slist_append, but |
399 | // this function adds to the beginning of dst, which currently breaks "pretty" | 425 | // this function adds to the beginning of dst, which currently breaks "pretty" |
@@ -422,7 +448,7 @@ nfa_create_state (struct GNUNET_REGEX_Context *ctx, int accepting) | |||
422 | s = GNUNET_malloc (sizeof (struct State)); | 448 | s = GNUNET_malloc (sizeof (struct State)); |
423 | s->id = ctx->state_id++; | 449 | s->id = ctx->state_id++; |
424 | s->accepting = accepting; | 450 | s->accepting = accepting; |
425 | s->transitions = GNUNET_CONTAINER_slist_create(); | 451 | s->transitions = GNUNET_CONTAINER_slist_create (); |
426 | s->marked = 0; | 452 | s->marked = 0; |
427 | s->name = NULL; | 453 | s->name = NULL; |
428 | GNUNET_asprintf (&s->name, "s%i", s->id); | 454 | GNUNET_asprintf (&s->name, "s%i", s->id); |
@@ -447,7 +473,7 @@ automaton_destroy_state (struct State *s) | |||
447 | if (NULL != s->name) | 473 | if (NULL != s->name) |
448 | GNUNET_free (s->name); | 474 | GNUNET_free (s->name); |
449 | if (NULL != s->nfa_set) | 475 | if (NULL != s->nfa_set) |
450 | GNUNET_CONTAINER_slist_destroy(s->nfa_set); | 476 | GNUNET_CONTAINER_slist_destroy (s->nfa_set); |
451 | GNUNET_free (s); | 477 | GNUNET_free (s); |
452 | } | 478 | } |
453 | 479 | ||
@@ -621,35 +647,36 @@ create_nfa_closure (struct GNUNET_CONTAINER_SList *states, const char literal) | |||
621 | // Add start state to closure only for epsilon closure | 647 | // Add start state to closure only for epsilon closure |
622 | if (0 == literal) | 648 | if (0 == literal) |
623 | { | 649 | { |
624 | GNUNET_CONTAINER_slist_add (cls, | 650 | GNUNET_CONTAINER_slist_add (cls, |
625 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | 651 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, s, |
626 | s, sizeof *s); | 652 | sizeof *s); |
627 | } | 653 | } |
628 | 654 | ||
629 | stack_push (cls_check, s, sizeof *s); | 655 | stack_push (cls_check, s, sizeof *s); |
630 | 656 | ||
631 | while (!stack_empty(cls_check)) | 657 | while (!stack_empty (cls_check)) |
632 | { | 658 | { |
633 | currentstate = stack_pop(cls_check, sizeof (struct State)); | 659 | currentstate = stack_pop (cls_check, sizeof (struct State)); |
634 | 660 | ||
635 | for (tranit = GNUNET_CONTAINER_slist_begin (currentstate->transitions); | 661 | for (tranit = GNUNET_CONTAINER_slist_begin (currentstate->transitions); |
636 | GNUNET_CONTAINER_slist_end (&tranit) != GNUNET_YES; | 662 | GNUNET_CONTAINER_slist_end (&tranit) != GNUNET_YES; |
637 | GNUNET_CONTAINER_slist_next (&tranit)) | 663 | GNUNET_CONTAINER_slist_next (&tranit)) |
638 | { | 664 | { |
639 | currenttransition = GNUNET_CONTAINER_slist_get (&tranit, NULL); | 665 | currenttransition = GNUNET_CONTAINER_slist_get (&tranit, NULL); |
640 | 666 | ||
641 | if (NULL != currenttransition->state | 667 | if (NULL != currenttransition->state && |
642 | && literal == currenttransition->literal) | 668 | literal == currenttransition->literal) |
643 | { | 669 | { |
644 | clsstate = currenttransition->state; | 670 | clsstate = currenttransition->state; |
645 | 671 | ||
646 | if (NULL == clsstate) | 672 | if (NULL == clsstate) |
647 | break; | 673 | break; |
648 | 674 | ||
649 | if (GNUNET_YES != GNUNET_CONTAINER_slist_contains (cls, clsstate, sizeof *clsstate)) | 675 | if (GNUNET_YES != |
676 | GNUNET_CONTAINER_slist_contains (cls, clsstate, sizeof *clsstate)) | ||
650 | { | 677 | { |
651 | GNUNET_CONTAINER_slist_add (cls, | 678 | GNUNET_CONTAINER_slist_add (cls, |
652 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | 679 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, |
653 | clsstate, sizeof *clsstate); | 680 | clsstate, sizeof *clsstate); |
654 | stack_push (cls_check, clsstate, sizeof *clsstate); | 681 | stack_push (cls_check, clsstate, sizeof *clsstate); |
655 | } | 682 | } |
@@ -667,7 +694,8 @@ create_nfa_closure (struct GNUNET_CONTAINER_SList *states, const char literal) | |||
667 | } | 694 | } |
668 | 695 | ||
669 | struct GNUNET_CONTAINER_SList * | 696 | struct GNUNET_CONTAINER_SList * |
670 | GNUNET_REGEX_move (struct GNUNET_REGEX_Automaton *a, struct State *s, const char literal) | 697 | GNUNET_REGEX_move (struct GNUNET_REGEX_Automaton *a, struct State *s, |
698 | const char literal) | ||
671 | { | 699 | { |
672 | struct GNUNET_CONTAINER_SList *l; | 700 | struct GNUNET_CONTAINER_SList *l; |
673 | struct GNUNET_CONTAINER_SList_Iterator it; | 701 | struct GNUNET_CONTAINER_SList_Iterator it; |
@@ -675,7 +703,7 @@ GNUNET_REGEX_move (struct GNUNET_REGEX_Automaton *a, struct State *s, const char | |||
675 | 703 | ||
676 | if (!GNUNET_CONTAINER_slist_contains (a->states, s, sizeof *s)) | 704 | if (!GNUNET_CONTAINER_slist_contains (a->states, s, sizeof *s)) |
677 | { | 705 | { |
678 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 706 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
679 | "State %s is not part of the given automaton", s->name); | 707 | "State %s is not part of the given automaton", s->name); |
680 | return NULL; | 708 | return NULL; |
681 | } | 709 | } |
@@ -710,7 +738,7 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len) | |||
710 | { | 738 | { |
711 | int altcount; | 739 | int altcount; |
712 | int atomcount; | 740 | int atomcount; |
713 | }*p; | 741 | } *p; |
714 | 742 | ||
715 | GNUNET_REGEX_context_init (&ctx); | 743 | GNUNET_REGEX_context_init (&ctx); |
716 | 744 | ||
@@ -822,8 +850,7 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len) | |||
822 | 850 | ||
823 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 851 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
824 | "Created NFA with %i States and a total of %i Transitions\n", | 852 | "Created NFA with %i States and a total of %i Transitions\n", |
825 | GNUNET_CONTAINER_slist_count (nfa->states), | 853 | GNUNET_CONTAINER_slist_count (nfa->states), ctx.transition_id); |
826 | ctx.transition_id); | ||
827 | 854 | ||
828 | GNUNET_REGEX_context_destroy (&ctx); | 855 | GNUNET_REGEX_context_destroy (&ctx); |
829 | 856 | ||
@@ -835,7 +862,9 @@ error: | |||
835 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg); | 862 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg); |
836 | GNUNET_free (p); | 863 | GNUNET_free (p); |
837 | while (!stack_empty (ctx.stack)) | 864 | while (!stack_empty (ctx.stack)) |
838 | GNUNET_REGEX_destroy_automaton (stack_pop (ctx.stack, sizeof (struct GNUNET_REGEX_Automaton))); | 865 | GNUNET_REGEX_destroy_automaton (stack_pop |
866 | (ctx.stack, | ||
867 | sizeof (struct GNUNET_REGEX_Automaton))); | ||
839 | GNUNET_REGEX_context_destroy (&ctx); | 868 | GNUNET_REGEX_context_destroy (&ctx); |
840 | return NULL; | 869 | return NULL; |
841 | } | 870 | } |
@@ -894,35 +923,37 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len) | |||
894 | nfa_set = create_nfa_closure (sset, 0); | 923 | nfa_set = create_nfa_closure (sset, 0); |
895 | GNUNET_CONTAINER_slist_destroy (sset); | 924 | GNUNET_CONTAINER_slist_destroy (sset); |
896 | dfa->start = dfa_create_state (&ctx, nfa_set); | 925 | dfa->start = dfa_create_state (&ctx, nfa_set); |
897 | GNUNET_CONTAINER_slist_add (dfa->states, GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | 926 | GNUNET_CONTAINER_slist_add (dfa->states, |
898 | dfa->start, sizeof *(dfa->start)); | 927 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, |
928 | dfa->start, sizeof *(dfa->start)); | ||
899 | stack_push (dfa_stack, dfa->start, sizeof *(dfa->start)); | 929 | stack_push (dfa_stack, dfa->start, sizeof *(dfa->start)); |
900 | 930 | ||
901 | while (!stack_empty(dfa_stack)) | 931 | while (!stack_empty (dfa_stack)) |
902 | { | 932 | { |
903 | dfa_state = stack_pop (dfa_stack, sizeof (struct State)); | 933 | dfa_state = stack_pop (dfa_stack, sizeof (struct State)); |
904 | 934 | ||
905 | for (tranit = GNUNET_CONTAINER_slist_begin(dfa_state->transitions); | 935 | for (tranit = GNUNET_CONTAINER_slist_begin (dfa_state->transitions); |
906 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&tranit); | 936 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&tranit); |
907 | GNUNET_CONTAINER_slist_next (&tranit)) | 937 | GNUNET_CONTAINER_slist_next (&tranit)) |
908 | { | 938 | { |
909 | currenttransition = GNUNET_CONTAINER_slist_get (&tranit, NULL); | 939 | currenttransition = GNUNET_CONTAINER_slist_get (&tranit, NULL); |
910 | 940 | ||
911 | if (0 != currenttransition->literal | 941 | if (0 != currenttransition->literal && NULL == currenttransition->state) |
912 | && NULL == currenttransition->state) | ||
913 | { | 942 | { |
914 | tmp = create_nfa_closure (dfa_state->nfa_set, currenttransition->literal); | 943 | tmp = create_nfa_closure (dfa_state->nfa_set, |
944 | currenttransition->literal); | ||
915 | nfa_set = create_nfa_closure (tmp, 0); | 945 | nfa_set = create_nfa_closure (tmp, 0); |
916 | new_dfa_state = dfa_create_state (&ctx, nfa_set); | 946 | new_dfa_state = dfa_create_state (&ctx, nfa_set); |
917 | GNUNET_CONTAINER_slist_destroy (tmp); | 947 | GNUNET_CONTAINER_slist_destroy (tmp); |
918 | 948 | ||
919 | state_contains = GNUNET_CONTAINER_slist_contains2 (dfa->states, | 949 | state_contains = |
920 | new_dfa_state, | 950 | GNUNET_CONTAINER_slist_contains2 (dfa->states, new_dfa_state, |
921 | sizeof *new_dfa_state, | 951 | sizeof *new_dfa_state, |
922 | &set_compare); | 952 | &set_compare); |
923 | if (NULL == state_contains) | 953 | if (NULL == state_contains) |
924 | { | 954 | { |
925 | GNUNET_CONTAINER_slist_add_end (dfa->states, GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | 955 | GNUNET_CONTAINER_slist_add_end (dfa->states, |
956 | GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, | ||
926 | new_dfa_state, sizeof *new_dfa_state); | 957 | new_dfa_state, sizeof *new_dfa_state); |
927 | stack_push (dfa_stack, new_dfa_state, sizeof *new_dfa_state); | 958 | stack_push (dfa_stack, new_dfa_state, sizeof *new_dfa_state); |
928 | currenttransition->state = new_dfa_state; | 959 | currenttransition->state = new_dfa_state; |
@@ -931,22 +962,23 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len) | |||
931 | currenttransition->state = state_contains; | 962 | currenttransition->state = state_contains; |
932 | } | 963 | } |
933 | } | 964 | } |
934 | 965 | ||
935 | GNUNET_CONTAINER_slist_iter_destroy (&tranit); | 966 | GNUNET_CONTAINER_slist_iter_destroy (&tranit); |
936 | } | 967 | } |
937 | GNUNET_CONTAINER_slist_destroy (dfa_stack); | 968 | GNUNET_CONTAINER_slist_destroy (dfa_stack); |
938 | GNUNET_REGEX_destroy_automaton (nfa); | 969 | GNUNET_REGEX_destroy_automaton (nfa); |
939 | GNUNET_REGEX_context_destroy (&ctx); | 970 | GNUNET_REGEX_context_destroy (&ctx); |
971 | dfa_clear_nfa_set (dfa->states); | ||
940 | 972 | ||
941 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 973 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created DFA with %i States\n", |
942 | "Created DFA with %i States\n", | ||
943 | GNUNET_CONTAINER_slist_count (dfa->states)); | 974 | GNUNET_CONTAINER_slist_count (dfa->states)); |
944 | 975 | ||
945 | return dfa; | 976 | return dfa; |
946 | } | 977 | } |
947 | 978 | ||
948 | void | 979 | void |
949 | GNUNET_REGEX_save_nfa_graph (struct GNUNET_REGEX_Automaton *n, const char *filename) | 980 | GNUNET_REGEX_save_nfa_graph (struct GNUNET_REGEX_Automaton *n, |
981 | const char *filename) | ||
950 | { | 982 | { |
951 | struct GNUNET_CONTAINER_SList_Iterator stateit; | 983 | struct GNUNET_CONTAINER_SList_Iterator stateit; |
952 | struct GNUNET_CONTAINER_SList_Iterator tranit; | 984 | struct GNUNET_CONTAINER_SList_Iterator tranit; |
@@ -998,9 +1030,9 @@ GNUNET_REGEX_save_nfa_graph (struct GNUNET_REGEX_Automaton *n, const char *filen | |||
998 | 1030 | ||
999 | s->marked = 1; | 1031 | s->marked = 1; |
1000 | 1032 | ||
1001 | for (tranit = GNUNET_CONTAINER_slist_begin(s->transitions); | 1033 | for (tranit = GNUNET_CONTAINER_slist_begin (s->transitions); |
1002 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&tranit); | 1034 | GNUNET_YES != GNUNET_CONTAINER_slist_end (&tranit); |
1003 | GNUNET_CONTAINER_slist_next (&tranit)) | 1035 | GNUNET_CONTAINER_slist_next (&tranit)) |
1004 | { | 1036 | { |
1005 | ctran = GNUNET_CONTAINER_slist_get (&tranit, NULL); | 1037 | ctran = GNUNET_CONTAINER_slist_get (&tranit, NULL); |
1006 | 1038 | ||
@@ -1014,13 +1046,13 @@ GNUNET_REGEX_save_nfa_graph (struct GNUNET_REGEX_Automaton *n, const char *filen | |||
1014 | 1046 | ||
1015 | if (ctran->literal == 0) | 1047 | if (ctran->literal == 0) |
1016 | { | 1048 | { |
1017 | GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n", s->name, | 1049 | GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n", |
1018 | ctran->state->name); | 1050 | s->name, ctran->state->name); |
1019 | } | 1051 | } |
1020 | else | 1052 | else |
1021 | { | 1053 | { |
1022 | GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n", s->name, | 1054 | GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n", |
1023 | ctran->state->name, ctran->literal); | 1055 | s->name, ctran->state->name, ctran->literal); |
1024 | } | 1056 | } |
1025 | 1057 | ||
1026 | fwrite (s_tran, strlen (s_tran), 1, p); | 1058 | fwrite (s_tran, strlen (s_tran), 1, p); |