Browse Source

Fix faux_list_match...() functions. Iterator must be initialized to list head first

Serj Kalichev 1 year ago
parent
commit
441a0eb86c
4 changed files with 56 additions and 34 deletions
  1. 1 0
      faux/ini/ini.c
  2. 4 4
      faux/list.h
  3. 45 30
      faux/list/list.c
  4. 6 0
      faux/sched/sched.c

+ 1 - 0
faux/ini/ini.c

@@ -598,6 +598,7 @@ faux_ini_t *faux_ini_extract_subini(const faux_ini_t *ini, const char *prefix)
 		return NULL;
 
 	prefix_len = strlen(prefix);
+	iter = faux_list_head(ini->list);
 	while ((pair = (faux_pair_t *)faux_list_match(ini->list,
 		faux_ini_kcompare_prefix, prefix, &iter))) {
 		const char *name = faux_pair_name(pair);

+ 4 - 4
faux/list.h

@@ -57,14 +57,14 @@ ssize_t faux_list_del_all(faux_list_t *list);
 
 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 **iter);
 faux_list_node_t *faux_list_kmatch_node(const faux_list_t *list,
-	const void *userkey, faux_list_node_t **saveptr);
+	const void *userkey, faux_list_node_t **iter);
 void *faux_list_match(const faux_list_t *list,
 	faux_list_kcmp_fn matchFn, const void *userkey,
-	faux_list_node_t **saveptr);
+	faux_list_node_t **iter);
 void *faux_list_kmatch(const faux_list_t *list,
-	const void *userkey, faux_list_node_t **saveptr);
+	const void *userkey, faux_list_node_t **iter);
 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,

+ 45 - 30
faux/list/list.c

@@ -556,11 +556,12 @@ bool_t faux_list_kdel(faux_list_t *list, const void *userkey)
  *
  * Function iterates through the list and executes special matching user defined
  * callback function matchFn for every list node. User can provide "userkey" -
- * the data that matchFn can use how it wants. The matchFn is arbitrary
+ * the data that matchFn can use how it wants. The matchFn is mandatory
  * argument. The userkey argument can be NULL. The function will immediately
- * return matched list node. To continue searching the saveptr argument contains
+ * return matched list node. To continue searching the iter argument contains
  * current iterator. So user can call to faux_list_match_node() for several
  * times and gets all matched nodes from list.
+ * Before function using the iterator must be initialised by list head node.
  *
  * Prototype for matchFn callback function:
  * @code
@@ -570,34 +571,24 @@ bool_t faux_list_kdel(faux_list_t *list, const void *userkey)
  * @param [in] list List.
  * @param [in] matchFn User defined matching callback function.
  * @param [in] userkey User defined data to use in matchFn function.
- * @param [in,out] saveptr Ptr to save iterator.
+ * @param [in,out] iter List node ptr used as an iterator.
  * @return Matched list 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_kcmp_fn matchFn, const void *userkey, faux_list_node_t **iter)
 {
-	faux_list_node_t *iter = NULL;
+	faux_list_node_t *node = NULL;
 
 	assert(list);
+	assert(iter);
 	assert(matchFn);
-	if (!list || !matchFn || !list->head)
+	if (!iter || !matchFn || !list)
 		return NULL;
 
-	if (saveptr)
-		iter = *saveptr;
-	if (!iter)
-		iter = list->head;
-	while (iter) {
-		int res = 0;
-		faux_list_node_t *node = iter;
-
-		iter = faux_list_next_node(node);
-		if (saveptr)
-			*saveptr = iter;
-		res = matchFn(userkey, faux_list_data(node));
+	while ((node = faux_list_each_node(iter))) {
+		int res = matchFn(userkey, faux_list_data(node));
 		if (0 == res)
-			return node;
+			return node; // Match
 		if (list->sorted && (res < 0)) // No chances to find match
 			return NULL;
 	}
@@ -614,13 +605,13 @@ faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  * @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)
+	const void *userkey, faux_list_node_t **iter)
 {
 	assert(list);
 	if (!list)
 		return NULL;
 
-	return faux_list_match_node(list, list->kcmpFn, userkey, saveptr);
+	return faux_list_match_node(list, list->kcmpFn, userkey, iter);
 }
 
 
