- sync wait period with deamon
[oweals/gnunet.git] / src / regex / regex_test_lib.c
index 7a11fe05ece16ac560a39558392736c95f4dbf7a..56ccb3496885a88c088c2d805f182b690c233c26 100644 (file)
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
+
+/**
+ * Struct to hold the tree formed by prefix-combining the regexes.
+ */
 struct RegexCombineCtx {
+
+  /**
+   * Next node with same prefix but different token.
+   */
   struct RegexCombineCtx *next;
+
+  /**
+   * Prev node with same prefix but different token.
+   */
   struct RegexCombineCtx *prev;
 
+  /**
+   * First child node with same prefix and token.
+   */
   struct RegexCombineCtx *head;
+
+  /**
+   * Last child node.
+   */
   struct RegexCombineCtx *tail;
 
+  /**
+   * Token.
+   */
   char *s;
 };
 
+// static void
+// space (int n)
+// {
+//   int i;
+//   for (i = 0; i < n; i++)
+//     printf ("  ");
+// }
+// 
+// static void
+// debugctx (struct RegexCombineCtx *ctx, int level)
+// {
+//   struct RegexCombineCtx *p;
+//   space (level);
+//   if (NULL != ctx->s)
+//     printf ("'%s'\n", ctx->s);
+//   else
+//     printf ("NULL\n");
+//   for (p = ctx->head; NULL != p; p = p->next)
+//   {
+//     debugctx (p, level + 1);
+//   }
+// }
+
 
 /**
  * Extract a string from all prefix-combined regexes.
@@ -54,34 +99,50 @@ regex_combine (struct RegexCombineCtx *ctx)
   char *regex;
   char *tmp;
   char *s;
+  int opt;
 
-  if (NULL != ctx->s)
-    GNUNET_asprintf (&regex, "%s(", ctx->s);
-  else
-    regex = GNUNET_strdup ("(");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix: %s\n", regex);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s);
+  regex = GNUNET_strdup ("");
+  opt = GNUNET_NO;
   for (p = ctx->head; NULL != p; p = p->next)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding '%s' to innner %s\n", p->s, ctx->s);
     s = regex_combine (p);
-    GNUNET_asprintf (&tmp, "%s%s|", regex, s);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  total '%s'\n", s);
+    if (strlen(s) == 0)
+      opt = GNUNET_YES;
+    else
+    {
+      GNUNET_asprintf (&tmp, "%s%s|", regex, s);
+      GNUNET_free_non_null (regex);
+      regex = tmp;
+    }
     GNUNET_free_non_null (s);
-    GNUNET_free_non_null (regex);
-    regex = tmp;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  so far '%s' for inner %s\n", regex, ctx->s);
   }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex);
   len = strlen (regex);
-  if (1 == len)
+  if (0 == len)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n");
     GNUNET_free (regex);
-    return GNUNET_strdup ("");
+    return GNUNET_strdup (ctx->s);
   }
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-partial: %s\n", regex);
   if ('|' == regex[len - 1])
-    regex[len - 1] = ')';
-  if ('(' == regex[len - 1])
     regex[len - 1] = '\0';
 
+  if (NULL != ctx->s)
+  {
+    if (opt)
+      GNUNET_asprintf (&s, "%s[%s]", ctx->s, regex);
+    else
+      GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex);
+    GNUNET_free (regex);
+    regex = s;
+  }
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex);
   return regex;
 }
@@ -112,16 +173,16 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex)
       }
       else
       {
-        struct RegexCombineCtx *new;
-        new = GNUNET_malloc (sizeof (struct RegexCombineCtx));
-        new->s = GNUNET_strdup (&p->s[1]);
+        struct RegexCombineCtx *newctx;
+        newctx = GNUNET_malloc (sizeof (struct RegexCombineCtx));
+        newctx->s = GNUNET_strdup (&p->s[1]);
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s);
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s);
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex);
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", new->s);
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", newctx->s);
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest);
         p->s[1] = '\0'; /* dont realloc */
-        GNUNET_CONTAINER_DLL_insert (p->head, p->tail, new);
+        GNUNET_CONTAINER_DLL_insert (p->head, p->tail, newctx);
         regex_add (p, rest);
       }
       return;
@@ -152,7 +213,7 @@ regex_ctx_destroy (struct RegexCombineCtx *ctx)
     next = p->next;
     regex_ctx_destroy (p);
   }
-  GNUNET_free (ctx->s);
+  GNUNET_free_non_null (ctx->s); /* 's' on root node is null */
   GNUNET_free (ctx);
 }
 
@@ -181,6 +242,7 @@ GNUNET_REGEX_combine (char * const regexes[])
     regex_add (ctx, current);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n");
+  //debugctx (ctx, 0);
 
   combined = regex_combine (ctx);
 
@@ -205,7 +267,7 @@ GNUNET_REGEX_read_from_file (const char *filename)
   struct GNUNET_DISK_FileHandle *f;
   unsigned int nr;
   unsigned int offset;
-  off_t size;
+  uint64_t size;
   size_t len;
   char *buffer;
   char *regex;
@@ -258,10 +320,11 @@ GNUNET_REGEX_read_from_file (const char *filename)
     else
     {
       len -= 6;
-      buffer[len] = '\0';
+      regex[len] = '\0';
     }
     regex = GNUNET_realloc (regex, len + 1);
     GNUNET_array_grow (regexes, nr, nr + 1);
+    GNUNET_assert (NULL == regexes[nr - 2]);
     regexes[nr - 2] = regex;
     regexes[nr - 1] = NULL;
     regex = NULL;