mxml.h

Go to the documentation of this file.
00001 /*
00002  * "$Id: mxml.h 307 2007-09-15 20:03:15Z mike $"
00003  *
00004  * Header file for Mini-XML, a small XML-like file parsing library.
00005  *
00006  * Copyright 2003-2007 by Michael Sweet.
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  */
00018 
00019 /*
00020  * Prevent multiple inclusion...
00021  */
00022 
00023 #ifndef _mxml_h_
00024 #  define _mxml_h_
00025 
00026 /*
00027  * Include necessary headers...
00028  */
00029 
00030 #  include <stdio.h>
00031 #  include <stdlib.h>
00032 #  include <string.h>
00033 #  include <ctype.h>
00034 #  include <errno.h>
00035 
00036 
00037 /*
00038  * Constants...
00039  */
00040 
00041 #  define MXML_TAB      8   /* Tabs every N columns */
00042 
00043 #  define MXML_NO_CALLBACK  0   /* Don't use a type callback */
00044 #  define MXML_INTEGER_CALLBACK mxml_integer_cb
00045                     /* Treat all data as integers */
00046 #  define MXML_OPAQUE_CALLBACK  mxml_opaque_cb
00047                     /* Treat all data as opaque */
00048 #  define MXML_REAL_CALLBACK    mxml_real_cb
00049                     /* Treat all data as real numbers */
00050 #  define MXML_TEXT_CALLBACK    0   /* Treat all data as text */
00051 #  define MXML_IGNORE_CALLBACK  mxml_ignore_cb
00052                     /* Ignore all non-element content */
00053 
00054 #  define MXML_NO_PARENT    0   /* No parent for the node */
00055 
00056 #  define MXML_DESCEND      1   /* Descend when finding/walking */
00057 #  define MXML_NO_DESCEND   0   /* Don't descend when finding/walking */
00058 #  define MXML_DESCEND_FIRST    -1  /* Descend for first find */
00059 
00060 #  define MXML_WS_BEFORE_OPEN   0   /* Callback for before open tag */
00061 #  define MXML_WS_AFTER_OPEN    1   /* Callback for after open tag */
00062 #  define MXML_WS_BEFORE_CLOSE  2   /* Callback for before close tag */
00063 #  define MXML_WS_AFTER_CLOSE   3   /* Callback for after close tag */
00064 
00065 #  define MXML_ADD_BEFORE   0   /* Add node before specified node */
00066 #  define MXML_ADD_AFTER    1   /* Add node after specified node */
00067 #  define MXML_ADD_TO_PARENT    NULL    /* Add node relative to parent */
00068 
00069 
00070 /*
00071  * Data types...
00072  */
00073 
00074 typedef enum mxml_sax_event_e       /**** SAX event type. ****/
00075 {
00076   MXML_SAX_CDATA,           /* CDATA node */
00077   MXML_SAX_COMMENT,         /* Comment node */
00078   MXML_SAX_DATA,            /* Data node */
00079   MXML_SAX_DIRECTIVE,           /* Processing directive node */
00080   MXML_SAX_ELEMENT_CLOSE,       /* Element closed */
00081   MXML_SAX_ELEMENT_OPEN         /* Element opened */
00082 } mxml_sax_event_t;
00083 
00084 typedef enum mxml_type_e        /**** The XML node type. ****/
00085 {
00086   MXML_IGNORE = -1,         /* Ignore/throw away node @since Mini-XML 2.3@ */
00087   MXML_ELEMENT,             /* XML element with attributes */
00088   MXML_INTEGER,             /* Integer value */
00089   MXML_OPAQUE,              /* Opaque string */
00090   MXML_REAL,                /* Real value */
00091   MXML_TEXT,                /* Text fragment */
00092   MXML_CUSTOM               /* Custom data @since Mini-XML 2.1@ */
00093 } mxml_type_t;
00094 
00095 typedef void (*mxml_custom_destroy_cb_t)(void *);
00096                     /**** Custom data destructor ****/
00097 
00098 typedef void (*mxml_error_cb_t)(const char *);
00099                     /**** Error callback function ****/
00100 
00101 typedef struct mxml_attr_s      /**** An XML element attribute value. ****/
00102 {
00103   char          *name;      /* Attribute name */
00104   char          *value;     /* Attribute value */
00105 } mxml_attr_t;
00106 
00107 typedef struct mxml_element_s       /**** An XML element value. ****/
00108 {
00109   char          *name;      /* Name of element */
00110   int           num_attrs;  /* Number of attributes */
00111   mxml_attr_t       *attrs;     /* Attributes */
00112 } mxml_element_t;
00113 
00114 typedef struct mxml_text_s      /**** An XML text value. ****/
00115 {
00116   int           whitespace; /* Leading whitespace? */
00117   char          *string;    /* Fragment string */
00118 } mxml_text_t;
00119 
00120 typedef struct mxml_custom_s        /**** An XML custom value. @since Mini-XML 2.1@ ****/
00121 {
00122   void          *data;      /* Pointer to (allocated) custom data */
00123   mxml_custom_destroy_cb_t destroy; /* Pointer to destructor function */
00124 } mxml_custom_t;
00125 
00126 typedef union mxml_value_u      /**** An XML node value. ****/
00127 {
00128   mxml_element_t    element;    /* Element */
00129   int           integer;    /* Integer number */
00130   char          *opaque;    /* Opaque string */
00131   double        real;       /* Real number */
00132   mxml_text_t       text;       /* Text fragment */
00133   mxml_custom_t     custom;     /* Custom data @since Mini-XML 2.1@ */
00134 } mxml_value_t;
00135 
00136 typedef struct mxml_node_s      /**** An XML node. ****/
00137 {
00138   mxml_type_t       type;       /* Node type */
00139   struct mxml_node_s    *next;      /* Next node under same parent */
00140   struct mxml_node_s    *prev;      /* Previous node under same parent */
00141   struct mxml_node_s    *parent;    /* Parent node */
00142   struct mxml_node_s    *child;     /* First child node */
00143   struct mxml_node_s    *last_child;    /* Last child node */
00144   mxml_value_t      value;      /* Node value */
00145   int           ref_count;  /* Use count */
00146   void          *user_data; /* User data */
00147 } mxml_node_t;
00148 
00149 typedef struct mxml_index_s     /**** An XML node index. ****/
00150 {
00151   char          *attr;      /* Attribute used for indexing or NULL */
00152   int           num_nodes;  /* Number of nodes in index */
00153   int           alloc_nodes;    /* Allocated nodes in index */
00154   int           cur_node;   /* Current node */
00155   mxml_node_t       **nodes;    /* Node array */
00156 } mxml_index_t;
00157 
00158 typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
00159                     /**** Custom data load callback function ****/
00160 
00161 typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
00162                     /**** Custom data save callback function ****/
00163 
00164 typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
00165                     /**** Load callback function ****/
00166 
00167 typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int);
00168                     /**** Save callback function ****/
00169 
00170 typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *);
00171                     /**** SAX callback function ****/
00172 
00173 
00174 /*
00175  * C++ support...
00176  */
00177 
00178 #  ifdef __cplusplus
00179 extern "C" {
00180 #  endif /* __cplusplus */
00181 
00182 /*
00183  * Prototypes...
00184  */
00185 
00186 extern void     mxmlAdd(mxml_node_t *parent, int where,
00187                     mxml_node_t *child, mxml_node_t *node);
00188 extern void     mxmlDelete(mxml_node_t *node);
00189 extern void     mxmlElementDeleteAttr(mxml_node_t *node,
00190                                   const char *name);
00191 extern const char   *mxmlElementGetAttr(mxml_node_t *node, const char *name);
00192 extern void     mxmlElementSetAttr(mxml_node_t *node, const char *name,
00193                                const char *value);
00194 extern void     mxmlElementSetAttrf(mxml_node_t *node, const char *name,
00195                                 const char *format, ...)
00196 #    ifdef __GNUC__
00197 __attribute__ ((__format__ (__printf__, 3, 4)))
00198 #    endif /* __GNUC__ */
00199 ;
00200 extern int      mxmlEntityAddCallback(int (*cb)(const char *name));
00201 extern const char   *mxmlEntityGetName(int val);
00202 extern int      mxmlEntityGetValue(const char *name);
00203 extern void     mxmlEntityRemoveCallback(int (*cb)(const char *name));
00204 extern mxml_node_t  *mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
00205                              const char *name, const char *attr,
00206                      const char *value, int descend);
00207 extern void     mxmlIndexDelete(mxml_index_t *ind);
00208 extern mxml_node_t  *mxmlIndexEnum(mxml_index_t *ind);
00209 extern mxml_node_t  *mxmlIndexFind(mxml_index_t *ind,
00210                            const char *element,
00211                            const char *value);
00212 extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element,
00213                           const char *attr);
00214 extern mxml_node_t  *mxmlIndexReset(mxml_index_t *ind);
00215 extern mxml_node_t  *mxmlLoadFd(mxml_node_t *top, int fd,
00216                         mxml_type_t (*cb)(mxml_node_t *));
00217 extern mxml_node_t  *mxmlLoadFile(mxml_node_t *top, FILE *fp,
00218                           mxml_type_t (*cb)(mxml_node_t *));
00219 extern mxml_node_t  *mxmlLoadString(mxml_node_t *top, const char *s,
00220                             mxml_type_t (*cb)(mxml_node_t *));
00221 extern mxml_node_t  *mxmlNewCDATA(mxml_node_t *parent, const char *string);
00222 extern mxml_node_t  *mxmlNewCustom(mxml_node_t *parent, void *data,
00223                            mxml_custom_destroy_cb_t destroy);
00224 extern mxml_node_t  *mxmlNewElement(mxml_node_t *parent, const char *name);
00225 extern mxml_node_t  *mxmlNewInteger(mxml_node_t *parent, int integer);
00226 extern mxml_node_t  *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
00227 extern mxml_node_t  *mxmlNewReal(mxml_node_t *parent, double real);
00228 extern mxml_node_t  *mxmlNewText(mxml_node_t *parent, int whitespace,
00229                          const char *string);
00230 extern mxml_node_t  *mxmlNewTextf(mxml_node_t *parent, int whitespace,
00231                           const char *format, ...)
00232 #    ifdef __GNUC__
00233 __attribute__ ((__format__ (__printf__, 3, 4)))
00234 #    endif /* __GNUC__ */
00235 ;
00236 extern mxml_node_t  *mxmlNewXML(const char *version);
00237 extern int      mxmlRelease(mxml_node_t *node);
00238 extern void     mxmlRemove(mxml_node_t *node);
00239 extern int      mxmlRetain(mxml_node_t *node);
00240 extern char     *mxmlSaveAllocString(mxml_node_t *node,
00241                              mxml_save_cb_t cb);
00242 extern int      mxmlSaveFd(mxml_node_t *node, int fd,
00243                        mxml_save_cb_t cb);
00244 extern int      mxmlSaveFile(mxml_node_t *node, FILE *fp,
00245                          mxml_save_cb_t cb);
00246 extern int      mxmlSaveString(mxml_node_t *node, char *buffer,
00247                            int bufsize, mxml_save_cb_t cb);
00248 extern mxml_node_t  *mxmlSAXLoadFd(mxml_node_t *top, int fd,
00249                            mxml_type_t (*cb)(mxml_node_t *),
00250                            mxml_sax_cb_t sax, void *sax_data);
00251 extern mxml_node_t  *mxmlSAXLoadFile(mxml_node_t *top, FILE *fp,
00252                              mxml_type_t (*cb)(mxml_node_t *),
00253                              mxml_sax_cb_t sax, void *sax_data);
00254 extern mxml_node_t  *mxmlSAXLoadString(mxml_node_t *top, const char *s,
00255                                mxml_type_t (*cb)(mxml_node_t *),
00256                                mxml_sax_cb_t sax, void *sax_data);
00257 extern int      mxmlSetCDATA(mxml_node_t *node, const char *data);
00258 extern int      mxmlSetCustom(mxml_node_t *node, void *data,
00259                           mxml_custom_destroy_cb_t destroy);
00260 extern void     mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
00261                                   mxml_custom_save_cb_t save);
00262 extern int      mxmlSetElement(mxml_node_t *node, const char *name);
00263 extern void     mxmlSetErrorCallback(mxml_error_cb_t cb);
00264 extern int      mxmlSetInteger(mxml_node_t *node, int integer);
00265 extern int      mxmlSetOpaque(mxml_node_t *node, const char *opaque);
00266 extern int      mxmlSetReal(mxml_node_t *node, double real);
00267 extern int      mxmlSetText(mxml_node_t *node, int whitespace,
00268                         const char *string);
00269 extern int      mxmlSetTextf(mxml_node_t *node, int whitespace,
00270                          const char *format, ...)
00271 #    ifdef __GNUC__
00272 __attribute__ ((__format__ (__printf__, 3, 4)))
00273 #    endif /* __GNUC__ */
00274 ;
00275 extern void     mxmlSetWrapMargin(int column);
00276 extern mxml_node_t  *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
00277                           int descend);
00278 extern mxml_node_t  *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
00279                           int descend);
00280 
00281 
00282 /*
00283  * Semi-private functions...
00284  */
00285 
00286 extern void     mxml_error(const char *format, ...);
00287 extern mxml_type_t  mxml_ignore_cb(mxml_node_t *node);
00288 extern mxml_type_t  mxml_integer_cb(mxml_node_t *node);
00289 extern mxml_type_t  mxml_opaque_cb(mxml_node_t *node);
00290 extern mxml_type_t  mxml_real_cb(mxml_node_t *node);
00291 
00292 
00293 /*
00294  * C++ support...
00295  */
00296 
00297 #  ifdef __cplusplus
00298 }
00299 #  endif /* __cplusplus */
00300 #endif /* !_mxml_h_ */

Generated by  doxygen 1.6.2