strings/json: Add string delimter match, and object create with vars methods

Add a function to check if there is an exact match a one string between
delimiters in another string.

Add a function that will create an ast_json object out of a list of
Asterisk variables. An excludes string can also optionally be passed
in.

Also, add a macro to make it easier to get object integers.

Change-Id: I5f34f18e102126aef3997f19a553a266d70d6226
This commit is contained in:
Kevin Harwell
2021-10-21 12:29:11 -05:00
committed by Friendly Automation
parent 2e55c0fded
commit ae97aaedb0
6 changed files with 250 additions and 0 deletions

View File

@@ -852,3 +852,22 @@ struct ast_json *ast_json_channel_vars(struct varshead *channelvars)
return ret;
}
struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)
{
const struct ast_variable *i;
struct ast_json *obj;
obj = ast_json_object_create();
if (!obj) {
return NULL;
}
for (i = variables; i; i = i->next) {
if (!excludes || !ast_in_delimited_string(i->name, excludes, ',')) {
ast_json_object_set(obj, i->name, ast_json_string_create(i->value));
}
}
return obj;
}

View File

@@ -430,3 +430,28 @@ int ast_vector_string_split(struct ast_vector_string *dest,
return 0;
}
int ast_in_delimited_string(const char *needle, const char *haystack, char delim)
{
const char *end;
unsigned long needle_size;
ast_assert(haystack != NULL);
if (!needle) {
return 0;
}
needle_size = strlen(needle);
haystack = ast_skip_blanks(haystack);
while ((end = strchr(haystack, delim))) {
if (needle_size == end - haystack && !strncmp(haystack, needle, needle_size)) {
return 1;
}
haystack = ast_skip_blanks(end + 1);
}
return strcmp(haystack, needle) ? 0 : -1;
}