1 /* svg_parser_expat.c: Expat SAX-based parser for SVG documents
2 
3    Copyright � 2000 Eazel, Inc.
4    Copyright � 2002 Dom Lachowicz <cinamod@hotmail.com>
5    Copyright � 2002 USC/Information Sciences Institute
6    Copyright � 2005 Phil Blundell <pb@handhelds.org>
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 License as
10    published by the Free Software Foundation; either version 2 of the
11    License, 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 GNU
16    Library General Public License for more details.
17 
18    You should have received a copy of the GNU Library General Public
19    License along with this program; if not, write to the
20    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.
22 
23    Author: Raph Levien <raph@artofcode.com>
24 */
25 
26 #include <stdarg.h>
27 #include <math.h>
28 #include <string.h>
29 
30 #include "svgint.h"
31 #include "svg_hash.h"
32 
33 static void
34 _svg_parser_sax_start_element (void		*closure,
35 			       const XML_Char	*name,
36 			       const XML_Char	**atts);
37 
38 static void
39 _svg_parser_sax_end_element (void		*closure,
40 			     const XML_Char	*name);
41 
42 static void
43 _svg_parser_sax_characters (void		*closure,
44 			    const XML_Char	*ch,
45 			    int			len);
46 
47 static void
48 _svg_parser_sax_warning (void		*closure,
49 			 const char	*msg, ...);
50 
51 static void
52 _svg_parser_sax_error (void		*closure,
53 		       const char	*msg, ...);
54 
55 static void
56 _svg_parser_sax_fatal_error (void	*closure,
57 			     const char	*msg, ...);
58 svg_status_t
_svg_parser_init(svg_parser_t * parser,svg_t * svg)59 _svg_parser_init (svg_parser_t *parser, svg_t *svg)
60 {
61     parser->svg = svg;
62     parser->ctxt = NULL;
63 
64     parser->unknown_element_depth = 0;
65 
66     parser->state = NULL;
67 
68     parser->status = SVG_STATUS_SUCCESS;
69 
70     return parser->status;
71 }
72 
73 svg_status_t
_svg_parser_deinit(svg_parser_t * parser)74 _svg_parser_deinit (svg_parser_t *parser)
75 {
76     parser->svg = NULL;
77     parser->ctxt = NULL;
78 
79     parser->status = SVG_STATUS_SUCCESS;
80 
81     return parser->status;
82 }
83 
84 svg_status_t
_svg_parser_begin(svg_parser_t * parser)85 _svg_parser_begin (svg_parser_t *parser)
86 {
87     /* Innocent until proven guilty */
88     parser->status = SVG_STATUS_SUCCESS;
89 
90     if (parser->ctxt)
91 	parser->status = SVG_STATUS_INVALID_CALL;
92 
93     parser->ctxt = XML_ParserCreate (NULL);
94     XML_SetUserData (parser->ctxt, parser);
95 
96     XML_SetStartElementHandler (parser->ctxt, _svg_parser_sax_start_element);
97     XML_SetEndElementHandler (parser->ctxt, _svg_parser_sax_end_element);
98     XML_SetCharacterDataHandler (parser->ctxt, _svg_parser_sax_characters);
99 
100     if (parser->ctxt == NULL)
101 	parser->status = SVG_STATUS_NO_MEMORY;
102 
103     return parser->status;
104 }
105 
106 svg_status_t
_svg_parser_parse_chunk(svg_parser_t * parser,const char * buf,size_t count)107 _svg_parser_parse_chunk (svg_parser_t *parser, const char *buf, size_t count)
108 {
109     if (parser->status)
110 	return parser->status;
111 
112     if (parser->ctxt == NULL)
113 	return SVG_STATUS_INVALID_CALL;
114 
115     if (XML_Parse (parser->ctxt, buf, count, 0) != XML_STATUS_OK)
116 	parser->status = SVG_STATUS_PARSE_ERROR;
117 
118     return parser->status;
119 }
120 
121 svg_status_t
_svg_parser_end(svg_parser_t * parser)122 _svg_parser_end (svg_parser_t *parser)
123 {
124     if (parser->ctxt == NULL)
125 	return SVG_STATUS_INVALID_CALL;
126 
127     if (XML_Parse (parser->ctxt, NULL, 0, 1) != XML_STATUS_OK)
128 	parser->status = SVG_STATUS_PARSE_ERROR;
129 
130     XML_ParserFree (parser->ctxt);
131 
132     parser->ctxt = NULL;
133 
134     return parser->status;
135 }
136 
137 static void
_svg_parser_sax_warning(void * closure,const char * msg,...)138 _svg_parser_sax_warning (void *closure, const char *msg, ...)
139 {
140     va_list args;
141 
142     va_start (args, msg);
143     vfprintf (stderr, msg, args);
144     va_end (args);
145 }
146 
147 static void
_svg_parser_sax_error(void * closure,const char * msg,...)148 _svg_parser_sax_error (void *closure, const char *msg, ...)
149 {
150     va_list args;
151 
152     va_start (args, msg);
153     vfprintf (stderr, msg, args);
154     va_end (args);
155 }
156 
157 static void
_svg_parser_sax_fatal_error(void * closure,const char * msg,...)158 _svg_parser_sax_fatal_error (void *closure, const char *msg, ...)
159 {
160     va_list args;
161 
162     va_start (args, msg);
163     vfprintf (stderr, msg, args);
164     va_end (args);
165 }
166 
167