/**
* @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;
}
}
/**
- * @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];
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);
}
}
}
/**
- * @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)
{
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);
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;
}