1/*
2 * "$Id: mxml.h,v 1.2 2008/06/08 15:10:08 rlk Exp $"
3 *
4 * Header file for mini-XML, a small XML-like file parsing library.
5 *
6 * Copyright 2003 by Michael Sweet.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 */
18
19/**
20 * @file gutenprint/mxml.h
21 * @brief Mini-XML XML parsing functions.
22 */
23
24/*
25 * Prevent multiple inclusion...
26 */
27
28#ifndef GUTENPRINT_MXML_H
29#  define GUTENPRINT_MXML_H
30
31/*
32 * Include necessary headers...
33 */
34
35#  include <stdio.h>
36#  include <stdlib.h>
37#  include <string.h>
38#  include <ctype.h>
39#  include <errno.h>
40
41
42/*
43 * Constants...
44 */
45
46#  define STP_MXML_WRAP		70	/* Wrap XML output at this column position */
47#  define STP_MXML_TAB		8	/* Tabs every N columns */
48
49#  define STP_MXML_NO_CALLBACK	0	/* Don't use a type callback */
50#  define STP_MXML_NO_PARENT	0	/* No parent for the node */
51
52#  define STP_MXML_DESCEND		1	/* Descend when finding/walking */
53#  define STP_MXML_NO_DESCEND	0	/* Don't descend when finding/walking */
54#  define STP_MXML_DESCEND_FIRST	-1	/* Descend for first find */
55
56#  define STP_MXML_WS_BEFORE_OPEN	0	/* Callback for before open tag */
57#  define STP_MXML_WS_AFTER_OPEN	1	/* Callback for after open tag */
58#  define STP_MXML_WS_BEFORE_CLOSE	2	/* Callback for before close tag */
59#  define STP_MXML_WS_AFTER_CLOSE	3	/* Callback for after close tag */
60
61#  define STP_MXML_ADD_BEFORE	0	/* Add node before specified node */
62#  define STP_MXML_ADD_AFTER	1	/* Add node after specified node */
63#  define STP_MXML_ADD_TO_PARENT	NULL	/* Add node relative to parent */
64
65
66/*
67 * Data types...
68 */
69
70typedef enum stp_mxml_type_e		/**** The XML node type. ****/
71{
72  STP_MXML_ELEMENT,				/* XML element with attributes */
73  STP_MXML_INTEGER,				/* Integer value */
74  STP_MXML_OPAQUE,				/* Opaque string */
75  STP_MXML_REAL,				/* Real value */
76  STP_MXML_TEXT				/* Text fragment */
77} stp_mxml_type_t;
78
79typedef struct stp_mxml_attr_s		/**** An XML element attribute value. ****/
80{
81  char	*name;				/* Attribute name */
82  char	*value;				/* Attribute value */
83} stp_mxml_attr_t;
84
85typedef struct stp_mxml_value_s		/**** An XML element value. ****/
86{
87  char		*name;			/* Name of element */
88  int		num_attrs;		/* Number of attributes */
89  stp_mxml_attr_t	*attrs;			/* Attributes */
90} stp_mxml_element_t;
91
92typedef struct stp_mxml_text_s		/**** An XML text value. ****/
93{
94  int		whitespace;		/* Leading whitespace? */
95  char		*string;		/* Fragment string */
96} stp_mxml_text_t;
97
98typedef union stp_mxml_value_u		/**** An XML node value. ****/
99{
100  stp_mxml_element_t	element;	/* Element */
101  int			integer;	/* Integer number */
102  char			*opaque;	/* Opaque string */
103  double		real;		/* Real number */
104  stp_mxml_text_t		text;		/* Text fragment */
105} stp_mxml_value_t;
106
107typedef struct stp_mxml_node_s stp_mxml_node_t;	/**** An XML node. ****/
108
109struct stp_mxml_node_s			/**** An XML node. ****/
110{
111  stp_mxml_type_t	type;			/* Node type */
112  stp_mxml_node_t	*next;			/* Next node under same parent */
113  stp_mxml_node_t	*prev;			/* Previous node under same parent */
114  stp_mxml_node_t	*parent;		/* Parent node */
115  stp_mxml_node_t	*child;			/* First child node */
116  stp_mxml_node_t	*last_child;		/* Last child node */
117  stp_mxml_value_t	value;			/* Node value */
118};
119
120
121/*
122 * C++ support...
123 */
124
125#  ifdef __cplusplus
126extern "C" {
127#  endif /* __cplusplus */
128
129/*
130 * Prototypes...
131 */
132
133extern void		stp_mxmlAdd(stp_mxml_node_t *parent, int where,
134			        stp_mxml_node_t *child, stp_mxml_node_t *node);
135extern void		stp_mxmlDelete(stp_mxml_node_t *node);
136extern const char	*stp_mxmlElementGetAttr(stp_mxml_node_t *node, const char *name);
137extern void		stp_mxmlElementSetAttr(stp_mxml_node_t *node, const char *name,
138			                   const char *value);
139extern stp_mxml_node_t	*stp_mxmlFindElement(stp_mxml_node_t *node, stp_mxml_node_t *top,
140			                 const char *name, const char *attr,
141					 const char *value, int descend);
142extern stp_mxml_node_t	*stp_mxmlLoadFile(stp_mxml_node_t *top, FILE *fp,
143			              stp_mxml_type_t (*cb)(stp_mxml_node_t *));
144extern stp_mxml_node_t	*stp_mxmlLoadFromFile(stp_mxml_node_t *top, const char *file,
145			              stp_mxml_type_t (*cb)(stp_mxml_node_t *));
146extern stp_mxml_node_t	*stp_mxmlLoadString(stp_mxml_node_t *top, const char *s,
147			                stp_mxml_type_t (*cb)(stp_mxml_node_t *));
148extern stp_mxml_node_t	*stp_mxmlNewElement(stp_mxml_node_t *parent, const char *name);
149extern stp_mxml_node_t	*stp_mxmlNewInteger(stp_mxml_node_t *parent, int integer);
150extern stp_mxml_node_t	*stp_mxmlNewOpaque(stp_mxml_node_t *parent, const char *opaque);
151extern stp_mxml_node_t	*stp_mxmlNewReal(stp_mxml_node_t *parent, double real);
152extern stp_mxml_node_t	*stp_mxmlNewText(stp_mxml_node_t *parent, int whitespace,
153			             const char *string);
154extern void		stp_mxmlRemove(stp_mxml_node_t *node);
155extern char		*stp_mxmlSaveAllocString(stp_mxml_node_t *node,
156			        	     int (*cb)(stp_mxml_node_t *, int));
157extern int		stp_mxmlSaveFile(stp_mxml_node_t *node, FILE *fp,
158			             int (*cb)(stp_mxml_node_t *, int));
159extern int		stp_mxmlSaveToFile(stp_mxml_node_t *node, const char *fp,
160			             int (*cb)(stp_mxml_node_t *, int));
161extern int		stp_mxmlSaveString(stp_mxml_node_t *node, char *buffer,
162			               int bufsize,
163			               int (*cb)(stp_mxml_node_t *, int));
164extern stp_mxml_node_t	*stp_mxmlWalkNext(stp_mxml_node_t *node, stp_mxml_node_t *top,
165			              int descend);
166extern stp_mxml_node_t	*stp_mxmlWalkPrev(stp_mxml_node_t *node, stp_mxml_node_t *top,
167			              int descend);
168
169
170/*
171 * C++ support...
172 */
173
174#  ifdef __cplusplus
175}
176#  endif /* __cplusplus */
177#endif /* !GUTENPRINT_MXML_H */
178
179
180/*
181 * End of "$Id: mxml.h,v 1.2 2008/06/08 15:10:08 rlk Exp $".
182 */
183