xode.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * $Id$
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Jabber
  19. * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
  20. */
  21. /*! \file
  22. * \ingroup xmpp
  23. */
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <sys/types.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <syslog.h>
  30. #include <strings.h>
  31. #include <unistd.h>
  32. #include <sys/time.h>
  33. #include "expat.h"
  34. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif /* HAVE_CONFIG_H */
  37. /*
  38. ** Arrange to use either varargs or stdargs
  39. */
  40. #define MAXSHORTSTR 203 /* max short string length */
  41. #define QUAD_T unsigned long long
  42. #ifdef __STDC__
  43. #include <stdarg.h>
  44. # define VA_LOCAL_DECL va_list ap;
  45. # define VA_START(f) va_start(ap, f)
  46. # define VA_END va_end(ap)
  47. #else /* __STDC__ */
  48. # include <varargs.h>
  49. # define VA_LOCAL_DECL va_list ap;
  50. # define VA_START(f) va_start(ap)
  51. # define VA_END va_end(ap)
  52. #endif /* __STDC__ */
  53. #ifndef INCL_LIBXODE_H
  54. #define INCL_LIBXODE_H
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #ifndef __OS_darwin
  59. #ifndef HAVE_SNPRINTF
  60. extern int ap_snprintf(char *, size_t, const char *, ...);
  61. #define snprintf ap_snprintf
  62. #endif
  63. #ifndef HAVE_VSNPRINTF
  64. extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
  65. #define vsnprintf ap_vsnprintf
  66. #endif
  67. #endif
  68. /* --------------------------------------------------------- */
  69. /* */
  70. /* Pool-based memory management routines */
  71. /* */
  72. /* --------------------------------------------------------- */
  73. /* xode_pool_cleaner - callback type which is associated
  74. with a pool entry; invoked when the pool entry is
  75. free'd */
  76. typedef void (*xode_pool_cleaner)(void *arg);
  77. /* pheap - singular allocation of memory */
  78. struct xode_pool_heap
  79. {
  80. void *block;
  81. int size, used;
  82. };
  83. /* pool - base node for a pool. Maintains a linked list
  84. of pool entries (pool_free) */
  85. typedef struct xode_pool_struct
  86. {
  87. int size;
  88. struct xode_pool_free *cleanup;
  89. struct xode_pool_heap *heap;
  90. } _xode_pool, *xode_pool;
  91. /* pool creation routines */
  92. xode_pool xode_pool_heap(int bytes);
  93. xode_pool xode_pool_new(void);
  94. /* pool wrappers for malloc */
  95. void *xode_pool_malloc (xode_pool p, int size);
  96. void *xode_pool_mallocx (xode_pool p, int size, char c);
  97. void *xode_pool_malloco (xode_pool p, int size);
  98. /* wrapper around strdup, gains mem from pool */
  99. char *xode_pool_strdup (xode_pool p, const char *src);
  100. /* calls f(arg) before the pool is freed during cleanup */
  101. void xode_pool_cleanup (xode_pool p, xode_pool_cleaner f, void *arg);
  102. /* pool wrapper for free, called on a pool */
  103. void xode_pool_free (xode_pool p);
  104. /* returns total bytes allocated in this pool */
  105. int xode_pool_size (xode_pool p);
  106. /* --------------------------------------------------------- */
  107. /* */
  108. /* XML escaping utils */
  109. /* */
  110. /* --------------------------------------------------------- */
  111. char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
  112. char *xode_strunescape(xode_pool p, char *buf);
  113. /* --------------------------------------------------------- */
  114. /* */
  115. /* String pools (spool) functions */
  116. /* */
  117. /* --------------------------------------------------------- */
  118. struct xode_spool_node
  119. {
  120. char *c;
  121. struct xode_spool_node *next;
  122. };
  123. typedef struct xode_spool_struct
  124. {
  125. xode_pool p;
  126. int len;
  127. struct xode_spool_node *last;
  128. struct xode_spool_node *first;
  129. } *xode_spool;
  130. xode_spool xode_spool_new ( void ); /* create a string pool on a new pool */
  131. xode_spool xode_spool_newfrompool ( xode_pool p ); /* create a string pool from an existing pool */
  132. xode_pool xode_spool_getpool ( const xode_spool s ); /* returns the xode_pool used by this xode_spool */
  133. void xode_spooler ( xode_spool s, ... ); /* append all the char * args to the pool, terminate args with s again */
  134. char *xode_spool_tostr ( xode_spool s ); /* return a big string */
  135. void xode_spool_add ( xode_spool s, char *str ); /* add a single char to the pool */
  136. char *xode_spool_str ( xode_pool p, ... ); /* wrap all the spooler stuff in one function, the happy fun ball! */
  137. int xode_spool_getlen ( const xode_spool s ); /* returns the total length of the string contained in the pool */
  138. void xode_spool_free ( xode_spool s ); /* Free's the pool associated with the xode_spool */
  139. /* --------------------------------------------------------- */
  140. /* */
  141. /* xodes - Document Object Model */
  142. /* */
  143. /* --------------------------------------------------------- */
  144. #define XODE_TYPE_TAG 0
  145. #define XODE_TYPE_ATTRIB 1
  146. #define XODE_TYPE_CDATA 2
  147. #define XODE_TYPE_LAST 2
  148. #define XODE_TYPE_UNDEF -1
  149. /* --------------------------------------------------------------------------
  150. Node structure. Do not use directly! Always use accessors macros
  151. and methods!
  152. -------------------------------------------------------------------------- */
  153. typedef struct xode_struct
  154. {
  155. char* name;
  156. unsigned short type;
  157. char* data;
  158. int data_sz;
  159. int complete;
  160. xode_pool p;
  161. struct xode_struct* parent;
  162. struct xode_struct* firstchild;
  163. struct xode_struct* lastchild;
  164. struct xode_struct* prev;
  165. struct xode_struct* next;
  166. struct xode_struct* firstattrib;
  167. struct xode_struct* lastattrib;
  168. } _xode, *xode;
  169. /* Node creation routines */
  170. xode xode_wrap(xode x,const char* wrapper);
  171. xode xode_new(const char* name);
  172. xode xode_new_tag(const char* name);
  173. xode xode_new_frompool(xode_pool p, const char* name);
  174. xode xode_insert_tag(xode parent, const char* name);
  175. xode xode_insert_cdata(xode parent, const char* CDATA, unsigned int size);
  176. xode xode_insert_tagnode(xode parent, xode node);
  177. void xode_insert_node(xode parent, xode node);
  178. xode xode_from_str(char *str, int len);
  179. xode xode_from_strx(char *str, int len, int *err, int *pos);
  180. xode xode_from_file(char *file);
  181. xode xode_dup(xode x); /* duplicate x */
  182. xode xode_dup_frompool(xode_pool p, xode x);
  183. /* Node Memory Pool */
  184. xode_pool xode_get_pool(xode node);
  185. /* Node editing */
  186. void xode_hide(xode child);
  187. void xode_hide_attrib(xode parent, const char *name);
  188. /* Node deletion routine, also frees the node pool! */
  189. void xode_free(xode node);
  190. /* Locates a child tag by name and returns it */
  191. xode xode_get_tag(xode parent, const char* name);
  192. char* xode_get_tagdata(xode parent, const char* name);
  193. /* Attribute accessors */
  194. void xode_put_attrib(xode owner, const char* name, const char* value);
  195. char* xode_get_attrib(xode owner, const char* name);
  196. /* Bastard am I, but these are fun for internal use ;-) */
  197. void xode_put_vattrib(xode owner, const char* name, void *value);
  198. void* xode_get_vattrib(xode owner, const char* name);
  199. /* Node traversal routines */
  200. xode xode_get_firstattrib(xode parent);
  201. xode xode_get_firstchild(xode parent);
  202. xode xode_get_lastchild(xode parent);
  203. xode xode_get_nextsibling(xode sibling);
  204. xode xode_get_prevsibling(xode sibling);
  205. xode xode_get_parent(xode node);
  206. /* Node information routines */
  207. char* xode_get_name(xode node);
  208. char* xode_get_data(xode node);
  209. int xode_get_datasz(xode node);
  210. int xode_get_type(xode node);
  211. int xode_has_children(xode node);
  212. int xode_has_attribs(xode node);
  213. /* Node-to-string translation */
  214. char* xode_to_str(xode node);
  215. char* xode_to_prettystr(xode node); /* Puts \t and \n to make a human-easily readable string */
  216. int xode_cmp(xode a, xode b); /* compares a and b for equality */
  217. int xode_to_file(char *file, xode node); /* writes node to file */
  218. /***********************
  219. * XSTREAM Section
  220. ***********************/
  221. #define XODE_STREAM_MAXNODE 1000000
  222. #define XODE_STREAM_MAXDEPTH 100
  223. #define XODE_STREAM_ROOT 0 /* root element */
  224. #define XODE_STREAM_NODE 1 /* normal node */
  225. #define XODE_STREAM_CLOSE 2 /* closed root node */
  226. #define XODE_STREAM_ERROR 4 /* parser error */
  227. typedef void (*xode_stream_onNode)(int type, xode x, void *arg); /* xstream event handler */
  228. typedef struct xode_stream_struct
  229. {
  230. XML_Parser parser;
  231. xode node;
  232. char *cdata;
  233. int cdata_len;
  234. xode_pool p;
  235. xode_stream_onNode f;
  236. void *arg;
  237. int status;
  238. int depth;
  239. } *xode_stream, _xode_stream;
  240. xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg); /* create a new xstream */
  241. int xode_stream_eat(xode_stream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */
  242. /* convenience functions */
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif /* INCL_LIBXODE_H */