xmpp/xode.h

Go to the documentation of this file.
00001 /*
00002  * $Id: xode.h 4494 2008-07-22 13:43:48Z henningw $
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  *  Jabber
00019  *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
00020  */
00021 
00022 /*! \file
00023  * \ingroup xmpp
00024  */
00025 
00026 #include <string.h>
00027 #include <stdlib.h>
00028 #include <sys/types.h>
00029 #include <stdio.h>
00030 #include <errno.h>
00031 #include <syslog.h>
00032 #include <strings.h>
00033 #include <unistd.h>
00034 #include <sys/time.h>
00035 
00036 #include "expat.h"
00037 #ifdef HAVE_CONFIG_H
00038 #include <config.h>
00039 #endif /* HAVE_CONFIG_H */
00040 
00041 /*
00042 **  Arrange to use either varargs or stdargs
00043 */
00044 
00045 #define MAXSHORTSTR  203      /* max short string length */
00046 #define QUAD_T unsigned long long
00047 
00048 #ifdef __STDC__
00049 
00050 #include <stdarg.h>
00051 
00052 # define VA_LOCAL_DECL  va_list ap;
00053 # define VA_START(f) va_start(ap, f)
00054 # define VA_END      va_end(ap)
00055 
00056 #else /* __STDC__ */
00057 
00058 # include <varargs.h>
00059 
00060 # define VA_LOCAL_DECL  va_list ap;
00061 # define VA_START(f) va_start(ap)
00062 # define VA_END      va_end(ap)
00063 
00064 #endif /* __STDC__ */
00065 
00066 
00067 #ifndef INCL_LIBXODE_H
00068 #define INCL_LIBXODE_H
00069 
00070 #ifdef __cplusplus
00071 extern "C" {
00072 #endif
00073 
00074 
00075 #ifndef HAVE_SNPRINTF
00076 extern int ap_snprintf(char *, size_t, const char *, ...);
00077 #define snprintf ap_snprintf
00078 #endif
00079 
00080 #ifndef HAVE_VSNPRINTF
00081 extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
00082 #define vsnprintf ap_vsnprintf
00083 #endif
00084 
00085 /* --------------------------------------------------------- */
00086 /*                                                           */
00087 /* Pool-based memory management routines                     */
00088 /*                                                           */
00089 /* --------------------------------------------------------- */
00090 
00091 
00092 /* xode_pool_cleaner - callback type which is associated
00093    with a pool entry; invoked when the pool entry is 
00094    free'd */
00095 typedef void (*xode_pool_cleaner)(void *arg);
00096 
00097 
00098 /* pheap - singular allocation of memory */
00099 struct xode_pool_heap
00100 {
00101     void *block;
00102     int size, used;
00103 };
00104 
00105 /* pool - base node for a pool. Maintains a linked list
00106    of pool entries (pool_free) */
00107 typedef struct xode_pool_struct
00108 {
00109     int size;
00110     struct xode_pool_free *cleanup;
00111     struct xode_pool_heap *heap;
00112 } _xode_pool, *xode_pool;
00113 
00114 /* pool creation routines */
00115 xode_pool xode_pool_heap(int bytes);
00116 xode_pool xode_pool_new(void);
00117 
00118 /* pool wrappers for malloc */
00119 void *xode_pool_malloc  (xode_pool p, int size);
00120 void *xode_pool_mallocx (xode_pool p, int size, char c); 
00121 void *xode_pool_malloco (xode_pool p, int size); 
00122 
00123 /* wrapper around strdup, gains mem from pool */
00124 char *xode_pool_strdup  (xode_pool p, const char *src); 
00125 
00126 /* calls f(arg) before the pool is freed during cleanup */
00127 void xode_pool_cleanup  (xode_pool p, xode_pool_cleaner f, void *arg); 
00128 
00129 /* pool wrapper for free, called on a pool */
00130 void xode_pool_free     (xode_pool p); 
00131 
00132 /* returns total bytes allocated in this pool */
00133 int  xode_pool_size     (xode_pool p); 
00134 
00135 /* --------------------------------------------------------- */
00136 /*                                                           */
00137 /* XML escaping utils                                        */
00138 /*                                                           */
00139 /* --------------------------------------------------------- */
00140 char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
00141 char *xode_strunescape(xode_pool p, char *buf);
00142 
00143 
00144 /* --------------------------------------------------------- */
00145 /*                                                           */
00146 /* String pools (spool) functions                            */
00147 /*                                                           */
00148 /* --------------------------------------------------------- */
00149 struct xode_spool_node
00150 {
00151     char *c;
00152     struct xode_spool_node *next;
00153 };
00154 
00155 typedef struct xode_spool_struct
00156 {
00157     xode_pool p;
00158     int len;
00159     struct xode_spool_node *last;
00160     struct xode_spool_node *first;
00161 } *xode_spool;
00162 
00163 xode_spool xode_spool_new         ( void                          ); /* create a string pool on a new pool */
00164 xode_spool xode_spool_newfrompool ( xode_pool        p            ); /* create a string pool from an existing pool */
00165 xode_pool  xode_spool_getpool     ( const xode_spool s            ); /* returns the xode_pool used by this xode_spool */
00166 void       xode_spooler           ( xode_spool       s, ...       ); /* append all the char * args to the pool, terminate args with s again */
00167 char       *xode_spool_tostr      ( xode_spool       s            ); /* return a big string */
00168 void       xode_spool_add         ( xode_spool       s, char *str ); /* add a single char to the pool */
00169 char       *xode_spool_str        ( xode_pool        p, ...       ); /* wrap all the spooler stuff in one function, the happy fun ball! */
00170 int        xode_spool_getlen      ( const xode_spool s            ); /* returns the total length of the string contained in the pool */
00171 void       xode_spool_free        ( xode_spool       s            ); /* Free's the pool associated with the xode_spool */
00172 
00173 
00174 /* --------------------------------------------------------- */
00175 /*                                                           */
00176 /* xodes - Document Object Model                          */
00177 /*                                                           */
00178 /* --------------------------------------------------------- */
00179 #define XODE_TYPE_TAG    0
00180 #define XODE_TYPE_ATTRIB 1
00181 #define XODE_TYPE_CDATA  2
00182 
00183 #define XODE_TYPE_LAST   2
00184 #define XODE_TYPE_UNDEF  -1
00185 
00186 /* -------------------------------------------------------------------------- 
00187    Node structure. Do not use directly! Always use accessors macros 
00188    and methods!
00189    -------------------------------------------------------------------------- */
00190 typedef struct xode_struct
00191 {
00192      char*                name;
00193      unsigned short       type;
00194      char*                data;
00195      int                  data_sz;
00196      int                  complete;
00197      xode_pool            p;
00198      struct xode_struct*  parent;
00199      struct xode_struct*  firstchild; 
00200      struct xode_struct*  lastchild;
00201      struct xode_struct*  prev; 
00202      struct xode_struct*  next;
00203      struct xode_struct*  firstattrib;
00204      struct xode_struct*  lastattrib;
00205 } _xode, *xode;
00206 
00207 /* Node creation routines */
00208 xode  xode_wrap(xode x,const char* wrapper);
00209 xode  xode_new(const char* name);
00210 xode  xode_new_tag(const char* name);
00211 xode  xode_new_frompool(xode_pool p, const char* name);
00212 xode  xode_insert_tag(xode parent, const char* name); 
00213 xode  xode_insert_cdata(xode parent, const char* CDATA, unsigned int size);
00214 xode  xode_insert_tagnode(xode parent, xode node);
00215 void  xode_insert_node(xode parent, xode node);
00216 xode  xode_from_str(char *str, int len);
00217 xode  xode_from_strx(char *str, int len, int *err, int *pos);
00218 xode  xode_from_file(char *file);
00219 xode  xode_dup(xode x); /* duplicate x */
00220 xode  xode_dup_frompool(xode_pool p, xode x);
00221 
00222 /* Node Memory Pool */
00223 xode_pool xode_get_pool(xode node);
00224 
00225 /* Node editing */
00226 void xode_hide(xode child);
00227 void xode_hide_attrib(xode parent, const char *name);
00228 
00229 /* Node deletion routine, also frees the node pool! */
00230 void xode_free(xode node);
00231 
00232 /* Locates a child tag by name and returns it */
00233 xode  xode_get_tag(xode parent, const char* name);
00234 char* xode_get_tagdata(xode parent, const char* name);
00235 
00236 /* Attribute accessors */
00237 void     xode_put_attrib(xode owner, const char* name, const char* value);
00238 char*    xode_get_attrib(xode owner, const char* name);
00239 
00240 /* Bastard am I, but these are fun for internal use ;-) */
00241 void     xode_put_vattrib(xode owner, const char* name, void *value);
00242 void*    xode_get_vattrib(xode owner, const char* name);
00243 
00244 /* Node traversal routines */
00245 xode  xode_get_firstattrib(xode parent);
00246 xode  xode_get_firstchild(xode parent);
00247 xode  xode_get_lastchild(xode parent);
00248 xode  xode_get_nextsibling(xode sibling);
00249 xode  xode_get_prevsibling(xode sibling);
00250 xode  xode_get_parent(xode node);
00251 
00252 /* Node information routines */
00253 char*    xode_get_name(xode node);
00254 char*    xode_get_data(xode node);
00255 int      xode_get_datasz(xode node);
00256 int      xode_get_type(xode node);
00257 
00258 int      xode_has_children(xode node);
00259 int      xode_has_attribs(xode node);
00260 
00261 /* Node-to-string translation */
00262 char*    xode_to_str(xode node);
00263 char*    xode_to_prettystr(xode node);  /* Puts \t and \n to make a human-easily readable string */
00264 
00265 int      xode_cmp(xode a, xode b); /* compares a and b for equality */
00266 
00267 int      xode_to_file(char *file, xode node); /* writes node to file */
00268 
00269 
00270 /***********************
00271  * XSTREAM Section
00272  ***********************/
00273 
00274 #define XODE_STREAM_MAXNODE 1000000
00275 #define XODE_STREAM_MAXDEPTH 100
00276 
00277 #define XODE_STREAM_ROOT        0 /* root element */
00278 #define XODE_STREAM_NODE        1 /* normal node */
00279 #define XODE_STREAM_CLOSE       2 /* closed root node */
00280 #define XODE_STREAM_ERROR       4 /* parser error */
00281 
00282 typedef void (*xode_stream_onNode)(int type, xode x, void *arg); /* xstream event handler */
00283 
00284 typedef struct xode_stream_struct
00285 {
00286     XML_Parser parser;
00287     xode node;
00288     char *cdata;
00289     int cdata_len;
00290     xode_pool p;
00291     xode_stream_onNode f;
00292     void *arg;
00293     int status;
00294     int depth;
00295 } *xode_stream, _xode_stream;
00296 
00297 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg); /* create a new xstream */
00298 int xode_stream_eat(xode_stream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */
00299 
00300 /* convenience functions */
00301 
00302 #ifdef __cplusplus
00303 }
00304 #endif
00305 
00306 #endif /* INCL_LIBXODE_H */

Generated on Fri May 25 00:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6