Add support for reading and writing SPV files.
[pspp] / src / output / spv / spvxml-helpers.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2018 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef SPVXML_HELPERS_H
18 #define SPVXML_HELPERS_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <libxml/xmlreader.h>
23 #include "libpspp/compiler.h"
24 #include "libpspp/hmap.h"
25
26 struct spvxml_node;
27
28 struct spvxml_context
29   {
30     struct hmap id_map;
31
32     char *error;
33     bool hard_error;
34   };
35
36 #define SPVXML_CONTEXT_INIT(CONTEXT) \
37   { HMAP_INITIALIZER ((CONTEXT).id_map), NULL, false }
38
39 char *spvxml_context_finish (struct spvxml_context *, struct spvxml_node *root)
40   WARN_UNUSED_RESULT;
41
42 struct spvxml_node_context
43   {
44     struct spvxml_context *up;
45     const xmlNode *parent;
46
47     struct spvxml_attribute *attrs;
48     size_t n_attrs;
49   };
50
51 void spvxml_node_context_uninit (struct spvxml_node_context *);
52
53 struct spvxml_node_class
54   {
55     const char *name;
56     void (*spvxml_node_free) (struct spvxml_node *);
57     void (*spvxml_node_collect_ids) (struct spvxml_context *,
58                                      struct spvxml_node *);
59     void (*spvxml_node_resolve_refs) (struct spvxml_context *,
60                                       struct spvxml_node *);
61   };
62
63 struct spvxml_node
64   {
65     struct hmap_node id_node;
66     char *id;
67
68     const struct spvxml_node_class *class_;
69     const xmlNode *raw;
70   };
71
72 void spvxml_node_collect_id (struct spvxml_context *, struct spvxml_node *);
73 struct spvxml_node *spvxml_node_resolve_ref (
74   struct spvxml_context *, const xmlNode *, const char *attr_name,
75   const struct spvxml_node_class *const *, size_t n);
76
77 /* Attribute parsing. */
78 struct spvxml_attribute
79   {
80     const char *name;
81     bool required;
82     char *value;
83   };
84
85 void spvxml_parse_attributes (struct spvxml_node_context *);
86 void spvxml_attr_error (struct spvxml_node_context *, const char *format, ...)
87   PRINTF_FORMAT (2, 3);
88
89 struct spvxml_enum
90   {
91     const char *name;
92     int value;
93   };
94
95 int spvxml_attr_parse_enum (struct spvxml_node_context *,
96                             const struct spvxml_attribute *,
97                             const struct spvxml_enum[]);
98 int spvxml_attr_parse_bool (struct spvxml_node_context *,
99                             const struct spvxml_attribute *);
100 bool spvxml_attr_parse_fixed (struct spvxml_node_context *,
101                              const struct spvxml_attribute *,
102                              const char *attr_value);
103 int spvxml_attr_parse_int (struct spvxml_node_context *,
104                            const struct spvxml_attribute *);
105 int spvxml_attr_parse_color (struct spvxml_node_context *,
106                              const struct spvxml_attribute *);
107 double spvxml_attr_parse_real (struct spvxml_node_context *,
108                                const struct spvxml_attribute *);
109 double spvxml_attr_parse_dimension (struct spvxml_node_context *,
110                                     const struct spvxml_attribute *);
111 struct spvxml_node *spvxml_attr_parse_ref (struct spvxml_node_context *,
112                                            const struct spvxml_attribute *);
113 \f
114 /* Content parsing. */
115
116 void spvxml_content_error (struct spvxml_node_context *, const xmlNode *,
117                            const char *format, ...)
118   PRINTF_FORMAT (3, 4);
119 bool spvxml_content_parse_element (struct spvxml_node_context *, xmlNode **,
120                                    const char *elem_name, xmlNode **);
121 bool spvxml_content_parse_text (struct spvxml_node_context *, xmlNode **,
122                                 char **textp);
123 void spvxml_content_parse_etc (xmlNode **);
124 bool spvxml_content_parse_end (struct spvxml_node_context *, xmlNode *);
125
126 #endif /* output/spv/spvxml-helpers.h */