]> git.itanic.dy.fi Git - log-plotter/commitdiff
config.c: Implement store_int_variable_value_to_array()
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 29 Dec 2013 15:10:55 +0000 (17:10 +0200)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 29 Dec 2013 15:10:55 +0000 (17:10 +0200)
This can be used to insert variable entries with integer type into the
variable array.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
config.c
config.h

index 7caa63de662b20fd2b23d1720f4b8b1d5beb9ec2..1dcc9c859224d394fd30d64e369417470cea1dbf 100644 (file)
--- a/config.c
+++ b/config.c
@@ -62,9 +62,8 @@ static void init_variable_value(struct variable_value *v)
        bzero(v, sizeof(*v));
 }
 
-static int store_str_variable_value_to_array(const char *variable,
-                                       const char *value,
-                                       struct plotter_config *cfg)
+static struct variable_value *prepare_slot_for_insertion(
+       struct plotter_config *cfg, const char *variable)
 {
        struct variable_value *v;
        int i;
@@ -82,12 +81,15 @@ static int store_str_variable_value_to_array(const char *variable,
                        continue;
 
                free(v[i].name);
+               v[i].name = NULL;
                free(v[i].ptr);
-               goto out_store;
+               v[i].ptr = NULL;
+
+               return &v[i];
        }
 
        /*
-        * Failed to find an existing variable with same type, create
+        * Failed to find an existing variable with same name, create
         * a new one. Resize the variable array so that there is room
         * for the new entry and one terminator entry (with all fields
         * zeroed).
@@ -97,10 +99,36 @@ static int store_str_variable_value_to_array(const char *variable,
        v = cfg->variables;
        init_variable_value(&cfg->variables[i + 1]);
 
-out_store:
-       v[i].name = strdup(variable);
-       v[i].type = TYPE_STRING;
-       v[i].ptr = strdup(value);
+       return &v[i];
+}
+
+static int store_str_variable_value_to_array(const char *variable,
+                                       const char *value,
+                                       struct plotter_config *cfg)
+{
+       struct variable_value *v;
+
+       v = prepare_slot_for_insertion(cfg, variable);
+
+       v->name = strdup(variable);
+       v->type = TYPE_STRING;
+       v->ptr = strdup(value);
+
+       return 0;
+}
+
+int store_int_variable_value_to_array(const char *variable, int value,
+                               struct plotter_config *cfg)
+{
+       struct variable_value *v;
+       int *p;
+
+       v = prepare_slot_for_insertion(cfg, variable);
+
+       v->name = strdup(variable);
+       v->type = TYPE_INT;
+       p = v->ptr = malloc(sizeof(value));
+       *p = value;
 
        return 0;
 }
index 851c7d42ca7d8fbed65c58e791577f115d3188bc..c8abadc9f8cbb6f7de17e9d0cb3ec2e619e4fc07 100644 (file)
--- a/config.h
+++ b/config.h
@@ -30,6 +30,8 @@ int read_args(int argc, char *argv[], struct plotter_config *cfg);
 int populate_config_data_from_file(const char *path,
                                struct plotter_config *cfg);
 
+int store_int_variable_value_to_array(const char *variable, int value,
+                               struct plotter_config *cfg);
 int replace_variables_with_values(char *str, size_t len,
                                const struct plotter_config *cfg);