1 /*****************************************************************
2 * gmerlin - a general purpose multimedia framework and applications
3 *
4 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
5 * gmerlin-general@lists.sourceforge.net
6 * http://gmerlin.sourceforge.net
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (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 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * *****************************************************************/
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include <errno.h>
32
33 #include <gmerlin/utils.h>
34 #include <gmerlin/xmlutils.h>
35
36 #define BLOCK_SIZE 2048
37
38 #include <gmerlin/log.h>
39 #define LOG_DOMAIN "xmlutils"
40
bg_xml_write_callback(void * context,const char * buffer,int len)41 int bg_xml_write_callback(void * context, const char * buffer,
42 int len)
43 {
44 bg_xml_output_mem_t * o = (bg_xml_output_mem_t*)context;
45
46 if(o->bytes_allocated - o->bytes_written < len)
47 {
48 o->bytes_allocated += BLOCK_SIZE;
49 while(o->bytes_allocated < o->bytes_written + len)
50 o->bytes_allocated += BLOCK_SIZE;
51 o->buffer = realloc(o->buffer, o->bytes_allocated);
52 }
53 memcpy(&o->buffer[o->bytes_written], buffer, len);
54 o->bytes_written += len;
55 return len;
56 }
57
bg_xml_close_callback(void * context)58 int bg_xml_close_callback(void * context)
59 {
60 bg_xml_output_mem_t * o = (bg_xml_output_mem_t*)context;
61
62 if(o->bytes_allocated == o->bytes_written)
63 {
64 o->bytes_allocated++;
65 o->buffer = realloc(o->buffer, o->bytes_allocated);
66 }
67 o->buffer[o->bytes_written] = '\0';
68 return 0;
69 }
70
bg_xml_parse_file(const char * filename)71 xmlDocPtr bg_xml_parse_file(const char * filename)
72 {
73 struct stat st;
74
75 if(stat(filename, &st))
76 {
77 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot stat %s: %s",
78 filename, strerror(errno));
79 return NULL;
80 }
81
82 /* Return silently */
83 if(!st.st_size)
84 return NULL;
85 return xmlParseFile(filename);
86 }
87