logconfig: Simplify log_config_set API 73/129973/5
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 9 Jun 2017 11:48:49 +0000 (13:48 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 12 Jun 2017 17:47:08 +0000 (19:47 +0200)
Previously, logconfig provided push() function which only did
append new entry and set() which only did overwrite existing
entry.

This commit leaves only set() in public API, which does both
add & replace.  Original functions are hidden and available
for internal purpose only (modify, add).

Change-Id: Id5c735d3b2b9ca4f7a2dafd2879823fd42d3f56e

include/logconfig.h
src/logctrl/logctrl.c
src/shared/logconfig.c
src/tests/config.c

index 14e2993..6315cb5 100644 (file)
@@ -46,7 +46,7 @@ struct log_config {
  */
 typedef void (*configIter) (const char *key, const char* value, void *userdata);
 
-int log_config_set(struct log_config* config, const char* key, const char* value);
+void log_config_set(struct log_config* config, const char* key, const char* value);
 const char* log_config_get(struct log_config* config, const char* key);
 int log_config_get_int(struct log_config* config, const char* key, int default_val);
 int log_config_get_boolean(struct log_config* config, const char* key, int default_val);
@@ -57,7 +57,6 @@ void log_config_free(struct log_config* config);
 
 void log_config_print_out(struct log_config* config);
 int log_config_print_key(struct log_config* config, const char* key);
-void log_config_push(struct log_config* config, const char* key, const char* value);
 int log_config_remove(struct log_config* config, const char* key);
 void log_config_foreach(struct log_config* config, configIter iter, void *userdata);
 
index 251c7bb..d31c933 100644 (file)
@@ -139,8 +139,7 @@ int main(int argc, char ** argv)
                if (log_config_write(conf, filename) < 0)
                        goto err_save;
        } else if (opt.should_set) {
-               if (!log_config_set(conf, key, val))
-                       log_config_push(conf, key, val);
+               log_config_set(conf, key, val);
                if (log_config_write(conf, filename) < 0)
                        goto err_save;
        } else {
index 99f4e7c..9cf65f9 100644 (file)
@@ -28,24 +28,22 @@ struct log_conf_entry {
 /**
  * @brief Get a config value
  * @details Returns a value with the given key from the given config
- * @param[in] c The config to work with
+ * @param[in] config The config to work with
  * @param[in] key The key of the value entry to get
  * @return The value of the entry if such exists, else NULL
- * @see log_config_set, log_config_push, log_config_remove
+ * @see log_config_set, log_config_remove
  */
-const char * log_config_get(struct log_config* c, const char* key)
+const char *log_config_get(struct log_config *config, const char *key)
 {
-       struct log_conf_entry * e;
+       assert(config);
+       assert(key);
 
-       if (!c)
-               return NULL;
+       struct log_conf_entry *e;
 
-       e = c->begin;
-       while (e)
+       for (e = config->begin; e; e = e->next) {
                if (!strcmp(e->key, key))
                        return e->value;
-               else
-                       e = e->next;
+       }
 
        return NULL;
 }
@@ -109,41 +107,82 @@ int log_config_get_boolean(struct log_config* c, const char* key, int default_va
 }
 
 /**
- * @brief Set a config value
+ * @brief Modify a config entry
  * @details Sets the entry with the given key in the given config to the given value
- * @param[in] c The config to work with
+ * @param[in] config The config to work with
  * @param[in] key The key of the value entry to set
  * @param[in] value The value to set
  * @remarks The entry must already exist
- * @return 0 if the entry does not exist, else 1
- * @see log_config_get, log_config_push, log_config_remove
+ * @return 0 if the entry was not added, else 1
+ * @see log_config_set, log_config_get, log_config_remove
  */
-int log_config_set(struct log_config* c, const char* key, const char* value)
+static int log_config_modify(struct log_config *config, const char *key, const char *value)
 {
-       struct log_conf_entry * e;
+       assert(config);
+       assert(key);
+       assert(value);
 
-       if (!c || !key || !value)
-               return 0;
+       struct log_conf_entry *e;
 
-       e = c->begin;
-       while (e) {
-               if (!strcmp(e->key, key)) {
-                       strncpy(e->value, value, MAX_CONF_VAL_LEN - 1);
-                       e->value[MAX_CONF_VAL_LEN - 1] = '\0';
-                       return 1;
-               }
-               e = e->next;
+       for (e = config->begin; e; e = e->next) {
+               if (!strcmp(e->key, key))
+                       return snprintf(e->value, sizeof e->value, "%s", value) == (strlen(value) + 1);
        }
+
        return 0;
 }
 
+/**
+ * @brief Append a config entry
+ * @details Create an entry composed of the given key and value and add it to the given config
+ * @remarks Does not check for duplicates
+ * @param[in] config The config to work with
+ * @param[in] key The key of the entry to create
+ * @param[in] value The value of the new entry
+ * @see log_config_set, log_config_get, log_config_remove
+ */
+static void log_config_add(struct log_config *config, const char *key, const char *value)
+{
+       assert(config);
+       assert(key);
+       assert(value);
+
+       struct log_conf_entry *e = calloc(1, sizeof(struct log_conf_entry));
+
+       snprintf(e->key, sizeof e->key, "%s", key);
+       snprintf(e->value, sizeof e->value, "%s", value);
+
+       if (!config->last) {
+               config->begin = config->last = e;
+               return;
+       }
+
+       config->last->next = e;
+       config->last = e;
+}
+
+/**
+ * @brief Set a config entry
+ * @details Sets an entry to given value
+ * @remarks Adds new key if needed
+ * @param[in] config The config to work with
+ * @param[in] key The key of the entry
+ * @param[in] value The value of the entry
+ * @see log_config_get, log_config_remove
+ */
+void log_config_set(struct log_config *config, const char *key, const char *value)
+{
+       if (!log_config_modify(config, key, value))
+               log_config_add(config, key, value);
+}
+
 static void log_config_read_file_by_handle(struct log_config *config, FILE *fp)
 {
        assert(config);
        assert(fp);
 
        char line[MAX_CONF_ENTRY_LEN];
-       while (fgets(line, MAX_CONF_ENTRY_LEN, fp)) {
+       while (fgets(line, sizeof line, fp)) {
                int len = strlen(line);
                char key[MAX_CONF_KEY_LEN];
 
@@ -154,14 +193,12 @@ static void log_config_read_file_by_handle(struct log_config *config, FILE *fp)
                        line[len - 1] = '\0';
 
                char *tok = strchr(line, '=');
-               if (!tok || (tok - line > MAX_CONF_KEY_LEN))
+               if (!tok || (tok - line > sizeof key))
                        continue;
                ++tok;
 
                snprintf(key, tok - line, "%s", line);
-               if (!log_config_set(config, key, tok))
-                       log_config_push(config, key, tok);
-
+               log_config_set(config, key, tok);
        }
 }
 
@@ -367,36 +404,12 @@ int log_config_print_key(struct log_config* config, const char* key)
 }
 
 /**
- * @brief Append a config entry
- * @details Create an entry composed of the given key and value and add it to the given config
- * @remarks Does not check for duplicates
- * @param[in] config The config to work with
- * @param[in] key The key of the entry to create
- * @param[in] value The value of the new entry
- * @see log_config_set, log_config_get, log_config_remove
- */
-void log_config_push(struct log_config* config, const char* key, const char* value)
-{
-       struct log_conf_entry* e = calloc(1, sizeof(struct log_conf_entry));
-       snprintf(e->key, MAX_CONF_KEY_LEN, "%s", key);
-       snprintf(e->value, MAX_CONF_VAL_LEN, "%s", value);
-
-       if (!config->last) {
-               config->begin = config->last = e;
-               return;
-       }
-
-       config->last->next = e;
-       config->last = e;
-}
-
-/**
  * @brief Remove an entry
  * @details Remove the entry under the given key from the given config
  * @param[in] config The config to work with
  * @param[in] key The key of the entry to remove
  * @return 1 if the entry existed and was removed, else 0
- * @see log_config_set, log_config_get, log_config_push
+ * @see log_config_set, log_config_get
  */
 int log_config_remove(struct log_config* config, const char* key)
 {
index 474e383..884720b 100644 (file)
@@ -16,19 +16,12 @@ int main()
        setenv("DLOG_CONFIG_PATH", "src/tests/test.conf", 1);
        assert(!log_config_read(&config));
 
+       /* test if key retrieval works first (needed for set tests) */
        get = log_config_get(&config, "foo");
        assert(get);
        assert(!strncmp(get, "bar", 4));
-
        assert(!log_config_get(&config, "baz"));
        assert(!log_config_get(&config, "tony"));
-       assert(!log_config_set(&config, "baz", "bar"));
-       assert(log_config_set(&config, "foo", "quux"));
-
-       get = log_config_get(&config, "foo");
-       assert(get);
-       assert(strncmp(get, "bar", 4));
-       assert(!strncmp(get, "quux", 5));
 
        get = log_config_get(&config, "valid_extra_conf");
        assert(get);
@@ -37,5 +30,18 @@ int main()
        get = log_config_get(&config, "invalid_extra_conf");
        assert(!get);
 
+       /* test existing key modification */
+       log_config_set(&config, "foo", "quux");
+       get = log_config_get(&config, "foo");
+       assert(get);
+       assert(strncmp(get, "bar", 4));
+       assert(!strncmp(get, "quux", 5));
+
+       /* test new key addition */
+       log_config_set(&config, "test_add_key", "ok");
+       get = log_config_get(&config, "test_add_key");
+       assert(get);
+       assert(!strncmp(get, "ok", 3));
+
        return 0;
 }