26 #include <string.h> |
26 #include <string.h> |
27 |
27 |
28 #include "lm-internals.h" |
28 #include "lm-internals.h" |
29 #include "lm-message-node.h" |
29 #include "lm-message-node.h" |
30 |
30 |
31 typedef struct { |
|
32 gchar *key; |
|
33 gchar *value; |
|
34 } KeyValuePair; |
|
35 |
|
36 static void message_node_free (LmMessageNode *node); |
31 static void message_node_free (LmMessageNode *node); |
37 static LmMessageNode * message_node_last_child (LmMessageNode *node); |
32 static LmMessageNode * message_node_last_child (LmMessageNode *node); |
38 |
33 |
39 static void |
34 static void |
40 message_node_free (LmMessageNode *node) |
35 message_node_free (LmMessageNode *node) |
41 { |
36 { |
42 LmMessageNode *l; |
37 LmMessageNode *l; |
43 GSList *list; |
38 LmMessageNodeAttribute *a; |
44 |
39 |
45 g_return_if_fail (node != NULL); |
40 g_return_if_fail (node != NULL); |
46 |
41 |
47 for (l = node->children; l;) { |
42 for (l = node->children; l;) { |
48 LmMessageNode *next = l->next; |
43 LmMessageNode *next = l->next; |
236 void |
231 void |
237 lm_message_node_set_attribute (LmMessageNode *node, |
232 lm_message_node_set_attribute (LmMessageNode *node, |
238 const gchar *name, |
233 const gchar *name, |
239 const gchar *value) |
234 const gchar *value) |
240 { |
235 { |
241 gboolean found = FALSE; |
236 gboolean found = FALSE; |
242 GSList *l; |
237 LmMessageNodeAttribute *a; |
243 |
238 |
244 g_return_if_fail (node != NULL); |
239 g_return_if_fail (node != NULL); |
245 g_return_if_fail (name != NULL); |
240 g_return_if_fail (name != NULL); |
246 g_return_if_fail (value != NULL); |
241 g_return_if_fail (value != NULL); |
247 |
242 |
248 for (l = node->attributes; l; l = l->next) { |
243 for (a = node->attributes; a; a = a->next) { |
249 KeyValuePair *kvp = (KeyValuePair *) l->data; |
244 if (strcmp (a->name, name) == 0) { |
250 |
245 g_free (a->value); |
251 if (strcmp (kvp->key, name) == 0) { |
246 a->value = g_strdup (value); |
252 g_free (kvp->value); |
|
253 kvp->value = g_strdup (value); |
|
254 found = TRUE; |
247 found = TRUE; |
255 break; |
248 break; |
256 } |
249 } |
257 } |
250 } |
258 |
251 |
259 if (!found) { |
252 if (!found) { |
260 KeyValuePair *kvp; |
253 a = g_new0 (LmMessageNodeAttribute, 1); |
261 |
254 a->name = g_strdup (name); |
262 kvp = g_new0 (KeyValuePair, 1); |
255 a->value = g_strdup (value); |
263 kvp->key = g_strdup (name); |
256 |
264 kvp->value = g_strdup (value); |
257 a->next = node->attributes; |
265 |
258 node->attributes = a; |
266 node->attributes = g_slist_prepend (node->attributes, kvp); |
|
267 } |
259 } |
268 } |
260 } |
269 |
261 |
270 /** |
262 /** |
271 * lm_message_node_get_attribute: |
263 * lm_message_node_get_attribute: |
277 * Return value: the attribute value or %NULL if not set |
269 * Return value: the attribute value or %NULL if not set |
278 **/ |
270 **/ |
279 const gchar * |
271 const gchar * |
280 lm_message_node_get_attribute (LmMessageNode *node, const gchar *name) |
272 lm_message_node_get_attribute (LmMessageNode *node, const gchar *name) |
281 { |
273 { |
282 GSList *l; |
274 LmMessageNodeAttribute *a; |
283 const gchar *ret_val = NULL; |
275 const gchar *ret_val = NULL; |
284 |
276 |
285 g_return_val_if_fail (node != NULL, NULL); |
277 g_return_val_if_fail (node != NULL, NULL); |
286 g_return_val_if_fail (name != NULL, NULL); |
278 g_return_val_if_fail (name != NULL, NULL); |
287 |
279 |
288 for (l = node->attributes; l; l = l->next) { |
280 for (a = node->attributes; a; a = a->next) { |
289 KeyValuePair *kvp = (KeyValuePair *) l->data; |
281 if (strcmp (a->name, name) == 0) { |
290 |
282 ret_val = a->value; |
291 if (strcmp (kvp->key, name) == 0) { |
283 break; |
292 ret_val = kvp->value; |
|
293 } |
284 } |
294 } |
285 } |
295 |
286 |
296 return ret_val; |
287 return ret_val; |
297 } |
288 } |
440 * Return value: an XML string representation of @node |
431 * Return value: an XML string representation of @node |
441 **/ |
432 **/ |
442 gchar * |
433 gchar * |
443 lm_message_node_to_string (LmMessageNode *node) |
434 lm_message_node_to_string (LmMessageNode *node) |
444 { |
435 { |
445 GString *ret; |
436 GString *ret; |
446 GSList *l; |
437 LmMessageNodeAttribute *a; |
447 LmMessageNode *child; |
438 LmMessageNode *child; |
448 |
439 |
449 g_return_val_if_fail (node != NULL, NULL); |
440 g_return_val_if_fail (node != NULL, NULL); |
450 |
441 |
451 if (node->name == NULL) { |
442 if (node->name == NULL) { |
452 return g_strdup (""); |
443 return g_strdup (""); |
453 } |
444 } |
454 |
445 |
455 ret = g_string_new ("<"); |
446 ret = g_string_new ("<"); |
456 g_string_append (ret, node->name); |
447 g_string_append (ret, node->name); |
457 |
448 |
458 for (l = node->attributes; l; l = l->next) { |
449 for (a = node->attributes; a; a = a->next) { |
459 KeyValuePair *kvp = (KeyValuePair *) l->data; |
|
460 |
|
461 if (node->raw_mode == FALSE) { |
450 if (node->raw_mode == FALSE) { |
462 gchar *escaped; |
451 gchar *escaped; |
463 |
452 |
464 escaped = g_markup_escape_text (kvp->value, -1); |
453 escaped = g_markup_escape_text (a->value, -1); |
465 g_string_append_printf (ret, " %s=\"%s\"", |
454 g_string_append_printf (ret, " %s=\"%s\"", |
466 kvp->key, escaped); |
455 a->name, escaped); |
467 g_free (escaped); |
456 g_free (escaped); |
468 } else { |
457 } else { |
469 g_string_append_printf (ret, " %s=\"%s\"", |
458 g_string_append_printf (ret, " %s=\"%s\"", |
470 kvp->key, kvp->value); |
459 a->name, a->value); |
471 } |
460 } |
472 |
|
473 } |
461 } |
474 |
462 |
475 g_string_append_c (ret, '>'); |
463 g_string_append_c (ret, '>'); |
476 |
464 |
477 if (node->value) { |
465 if (node->value) { |