<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14117] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14117">14117</a></dd>
<dt>Author</dt> <dd>ledr</dd>
<dt>Date</dt> <dd>2009-07-02 11:57:54 -0500 (Thu, 02 Jul 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>major rewrite, working again, now with seperated functions per xml-odbc-do, using switch_memory_pool_t, using helper from first search through all functions, storing xml temporarily on disk like mod_xml_curl does - will this fix problems with freeing strdup's to switch_xml_t ? valgrind is next :)</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribmodxml_intmod_xml_odbcmod_xml_odbcc">freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/mod_xml_odbc.c</a></li>
<li><a href="#freeswitchtrunkcontribmodxml_intmod_xml_odbcxml_odbcconfxml">freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/xml_odbc.conf.xml</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribmodxml_intmod_xml_odbcMakefile">freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/Makefile</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkcontribmodxml_intmod_xml_odbcMakefile"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/Makefile (0 => 14117)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/Makefile         (rev 0)
+++ freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/Makefile        2009-07-02 16:57:54 UTC (rev 14117)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+BASE=../../../..
+include $(BASE)/build/modmake.rules
</ins></span></pre></div>
<a id="freeswitchtrunkcontribmodxml_intmod_xml_odbcmod_xml_odbcc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/mod_xml_odbc.c (14116 => 14117)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/mod_xml_odbc.c        2009-07-02 16:11:40 UTC (rev 14116)
+++ freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/mod_xml_odbc.c        2009-07-02 16:57:54 UTC (rev 14117)
</span><span class="lines">@@ -26,10 +26,13 @@
</span><span class="cx"> * All the FreeSWITCH contributors that I borrowed code from
</span><span class="cx"> * Leon de Rooij <leon@scarlet-internet.nl>
</span><span class="cx"> *
</span><ins>+ * Also thanks to:
+ * Scarlet Nederland for donating time and money
</ins><span class="cx"> *
</span><del>- * mod_xml_odbc.c -- Use FreeSWITCH ODBC to realtime create XML
</del><span class="cx"> *
</span><ins>+ * mod_xml_odbc.c -- Simple templating that can use ODBC to generate XML at search-time
</ins><span class="cx"> *
</span><ins>+ *
</ins><span class="cx"> */
</span><span class="cx"> #include <switch.h>
</span><span class="cx">
</span><span class="lines">@@ -55,20 +58,23 @@
</span><span class="cx">         switch_memory_pool_t *pool;
</span><span class="cx">         switch_odbc_handle_t *master_odbc;
</span><span class="cx">         switch_bool_t debug;
</span><ins>+        switch_bool_t keep_files_around;
</ins><span class="cx"> } globals;
</span><span class="cx">
</span><del>-static struct xml_odbc_session_helper {
-        switch_memory_pool_t *pool;
-        char *template_name;
-        switch_event_t *event;
-        switch_xml_t xml_in;
-        switch_xml_t xml_out;
-        int *off;
-        int tmp_i;
</del><ins>+typedef struct xml_odbc_session_helper {
+        switch_memory_pool_t *pool;        /* memory pool that is destroyed at the end of the session to ease free'ing */
+        char *next_template_name;        /* the name of the next template that has to be rendered */
+        switch_event_t *event;                /* contains headers which is a hash that can easily be expanded ${var} to *chars */
+        switch_xml_t xml_in;                /* root tag of xml template that is rendered from */
+        switch_xml_t xml_in_cur;        /* current tag in xml template that is rendered from */
+        switch_xml_t xml_out;                /* root tag of xml that is rendered to */
+        switch_xml_t xml_out_cur;        /* current tag in xml that is rendered to */
+        int xml_out_cur_off;                /* depth counter of xml_out_cur */
+        int tmp_i;                                        /* temporary counter, used for counting rows in the callback */
</ins><span class="cx"> } xml_odbc_session_helper_t;
</span><span class="cx">
</span><del>-static switch_status_t xml_odbc_render_template(xml_odbc_session_helper_t helper);
-static switch_status_t xml_odbc_render_tag(xml_odbc_session_helper_t helper);
</del><ins>+static switch_status_t xml_odbc_render_template(xml_odbc_session_helper_t *helper);
+static switch_status_t xml_odbc_render_tag(xml_odbc_session_helper_t *helper);
</ins><span class="cx">
</span><span class="cx"> #define XML_ODBC_SYNTAX "[debug_on|debug_off]"
</span><span class="cx">
</span><span class="lines">@@ -109,47 +115,52 @@
</span><span class="cx">         switch_xml_t xml_in_tmp;
</span><span class="cx">         int i;
</span><span class="cx">
</span><ins>+// only continue if helper->next_template_name is set !
+
</ins><span class="cx">         /* up the row counter */
</span><span class="cx">         helper->tmp_i++; // TODO: THIS WILL GO WRONG FOR NESTED QUERIES.. THINK ABOUT IT !!!
</span><span class="cx">
</span><del>-        /* loop through all columns and store them in helper->event->params */
</del><ins>+        /* loop through all columns and store them in helper->event->headers */
</ins><span class="cx">         for (i = 0; i < argc; i++) {
</span><span class="cx">                 switch_event_del_header(helper->event, columnName[i]);
</span><span class="cx">                 switch_event_add_header_string(helper->event, SWITCH_STACK_BOTTOM, columnName[i], argv[i]);
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /* render all children of xml_in */
</span><del>-        for (xml_in_tmp = helper->xml_in->child; xml_in_tmp; xml_in_tmp = xml_in_tmp->ordered) {
-                xml_odbc_render_tag(xml_in_tmp, helper->event, helper->xml_out, helper->off);
</del><ins>+        for (xml_in_tmp = helper->xml_in_cur->child; xml_in_tmp; xml_in_tmp = xml_in_tmp->ordered) {
+// only continue if helper->next_template_name is set !
+                helper->xml_in_cur = xml_in_tmp;
+                xml_odbc_render_tag(helper);
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t xml_odbc_do_break_to(xml_odbc_session_helper_t helper)
</del><ins>+static switch_status_t xml_odbc_do_break_to(xml_odbc_session_helper_t *helper)
</ins><span class="cx"> {
</span><del>-        char *name = (char *) switch_xml_attr_soft(helper->xml_in, "name");
</del><span class="cx">         char *value = (char *) switch_xml_attr_soft(helper->xml_in, "value");
</span><span class="cx">         char *new_value = switch_event_expand_headers(helper->event, value);
</span><span class="cx">
</span><del>-        helper->template_name = strdup(new_value);
</del><ins>+        helper->next_template_name = switch_core_strdup(helper->pool, new_value);
+
+        return SWITCH_STATUS_SUCCESS;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t xml_odbc_do_replace_header_value(xml_odbc_session_helper_t helper)
</del><ins>+static switch_status_t xml_odbc_do_if_header(xml_odbc_session_helper_t *helper)
</ins><span class="cx"> {
</span><ins>+        return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t xml_odbc_do_replace_header_value(xml_odbc_session_helper_t *helper)
+{
</ins><span class="cx">         char *old_header_value = NULL;
</span><del>-        char *name = (char *) switch_xml_attr_soft(helper->xml_in, "name");
-        char *when_name = (char *) switch_xml_attr_soft(helper->xml_in, "when_name");
-        char *when_value = (char *) switch_xml_attr_soft(helper->xml_in, "when_value");
-        char *value = (char *) switch_xml_attr_soft(helper->xml_in, "value");
-        char *new_value = switch_event_expand_headers(helper->event, value);
</del><ins>+        char *name = (char *) switch_xml_attr_soft(helper->xml_in_cur, "name");
+        char *when_name = (char *) switch_xml_attr_soft(helper->xml_in_cur, "when-name");
+        char *when_value = (char *) switch_xml_attr_soft(helper->xml_in_cur, "when-value");
+        char *value = (char *) switch_xml_attr_soft(helper->xml_in_cur, "value");
+        char *new_value = switch_event_expand_headers(helper->event, value); /////// LEAK !!!!!!!
</ins><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><span class="cx">
</span><del>-        if (switch_strlen_zero(when_name)) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no when_name is given\n", name);
-                goto done;
-        }
-
</del><span class="cx">         if (switch_strlen_zero(value)) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no value is given\n", name);
</span><span class="cx">                 goto done;
</span><span class="lines">@@ -168,11 +179,10 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t xml_odbc_do_query(xml_odbc_session_helper_t helper)
</del><ins>+static switch_status_t xml_odbc_do_query(xml_odbc_session_helper_t *helper)
</ins><span class="cx"> {
</span><del>-        char *name = (char *) switch_xml_attr_soft(helper->xml_in, "name");
-        char *value = (char *) switch_xml_attr_soft(helper->xml_in, "value");
-        char *empty_result_break_to = (char *) switch_xml_attr_soft(xml_in, "on-empty-result-break-to");
</del><ins>+        char *value = (char *) switch_xml_attr_soft(helper->xml_in_cur, "value");
+        char *empty_result_break_to = (char *) switch_xml_attr_soft(helper->xml_in_cur, "on-empty-result-break-to");
</ins><span class="cx">         char *new_value = switch_event_expand_headers(helper->event, value);
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><span class="cx">
</span><span class="lines">@@ -185,8 +195,9 @@
</span><span class="cx">                 goto done;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if (!switch_strlen_zero(empty_result_break_to) && helper.tmp_i == 0) {
-                helper->template_name = empty_result_break_to;
</del><ins>+        if (!switch_strlen_zero(empty_result_break_to) && helper->tmp_i == 0) {
+switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "HIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIRRRRRR [%s]\n", empty_result_break_to);
+                helper->next_template_name = empty_result_break_to;
</ins><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         status = SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -195,60 +206,69 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t xml_odbc_render_tag(xml_odbc_session_helper_t helper)
</del><ins>+static switch_status_t xml_odbc_render_tag(xml_odbc_session_helper_t *helper)
</ins><span class="cx"> {
</span><ins>+        switch_xml_t xml_in_tmp, xml_out_tmp;
</ins><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><del>-        switch_xml_t xml_in_tmp, xml_out_tmp;
</del><span class="cx">         int i;
</span><span class="cx">
</span><del>-        if (!strcasecmp(helper->xml_in->name, "xml-odbc-do")) {
-                char *name = (char *) switch_xml_attr_soft(helper->xml_in, "name");
</del><ins>+        /* special case xml-odbc-do */
+        if (!strcasecmp(helper->xml_in_cur->name, "xml-odbc-do")) {
+                char *name = (char *) switch_xml_attr_soft(helper->xml_in_cur, "name");
</ins><span class="cx">
</span><span class="cx">                 if (switch_strlen_zero(name)) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do because no name is given\n");
</span><span class="cx">                         goto done;
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                /* special cases for xml-odbc-do */
</del><span class="cx">                 if (!strcasecmp(name, "break-to")) {
</span><span class="cx">                         status = xml_odbc_do_break_to(helper);
</span><del>-                        goto done;
-                } else if (!strcasecmp(name, "replace-header-value")) {
-                        status = xml_odbc_do_replace_header_value(helper);
-                        goto done;
</del><ins>+                } else if (!strcasecmp(name, "if_header")) {
+                        status = xml_odbc_do_if_header(helper);
+//                } else if (!strcasecmp(name, "set-header-name")) {
+//                        status = xml_odbc_do_set_header_name(helper);
+//                } else if (!strcasecmp(name, "set-header-value")) {
+//                        status = xml_odbc_do_set_header_value(helper);
+                } else if (!strcasecmp(name, "replace_header_value")) {        // DEPRECATED REMOVE THIS FUNCTION!!
+                        status = xml_odbc_do_replace_header_value(helper);        // DEPRECATED REMOVE THIS FUNCTION!!
</ins><span class="cx">                 } else if (!strcasecmp(name, "query")) {
</span><span class="cx">                         status = xml_odbc_do_query(helper);
</span><del>-                        goto done;
</del><ins>+                } else {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring invalid xml-odbc-do name=[%s]\n", name);
</ins><span class="cx">                 }
</span><span class="cx">
</span><del>-                /* just copy xml_in to xml_out */
-                if (switch_strlen_zero(helper->xml_out->name)) {
-                        /* remove all children from helper->xml_out TODO DO THAT HERE !!!!!!!!!!!!!!!!!!!!!! */
</del><ins>+                goto done;
+        }
</ins><span class="cx">
</span><del>-                        helper->xml_out->name = strdup(helper->xml_in->name);
</del><ins>+        /* just copy xml_in_cur to xml_out */
+        if (switch_strlen_zero(helper->xml_out_cur->name)) {
</ins><span class="cx">
</span><del>-                } else if (!(xml_out_tmp = switch_xml_add_child_d(helper->xml_out, helper->xml_in->name, (helper->off)++))) {
-                        goto done;
-                }
</del><ins>+switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "COPYING helper->xml_in_cur->name=[%s]\n", helper->xml_in_cur->name);
+                helper->xml_out_cur->name = switch_core_strdup(helper->pool, helper->xml_in_cur->name);
</ins><span class="cx">
</span><del>-                /* copy all attrs */
-                for (i=0; helper->xml_in->attr[i]; i+=2) {
-                        char *tmp_attr;
-                        tmp_attr = switch_event_exapand_headers(helper->event, helper->xml_in->attr[i+1]);
-                        switch_xml_set_attr(helper->xml_out, helper->xml_in->attr[i], tmp_attr);
</del><ins>+        } else if (!(helper->xml_out_cur = switch_xml_add_child_d(helper->xml_out_cur, helper->xml_in_cur->name, helper->xml_out_cur_off++))) {
+                goto done;
+        }
</ins><span class="cx">
</span><del>-                        // if (tmp_attr != helper->xml_in->attr[i+1]) {
-                        //         switch_safe_free(tmp_attr);
-                        // }
-                }
</del><ins>+        /* copy all attrs */
+        for (i=0; helper->xml_in_cur->attr[i]; i+=2) {
+                char *tmp_attr;
+                tmp_attr = switch_event_expand_headers(helper->event, helper->xml_in_cur->attr[i+1]); // LEAK !!!!
+                switch_xml_set_attr(helper->xml_out_cur, helper->xml_in_cur->attr[i], tmp_attr);
</ins><span class="cx">
</span><del>-                /* render all children */
-                for (xml_in_tmp = helper->xml_in->child; xml_in_tmp; xml_in_tmp = xml_in_tmp->ordered) {
-                        if (xml_odbc_render_tag(xml_in_tmp, helper->event, event->xml_out, helper->off) != SWITCH_STATUS_SUCCESS) {
-                                goto done;
-                        }
</del><ins>+                // if (tmp_attr != helper->xml_in->attr[i+1]) {
+                //         switch_safe_free(tmp_attr);
+                // }
+        }
+
+        /* render all children */
+        xml_out_tmp = helper->xml_out_cur;
+        for (xml_in_tmp = helper->xml_in_cur->child; xml_in_tmp; xml_in_tmp = xml_in_tmp->ordered) {
+                helper->xml_in_cur = xml_in_tmp;
+                if (xml_odbc_render_tag(helper) != SWITCH_STATUS_SUCCESS) {
+                        goto done;
</ins><span class="cx">                 }
</span><del>-
</del><ins>+                helper->xml_out_cur = xml_out_tmp;
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         status = SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -257,136 +277,169 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t xml_odbc_render_template(xml_odbc_session_helper_t helper)
</del><ins>+static switch_status_t xml_odbc_render_template(xml_odbc_session_helper_t *helper)
</ins><span class="cx"> {
</span><del>-        switch_xml_t template_tag = NULL, sub_tag = NULL;
</del><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><span class="cx">
</span><del>-        if ((template_tag = switch_xml_find_child(globals.templates_tag, "template", "name", helper->template_name))) {
-                for (sub_tag = template_tag->child; sub_tag; sub_tag = sub_tag->ordered) {
-                        if (xml_odbc_render_tag(sub_tag, helper->event, helper->xml_out, helper->off) != SWITCH_STATUS_SUCCESS) {
-                        }
</del><ins>+        /* free helper -> xml_out, xml_out_cur, xml_in and xml_in_cur */
+        switch_xml_free(helper->xml_out);
+        switch_xml_free(helper->xml_out_cur);
+        switch_xml_free(helper->xml_in);
+        switch_xml_free(helper->xml_in_cur);
</ins><span class="cx">
</span><del>-                        if ((!switch_strlen_zero(helper->template_name))) {
-                                goto rewind;
-                        }
-                }
</del><ins>+        /* init helper-> xml_out and xml_out_cur */
+        if (!(helper->xml_out = helper->xml_out_cur = switch_xml_new(""))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Allocation Error!\n");
+                helper->next_template_name = "";
</ins><span class="cx">                 goto done;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        helper->template_name = "not_found";
</del><ins>+        /* reset helper->xml_out_cur_off */
+        helper->xml_out_cur_off = 0;
</ins><span class="cx">
</span><del>- rewind:
</del><ins>+        /* render child tag */
+        if ((helper->xml_in = helper->xml_in_cur = switch_xml_find_child(globals.templates_tag, "template", "name", helper->next_template_name))) {
+                helper->xml_in_cur = helper->xml_in_cur->child;
+                helper->next_template_name = "";
+                if (xml_odbc_render_tag(helper) != SWITCH_STATUS_SUCCESS) {
+                        goto done;
+                }
+
+                if (!switch_strlen_zero(helper->next_template_name)) {
+                        goto reset;
+                }
+
+        } else {
+                helper->next_template_name = "not_found";
+                goto reset;
+        }
+
+        status = SWITCH_STATUS_SUCCESS;
+        goto done;
+
+ reset:
</ins><span class="cx">         /* remove all children of helper->xml_out here */
</span><del>-        xml_odbc_render_template(helper);
</del><ins>+        status = xml_odbc_render_template(helper);
</ins><span class="cx">
</span><span class="cx"> done:
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_xml_t xml_odbc_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, void *user_data)
</del><ins>+static switch_xml_t xml_odbc_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *event, void *user_data)
</ins><span class="cx"> {
</span><span class="cx">         xml_binding_t *binding = (xml_binding_t *) user_data;
</span><span class="cx">         switch_event_header_t *hi;
</span><del>-        char *template_name;
</del><span class="cx">         switch_uuid_t uuid;
</span><span class="cx">         char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
</span><del>-        int off = 0, ret = 1;
-        switch_xml_t xml_out = NULL;
-        session_helper_t helper;
</del><ins>+        char *xml_char;                                                /* a char representation of the generated xml that will be written to temp file */
+        switch_memory_pool_t *pool;                        /* for easy memory cleanup */
+        xml_odbc_session_helper_t helper;        /* this helper is sent through all other functions to generate the xml */
+        switch_xml_t xml_out = NULL;                /* the xml that will be returned by this function */
+        char hostname[256] = "";                        /* added as header to switch_event_t event */
+        char filename[512] = "";                        /* the temporary, uuid-based filename */
+        int fd;
</ins><span class="cx">
</span><span class="cx">         if (!binding) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No bindings... sorry bud returning now\n");
</span><span class="cx">                 return NULL;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        /* create a new xml_out used to render the template into */
-        if (!(xml_out = switch_xml_new(""))) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Allocation Error\n");
-                return NULL;                
-        }
</del><ins>+        /* create a new memory pool for this session and put it in the helper */
+        switch_core_new_memory_pool(&pool);
+        helper.pool = pool;
</ins><span class="cx">
</span><del>-        /* find out what template to render, based on the section */
</del><ins>+        /* find out what template to render, and put it in helper.next_template_name */
</ins><span class="cx">         if (!strcmp(section, "configuration")) {
</span><del>-                template_name = strdup(globals.configuration_template_name);
</del><ins>+                helper.next_template_name = switch_core_strdup(pool, globals.configuration_template_name);
</ins><span class="cx">         } else if (!strcmp(section, "directory")) {
</span><del>-                template_name = strdup(globals.directory_template_name);
</del><ins>+                helper.next_template_name = switch_core_strdup(pool, globals.directory_template_name);
</ins><span class="cx">         } else if (!strcmp(section, "dialplan")) {
</span><del>-                template_name = strdup(globals.dialplan_template_name);
</del><ins>+                helper.next_template_name = switch_core_strdup(pool, globals.dialplan_template_name);
</ins><span class="cx">         } else if (!strcmp(section, "phrases")) {
</span><del>-                template_name = strdup(globals.phrases_template_name);
</del><ins>+                helper.next_template_name = switch_core_strdup(pool, globals.phrases_template_name);
</ins><span class="cx">         } else {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid section\n");
</span><del>-                return NULL;
</del><ins>+                goto cleanup;
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        /* add some headers to params */
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, "hostname", hostname);
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, "section", switch_str_nil(section));
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, "tag_name", switch_str_nil(tag_name));
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, "key_name", switch_str_nil(key_name));
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, "key_value", switch_str_nil(key_value));
</del><ins>+        /* add some headers to event and put it in the helper */
+        gethostname(hostname, sizeof(hostname));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, "hostname", hostname);
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, "section", switch_str_nil(section));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, "tag_name", switch_str_nil(tag_name));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, "key_name", switch_str_nil(key_name));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, "key_value", switch_str_nil(key_value));
+        helper.event = event;
</ins><span class="cx">
</span><span class="cx">         /* generate a new uuid */
</span><span class="cx">         switch_uuid_get(&uuid);
</span><span class="cx">         switch_uuid_format(uuid_str, &uuid);
</span><span class="cx">
</span><span class="cx">         /* generate a temporary filename to store the generated xml in */
</span><ins>+        // use switch_core_sprintf here ?!?!
</ins><span class="cx">         switch_snprintf(filename, sizeof(filename), "%s%s.tmp.xml", SWITCH_GLOBAL_dirs.temp_dir, uuid_str);
</span><span class="cx">
</span><del>-        /* debug print all switch_event_t params headers */
</del><ins>+        /* debug print all switch_event_t event headers */
</ins><span class="cx">         if (globals.debug == SWITCH_TRUE) {
</span><del>-                if ((hi = params->headers)) {
</del><ins>+                if ((hi = event->headers)) {
</ins><span class="cx">                         for (; hi; hi = hi->next) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "DEBUG in xml_odbc_search, header [%s]=[%s]\n", hi->name, hi->value);
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        /* put all necessary data in session_helper_t helper */
-        // switch_memory_pool_t
-        // template_name
-        // params
-        // xml_out
-        // &off
-
</del><span class="cx">         /* render xml from template */
</span><del>-        if (xml_odbc_render_template(helper) == SWITCH_STATUS_SUCCESS) {
</del><ins>+        if (xml_odbc_render_template(&helper) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Something went horribly wrong while generating an XML template!\n");
+                goto cleanup;
+        }
</ins><span class="cx">
</span><span class="cx">         /* dump the generated file to *xml_char */
</span><del>-        xml_char = switch_xml_toxml(xml_out, SWITCH_TRUE);
</del><ins>+        xml_char = switch_xml_toxml(helper.xml_out, SWITCH_FALSE);
</ins><span class="cx">
</span><span class="cx">         /* debug dump the generated xml to console */
</span><span class="cx">         if (globals.debug == SWITCH_TRUE) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Debug dump of generated XML:\n%s", xml_char);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        /* dump *xml_char to disk */
</del><ins>+        /* open (temporary) file */
+        if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR| S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening temp file [%s]!\n", filename);
+                goto cleanup;
+        }
</ins><span class="cx">
</span><del>-        /* close the file */
</del><ins>+        /* dump xml_char to (temporary) file */
+        if (!write(fd, xml_char, (unsigned) strlen(xml_char))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error writing generated XML to temp file [%s]!\n", filename);
+                close(fd);
+                goto cleanup;
+        }
</ins><span class="cx">
</span><del>-        /* free *xml_char */
</del><ins>+        /* close (temporary) file */
+        close(fd);
+        fd = -1;
</ins><span class="cx">
</span><del>-        /* free switch_memory_pool_t */
-
-        /* free helper ?? */
-
-        /* copy the generated xml to xml_out - this should never happen */
</del><ins>+        /* copy the (temporary) file to switch_xml_t xml_out - this should never go wrong */
</ins><span class="cx">         if (!(xml_out = switch_xml_parse_file(filename))) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result!\n");
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /* debug by leaving the file behind for review */
</span><del>-        if (keep_files_around) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Generated XML is in %s\n", filename);
</del><ins>+        if (globals.keep_files_around == SWITCH_TRUE) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Generated XML is in [%s]\n", filename);
</ins><span class="cx">         } else {
</span><span class="cx">                 if (unlink(filename) != 0) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Generated XML file [%s] delete failed\n", filename);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        /* other things to free ?! */
</del><ins>+ cleanup:
+        switch_core_destroy_memory_pool(&pool);
+        /* free *xml_char */
+        /* free helper ?? */
+        /* free filename */
</ins><span class="cx">
</span><ins>+ //done:
</ins><span class="cx">         return xml_out;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -445,8 +498,16 @@
</span><span class="cx">                         }
</span><span class="cx">                 } else if (!strcasecmp(var, "debug") && !switch_strlen_zero(val)) {
</span><span class="cx">                         if (!strcasecmp(val, "true") || !strcasecmp(val, "on")) {
</span><del>-                                debug = SWITCH_TRUE;
</del><ins>+                                globals.debug = SWITCH_TRUE;
+                        } else {
+                                globals.debug = SWITCH_FALSE;
</ins><span class="cx">                         }
</span><ins>+                } else if (!strcasecmp(var, "keep_files_around") && !switch_strlen_zero(val)) {
+                        if (!strcasecmp(val, "true") || !strcasecmp(val, "on")) {
+                                globals.keep_files_around = SWITCH_TRUE;
+                        } else {
+                                globals.keep_files_around = SWITCH_FALSE;
+                        }
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -469,13 +530,13 @@
</span><span class="cx">
</span><span class="cx">                         if (!strcasecmp(var, "template") && !switch_strlen_zero(val)) {
</span><span class="cx">                                 if (!strcmp(bname, "configuration")) {
</span><del>-                                        globals.configuration_template_name = strdup(val);
</del><ins>+                                        globals.configuration_template_name = switch_core_strdup(globals.pool, val);
</ins><span class="cx">                                 } else if (!strcmp(bname, "directory")) {
</span><del>-                                        globals.directory_template_name = strdup(val);
</del><ins>+                                        globals.directory_template_name = switch_core_strdup(globals.pool, val);
</ins><span class="cx">                                 } else if (!strcmp(bname, "dialplan")) {
</span><del>-                                        globals.dialplan_template_name = strdup(val);
</del><ins>+                                        globals.dialplan_template_name = switch_core_strdup(globals.pool, val);
</ins><span class="cx">                                 } else if (!strcmp(bname, "phrases")) {
</span><del>-                                        globals.phrases_template_name = strdup(val);
</del><ins>+                                        globals.phrases_template_name = switch_core_strdup(globals.pool, val);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid binding name [%s]\n", bname);
</span><span class="cx">                                         goto done;
</span><span class="lines">@@ -490,6 +551,7 @@
</span><span class="cx">                 }
</span><span class="cx">                 memset(binding, 0, sizeof(*binding));
</span><span class="cx">
</span><ins>+                // use switch_core_strdup on globals.pool here as well ?
</ins><span class="cx">                 binding->bindings = strdup(bname);
</span><span class="cx">
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML ODBC Fetch Function [%s]\n",
</span></span></pre></div>
<a id="freeswitchtrunkcontribmodxml_intmod_xml_odbcxml_odbcconfxml"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/xml_odbc.conf.xml (14116 => 14117)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/xml_odbc.conf.xml        2009-07-02 16:11:40 UTC (rev 14116)
+++ freeswitch/trunk/contrib/mod/xml_int/mod_xml_odbc/xml_odbc.conf.xml        2009-07-02 16:57:54 UTC (rev 14117)
</span><span class="lines">@@ -3,6 +3,7 @@
</span><span class="cx"> <settings>
</span><span class="cx"> <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
</span><span class="cx"> <param name="debug" value="true"/>
</span><ins>+ <param name="keep_files_around" value="true"/>
</ins><span class="cx"> </settings>
</span><span class="cx">
</span><span class="cx"> <bindings>
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>