command SAVE
authorChristian Thaeter <ct@pipapo.org>
Mon, 8 Oct 2007 16:48:57 +0000 (18:48 +0200)
committerChristian Thaeter <ct@pipapo.org>
Mon, 8 Oct 2007 16:48:57 +0000 (18:48 +0200)
rxpd.c
rxpd.h

diff --git a/rxpd.c b/rxpd.c
index 1440882..924a4e0 100644 (file)
--- a/rxpd.c
+++ b/rxpd.c
@@ -210,6 +210,26 @@ rxpd_file_load (struct rxpd_file* self)
     return 0;
 }
 
+int
+rxpd_file_save (struct rxpd_file* self)
+{
+  FILE* f = fopen (self->filename, "w");
+  // TODO error handling
+  if (f)
+    {
+      LLIST_FOREACH (&self->rules, n)
+        {
+          struct rxpd_rule* node = (struct rxpd_rule*)n;
+          fprintf (f, "%s\n", node->string);
+        }
+
+      fclose (f);
+      return 1;
+    }
+  else
+    return 0;
+}
+
 int
 rxpd_file_cmp (const void* A, const void* B)
 {
@@ -737,7 +757,24 @@ void
 rxpd_connection_cmd_SAVE (int fd, short event, void* ptr)
 {
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
+
+  if (!event)
+    {
+      if (self->file)
+        {
+          if (rxpd_file_save (self->file))
+            {
+              rxpd_buffer_printf (&self->out, "#OK:\n");
+            }
+          else
+            {
+              rxpd_buffer_printf (&self->out, "#ERROR: saving file '%s'\n", (const char*)self->file->node.key);
+            }
+        }
+      else
+        rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
+      close (fd);
+    }
 }
 
 void
diff --git a/rxpd.h b/rxpd.h
index 07beb28..fceeacb 100644 (file)
--- a/rxpd.h
+++ b/rxpd.h
@@ -116,6 +116,9 @@ rxpd_file_delete (PSplay file);
 int
 rxpd_file_load (struct rxpd_file* self);
 
+int
+rxpd_file_save (struct rxpd_file* self);
+
 int
 rxpd_file_cmp (const void* A, const void* B);