File: xode.h

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (307 lines) | stat: -rw-r--r-- 9,988 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * $Id$
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 *  Jabber
 *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
 */

/*! \file
 * \ingroup xmpp
 */

#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <strings.h>
#include <unistd.h>
#include <sys/time.h>

#include "expat.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

/*
**  Arrange to use either varargs or stdargs
*/

#define MAXSHORTSTR	203		/* max short string length */
#define QUAD_T	unsigned long long

#ifdef __STDC__

#include <stdarg.h>

# define VA_LOCAL_DECL	va_list ap;
# define VA_START(f)	va_start(ap, f)
# define VA_END		va_end(ap)

#else /* __STDC__ */

# include <varargs.h>

# define VA_LOCAL_DECL	va_list ap;
# define VA_START(f)	va_start(ap)
# define VA_END		va_end(ap)

#endif /* __STDC__ */


#ifndef INCL_LIBXODE_H
#define INCL_LIBXODE_H

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __OS_darwin
#ifndef HAVE_SNPRINTF
extern int ap_snprintf(char *, size_t, const char *, ...);
#define snprintf ap_snprintf
#endif

#ifndef HAVE_VSNPRINTF
extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
#define vsnprintf ap_vsnprintf
#endif
#endif

/* --------------------------------------------------------- */
/*                                                           */
/* Pool-based memory management routines                     */
/*                                                           */
/* --------------------------------------------------------- */


/* xode_pool_cleaner - callback type which is associated
   with a pool entry; invoked when the pool entry is 
   free'd */
typedef void (*xode_pool_cleaner)(void *arg);


/* pheap - singular allocation of memory */
struct xode_pool_heap
{
    void *block;
    int size, used;
};

/* pool - base node for a pool. Maintains a linked list
   of pool entries (pool_free) */
typedef struct xode_pool_struct
{
    int size;
    struct xode_pool_free *cleanup;
    struct xode_pool_heap *heap;
} _xode_pool, *xode_pool;

/* pool creation routines */
xode_pool xode_pool_heap(int bytes);
xode_pool xode_pool_new(void);

/* pool wrappers for malloc */
void *xode_pool_malloc  (xode_pool p, int size);
void *xode_pool_mallocx (xode_pool p, int size, char c); 
void *xode_pool_malloco (xode_pool p, int size); 

/* wrapper around strdup, gains mem from pool */
char *xode_pool_strdup  (xode_pool p, const char *src); 

/* calls f(arg) before the pool is freed during cleanup */
void xode_pool_cleanup  (xode_pool p, xode_pool_cleaner f, void *arg); 

/* pool wrapper for free, called on a pool */
void xode_pool_free     (xode_pool p); 

/* returns total bytes allocated in this pool */
int  xode_pool_size     (xode_pool p); 

/* --------------------------------------------------------- */
/*                                                           */
/* XML escaping utils                                        */
/*                                                           */
/* --------------------------------------------------------- */
char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
char *xode_strunescape(xode_pool p, char *buf);


/* --------------------------------------------------------- */
/*                                                           */
/* String pools (spool) functions                            */
/*                                                           */
/* --------------------------------------------------------- */
struct xode_spool_node
{
    char *c;
    struct xode_spool_node *next;
};

typedef struct xode_spool_struct
{
    xode_pool p;
    int len;
    struct xode_spool_node *last;
    struct xode_spool_node *first;
} *xode_spool;

xode_spool xode_spool_new         ( void                          ); /* create a string pool on a new pool */
xode_spool xode_spool_newfrompool ( xode_pool        p            ); /* create a string pool from an existing pool */
xode_pool  xode_spool_getpool     ( const xode_spool s            ); /* returns the xode_pool used by this xode_spool */
void       xode_spooler           ( xode_spool       s, ...       ); /* append all the char * args to the pool, terminate args with s again */
char       *xode_spool_tostr      ( xode_spool       s            ); /* return a big string */
void       xode_spool_add         ( xode_spool       s, char *str ); /* add a single char to the pool */
char       *xode_spool_str        ( xode_pool        p, ...       ); /* wrap all the spooler stuff in one function, the happy fun ball! */
int        xode_spool_getlen      ( const xode_spool s            ); /* returns the total length of the string contained in the pool */
void       xode_spool_free        ( xode_spool       s            ); /* Free's the pool associated with the xode_spool */


