obsolete/jabber_s/xode.h

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