]> git.itanic.dy.fi Git - log-plotter/blobdiff - config.c
data.c: Reset time stamp when starting new log
[log-plotter] / config.c
index 7caa63de662b20fd2b23d1720f4b8b1d5beb9ec2..5f1f633564524004b7eef0dd70cf9144570e2487 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,35 @@ 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];
+}
+
+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;
 }
@@ -219,7 +246,7 @@ static char *get_value_for_variable(char *variable,
 
 static int replace_variable_with_value(char *str, size_t len,
                                char *variable_start, char *variable_end,
-                               const char *value)
+                               const char *value, char **endptr)
 {
        int value_len = strlen(value);
        char *from, *to;
@@ -237,6 +264,8 @@ static int replace_variable_with_value(char *str, size_t len,
                from = variable_end + 1;
                to = variable_start + value_len;
 
+               *endptr = to;
+
                while (*from) {
                        *to = *from;
                        from++;
@@ -273,6 +302,8 @@ static int replace_variable_with_value(char *str, size_t len,
                        to--;
                }
 
+               *endptr = from;
+
                /*
                 * use memcpy to avoid NULL terminating prematurely
                 * the target string
@@ -315,7 +346,6 @@ int replace_variables_with_values(char *str, size_t len,
 
                variable_end = ptr;
                *ptr = 0;
-               ptr++;
 
                value = get_value_for_variable(variable_start, cfg);
                if (!value) {
@@ -330,7 +360,7 @@ int replace_variables_with_values(char *str, size_t len,
                        continue;
                }
                overflow = replace_variable_with_value(str, len, replace_start,
-                                       variable_end, value);
+                                       variable_end, value, &ptr);
        }
 
        if (overflow)