function for commenting out rules with some annotation
authorChristian Thaeter <ct@pipapo.org>
Sun, 28 Oct 2007 04:20:37 +0000 (05:20 +0100)
committerChristian Thaeter <ct@pipapo.org>
Sun, 28 Oct 2007 04:20:37 +0000 (05:20 +0100)
src/rxpd.h
src/rxpd_rule.c

index 7fbbaa4..834656e 100644 (file)
@@ -143,6 +143,9 @@ rxpd_rule_new (const char* buf);
 struct rxpd_rule*
 rxpd_rule_copy (const struct rxpd_rule* src);
 
+struct rxpd_rule*
+rxpd_rule_comment (struct rxpd_rule* self, const char* comment);
+
 void
 rxpd_rule_delete (struct rxpd_rule* self);
 
index cd3aa0f..3fc9d67 100644 (file)
@@ -96,6 +96,42 @@ rxpd_rule_copy (const struct rxpd_rule* src)
   return self;
 }
 
+struct rxpd_rule*
+rxpd_rule_comment (struct rxpd_rule* self, const char* comment)
+{
+  if (self)
+    {
+      int len;
+      if (self->atime != -1)
+        len = snprintf (NULL, 0, "#%s: %ld:%s", comment, self->atime, self->string);
+      else if (self->string[0] != '#')
+        len = snprintf (NULL, 0, "#%s: :%s", comment, self->string);
+      else
+        len = snprintf (NULL, 0, "#%s: %s", comment, self->string);
+
+      if (len < 0)
+        return NULL;
+
+      char* dst = rxpd_malloc (len+1);
+
+      if (self->atime != -1)
+        snprintf (dst, len+1, "#%s: %ld:%s", comment, self->atime, self->string);
+      else if (self->string[0] != '#')
+        snprintf (dst, len+1, "#%s: :%s", comment, self->string);
+      else
+        snprintf (dst, len+1, "#%s: %s", comment, self->string);
+
+      if (self->string[0] != '#')
+        regfree (&self->rx);
+
+      self->atime = -1;
+
+      free (self->string);
+      self->string = dst;
+    }
+  return self;
+}
+
 void
 rxpd_rule_delete (struct rxpd_rule* rule)
 {