<p>George Joseph has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18109">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">core: Config and XML tweaks needed for geolocation<br><br>Added:<br><br>Replace a variable in a list:<br>int ast_variable_list_replace_variable(struct ast_variable **head,<br> struct ast_variable *old, struct ast_variable *new);<br>Added test as well.<br><br>Create a "name=value" string from a variable list:<br>'name1="val1",name2="val2"', etc.<br>struct ast_str *ast_variable_list_join(<br> const struct ast_variable *head, const char *item_separator,<br> const char *name_value_separator, const char *quote_char,<br> struct ast_str **str);<br>Added test as well.<br><br>Allow the name of an XML element to be changed.<br>void ast_xml_set_name(struct ast_xml_node *node, const char *name);<br><br>Change-Id: I330a5f63dc0c218e0d8dfc0745948d2812141ccb<br>---<br>M include/asterisk/config.h<br>M include/asterisk/xml.h<br>M main/config.c<br>M main/xml.c<br>M tests/test_config.c<br>5 files changed, 149 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/09/18109/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/config.h b/include/asterisk/config.h</span><br><span>index f4d1dd2..f4f6c9d 100644</span><br><span>--- a/include/asterisk/config.h</span><br><span>+++ b/include/asterisk/config.h</span><br><span>@@ -968,6 +968,44 @@</span><br><span> int ast_variable_list_replace(struct ast_variable **head, struct ast_variable *replacement);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Replace a variable in the given list with a new variable</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param head A pointer to the current variable list head. Since the variable to be</span><br><span style="color: hsl(120, 100%, 40%);">+ * replaced, this pointer may be updated with the new head.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param old A pointer to the existing variable to be replaced.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param new A pointer to the new variable that will replace the old one.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 if a variable was replaced in the list</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 if no replacement occured</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note The search for the old variable is done simply on the pointer.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note If a variable is replaced, its memory is freed.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_variable_list_replace_variable(struct ast_variable **head, struct ast_variable *old,</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *new);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Join an ast_variable list with specified separators and quoted values</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param head A pointer to an ast_variable list head.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param item_separator The string to use to separate the list items.</span><br><span style="color: hsl(120, 100%, 40%);">+ * If NULL, "," will be used.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param name_value_separator The string to use to separate each item's name and value.</span><br><span style="color: hsl(120, 100%, 40%);">+ * If NULL, "=" will be used.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param str A pointer to a pre-allocated ast_str in which to put the results.</span><br><span style="color: hsl(120, 100%, 40%);">+ * If NULL, one will be allocated and returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param quote_char The quote char to use for the values.</span><br><span style="color: hsl(120, 100%, 40%);">+ * May be NULL or empty for no quoting.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval A pointer to the result ast_str. This may NOT be the same as the pointer</span><br><span style="color: hsl(120, 100%, 40%);">+ * passed in if the original ast_str wasn't large enough to hold the result.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Regardless, the pointer MUST be freed after use.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval NULL if there was an error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_str *ast_variable_list_join(const struct ast_variable *head, const char *item_separator,</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *name_value_separator, const char *quote_char, struct ast_str **str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Update variable value within a config</span><br><span> *</span><br><span> * \param category Category element within the config</span><br><span>diff --git a/include/asterisk/xml.h b/include/asterisk/xml.h</span><br><span>index 3217750..13af3c6 100644</span><br><span>--- a/include/asterisk/xml.h</span><br><span>+++ b/include/asterisk/xml.h</span><br><span>@@ -202,6 +202,13 @@</span><br><span> void ast_xml_set_text(struct ast_xml_node *node, const char *content);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Set or reset an element's name.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param node Node whose name is to be set.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param name New name.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void ast_xml_set_name(struct ast_xml_node *node, const char *name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Get the name of a node. */</span><br><span> const char *ast_xml_node_get_name(struct ast_xml_node *node);</span><br><span> </span><br><span>diff --git a/main/config.c b/main/config.c</span><br><span>index 7b3b457..92a24de 100644</span><br><span>--- a/main/config.c</span><br><span>+++ b/main/config.c</span><br><span>@@ -679,6 +679,49 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int ast_variable_list_replace_variable(struct ast_variable **head, struct ast_variable *old,</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *new)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *v, **prev = head;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (v = *head; v; prev = &v->next, v = v->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (v == old) {</span><br><span style="color: hsl(120, 100%, 40%);">+ new->next = v->next;</span><br><span style="color: hsl(120, 100%, 40%);">+ *prev = new;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(v);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_str *ast_variable_list_join(const struct ast_variable *head, const char *item_separator,</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *name_value_separator, const char *quote_char, struct ast_str **str)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *var = (struct ast_variable *)head;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_str *local_str = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (str == NULL || *str == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ local_str = ast_str_create(AST_MAX_USER_FIELD);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!local_str) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ local_str = *str;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (; var; var = var->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&local_str, 0, "%s%s%s%s%s%s", var->name, name_value_separator, S_OR(quote_char, ""),</span><br><span style="color: hsl(120, 100%, 40%);">+ var->value, S_OR(quote_char, ""), var->next ? item_separator : "");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (str != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *str = local_str;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return local_str;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)</span><br><span> {</span><br><span> const char *tmp;</span><br><span>diff --git a/main/xml.c b/main/xml.c</span><br><span>index d60dd90..88c9edf 100644</span><br><span>--- a/main/xml.c</span><br><span>+++ b/main/xml.c</span><br><span>@@ -332,6 +332,15 @@</span><br><span> xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+void ast_xml_set_name(struct ast_xml_node *node, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!node || !name) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ xmlNodeSetName((xmlNode *) node, (const xmlChar *) name);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)</span><br><span> {</span><br><span> return xmlDocDump(output, (xmlDocPtr)doc);</span><br><span>diff --git a/tests/test_config.c b/tests/test_config.c</span><br><span>index 770aa15..5b44b44 100644</span><br><span>--- a/tests/test_config.c</span><br><span>+++ b/tests/test_config.c</span><br><span>@@ -1893,6 +1893,56 @@</span><br><span> return AST_TEST_PASS;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+AST_TEST_DEFINE(variable_list_join_replace)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_variable *, list, NULL, ast_variables_destroy);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_str *, str, NULL, ast_free);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *bbb;</span><br><span style="color: hsl(120, 100%, 40%);">+ int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ info->name = "variable_list_join_replace";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->category = "/main/config/";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->summary = "Test joining a variable list";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->description = info->summary;</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_NOT_RUN;</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_EXECUTE:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ list = ast_variable_new("aaa", "111", "");</span><br><span style="color: hsl(120, 100%, 40%);">+ bbb = ast_variable_new("bbb", "222", "");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_variable_list_append(&list, bbb);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_variable_list_append(&list, ast_variable_new("ccc", "33 33", ""));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ str = ast_variable_list_join(list, ", ", " = ", "\"", &str);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, strcmp(ast_str_buffer(str), "aaa = \"111\", bbb = \"222\", ccc = \"33 33\"") == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ str = ast_str_create(AST_MAX_USER_FIELD);</span><br><span style="color: hsl(120, 100%, 40%);">+ str = ast_variable_list_join(list, ", ", " = ", "\"", &str);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, strcmp(ast_str_buffer(str), "aaa = \"111\", bbb = \"222\", ccc = \"33 33\"") == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ str = ast_variable_list_join(list, ", ", " = ", "\"", NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, strcmp(ast_str_buffer(str), "aaa = \"111\", bbb = \"222\", ccc = \"33 33\"") == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Replace the head item in the list */</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = ast_variable_list_replace_variable(&list, list, ast_variable_new("ddd", "444", ""));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ str = ast_variable_list_join(list, ", ", " = ", "\"", NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, strcmp(ast_str_buffer(str), "ddd = \"444\", bbb = \"222\", ccc = \"33 33\"") == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = ast_variable_list_replace_variable(&list, bbb, ast_variable_new("eee", "555", ""));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ str = ast_variable_list_join(list, ", ", " = ", "\"", NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, strcmp(ast_str_buffer(str), "ddd = \"444\", eee = \"555\", ccc = \"33 33\"") == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_PASS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span> static int unload_module(void)</span><br><span> {</span><br><span> AST_TEST_UNREGISTER(config_save);</span><br><span>@@ -1905,6 +1955,7 @@</span><br><span> AST_TEST_UNREGISTER(config_options_test);</span><br><span> AST_TEST_UNREGISTER(config_dialplan_function);</span><br><span> AST_TEST_UNREGISTER(variable_lists_match);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_UNREGISTER(variable_list_join_replace);</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span>@@ -1920,6 +1971,7 @@</span><br><span> AST_TEST_REGISTER(config_options_test);</span><br><span> AST_TEST_REGISTER(config_dialplan_function);</span><br><span> AST_TEST_REGISTER(variable_lists_match);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_REGISTER(variable_list_join_replace);</span><br><span> return AST_MODULE_LOAD_SUCCESS;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18109">change 18109</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18109"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: I330a5f63dc0c218e0d8dfc0745948d2812141ccb </div>
<div style="display:none"> Gerrit-Change-Number: 18109 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>