1
/* nXml - Copyright (C) 2005-2007 bakunin - Andrea Marchesini
2
* <bakunin@autistici.org>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library 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 GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
__nxml_write_escape_string (void (*func) (void *, char *, ...), void *obj,
30
#define __NXML_CHECK_BUF \
31
if(j==sizeof(buf)-1) { buf[j]=0; func(obj, "%s",buf); j=0; }
38
for (j = i = 0; i < len; i++)
43
else if (str[i] == '<')
55
else if (str[i] == '>')
67
else if (str[i] == '&')
81
else if (str[i] == '\'')
97
else if (str[i] == '\"')
123
func (obj, "%s", buf);
129
__nxml_write_haslines (char *what)
131
while (what && *what)
142
__nxml_write_indent (void (*func) (void *, char *, ...), void *obj,
146
for (i = 0; i < indent; i++)
151
__nxml_write_newline (void (*func) (void *, char *, ...), void *obj)
157
__nxml_write_data_text (nxml_data_t * data,
158
void (*func) (void *, char *, ...), void *obj)
160
__nxml_write_escape_string (func, obj, data->value);
164
__nxml_write_data_comment (nxml_data_t * data,
165
void (*func) (void *, char *, ...), void *obj)
167
func (obj, "<!--%s-->", data->value);
171
__nxml_write_data_pi (nxml_data_t * data, void (*func) (void *, char *, ...),
174
func (obj, "<?%s?>", data->value);
178
__nxml_write_data_doctype (nxml_doctype_t * data,
179
void (*func) (void *, char *, ...), void *obj)
181
func (obj, "<!DOCTYPE %s %s>", data->name, data->value);
185
__nxml_write_data_element (nxml_data_t * data,
186
void (*func) (void *, char *, ...), void *obj)
191
if (data->ns && data->ns->prefix)
192
func (obj, "%s:", data->ns->prefix);
193
func (obj, "%s", data->value);
195
attr = data->attributes;
200
if (attr->ns && attr->ns->prefix)
201
func (obj, "%s:", attr->ns->prefix);
202
func (obj, "%s=\"", attr->name);
204
__nxml_write_escape_string (func, obj, attr->value);
216
__nxml_write_data (nxml_t * nxml, nxml_data_t * data,
217
void (*func) (void *, char *, ...), void *obj, int indent)
225
if (data->children || data->next || __nxml_write_haslines (data->value)
226
|| (data->parent && data->parent->children != data))
229
__nxml_write_indent (func, obj, indent);
234
__nxml_write_data_text (data, func, obj);
237
__nxml_write_newline (func, obj);
240
case NXML_TYPE_COMMENT:
241
__nxml_write_indent (func, obj, indent);
242
__nxml_write_data_comment (data, func, obj);
243
__nxml_write_newline (func, obj);
247
__nxml_write_indent (func, obj, indent);
248
__nxml_write_data_pi (data, func, obj);
249
__nxml_write_newline (func, obj);
253
__nxml_write_indent (func, obj, indent);
254
__nxml_write_data_element (data, func, obj);
256
if (!data->children || data->children->type != NXML_TYPE_TEXT
257
|| data->children->next
258
|| __nxml_write_haslines (data->children->value))
259
__nxml_write_newline (func, obj);
266
tmp = data->children;
270
__nxml_write_data (nxml, tmp, func, obj, indent + 1);
274
if (data->type == NXML_TYPE_ELEMENT)
276
if (!data->children || data->children->type != NXML_TYPE_TEXT
277
|| data->children->next || data->children->children
278
|| __nxml_write_haslines (data->children->value))
279
__nxml_write_indent (func, obj, indent);
282
if (data->ns && data->ns->prefix)
283
func (obj, "%s:", data->ns->prefix);
284
func (obj, "%s>", data->value);
286
__nxml_write_newline (func, obj);
292
__nxml_write_real (nxml_t * nxml, void (*func) (void *, char *, ...),
296
nxml_doctype_t *doctype;
300
func (obj, " version=\"");
302
switch (nxml->version)
304
case NXML_VERSION_1_0:
314
func (obj, " encoding=\"%s\"", nxml->encoding);
316
func (obj, " standalone=\"%s\"?>\n\n", nxml->standalone ? "yes" : "no");
318
doctype = nxml->doctype;
322
__nxml_write_indent (func, obj, 0);
323
__nxml_write_data_doctype (doctype, func, obj);
324
__nxml_write_newline (func, obj);
326
doctype = doctype->next;
333
__nxml_write_data (nxml, data, func, obj, 0);
342
__nxml_file_write (void *obj, char *str, ...)
347
vfprintf ((FILE *) obj, str, va);
352
__nxml_buffer_write (void *obj, char *str, ...)
357
char **buffer = (char **) obj;
360
len = vsnprintf (s, sizeof (s), str, va);
365
if (!(*buffer = (char *) malloc (sizeof (char) * (len + 1))))
372
if (!(*buffer = (char *) realloc (*buffer,
373
sizeof (char) * (strlen (*buffer) +
381
/*************************** EXTERNAL FUNCTION ******************************/
384
nxml_write_file (nxml_t * nxml, const char *file)
390
return NXML_ERR_DATA;
392
if (!(fl = fopen (file, "wb")))
393
return NXML_ERR_POSIX;
395
ret = __nxml_write_real (nxml, __nxml_file_write, fl);
402
nxml_write_buffer (nxml_t * nxml, char **buffer)
404
if (!nxml || !buffer)
405
return NXML_ERR_DATA;
407
return __nxml_write_real (nxml, __nxml_buffer_write, buffer);