Browse Source

faux.list: Key oriented find functions

Serj Kalichev 4 years ago
parent
commit
23938e3679
5 changed files with 91 additions and 18 deletions
  1. 9 18
      faux/ini/ini.c
  2. 9 0
      faux/ini/pair.c
  3. 1 0
      faux/ini/private.h
  4. 8 0
      faux/list.h
  5. 64 0
      faux/list/list.c

+ 9 - 18
faux/ini/ini.c

@@ -30,7 +30,8 @@ faux_ini_t *faux_ini_new(void) {
 		return NULL;
 
 	// Init
-	ini->list = faux_list_new(BOOL_TRUE, BOOL_TRUE, faux_pair_compare, NULL, faux_pair_free);
+	ini->list = faux_list_new(BOOL_TRUE, BOOL_TRUE, 
+		faux_pair_compare, faux_pair_kcompare, faux_pair_free);
 
 	return ini;
 }
@@ -82,20 +83,19 @@ const faux_pair_t *faux_ini_set(
 	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);
+		node = faux_list_kfind_node(ini->list, name);
 		if (node)
 			faux_list_del(ini->list, node);
 		return NULL;
 	}
 
+	pair = faux_pair_new(name, value);
+	assert(pair);
+	if (!pair)
+		return NULL;
+
 	// Try to add new entry or find existent entry with the same 'name'
 	node = faux_list_add_find(ini->list, pair);
 	if (!node) { // Something went wrong
@@ -141,21 +141,12 @@ void faux_ini_unset(faux_ini_t *ini, const char *name) {
  */
 const faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name) {
 
-	faux_list_node_t *iter = NULL;
-	faux_pair_t *pair = NULL;
-
 	assert(ini);
 	assert(name);
 	if (!ini || !name)
 		return NULL;
 
-	pair = faux_pair_new(name, NULL);
-	if (!pair)
-		return NULL;
-	iter = faux_list_find_node(ini->list, faux_pair_compare, pair);
-	faux_pair_free(pair);
-
-	return faux_list_data(iter);
+	return faux_list_kfind(ini->list, name);
 }
 
 

+ 9 - 0
faux/ini/pair.c

@@ -19,6 +19,15 @@ int faux_pair_compare(const void *first, const void *second) {
 }
 
 
+int faux_pair_kcompare(const void *key, const void *list_item) {
+
+	const char *f = (const char *)key;
+	const faux_pair_t *s = (const faux_pair_t *)list_item;
+
+	return strcmp(f, s->name);
+}
+
+
 faux_pair_t *faux_pair_new(const char *name, const char *value) {
 
 	faux_pair_t *pair = NULL;

+ 1 - 0
faux/ini/private.h

@@ -14,6 +14,7 @@ struct faux_ini_s {
 C_DECL_BEGIN
 
 int faux_pair_compare(const void *first, const void *second);
+int faux_pair_kcompare(const void *key, const void *list_item);
 faux_pair_t *faux_pair_new(const char *name, const char *value);
 void faux_pair_free(void *pair);
 

+ 8 - 0
faux/list.h

@@ -45,13 +45,21 @@ int faux_list_del(faux_list_t *list, faux_list_node_t *node);
 faux_list_node_t *faux_list_match_node(const faux_list_t *list,
 	faux_list_kcmp_fn matchFn, const void *userkey,
 	faux_list_node_t **saveptr);
+faux_list_node_t *faux_list_kmatch_node(const faux_list_t *list,
+	const void *userkey, faux_list_node_t **saveptr);
 void *faux_list_match(const faux_list_t *list,
 	faux_list_kcmp_fn matchFn, const void *userkey,
 	faux_list_node_t **saveptr);
+void *faux_list_kmatch(const faux_list_t *list,
+	const void *userkey, faux_list_node_t **saveptr);
 faux_list_node_t *faux_list_find_node(const faux_list_t *list,
 	faux_list_kcmp_fn matchFn, const void *userkey);
+faux_list_node_t *faux_list_kfind_node(const faux_list_t *list,
+	const void *userkey);
 void *faux_list_find(const faux_list_t *list,
 	faux_list_kcmp_fn matchFn, const void *userkey);
+void *faux_list_kfind(const faux_list_t *list,
+	const void *userkey);
 
 C_DECL_END
 

+ 64 - 0
faux/list/list.c

@@ -548,6 +548,24 @@ faux_list_node_t *faux_list_match_node(const faux_list_t *list,
 }
 
 
+/** @brief Search list for matching (key cmp function).
+ *
+ * Same as faux_list_match_node() but uses userkey compare function defined
+ * while faux_list_new() function call.
+ *
+ * @sa faux_list_match_node()
+ */
+faux_list_node_t *faux_list_kmatch_node(const faux_list_t *list,
+	const void *userkey, faux_list_node_t **saveptr) {
+
+	assert(list);
+	if (!list)
+		return NULL;
+
+	return faux_list_match_node(list, list->kcmpFn, userkey, saveptr);
+}
+
+
 /** @brief Search list for matching (match function) and returns user data.
  *
  * Same as faux_list_match_node() but returns user data structure.
@@ -566,6 +584,24 @@ void *faux_list_match(const faux_list_t *list, faux_list_kcmp_fn matchFn,
 }
 
 
+/** @brief Search list for matching (key cmp function) and returns user data.
+ *
+ * Same as faux_list_match() but uses userkey compare function defined
+ * while faux_list_new() function call.
+ *
+ * @sa faux_list_match_node()
+ */
+void *faux_list_kmatch(const faux_list_t *list, const void *userkey,
+	faux_list_node_t **saveptr) {
+
+	assert(list);
+	if (!list)
+		return NULL;
+
+	return faux_list_match(list, list->kcmpFn, userkey, saveptr);
+}
+
+
 /** @brief Search list for first matching (match function).
  *
  * Same as faux_list_match_node() but search for the fisrt matching.
@@ -580,6 +616,20 @@ faux_list_node_t *faux_list_find_node(const faux_list_t *list,
 }
 
 
+/** @brief Search list for first matching (key cmp function).
+ *
+ * Same as faux_list_find_node() but uses userkey compare function defined
+ * while faux_list_new() function call.
+ *
+ * @sa faux_list_match_node()
+ */
+faux_list_node_t *faux_list_kfind_node(const faux_list_t *list,
+	const void *userkey) {
+
+	return faux_list_find_node(list, list->kcmpFn, userkey);
+}
+
+
 /** @brief Search list for first matching (match function) and returns user data.
  *
  * Same as faux_list_match_node() but returns user data structure and search
@@ -592,3 +642,17 @@ void *faux_list_find(const faux_list_t *list, faux_list_kcmp_fn matchFn,
 
 	return faux_list_match(list, matchFn, userkey, NULL);
 }
+
+
+/** @brief Search list for first matching (key cmp function). Returns user data.
+ *
+ * Same as faux_list_find() but uses userkey compare function defined
+ * while faux_list_new() function call.
+ *
+ * @sa faux_list_match_node()
+ */
+void *faux_list_kfind(const faux_list_t *list,
+	const void *userkey) {
+
+	return faux_list_find(list, list->kcmpFn, userkey);
+}