@@ -630,11 +621,11 @@ faux_list_node_t *faux_list_kmatch_node(const faux_list_t *list,
  *
  * @sa faux_list_match_node()
  */
-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_match(const faux_list_t *list,
+	faux_list_kcmp_fn matchFn, const void *userkey, faux_list_node_t **iter)
 {
 	faux_list_node_t *res =
-		faux_list_match_node(list, matchFn, userkey, saveptr);
+		faux_list_match_node(list, matchFn, userkey, iter);
 	if (!res)
 		return NULL;
 
@@ -649,14 +640,14 @@ void *faux_list_match(const faux_list_t *list, faux_list_kcmp_fn matchFn,
  *
  * @sa faux_list_match_node()
  */
-void *faux_list_kmatch(const faux_list_t *list, const void *userkey,
-	faux_list_node_t **saveptr)
+void *faux_list_kmatch(const faux_list_t *list,
+	const void *userkey, faux_list_node_t **iter)
 {
 	assert(list);
 	if (!list)
 		return NULL;
 
-	return faux_list_match(list, list->kcmpFn, userkey, saveptr);
+	return faux_list_match(list, list->kcmpFn, userkey, iter);
 }
 
 
@@ -670,7 +661,15 @@ void *faux_list_kmatch(const faux_list_t *list, const void *userkey,
 faux_list_node_t *faux_list_find_node(const faux_list_t *list,
 	faux_list_kcmp_fn matchFn, const void *userkey)
 {
-	return faux_list_match_node(list, matchFn, userkey, NULL);
+	faux_list_node_t *iter = NULL;
+
+	assert(list);
+	if (!list)
+		return NULL;
+
+	iter = faux_list_head(list);
+
+	return faux_list_match_node(list, matchFn, userkey, &iter);
 }
 
 
@@ -684,6 +683,10 @@ faux_list_node_t *faux_list_find_node(const faux_list_t *list,
 faux_list_node_t *faux_list_kfind_node(const faux_list_t *list,
 	const void *userkey)
 {
+	assert(list);
+	if (!list)
+		return NULL;
+
 	return faux_list_find_node(list, list->kcmpFn, userkey);
 }
 
@@ -698,7 +701,15 @@ faux_list_node_t *faux_list_kfind_node(const faux_list_t *list,
 void *faux_list_find(const faux_list_t *list, faux_list_kcmp_fn matchFn,
 	const void *userkey)
 {
-	return faux_list_match(list, matchFn, userkey, NULL);
+	faux_list_node_t *iter = NULL;
+
+	assert(list);
+	if (!list)
+		return NULL;
+
+	iter = faux_list_head(list);
+
+	return faux_list_match(list, matchFn, userkey, &iter);
 }
 
 
@@ -712,6 +723,10 @@ void *faux_list_find(const faux_list_t *list, faux_list_kcmp_fn matchFn,
 void *faux_list_kfind(const faux_list_t *list,
 	const void *userkey)
 {
+	assert(list);
+	if (!list)
+		return NULL;
+
 	return faux_list_find(list, list->kcmpFn, userkey);
 }
 

+ 6 - 0
faux/sched/sched.c

@@ -321,6 +321,7 @@ static ssize_t faux_sched_del_by_something(faux_sched_t *sched, void *value,
 	if (!sched)
 		return -1;
 
+	saved = faux_list_head(sched->list);
 	while ((node = faux_list_match_node(sched->list, cmp_f,
 		value, &saved))) {
 		faux_list_del(sched->list, node);
@@ -370,6 +371,7 @@ ssize_t faux_sched_del_by_data(faux_sched_t *sched, void *data)
 /** @brief Get scheduled event by specified value.
  *
  * Static function.
+ * Saved iterator 'saved' must be initialized to list head before usage.
  *
  * @param [in] sched Allocated and initialized sched object.
  * @param [in] value Value to search for.
@@ -398,6 +400,8 @@ static faux_ev_t *faux_sched_get_by_something(faux_sched_t *sched, void *value,
 
 
 /** @brief Get sched entries with specified event ID.
+ *
+ * Saved iterator 'saved' must be initialized to list head before usage.
  *
  * @param [in] sched Allocated and initialized sched object.
  * @param [in] ev_id Event ID to search for.
@@ -413,6 +417,8 @@ faux_ev_t *faux_sched_get_by_id(faux_sched_t *sched, int ev_id,
 
 
 /** @brief Get sched entries with specified user data pointer.
+ *
+ * Saved iterator 'saved' must be initialized to list head before usage.
  *
  * @param [in] sched Allocated and initialized sched object.
  * @param [in] data Pointer to user data to search for.