mi_writer.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2006 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  *
00023  * History:
00024  * ---------
00025  *  2006-09-25  first version (bogdan)
00026  */
00027 
00035 #include <stdio.h>
00036 #include <string.h>
00037 
00038 #include "../../str.h"
00039 #include "../../ut.h"
00040 #include "../../dprint.h"
00041 #include "../../lib/kmi/mi.h"
00042 #include "../../mem/mem.h"
00043 #include "mi_fifo.h"
00044 #include "fifo_fnc.h"
00045 #include "mi_parser.h"
00046 
00047 
00048 static char *mi_write_buffer = 0;
00049 static unsigned int mi_write_buffer_len = 0;
00050 static str  mi_fifo_indent;
00051 
00052 
00053 int mi_writer_init( unsigned int size , char *indent)
00054 {
00055         mi_write_buffer_len = size;
00056 
00057         mi_write_buffer = pkg_malloc(size);
00058         if(!mi_write_buffer){
00059                 LM_ERR("pkg_malloc cannot allocate any more memory!\n");
00060                 return -1;
00061         }
00062 
00063         if (indent==NULL || indent[0]==0 ) {
00064                 mi_fifo_indent.s = 0;
00065                 mi_fifo_indent.len = 0;
00066         } else {
00067                 mi_fifo_indent.s = indent;
00068                 mi_fifo_indent.len = strlen(indent);
00069         }
00070 
00071         return 0;
00072 }
00073 
00074 
00075 void mi_writer_destroy(void)
00076 {
00077         pkg_free(mi_write_buffer);
00078 }
00079 
00080 
00081 
00082 static inline int mi_write_node(str *buf, struct mi_node *node, int level)
00083 {
00084         struct mi_attr *attr;
00085         char *end;
00086         char *p;
00087 
00088         p = buf->s;
00089         end = buf->s + buf->len;
00090 
00091         /* write indents */
00092         if (mi_fifo_indent.s) {
00093                 if (p + level*mi_fifo_indent.len>end)
00094                         return -1;
00095                 for( ; level>0 ; level-- ) {
00096                         memcpy( p, mi_fifo_indent.s, mi_fifo_indent.len);
00097                         p += mi_fifo_indent.len;
00098                 }
00099         }
00100 
00101         /* name and value */
00102         if (node->name.s!=NULL) {
00103                 if (p+node->name.len+3>end)
00104                         return -1;
00105                 memcpy(p,node->name.s,node->name.len);
00106                 p += node->name.len;
00107                 *(p++) = MI_ATTR_VAL_SEP1;
00108                 *(p++) = MI_ATTR_VAL_SEP2;
00109                 *(p++) = ' ';
00110         }
00111         if (node->value.s!=NULL) {
00112                 if (p+node->value.len>end)
00113                         return -1;
00114                 memcpy(p,node->value.s,node->value.len);
00115                 p += node->value.len;
00116         }
00117 
00118         /* attributes */
00119         for( attr=node->attributes ; attr!=NULL ; attr=attr->next ) {
00120                 if (attr->name.s!=NULL) {
00121                         if (p+attr->name.len+2>end)
00122                                 return -1;
00123                         *(p++) = ' ';
00124                         memcpy(p,attr->name.s,attr->name.len);
00125                         p += attr->name.len;
00126                         *(p++) = '=';
00127                 }
00128                 if (attr->value.s!=NULL) {
00129                         if (p+attr->value.len>end)
00130                                 return -1;
00131                         memcpy(p,attr->value.s,attr->value.len);
00132                         p += attr->value.len;
00133                 }
00134         }
00135 
00136         if (p+1>end)
00137                 return -1;
00138         *(p++) = '\n';
00139 
00140         buf->len -= p-buf->s;
00141         buf->s = p;
00142         return 0;
00143 }
00144 
00145 
00146 
00147 static int recur_write_tree(FILE *stream, struct mi_node *tree, str *buf, int level)
00148 {
00149         for( ; tree ; tree=tree->next ) {
00150                 if (mi_write_node( buf, tree, level)!=0) {
00151                         /* buffer is full -> write it and reset buffer */
00152                         if (mi_fifo_reply( stream,"%.*s", buf->s-mi_write_buffer, mi_write_buffer)!=0)
00153                                 return -1;
00154                         buf->s = mi_write_buffer;
00155                         buf->len = mi_write_buffer_len;
00156                         if (mi_write_node( buf, tree, level)!=0) {
00157                                 LM_ERR("failed to write MI tree - line too long!\n");
00158                                 return -1;
00159                         }
00160                 }
00161                 if (tree->kids) {
00162                         if (recur_write_tree(stream, tree->kids, buf, level+1)<0)
00163                                 return -1;
00164                 }
00165         }
00166         return 0;
00167 }
00168 
00169 
00170 
00171 int mi_write_tree(FILE *stream, struct mi_root *tree)
00172 {
00173         str buf;
00174         str code;
00175 
00176         buf.s = mi_write_buffer;
00177         buf.len = mi_write_buffer_len;
00178 
00179         /* write the root node */
00180         code.s = int2str((unsigned long)tree->code, &code.len);
00181         if (code.len+tree->reason.len+1>buf.len) {
00182                 LM_ERR("failed to write - reason too long!\n");
00183                 return -1;
00184         }
00185         memcpy( buf.s, code.s, code.len);
00186         buf.s += code.len;
00187         *(buf.s++) = ' ';
00188         if (tree->reason.len) {
00189                 memcpy( buf.s, tree->reason.s, tree->reason.len);
00190                 buf.s += tree->reason.len;
00191         }
00192         *(buf.s++) = '\n';
00193         buf.len -= code.len + 1 + tree->reason.len+1;
00194 
00195         if (recur_write_tree(stream, tree->node.kids, &buf, 0)!=0)
00196                 return -1;
00197 
00198         if (buf.len<=0) {
00199                 LM_ERR("failed to write - EOC does not fit in!\n");
00200                 return -1;
00201         }
00202         *(buf.s++)='\n';
00203         buf.len--;
00204 
00205         if (mi_fifo_reply(stream,"%.*s",buf.s-mi_write_buffer,mi_write_buffer)!=0)
00206                 return -1;
00207 
00208         return 0;
00209 }