/* --------------------------------------------------------- */
/*                                                           */
/* xodes - Document Object Model                          */
/*                                                           */
/* --------------------------------------------------------- */
#define XODE_TYPE_TAG    0
#define XODE_TYPE_ATTRIB 1
#define XODE_TYPE_CDATA  2

#define XODE_TYPE_LAST   2
#define XODE_TYPE_UNDEF  -1

/* -------------------------------------------------------------------------- 
   Node structure. Do not use directly! Always use accessors macros 
   and methods!
   -------------------------------------------------------------------------- */
typedef struct xode_struct
{
     char*                name;
     unsigned short       type;
     char*                data;
     int                  data_sz;
     int                  complete;
     xode_pool            p;
     struct xode_struct*  parent;
     struct xode_struct*  firstchild; 
     struct xode_struct*  lastchild;
     struct xode_struct*  prev; 
     struct xode_struct*  next;
     struct xode_struct*  firstattrib;
     struct xode_struct*  lastattrib;
} _xode, *xode;

/* Node creation routines */
xode  xode_wrap(xode x,const char* wrapper);
xode  xode_new(const char* name);
xode  xode_new_tag(const char* name);
xode  xode_new_frompool(xode_pool p, const char* name);
xode  xode_insert_tag(xode parent, const char* name); 
xode  xode_insert_cdata(xode parent, const char* CDATA, unsigned int size);
xode  xode_insert_tagnode(xode parent, xode node);
void  xode_insert_node(xode parent, xode node);
xode  xode_from_str(char *str, int len);
xode  xode_from_strx(char *str, int len, int *err, int *pos);
xode  xode_from_file(char *file);
xode  xode_dup(xode x); /* duplicate x */
xode  xode_dup_frompool(xode_pool p, xode x);

/* Node Memory Pool */
xode_pool xode_get_pool(xode node);

/* Node editing */
void xode_hide(xode child);
void xode_hide_attrib(xode parent, const char *name);

/* Node deletion routine, also frees the node pool! */
void xode_free(xode node);

/* Locates a child tag by name and returns it */
xode  xode_get_tag(xode parent, const char* name);
char* xode_get_tagdata(xode parent, const char* name);

/* Attribute accessors */
void     xode_put_attrib(xode owner, const char* name, const char* value);
char*    xode_get_attrib(xode owner, const char* name);

/* Bastard am I, but these are fun for internal use ;-) */
void     xode_put_vattrib(xode owner, const char* name, void *value);
void*    xode_get_vattrib(xode owner, const char* name);

/* Node traversal routines */
xode  xode_get_firstattrib(xode parent);
xode  xode_get_firstchild(xode parent);
xode  xode_get_lastchild(xode parent);
xode  xode_get_nextsibling(xode sibling);
xode  xode_get_prevsibling(xode sibling);
xode  xode_get_parent(xode node);

/* Node information routines */
char*    xode_get_name(xode node);
char*    xode_get_data(xode node);
int      xode_get_datasz(xode node);
int      xode_get_type(xode node);

int      xode_has_children(xode node);
int      xode_has_attribs(xode node);

/* Node-to-string translation */
char*    xode_to_str(xode node);
char*    xode_to_prettystr(xode node);  /* Puts \t and \n to make a human-easily readable string */

int      xode_cmp(xode a, xode b); /* compares a and b for equality */

int      xode_to_file(char *file, xode node); /* writes node to file */


/***********************
 * XSTREAM Section
 ***********************/

#define XODE_STREAM_MAXNODE 1000000
#define XODE_STREAM_MAXDEPTH 100

#define XODE_STREAM_ROOT        0 /* root element */
#define XODE_STREAM_NODE        1 /* normal node */
#define XODE_STREAM_CLOSE       2 /* closed root node */
#define XODE_STREAM_ERROR       4 /* parser error */

typedef void (*xode_stream_onNode)(int type, xode x, void *arg); /* xstream event handler */

typedef struct xode_stream_struct
{
    XML_Parser parser;
    xode node;
    char *cdata;
    int cdata_len;
    xode_pool p;
    xode_stream_onNode f;
    void *arg;
    int status;
    int depth;
} *xode_stream, _xode_stream;

xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg); /* create a new xstream */
int xode_stream_eat(xode_stream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */

/* convenience functions */

#ifdef __cplusplus
}
#endif

#endif /* INCL_LIBXODE_H */