#include <regex.h>
-#define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
#define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
struct cu_match_s
int (*callback) (const char *str, char * const *matches, size_t matches_num,
void *user_data);
void *user_data;
+ void (*free) (void *user_data);
};
/*
return (0);
} /* int default_callback */
+static void match_simple_free (void *data)
+{
+ free (data);
+} /* void match_simple_free */
+
/*
* Public functions
*/
cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
int (*callback) (const char *str,
char * const *matches, size_t matches_num, void *user_data),
- void *user_data)
+ void *user_data, void (*free_user_data) (void *user_data))
{
cu_match_t *obj;
int status;
obj->callback = callback;
obj->user_data = user_data;
+ obj->free = free_user_data;
return (obj);
} /* cu_match_t *match_create_callback */
user_data->ds_type = match_ds_type;
obj = match_create_callback (regex, excluderegex,
- default_callback, user_data);
+ default_callback, user_data, match_simple_free);
if (obj == NULL)
{
sfree (user_data);
return (NULL);
}
-
- obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
-
return (obj);
} /* cu_match_t *match_create_simple */
if (obj == NULL)
return;
- if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
- {
- sfree (obj->user_data);
- }
+ if ((obj->user_data != NULL) && (obj->free != NULL))
+ (*obj->free) (obj->user_data);
sfree (obj);
} /* void match_destroy */
* callback.
* The optional `excluderegex' allows to exclude the line from the match, if
* the excluderegex matches.
+ * When `match_destroy' is called the `user_data' pointer is freed using
+ * the `free_user_data' callback - if it is not NULL.
*/
cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
int (*callback) (const char *str,
char * const *matches, size_t matches_num, void *user_data),
- void *user_data);
+ void *user_data, void (*free_user_data) (void *user_data));
/*
* NAME