Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

libxode.h

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


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye