summaryrefslogtreecommitdiff
path: root/src/rexmpp_xml.h
blob: 38142ae03a8c590c055c9cbbc4e1c77701de81f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
   @file rexmpp_xml.h
   @brief XML structures and functions for rexmpp
   @author defanor <defanor@uberspace.net>
   @date 2023
   @copyright MIT license.
*/

#ifndef REXMPP_XML_H
#define REXMPP_XML_H

#include <stdio.h>

typedef struct rexmpp_xml_qname rexmpp_xml_qname_t;
typedef struct rexmpp_xml_attribute rexmpp_xml_attr_t;
typedef struct rexmpp_xml_node rexmpp_xml_t;

struct rexmpp_xml_qname {
  char *name;
  char *namespace;
};

struct rexmpp_xml_attribute {
  rexmpp_xml_qname_t qname;
  char *value;
  rexmpp_xml_attr_t *next;
};

enum rexmpp_xml_node_type {
  REXMPP_XML_ELEMENT,
  REXMPP_XML_TEXT
};

typedef enum rexmpp_xml_node_type rexmpp_xml_node_type_t;

struct rexmpp_xml_node {
  rexmpp_xml_node_type_t type;
  union {
    struct {
      rexmpp_xml_qname_t qname;
      rexmpp_xml_attr_t *attributes;
      rexmpp_xml_t *children;
    } elem;
    char *text;
  } alt;
  rexmpp_xml_t *next;
};

struct rexmpp_xml_builder {
  rexmpp_xml_t *current;
  rexmpp_xml_t *root;
};

void rexmpp_xml_qname_free (rexmpp_xml_qname_t *qname);
void rexmpp_xml_attribute_free (rexmpp_xml_attr_t *attr);
void rexmpp_xml_attribute_free_list (rexmpp_xml_attr_t *attr);

/**
   @brief Frees a single XML node. Does not free its siblings.
*/
void rexmpp_xml_free (rexmpp_xml_t *node);

/**
   @brief Frees an XML node and its siblings.
*/
void rexmpp_xml_free_list (rexmpp_xml_t *node);

/**
   @brief Clones a single XML node, without its siblings.
*/
rexmpp_xml_t *rexmpp_xml_clone (rexmpp_xml_t *node);

/**
   @brief Clones an XML node, together with its siblings.
*/
rexmpp_xml_t *rexmpp_xml_clone_list (rexmpp_xml_t *node);

/**
   @brief Creates a textual ::rexmpp_xml_t XML node (with type =
   ::REXMPP_XML_TEXT).
*/
rexmpp_xml_t *rexmpp_xml_new_text (const char *str);

/**
   @brief Creates a textual ::rexmpp_xml_t XML node (with type =
   ::REXMPP_XML_TEXT).
*/
rexmpp_xml_t *rexmpp_xml_new_text_len (const char *str, size_t len);

/**
   @brief Creates an element ::rexmpp_xml_t XML node (with type =
   ::REXMPP_XML_ELEMENT).
*/
rexmpp_xml_t *rexmpp_xml_new_elem (const char *name,
                                   const char *namespace);

/**
   @brief Adds a child node.
*/
void rexmpp_xml_add_child (rexmpp_xml_t *node,
                           rexmpp_xml_t *child);

/**
   @brief Creates a text node, and adds it as a child.
*/
int rexmpp_xml_add_text (rexmpp_xml_t *node,
                         const char *str);

/**
   @brief Creates a text node, and adds it as a child.
*/
int rexmpp_xml_add_text_len (rexmpp_xml_t *node,
                             const char *str,
                             size_t len);

rexmpp_xml_attr_t *rexmpp_xml_attr_new (const char *name,
                                        const char *namespace,
                                        const char *value);

int rexmpp_xml_add_attr (rexmpp_xml_t *node,
                         const char *name,
                         const char *value);

int rexmpp_xml_remove_attr_ns (rexmpp_xml_t *node,
                               const char *name,
                               const char *namespace);

int rexmpp_xml_remove_attr (rexmpp_xml_t *node,
                            const char *name);

int rexmpp_xml_add_attr_ns (rexmpp_xml_t *node,
                            const char *name,
                            const char *namespace,
                            const char *value);

