src/rrd_args.[ch]: Implement data structure for more powerful RRD argument construction.
authorFlorian Forster <ff@octo.it>
Fri, 2 Jul 2010 17:08:13 +0000 (19:08 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 2 Jul 2010 17:08:13 +0000 (19:08 +0200)
src/Makefile.am
src/rrd_args.c [new file with mode: 0644]
src/rrd_args.h [new file with mode: 0644]

index ed5393a..0f86311 100644 (file)
@@ -28,5 +28,6 @@ collection_fcgi_SOURCES = main.c \
                          graph_ident.c graph_ident.h \
                          graph_instance.c graph_instance.h \
                          graph_list.c graph_list.h \
+                         rrd_args.c rrd_args.h \
                          utils_array.c utils_array.h \
                          utils_cgi.c utils_cgi.h
diff --git a/src/rrd_args.c b/src/rrd_args.c
new file mode 100644 (file)
index 0000000..f5358c8
--- /dev/null
@@ -0,0 +1,105 @@
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "rrd_args.h"
+
+rrd_args_t *ra_create (void) /* {{{ */
+{
+  rrd_args_t *ra;
+
+  ra = malloc (sizeof (*ra));
+  if (ra == NULL)
+    return (NULL);
+  memset (ra, 0, sizeof (*ra));
+
+  ra->options = array_create ();
+  ra->data    = array_create ();
+  ra->calc    = array_create ();
+  ra->draw    = array_create ();
+
+  if ((ra->options == NULL)
+      || (ra->data == NULL)
+      || (ra->calc == NULL)
+      || (ra->draw == NULL))
+  {
+    ra_destroy (ra);
+    return (NULL);
+  }
+
+  return (ra);
+} /* }}} rrd_args_t *ra_create */
+
+void ra_destroy (rrd_args_t *ra) /* {{{ */
+{
+  if (ra == NULL)
+    return;
+
+  array_destroy (ra->options);
+  array_destroy (ra->data);
+  array_destroy (ra->calc);
+  array_destroy (ra->draw);
+
+  free (ra);
+} /* }}} void ra_destroy */
+
+int ra_argc (rrd_args_t *ra)
+{
+  if (ra == NULL)
+    return (-EINVAL);
+
+  return (array_argc (ra->options)
+      + array_argc (ra->data)
+      + array_argc (ra->calc)
+      + array_argc (ra->draw));
+} /* }}} int ra_argc */
+
+char **ra_argv (rrd_args_t *ra) /* {{{ */
+{
+  size_t argc;
+  char **argv;
+
+  size_t pos;
+  int tmp;
+
+  if (ra == NULL)
+    return (NULL);
+
+  tmp = ra_argc (ra);
+  if (tmp < 0)
+    return (NULL);
+  argc = (size_t) tmp;
+
+  argv = calloc (argc + 1, sizeof (*argv));
+  if (argv == NULL)
+    return (NULL);
+
+  pos = 0;
+
+#define APPEND_FIELD(field) do                                               \
+{                                                                            \
+  size_t ary_argc;                                                           \
+  char **ary_argv;                                                           \
+                                                                             \
+  ary_argc = (size_t) array_argc (ra->field);                                \
+  ary_argv = array_argv (ra->field);                                         \
+  if ((ary_argc > 0) && (ary_argv != NULL))                                  \
+  {                                                                          \
+    memcpy (argv + pos, ary_argv, ary_argc * sizeof (*ary_argv));            \
+    pos += ary_argc;                                                         \
+    argv[pos] = NULL;                                                        \
+  }                                                                          \
+  free (ary_argv);                                                           \
+} while (0)
+  APPEND_FIELD (options);
+  APPEND_FIELD (data);
+  APPEND_FIELD (calc);
+  APPEND_FIELD (draw);
+
+#undef APPEND_FIELD
+
+  return (argv);
+} /* }}} char **ra_argv */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/src/rrd_args.h b/src/rrd_args.h
new file mode 100644 (file)
index 0000000..4bfeea7
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef RRD_ARGS_H
+#define RRD_ARGS_H
+
+#include "utils_array.h"
+
+struct rrd_args_s
+{
+  str_array_t *options;
+  str_array_t *data;
+  str_array_t *calc;
+  str_array_t *draw;
+
+  char last_stack_cdef[64];
+};
+typedef struct rrd_args_s rrd_args_t;
+
+rrd_args_t *ra_create (void);
+void ra_destroy (rrd_args_t *ra);
+
+int ra_argc (rrd_args_t *ra);
+char **ra_argv (rrd_args_t *ra);
+
+#endif /* RRD_ARGS_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */