File: libxml2-writer-macros.h

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (200 lines) | stat: -rw-r--r-- 6,957 bytes parent folder | download | duplicates (11)
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
/* libguestfs
 * Copyright (C) 2009-2019 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * These macros make it easier to write XML.  To use them correctly
 * you must be aware of these assumptions:
 *
 * =over 4
 *
 * =item *
 *
 * The C<xmlTextWriterPtr> is called C<xo>.  It is used implicitly
 * by all the macros.
 *
 * =item *
 *
 * On failure, a function called C<xml_error> is called which you must
 * define (usually as a macro).  You must use C<CLEANUP_*> macros in
 * your functions if you want correct cleanup of local variables along
 * the error path.
 *
 * =item *
 *
 * All the "bad" casting is hidden inside the macros.
 *
 * =back
 */

#ifndef GUESTFS_LIBXML2_WRITER_MACROS_H_
#define GUESTFS_LIBXML2_WRITER_MACROS_H_

#include <stdarg.h>

/**
 * To define an XML element use:
 *
 *  start_element ("name") {
 *    ...
 *  } end_element ();
 *
 * which produces C<<< <name>...</name> >>>
 */
#define start_element(element)						\
  if (xmlTextWriterStartElement (xo, BAD_CAST (element)) == -1) {	\
    xml_error ("xmlTextWriterStartElement");				\
  }									\
  do

#define end_element()				\
  while (0);					\
  do {						\
    if (xmlTextWriterEndElement (xo) == -1) {	\
      xml_error ("xmlTextWriterEndElement");	\
    }						\
  } while (0)

/**
 * To define an empty element:
 *
 *  empty_element ("name");
 *
 * which produces C<<< <name/> >>>
 */
#define empty_element(element)                                  \
  do { start_element ((element)) {} end_element (); } while (0)

/**
 * To define a single element with no attributes containing some text:
 *
 *  single_element ("name", text);
 *
 * which produces C<<< <name>text</name> >>>
 */
#define single_element(element,str)             \
  do {                                          \
    start_element ((element)) {                 \
      string ((str));                           \
    } end_element ();                           \
  } while (0)

/**
 * To define a single element with no attributes containing some text
 * using a format string:
 *
 *  single_element_format ("cores", "%d", nr_cores);
 *
 * which produces C<<< <cores>4</cores> >>>
 */
#define single_element_format(element,fs,...)   \
  do {                                          \
    start_element ((element)) {                 \
      string_format ((fs), ##__VA_ARGS__);      \
    } end_element ();                           \
  } while (0)

/**
 * To define an XML element with attributes, use:
 *
 *  start_element ("name") {
 *    attribute ("foo", "bar");
 *    attribute_format ("count", "%d", count);
 *    ...
 *  } end_element ();
 *
 * which produces C<<< <name foo="bar" count="123">...</name> >>>
 */
#define attribute(key,value)                                            \
  do {                                                                  \
    if (xmlTextWriterWriteAttribute (xo, BAD_CAST (key),                \
                                     BAD_CAST (value)) == -1) {         \
      xml_error ("xmlTextWriterWriteAttribute");                        \
    }                                                                   \
  } while (0)

#define attribute_format(key,fs,...)                                    \
  do {                                                                  \
    if (xmlTextWriterWriteFormatAttribute (xo, BAD_CAST (key),          \
                                           fs, ##__VA_ARGS__) == -1) {  \
      xml_error ("xmlTextWriterWriteFormatAttribute");                  \
    }                                                                   \
  } while (0)

/**
 * C<attribute_ns (prefix, key, namespace_uri, value)> defines a
 * namespaced attribute.
 */
#define attribute_ns(prefix,key,namespace_uri,value)                    \
  do {                                                                  \
    if (xmlTextWriterWriteAttributeNS (xo, BAD_CAST (prefix),           \
                                       BAD_CAST (key),                  \
                                       BAD_CAST (namespace_uri),        \
                                       BAD_CAST (value)) == -1) {       \
      xml_error ("xmlTextWriterWriteAttribute");                        \
    }                                                                   \
  } while (0)

/**
 * To define a verbatim string, use:
 *
 *  string ("hello");
 */
#define string(str)                                                     \
  do {                                                                  \
    if (xmlTextWriterWriteString (xo, BAD_CAST(str)) == -1) {           \
      xml_error ("xmlTextWriterWriteString");                           \
    }                                                                   \
  } while (0)

/**
 * To define a verbatim string using a format string, use:
 *
 *  string ("%s, world", greeting);
 */
#define string_format(fs,...)                                           \
  do {                                                                  \
    if (xmlTextWriterWriteFormatString (xo, fs, ##__VA_ARGS__) == -1) { \
      xml_error ("xmlTextWriterWriteFormatString");                     \
    }                                                                   \
  } while (0)

/**
 * To write a string encoded as base64:
 *
 *  base64 (data, size);
 */
#define base64(data, size)                                              \
  do {                                                                  \
    if (xmlTextWriterWriteBase64 (xo, (data), 0, (size)) == -1) {       \
      xml_error ("xmlTextWriterWriteBase64");                           \
    }                                                                   \
  } while (0)

/**
 * To define a comment in the XML, use:
 *
 *   comment ("number of items = %d", nr_items);
 */
#define comment(fs,...)                                                 \
  do {                                                                  \
    if (xmlTextWriterWriteFormatComment (xo, fs, ##__VA_ARGS__) == -1) { \
      xml_error ("xmlTextWriterWriteFormatComment");                    \
    }                                                                   \
  } while (0)

#endif /* GUESTFS_LIBXML2_WRITER_MACROS_H_ */