<!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 &lt;leon@scarlet-internet.nl&gt;
</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 &lt;switch.h&gt;
</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 &quot;[debug_on|debug_off]&quot;
</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-&gt;next_template_name is set !
+
</ins><span class="cx">         /* up the row counter */
</span><span class="cx">         helper-&gt;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-&gt;event-&gt;params */
</del><ins>+        /* loop through all columns and store them in helper-&gt;event-&gt;headers */
</ins><span class="cx">         for (i = 0; i &lt; argc; i++) {
</span><span class="cx">                 switch_event_del_header(helper-&gt;event, columnName[i]);
</span><span class="cx">                 switch_event_add_header_string(helper-&gt;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-&gt;xml_in-&gt;child; xml_in_tmp; xml_in_tmp = xml_in_tmp-&gt;ordered) {
-                xml_odbc_render_tag(xml_in_tmp, helper-&gt;event, helper-&gt;xml_out, helper-&gt;off);
</del><ins>+        for (xml_in_tmp = helper-&gt;xml_in_cur-&gt;child; xml_in_tmp; xml_in_tmp = xml_in_tmp-&gt;ordered) {
+// only continue if helper-&gt;next_template_name is set !
+                helper-&gt;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-&gt;xml_in, &quot;name&quot;);
</del><span class="cx">         char *value = (char *) switch_xml_attr_soft(helper-&gt;xml_in, &quot;value&quot;);
</span><span class="cx">         char *new_value = switch_event_expand_headers(helper-&gt;event, value);
</span><span class="cx"> 
</span><del>-        helper-&gt;template_name = strdup(new_value);
</del><ins>+        helper-&gt;next_template_name = switch_core_strdup(helper-&gt;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-&gt;xml_in, &quot;name&quot;);
-        char *when_name = (char *) switch_xml_attr_soft(helper-&gt;xml_in, &quot;when_name&quot;);
-        char *when_value = (char *) switch_xml_attr_soft(helper-&gt;xml_in, &quot;when_value&quot;);
-        char *value = (char *) switch_xml_attr_soft(helper-&gt;xml_in, &quot;value&quot;);
-        char *new_value = switch_event_expand_headers(helper-&gt;event, value);
</del><ins>+        char *name = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;name&quot;);
+        char *when_name = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;when-name&quot;);
+        char *when_value = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;when-value&quot;);
+        char *value = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;value&quot;);
+        char *new_value = switch_event_expand_headers(helper-&gt;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, &quot;Ignoring xml-odbc-do name=[%s] because no when_name is given\n&quot;, 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, &quot;Ignoring xml-odbc-do name=[%s] because no value is given\n&quot;, 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-&gt;xml_in, &quot;name&quot;);
-        char *value = (char *) switch_xml_attr_soft(helper-&gt;xml_in, &quot;value&quot;);
-        char *empty_result_break_to = (char *) switch_xml_attr_soft(xml_in, &quot;on-empty-result-break-to&quot;);
</del><ins>+        char *value = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;value&quot;);
+        char *empty_result_break_to = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;on-empty-result-break-to&quot;);
</ins><span class="cx">         char *new_value = switch_event_expand_headers(helper-&gt;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) &amp;&amp; helper.tmp_i == 0) {
-                helper-&gt;template_name = empty_result_break_to;
</del><ins>+        if (!switch_strlen_zero(empty_result_break_to) &amp;&amp; helper-&gt;tmp_i == 0) {
+switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;HIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIEIRRRRRR [%s]\n&quot;, empty_result_break_to);
+                helper-&gt;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-&gt;xml_in-&gt;name, &quot;xml-odbc-do&quot;)) {
-                char *name = (char *) switch_xml_attr_soft(helper-&gt;xml_in, &quot;name&quot;);
</del><ins>+        /* special case xml-odbc-do */
+        if (!strcasecmp(helper-&gt;xml_in_cur-&gt;name, &quot;xml-odbc-do&quot;)) {
+                char *name = (char *) switch_xml_attr_soft(helper-&gt;xml_in_cur, &quot;name&quot;);
</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, &quot;Ignoring xml-odbc-do because no name is given\n&quot;);
</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, &quot;break-to&quot;)) {
</span><span class="cx">                         status = xml_odbc_do_break_to(helper);
</span><del>-                        goto done;
-                } else if (!strcasecmp(name, &quot;replace-header-value&quot;)) {
-                        status = xml_odbc_do_replace_header_value(helper);
-                        goto done;
</del><ins>+                } else if (!strcasecmp(name, &quot;if_header&quot;)) {
+                        status = xml_odbc_do_if_header(helper);
+//                } else if (!strcasecmp(name, &quot;set-header-name&quot;)) {
+//                        status = xml_odbc_do_set_header_name(helper);
+//                } else if (!strcasecmp(name, &quot;set-header-value&quot;)) {
+//                        status = xml_odbc_do_set_header_value(helper);
+                } else if (!strcasecmp(name, &quot;replace_header_value&quot;)) {        // DEPRECATED REMOVE THIS FUNCTION!!
+                        status = xml_odbc_do_replace_header_value(helper);        // DEPRECATED REMOVE THIS FUNCTION!!
</ins><span class="cx">                 } else if (!strcasecmp(name, &quot;query&quot;)) {
</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, &quot;Ignoring invalid xml-odbc-do name=[%s]\n&quot;, name);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                /* just copy xml_in to xml_out */
-                if (switch_strlen_zero(helper-&gt;xml_out-&gt;name)) {
-                        /* remove all children from helper-&gt;xml_out TODO DO THAT HERE !!!!!!!!!!!!!!!!!!!!!! */
</del><ins>+                goto done;
+        }
</ins><span class="cx"> 
</span><del>-                        helper-&gt;xml_out-&gt;name = strdup(helper-&gt;xml_in-&gt;name);
</del><ins>+        /* just copy xml_in_cur to xml_out */
+        if (switch_strlen_zero(helper-&gt;xml_out_cur-&gt;name)) {
</ins><span class="cx"> 
</span><del>-                } else if (!(xml_out_tmp = switch_xml_add_child_d(helper-&gt;xml_out, helper-&gt;xml_in-&gt;name, (helper-&gt;off)++))) {
-                        goto done;
-                }
</del><ins>+switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;COPYING helper-&gt;xml_in_cur-&gt;name=[%s]\n&quot;, helper-&gt;xml_in_cur-&gt;name);
+                helper-&gt;xml_out_cur-&gt;name = switch_core_strdup(helper-&gt;pool, helper-&gt;xml_in_cur-&gt;name);
</ins><span class="cx"> 
</span><del>-                /* copy all attrs */
-                for (i=0; helper-&gt;xml_in-&gt;attr[i]; i+=2) {
-                        char *tmp_attr;
-                        tmp_attr = switch_event_exapand_headers(helper-&gt;event, helper-&gt;xml_in-&gt;attr[i+1]);
-                        switch_xml_set_attr(helper-&gt;xml_out, helper-&gt;xml_in-&gt;attr[i], tmp_attr);
</del><ins>+        } else if (!(helper-&gt;xml_out_cur = switch_xml_add_child_d(helper-&gt;xml_out_cur, helper-&gt;xml_in_cur-&gt;name, helper-&gt;xml_out_cur_off++))) {
+                goto done;
+        }
</ins><span class="cx"> 
</span><del>-                        // if (tmp_attr != helper-&gt;xml_in-&gt;attr[i+1]) {
-                        //         switch_safe_free(tmp_attr);
-                        // }
-                }
</del><ins>+        /* copy all attrs */
+        for (i=0; helper-&gt;xml_in_cur-&gt;attr[i]; i+=2) {
+                char *tmp_attr;
+                tmp_attr = switch_event_expand_headers(helper-&gt;event, helper-&gt;xml_in_cur-&gt;attr[i+1]);         // LEAK !!!!
+                switch_xml_set_attr(helper-&gt;xml_out_cur, helper-&gt;xml_in_cur-&gt;attr[i], tmp_attr);
</ins><span class="cx"> 
</span><del>-                /* render all children */
-                for (xml_in_tmp = helper-&gt;xml_in-&gt;child; xml_in_tmp; xml_in_tmp = xml_in_tmp-&gt;ordered) {
-                        if (xml_odbc_render_tag(xml_in_tmp, helper-&gt;event, event-&gt;xml_out, helper-&gt;off) != SWITCH_STATUS_SUCCESS) {
-                                goto done;
-                        }
</del><ins>+                // if (tmp_attr != helper-&gt;xml_in-&gt;attr[i+1]) {
+                //         switch_safe_free(tmp_attr);
+                // }
+        }
+
+        /* render all children */
+        xml_out_tmp = helper-&gt;xml_out_cur;
+        for (xml_in_tmp = helper-&gt;xml_in_cur-&gt;child; xml_in_tmp; xml_in_tmp = xml_in_tmp-&gt;ordered) {
+                helper-&gt;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-&gt;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, &quot;template&quot;, &quot;name&quot;, helper-&gt;template_name))) {
-                for (sub_tag = template_tag-&gt;child; sub_tag; sub_tag = sub_tag-&gt;ordered) {
-                        if (xml_odbc_render_tag(sub_tag, helper-&gt;event, helper-&gt;xml_out, helper-&gt;off) != SWITCH_STATUS_SUCCESS) {
-                        }
</del><ins>+        /* free helper -&gt; xml_out, xml_out_cur, xml_in and xml_in_cur */
+        switch_xml_free(helper-&gt;xml_out);
+        switch_xml_free(helper-&gt;xml_out_cur);
+        switch_xml_free(helper-&gt;xml_in);
+        switch_xml_free(helper-&gt;xml_in_cur);
</ins><span class="cx"> 
</span><del>-                        if ((!switch_strlen_zero(helper-&gt;template_name))) {
-                                goto rewind;
-                        }
-                }
</del><ins>+        /* init helper-&gt; xml_out and xml_out_cur */
+        if (!(helper-&gt;xml_out = helper-&gt;xml_out_cur = switch_xml_new(&quot;&quot;))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Allocation Error!\n&quot;);
+                helper-&gt;next_template_name = &quot;&quot;;
</ins><span class="cx">                 goto done;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        helper-&gt;template_name = &quot;not_found&quot;;
</del><ins>+        /* reset helper-&gt;xml_out_cur_off */
+        helper-&gt;xml_out_cur_off = 0;
</ins><span class="cx"> 
</span><del>-  rewind:
</del><ins>+        /* render child tag */
+        if ((helper-&gt;xml_in = helper-&gt;xml_in_cur = switch_xml_find_child(globals.templates_tag, &quot;template&quot;, &quot;name&quot;, helper-&gt;next_template_name))) {
+                helper-&gt;xml_in_cur = helper-&gt;xml_in_cur-&gt;child;
+                helper-&gt;next_template_name = &quot;&quot;;
+                if (xml_odbc_render_tag(helper) != SWITCH_STATUS_SUCCESS) {
+                        goto done;
+                }
+
+                if (!switch_strlen_zero(helper-&gt;next_template_name)) {
+                        goto reset;
+                }
+
+        } else {
+                helper-&gt;next_template_name = &quot;not_found&quot;;
+                goto reset;
+        }
+
+        status = SWITCH_STATUS_SUCCESS;
+        goto done;
+
+  reset:
</ins><span class="cx">         /* remove all children of helper-&gt;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] = &quot;&quot;;                        /* added as header to switch_event_t event */
+        char filename[512] = &quot;&quot;;                        /* 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, &quot;No bindings... sorry bud returning now\n&quot;);
</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(&quot;&quot;))) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Allocation Error\n&quot;);
-                return NULL;                
-        }
</del><ins>+        /* create a new memory pool for this session and put it in the helper */
+        switch_core_new_memory_pool(&amp;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, &quot;configuration&quot;)) {
</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, &quot;directory&quot;)) {
</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, &quot;dialplan&quot;)) {
</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, &quot;phrases&quot;)) {
</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, &quot;Invalid section\n&quot;);
</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, &quot;hostname&quot;, hostname);
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, &quot;section&quot;, switch_str_nil(section));
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, &quot;tag_name&quot;, switch_str_nil(tag_name));
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, &quot;key_name&quot;, switch_str_nil(key_name));
-        switch_event_add_header_string(params, SWITCH_STACK_TOP, &quot;key_value&quot;, 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, &quot;hostname&quot;, hostname);
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, &quot;section&quot;, switch_str_nil(section));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, &quot;tag_name&quot;, switch_str_nil(tag_name));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, &quot;key_name&quot;, switch_str_nil(key_name));
+        switch_event_add_header_string(event, SWITCH_STACK_TOP, &quot;key_value&quot;, 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(&amp;uuid);
</span><span class="cx">         switch_uuid_format(uuid_str, &amp;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), &quot;%s%s.tmp.xml&quot;, 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-&gt;headers)) {
</del><ins>+                if ((hi = event-&gt;headers)) {
</ins><span class="cx">                         for (; hi; hi = hi-&gt;next) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;DEBUG in xml_odbc_search, header [%s]=[%s]\n&quot;, hi-&gt;name, hi-&gt;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
-        // &amp;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(&amp;helper) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Something went horribly wrong while generating an XML template!\n&quot;);
+                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, &quot;Debug dump of generated XML:\n%s&quot;, 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)) &lt; 0) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error opening temp file [%s]!\n&quot;, 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, &quot;Error writing generated XML to temp file [%s]!\n&quot;, 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, &quot;Error Parsing Result!\n&quot;);
</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, &quot;Generated XML is in %s\n&quot;, filename);
</del><ins>+        if (globals.keep_files_around == SWITCH_TRUE) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, &quot;Generated XML is in [%s]\n&quot;, 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, &quot;Generated XML file [%s] delete failed\n&quot;, 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(&amp;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, &quot;debug&quot;) &amp;&amp; !switch_strlen_zero(val)) {
</span><span class="cx">                         if (!strcasecmp(val, &quot;true&quot;) || !strcasecmp(val, &quot;on&quot;)) {
</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, &quot;keep_files_around&quot;) &amp;&amp; !switch_strlen_zero(val)) {
+                        if (!strcasecmp(val, &quot;true&quot;) || !strcasecmp(val, &quot;on&quot;)) {
+                                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, &quot;template&quot;) &amp;&amp; !switch_strlen_zero(val)) {
</span><span class="cx">                                 if (!strcmp(bname, &quot;configuration&quot;)) {
</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, &quot;directory&quot;)) {
</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, &quot;dialplan&quot;)) {
</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, &quot;phrases&quot;)) {
</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, &quot;Invalid binding name [%s]\n&quot;, 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-&gt;bindings = strdup(bname);
</span><span class="cx"> 
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Binding [%s] XML ODBC Fetch Function [%s]\n&quot;,
</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">   &lt;settings&gt;
</span><span class="cx">     &lt;param name=&quot;odbc-dsn&quot; value=&quot;freeswitch:freeswitch:secret&quot;/&gt;
</span><span class="cx">     &lt;param name=&quot;debug&quot; value=&quot;true&quot;/&gt;
</span><ins>+    &lt;param name=&quot;keep_files_around&quot; value=&quot;true&quot;/&gt;
</ins><span class="cx">   &lt;/settings&gt;
</span><span class="cx"> 
</span><span class="cx">   &lt;bindings&gt;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>