/**
   @brief Adds an "id" attribute to an XML stanza.
   @param[in,out] s ::rexmpp
   @param[in] node A pointer to an XML stanza.
   @returns The same pointer as on input, for more convenient
   composition.
*/
rexmpp_xml_t *
rexmpp_xml_add_id (rexmpp_xml_t *node);

/**
   @brief A helper function for XML serialisation.
   @param[in] node An XML node.
   @returns A string (must be freed by the caller).
*/
char *rexmpp_xml_serialize (const rexmpp_xml_t *node, int pretty);

/**
   @brief Count the number of siblings after a given node.
*/
unsigned int rexmpp_xml_siblings_count (rexmpp_xml_t *node);

/**
   @brief Compares the node's name and namespace to given ones.
*/
int rexmpp_xml_match (rexmpp_xml_t *node,
                      const char *namespace,
                      const char *name);

int rexmpp_xml_is_stanza (rexmpp_xml_t *node);

/**
   @brief Compose an 'error' element.
*/
rexmpp_xml_t *rexmpp_xml_error (const char *type, const char *condition);

/**
   @brief Matches an XML node against a namespace and an element name.
   @param[in] node An XML node to match.
   @param[in] namespace An XML namespace. Can be NULL (matches
   anything), and it is assumed that the default namespace is
   "jabber:client" (so if it is "jabber:client" and an element doesn't
   have a namespace defined, this function counts that as a match).
   @param[in] name Element name. Can be NULL (matches anything).
   @returns 1 on a successful match, 0 otherwise.
*/
int rexmpp_xml_attr_match (rexmpp_xml_attr_t *attr,
                           const char *namespace,
                           const char *name);

rexmpp_xml_attr_t *rexmpp_xml_find_attr (rexmpp_xml_t *node,
                                         const char *name,
                                         const char *namespace);

const char *rexmpp_xml_find_attr_val_ns (rexmpp_xml_t *node,
                                         const char *name,
                                         const char *namespace);

const char *rexmpp_xml_find_attr_val (rexmpp_xml_t *node,
                                      const char *name);

/**
   @brief Finds a child element of an XML node, which matches the
   given namespace and name.
   @param[in] node The node containing child nodes.
   @param[in] namespace The namespace to look for.
   @param[in] name The element name to look for.
   @returns A pointer to the first matching child node, or NULL if no
   matching child elements found.
*/
rexmpp_xml_t *rexmpp_xml_find_child (rexmpp_xml_t *node,
                                     const char *namespace,
                                     const char *name);

rexmpp_xml_t *rexmpp_xml_children (const rexmpp_xml_t *node);

char *rexmpp_xml_text (rexmpp_xml_t *node);

char *rexmpp_xml_text_child (rexmpp_xml_t *node);

rexmpp_xml_t *rexmpp_xml_first_elem_child (rexmpp_xml_t *node);

rexmpp_xml_t *rexmpp_xml_next_elem_sibling (rexmpp_xml_t *node);

/**
   @brief Compares two XML elements.
*/
int rexmpp_xml_eq (rexmpp_xml_t *n1, rexmpp_xml_t *n2);

/**
   @brief A helper function for XML parsing.
   @param[in] str A string to parse.
   @param[in] str_len String length.
   @returns Parsed XML, or NULL on failure.
*/
rexmpp_xml_t *rexmpp_xml_parse (const char *str, int str_len);

/**
   @brief Reads XML from a file stream, reading the stream line by
   line.
   @param[in] fd A file descriptor
   @returns Parsed XML, or NULL on failure.
*/
rexmpp_xml_t *rexmpp_xml_read_fd (int fd);

/**
   @brief Reads XML from a file
   @param[in] path A file path
   @returns Parsed XML, or NULL on failure.
*/
rexmpp_xml_t *rexmpp_xml_read_file (const char *path);

/**
   @brief Writes XML into a file
   @param[in] path A file path
   @param[in] node XML to write
   @returns 0 on success, -1 on failure.
*/
int rexmpp_xml_write_file (const char *path, rexmpp_xml_t* node);

/**
   @brief Reverses a linked list of XML nodes
   @param[in,out] node The head of the list to reverse
   @returns The new head of the list
*/
rexmpp_xml_t *rexmpp_xml_reverse_list (rexmpp_xml_t *node);

/**
   @brief Recursively reverses children of an XML element
   @param[in,out] node The root XML element
*/
void rexmpp_xml_reverse_children (rexmpp_xml_t *node);

#endif