modules_k/xmpp/xode.h

Go to the documentation of this file.
00001 /*
00002  * $Id$
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 
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 #ifndef __OS_darwin
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 #endif
00085 
00086 /* --------------------------------------------------------- */
00087 /*                                                           */
00088 /* Pool-based memory management routines                     */
00089 /*                                                           */
00090 /* --------------------------------------------------------- */
00091 
00092 
00093 /* xode_pool_cleaner - callback type which is associated
00094    with a pool entry; invoked when the pool entry is 
00095    free'd */
00096 typedef void (*xode_pool_cleaner)(void *arg);
00097 
00098 
00099 /* pheap - singular allocation of memory */
00100 struct xode_pool_heap
00101 {
00102     void *block;
00103     int size, used;
00104 };
00105 
00106 /* pool - base node for a pool. Maintains a linked list
00107    of pool entries (pool_free) */
00108 typedef struct xode_pool_struct
00109 {
00110     int size;
00111     struct xode_pool_free *cleanup;
00112     struct xode_pool_heap *heap;
00113 } _xode_pool, *xode_pool;
00114 
00115 /* pool creation routines */
00116 xode_pool xode_pool_heap(int bytes);
00117 xode_pool xode_pool_new(void);
00118 
00119 /* pool wrappers for malloc */
00120 void *xode_pool_malloc  (xode_pool p, int size);
00121 void *xode_pool_mallocx (xode_pool p, int size, char c); 
00122 void *xode_pool_malloco (xode_pool p, int size); 
00123 
00124 /* wrapper around strdup, gains mem from pool */
00125 char *xode_pool_strdup  (xode_pool p, const char *src); 
00126 
00127 /* calls f(arg) before the pool is freed during cleanup */
00128 void xode_pool_cleanup  (xode_pool p, xode_pool_cleaner f, void *arg); 
00129 
00130 /* pool wrapper for free, called on a pool */
00131 void xode_pool_free     (xode_pool p); 
00132 
00133 /* returns total bytes allocated in this pool */
00134 int  xode_pool_size     (xode_pool p); 
00135 
00136 /* --------------------------------------------------------- */
00137 /*                                                           */
00138 /* XML escaping utils                                        */
00139 /*                                                           */
00140 /* --------------------------------------------------------- */
00141 char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
00142 char *xode_strunescape(xode_pool p, char *buf);
00143 
00144 
00145 /* --------------------------------------------------------- */
00146 /*                                                           */
00147 /* String pools (spool) functions                            */
00148 /*                                                           */
00149 /* --------------------------------------------------------- */
00150 struct xode_spool_node
00151 {
00152     char *c;
00153     struct xode_spool_node *next;
00154 };
00155 
00156 typedef struct xode_spool_struct
00157 {
00158     xode_pool p;
00159     int len;
00160     struct xode_spool_node *last;
00161     struct xode_spool_node *first;
00162 } *xode_spool;
00163 
00164 xode_spool xode_spool_new         ( void                          ); /* create a string pool on a new pool */
00165 xode_spool xode_spool_newfrompool ( xode_pool        p            ); /* create a string pool from an existing pool */
00166 xode_pool  xode_spool_getpool     ( const xode_spool s            ); /* returns the xode_pool used by this xode_spool */
00167 void       xode_spooler           ( xode_spool       s, ...       ); /* append all the char * args to the pool, terminate args with s again */
00168 char       *xode_spool_tostr      ( xode_spool       s            ); /* return a big string */
00169 void       xode_spool_add         ( xode_spool       s, char *str ); /* add a single char to the pool */
00170 char       *xode_spool_str        ( xode_pool        p, ...       ); /* wrap all the spooler stuff in one function, the happy fun ball! */
00171 int        xode_spool_getlen      ( const xode_spool s            ); /* returns the total length of the string contained in the pool */
00172 void       xode_spool_free        ( xode_spool       s            ); /* Free's the pool associated with the xode_spool */
00173 
00174 
00175 /* --------------------------------------------------------- */
00176 /*                                                           */
00177 /* xodes - Document Object Model                          */
00178 /*                                                           */
00179 /* --------------------------------------------------------- */
00180 #define XODE_TYPE_TAG    0
00181 #define XODE_TYPE_ATTRIB 1
00182 #define XODE_TYPE_CDATA  2
00183 
00184 #define XODE_TYPE_LAST   2
00185 #define XODE_TYPE_UNDEF  -1
00186 
00187 /* -------------------------------------------------------------------------- 
00188    Node structure. Do not use directly! Always use accessors macros 
00189    and methods!
00190    -------------------------------------------------------------------------- */
00191 typedef struct xode_struct
00192 {
00193      char*                name;
00194      unsigned short       type;
00195      char*                data;
00196      int                  data_sz;
00197      int                  complete;
00198      xode_pool            p;
00199      struct xode_struct*  parent;
00200      struct xode_struct*  firstchild; 
00201      struct xode_struct*  lastchild;
00202      struct xode_struct*  prev; 
00203      struct xode_struct*  next;
00204      struct xode_struct*  firstattrib;
00205      struct xode_struct*  lastattrib;
00206 } _xode, *xode;
00207 
00208 /* Node creation routines */
00209 xode  xode_wrap(xode x,const char* wrapper);
00210 xode  xode_new(const char* name);
00211 xode  xode_new_tag(const char* name);
00212 xode  xode_new_frompool(xode_pool p, const char* name);
00213 xode  xode_insert_tag(xode parent, const char* name); 
00214 xode  xode_insert_cdata(xode parent, const char* CDATA, unsigned int size);
00215 xode  xode_insert_tagnode(xode parent, xode node);
00216 void  xode_insert_node(xode parent, xode node);
00217 xode  xode_from_str(char *str, int len);
00218 xode  xode_from_strx(char *str, int len, int *err, int *pos);
00219 xode  xode_from_file(char *file);
00220 xode  xode_dup(xode x); /* duplicate x */
00221 xode  xode_dup_frompool(xode_pool p, xode x);
00222 
00223 /* Node Memory Pool */
00224 xode_pool xode_get_pool(xode node);
00225 
00226 /* Node editing */
00227 void xode_hide(xode child);
00228 void xode_hide_attrib(xode parent, const char *name);
00229 
00230 /* Node deletion routine, also frees the node pool! */
00231 void xode_free(xode node);
00232 
00233 /* Locates a child tag by name and returns it */
00234 xode  xode_get_tag(xode parent, const char* name);
00235 char* xode_get_tagdata(xode parent, const char* name);
00236 
00237 /* Attribute accessors */
00238 void     xode_put_attrib(xode owner, const char* name, const char* value);
00239 char*    xode_get_attrib(xode owner, const char* name);
00240 
00241 /* Bastard am I, but these are fun for internal use ;-) */
00242 void     xode_put_vattrib(xode owner, const char* name, void *value);
00243 void*    xode_get_vattrib(xode owner, const char* name);
00244 
00245 /* Node traversal routines */
00246 xode  xode_get_firstattrib(xode parent);
00247 xode  xode_get_firstchild(xode parent);
00248 xode  xode_get_lastchild(xode parent);
00249 xode  xode_get_nextsibling(xode sibling);
00250 xode  xode_get_prevsibling(xode sibling);
00251 xode  xode_get_parent(xode node);
00252 
00253 /* Node information routines */
00254 char*    xode_get_name(xode node);
00255 char*    xode_get_data(xode node);
00256 int      xode_get_datasz(xode node);
00257 int      xode_get_type(xode node);
00258 
00259 int      xode_has_children(xode node);
00260 int      xode_has_attribs(xode node);
00261 
00262 /* Node-to-string translation */
00263 char*    xode_to_str(xode node);
00264 char*    xode_to_prettystr(xode node);  /* Puts \t and \n to make a human-easily readable string */
00265 
00266 int      xode_cmp(xode a, xode b); /* compares a and b for equality */
00267 
00268 int      xode_to_file(char *file, xode node); /* writes node to file */
00269 
00270 
00271 /***********************
00272  * XSTREAM Section
00273  ***********************/
00274 
00275 #define XODE_STREAM_MAXNODE 1000000
00276 #define XODE_STREAM_MAXDEPTH 100
00277 
00278 #define XODE_STREAM_ROOT        0 /* root element */
00279 #define XODE_STREAM_NODE        1 /* normal node */
00280 #define XODE_STREAM_CLOSE       2 /* closed root node */
00281 #define XODE_STREAM_ERROR       4 /* parser error */
00282 
00283 typedef void (*xode_stream_onNode)(int type, xode x, void *arg); /* xstream event handler */
00284 
00285 typedef struct xode_stream_struct
00286 {
00287     XML_Parser parser;
00288     xode node;
00289     char *cdata;
00290     int cdata_len;
00291     xode_pool p;
00292     xode_stream_onNode f;
00293     void *arg;
00294     int status;
00295     int depth;
00296 } *xode_stream, _xode_stream;
00297 
00298 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg); /* create a new xstream */
00299 int xode_stream_eat(xode_stream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */
00300 
00301 /* convenience functions */
00302 
00303 #ifdef __cplusplus
00304 }
00305 #endif
00306 
00307 #endif /* INCL_LIBXODE_H */