Browse Source

faux: ini: faux_ini_set() can remove entry if value == NULL

Serj Kalichev 4 years ago
parent
commit
399d080e7a
3 changed files with 31 additions and 9 deletions
  1. 1 1
      faux/ini.h
  2. 25 5
      faux/ini/ini.c
  3. 5 3
      faux/str.h

+ 1 - 1
faux/ini.h

@@ -35,7 +35,7 @@ faux_ini_node_t *faux_ini_prev(const faux_ini_node_t *node);
 faux_pair_t *faux_ini_data(const faux_ini_node_t *node);
 
 faux_pair_t *faux_ini_set(faux_ini_t *ini, const char *name, const char *value);
-faux_pair_t *faux_ini_unset(faux_ini_t *ini, const char *name);
+void faux_ini_unset(faux_ini_t *ini, const char *name);
 int faux_ini_parse_str(faux_ini_t *ini, const char *str);
 int faux_ini_parse_file(faux_ini_t *ini, const char *fn);
 

+ 25 - 5
faux/ini/ini.c

@@ -42,6 +42,12 @@ void faux_ini_free(faux_ini_t *ini) {
 
 static int faux_ini_del(faux_ini_t *ini, faux_ini_node_t *node) {
 
+	assert(ini);
+	assert(node);
+	if (!ini || !node)
+		return -1;
+
+	return faux_list_del(ini->list, (faux_list_node_t *)node);
 }
 
 faux_pair_t *faux_ini_set(faux_ini_t *ini, const char *name, const char *value) {
@@ -51,31 +57,45 @@ faux_pair_t *faux_ini_set(faux_ini_t *ini, const char *name, const char *value)
 	faux_pair_t *found_pair = NULL;
 
 	assert(ini);
-	if (!ini)
+	assert(name);
+	if (!ini || !name)
 		return NULL;
 
 	pair = faux_pair_new(name, value);
+	assert(pair);
 	if (!pair)
 		return NULL;
+
+	// NULL 'value' means: remove entry from list
+	if (!value) {
+		node = faux_list_find_node(ini->list, faux_pair_compare, pair);
+		faux_pair_free(pair);
+		if (node)
+			faux_list_del(ini->list, node);
+		return NULL;
+	}
+
+	// Try to add new entry or find existent entry with the same 'name'
 	node = faux_list_add_find(ini->list, pair);
-	if (NULL == node) { // Something went wrong
+	if (!node) { // Something went wrong
 		faux_pair_free(pair);
 		return NULL;
 	}
 	found_pair = faux_list_data(node);
-	if (found_pair != pair) { // Item already exists
+	if (found_pair != pair) { // Item already exists so use existent
 		faux_pair_free(pair);
 		faux_pair_set_value(found_pair, value); // Replace value by new one
 		return found_pair;
 	}
 
+	// The new entry was added
 	return pair;
 }
 
 
-faux_pair_t *faux_ini_unset(faux_ini_t *ini, const char *name) {
+void faux_ini_unset(faux_ini_t *ini, const char *name) {
 
-	return faux_ini_set(ini, name, NULL);
+	faux_ini_set(ini, name, NULL);
 }
 
 

+ 5 - 3
faux/str.h

@@ -10,9 +10,9 @@
 #include "faux/faux.h"
 
 #define UTF8_MASK 0xC0
-#define UTF8_7BIT_MASK 0x80 /* One byte or multibyte */
-#define UTF8_11   0xC0 /* First UTF8 byte */
-#define UTF8_10   0x80 /* Next UTF8 bytes */
+#define UTF8_7BIT_MASK 0x80 // One byte or multibyte
+#define UTF8_11   0xC0 // First UTF8 byte
+#define UTF8_10   0x80 // Next UTF8 bytes
 
 C_DECL_BEGIN
 
@@ -25,7 +25,9 @@ char *faux_str_catn(char **str, const char *text, size_t n);
 char *faux_str_cat(char **str, const char *text);
 
 char *faux_str_tolower(const char *str);
+char *faux_str_toupper(const char *str);
 
+int faux_str_ncasecmp(const char *str1, const char *str2, size_t n);
 int faux_str_casecmp(const char *str1, const char *str2);