summaryrefslogtreecommitdiff
path: root/res/res_sorcery_memory.c
diff options
context:
space:
mode:
authorJoshua Colp <jcolp@digium.com>2013-02-16 16:24:21 +0000
committerJoshua Colp <jcolp@digium.com>2013-02-16 16:24:21 +0000
commitcce1c9547facb5cf28dc4e74f9d2369e83f3a212 (patch)
treeffb07b4eb1b7d7ec2ce5b294fdca3810c8413436 /res/res_sorcery_memory.c
parentc209e85ad31ac3983a0eb7684055cdd808cbd8fe (diff)
Add support for retrieving multiple objects from sorcery using a regex on their id.
Review: https://reviewboard.asterisk.org/r/2329/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@381614 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_sorcery_memory.c')
-rw-r--r--res/res_sorcery_memory.c38
1 files changed, 34 insertions, 4 deletions
diff --git a/res/res_sorcery_memory.c b/res/res_sorcery_memory.c
index ccf195651..03b825313 100644
--- a/res/res_sorcery_memory.c
+++ b/res/res_sorcery_memory.c
@@ -32,6 +32,8 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include <regex.h>
+
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/astobj2.h"
@@ -45,6 +47,7 @@ static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void
static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
const struct ast_variable *fields);
+static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
static int sorcery_memory_update(void *data, void *object);
static int sorcery_memory_delete(void *data, void *object);
static void sorcery_memory_close(void *data);
@@ -56,6 +59,7 @@ static struct ast_sorcery_wizard memory_object_wizard = {
.retrieve_id = sorcery_memory_retrieve_id,
.retrieve_fields = sorcery_memory_retrieve_fields,
.retrieve_multiple = sorcery_memory_retrieve_multiple,
+ .retrieve_regex = sorcery_memory_retrieve_regex,
.update = sorcery_memory_update,
.delete = sorcery_memory_delete,
.close = sorcery_memory_close,
@@ -69,6 +73,9 @@ struct sorcery_memory_fields_cmp_params {
/*! \brief Pointer to the fields to check */
const struct ast_variable *fields;
+ /*! \brief Regular expression for checking object id */
+ regex_t *regex;
+
/*! \brief Optional container to put object into */
struct ao2_container *container;
};
@@ -101,13 +108,19 @@ static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy);
- /* If we can't turn the object into an object set OR if differences exist between the fields
- * passed in and what are present on the object they are not a match.
- */
- if (params->fields &&
+ if (params->regex) {
+ /* If a regular expression has been provided see if it matches, otherwise move on */
+ if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
+ ao2_link(params->container, obj);
+ }
+ return 0;
+ } else if (params->fields &&
(!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
(ast_sorcery_changeset_create(objset, params->fields, &diff)) ||
diff)) {
+ /* If we can't turn the object into an object set OR if differences exist between the fields
+ * passed in and what are present on the object they are not a match.
+ */
return 0;
}
@@ -154,6 +167,23 @@ static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery,
ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
}
+static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
+{
+ regex_t expression;
+ struct sorcery_memory_fields_cmp_params params = {
+ .sorcery = sorcery,
+ .container = objects,
+ .regex = &expression,
+ };
+
+ if (regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
+ return;
+ }
+
+ ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
+ regfree(&expression);
+}
+
static int sorcery_memory_update(void *data, void *object)
{
RAII_VAR(void *, existing, NULL, ao2_cleanup);