gdb/
[deliverable/binutils-gdb.git] / gdb / xml-support.c
1 /* Helper routines for parsing XML using Expat.
2
3 Copyright (C) 2006
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24
25 /* The contents of this file are only useful if XML support is
26 available. */
27 #ifdef HAVE_LIBEXPAT
28
29 #include "exceptions.h"
30 #include "xml-support.h"
31
32 #include <expat.h>
33
34 #include "gdb_string.h"
35
36 /* Returns the value of attribute ATTR from expat attribute list
37 ATTRLIST. If not found, throws an exception. */
38
39 const XML_Char *
40 xml_get_required_attribute (const XML_Char **attrs,
41 const XML_Char *attr)
42 {
43 const XML_Char **p;
44 for (p = attrs; *p; p += 2)
45 {
46 const char *name = p[0];
47 const char *val = p[1];
48
49 if (strcmp (name, attr) == 0)
50 return val;
51 }
52 throw_error (XML_PARSE_ERROR, _("Can't find attribute %s"), attr);
53 }
54
55 /* Parse a field VALSTR that we expect to contain an integer value.
56 The integer is returned in *VALP. The string is parsed with an
57 equivalent to strtoul.
58
59 Returns 0 for success, -1 for error. */
60
61 static int
62 xml_parse_unsigned_integer (const char *valstr, ULONGEST *valp)
63 {
64 const char *endptr;
65 ULONGEST result;
66
67 if (*valstr == '\0')
68 return -1;
69
70 result = strtoulst (valstr, &endptr, 0);
71 if (*endptr != '\0')
72 return -1;
73
74 *valp = result;
75 return 0;
76 }
77
78 /* Gets the value of an integer attribute named ATTR, if it's present.
79 If the attribute is not found, or can't be parsed as integer,
80 throws an exception. */
81
82 ULONGEST
83 xml_get_integer_attribute (const XML_Char **attrs,
84 const XML_Char *attr)
85 {
86 ULONGEST result;
87 const XML_Char *value = xml_get_required_attribute (attrs, attr);
88
89 if (xml_parse_unsigned_integer (value, &result) != 0)
90 {
91 throw_error (XML_PARSE_ERROR,
92 _("Can't convert value of attribute %s, %s, to integer"),
93 attr, value);
94 }
95 return result;
96 }
97
98 /* Obtains a value of attribute with enumerated type. In XML, enumerated
99 attributes have string as a value, and in C, they are represented as
100 values of enumerated type. This function maps the attribute onto
101 an integer value that can be immediately converted into enumerated
102 type.
103
104 First, obtains the string value of ATTR in ATTRS.
105 Then, finds the index of that value in XML_NAMES, which is a zero-terminated
106 array of strings. If found, returns the element of VALUES with that index.
107 Otherwise throws. */
108
109 int
110 xml_get_enum_value (const XML_Char **attrs,
111 const XML_Char *attr,
112 const XML_Char **xml_names,
113 int *values)
114 {
115 const XML_Char *value = xml_get_required_attribute (attrs, attr);
116
117 int i;
118 for (i = 0; xml_names[i]; ++i)
119 {
120 if (strcmp (xml_names[i], value) == 0)
121 return values[i];
122 }
123 throw_error (XML_PARSE_ERROR,
124 _("Invalid enumerated value in XML: %s"), value);
125 }
126
127 /* Cleanup wrapper for XML_ParserFree, with the correct type
128 for make_cleanup. */
129
130 static void
131 free_xml_parser (void *parser)
132 {
133 XML_ParserFree (parser);
134 }
135
136 /* Register a cleanup to release PARSER. Only the parser itself
137 is freed; another cleanup may be necessary to discard any
138 associated user data. */
139
140 void
141 make_cleanup_free_xml_parser (XML_Parser parser)
142 {
143 make_cleanup (free_xml_parser, parser);
144 }
145
146 #endif /* HAVE_LIBEXPAT */
This page took 0.049625 seconds and 4 git commands to generate.