35 #include <glib/gtypes.h>
44 #define G_LOG_DOMAIN "lib xml"
49 #define BUFFER_SIZE 1048576
63 entity = g_malloc (
sizeof (*entity));
64 entity->
name = g_strdup (name ? name :
"");
65 entity->
text = g_strdup (text ? text :
"");
117 *entities = g_slist_append (*entities, entity);
131 g_free (entity->
name);
132 g_free (entity->
text);
214 return match ? (
entity_t) match->data : NULL;
234 return (
const char *) g_hash_table_lookup (entity->
attributes, name);
248 if (names && values && *names && *values)
252 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
253 while (*names && *values)
256 g_hash_table_insert (entity->
attributes, g_strdup (*names),
276 const gchar **attribute_names,
277 const gchar **attribute_values, gpointer user_data,
291 entity =
add_entity (NULL, element_name, NULL);
296 if (data->
first == NULL)
297 data->
current = data->
first = g_slist_prepend (NULL, entity);
312 const gchar **attribute_names,
313 const gchar **attribute_values)
316 attribute_values, context, NULL);
329 gpointer user_data, GError **error)
339 assert (strcmp (element_name,
353 g_slist_free_1 (front);
379 handle_text (GMarkupParseContext *context,
const gchar *text, gsize text_len,
380 gpointer user_data, GError **error)
390 gchar *old = current->
text;
391 current->
text = g_strconcat (current->
text, text, NULL);
395 current->
text = g_strdup (text);
419 handle_error (GMarkupParseContext *context, GError *error, gpointer user_data)
423 g_message (
" Error: %s\n", error->message);
443 entity_t *entity, GString **string_return)
445 GMarkupParser xml_parser;
446 GError *error = NULL;
447 GMarkupParseContext *xml_context;
457 if (time (&last_time) == -1)
459 g_warning (
" failed to get current time: %s\n", strerror (errno));
467 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
468 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
479 if (string_return == NULL)
481 else if (*string_return == NULL)
482 string = g_string_new (
"");
484 string = *string_return;
491 xml_parser.passthrough = NULL;
495 context_data.
done = FALSE;
496 context_data.
first = NULL;
502 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
512 count = gnutls_record_recv (*session, buffer,
BUFFER_SIZE);
515 if (count == GNUTLS_E_INTERRUPTED)
518 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
521 if ((timeout - (time (NULL) - last_time)) <= 0)
523 g_warning (
" timeout\n");
524 if (fcntl (socket, F_SETFL, 0L) < 0)
525 g_warning (
"%s :failed to set socket flag: %s",
526 __FUNCTION__, strerror (errno));
527 g_markup_parse_context_free (xml_context);
533 if (count == GNUTLS_E_REHANDSHAKE)
536 if (context_data.
first && context_data.
first->data)
539 g_slist_free_1 (context_data.
first);
541 if (
string && *string_return == NULL)
542 g_string_free (
string, TRUE);
545 if (fcntl (socket, F_SETFL, 0L) < 0)
546 g_warning (
"%s :failed to set socket flag: %s",
547 __FUNCTION__, strerror (errno));
549 g_markup_parse_context_free (xml_context);
556 g_markup_parse_context_end_parse (xml_context, &error);
559 g_warning (
" End error: %s\n", error->message);
560 g_error_free (error);
562 if (context_data.
first && context_data.
first->data)
565 g_slist_free_1 (context_data.
first);
567 if (
string && *string_return == NULL)
568 g_string_free (
string, TRUE);
571 if (fcntl (socket, F_SETFL, 0L) < 0)
572 g_warning (
"%s :failed to set socket flag: %s",
573 __FUNCTION__, strerror (errno));
575 g_markup_parse_context_free (xml_context);
582 g_debug (
"<= %.*s\n", (
int) count, buffer);
585 g_string_append_len (
string, buffer, count);
587 g_markup_parse_context_parse (xml_context, buffer, count, &error);
590 g_error_free (error);
591 if (context_data.
first && context_data.
first->data)
594 g_slist_free_1 (context_data.
first);
596 if (
string && *string_return == NULL)
597 g_string_free (
string, TRUE);
600 if (fcntl (socket, F_SETFL, 0L) < 0)
601 g_warning (
"%s :failed to set socket flag: %s", __FUNCTION__,
604 g_markup_parse_context_free (xml_context);
608 if (context_data.
done)
610 g_markup_parse_context_end_parse (xml_context, &error);
613 g_warning (
" End error: %s\n", error->message);
614 g_error_free (error);
615 if (context_data.
first && context_data.
first->data)
618 g_slist_free_1 (context_data.
first);
621 fcntl (socket, F_SETFL, 0L);
622 g_markup_parse_context_free (xml_context);
628 *string_return = string;
630 fcntl (socket, F_SETFL, 0L);
631 g_markup_parse_context_free (xml_context);
636 if ((timeout > 0) && (time (&last_time) == -1))
638 g_warning (
" failed to get current time (1): %s\n",
640 if (fcntl (socket, F_SETFL, 0L) < 0)
641 g_warning (
"%s :failed to set socket flag: %s", __FUNCTION__,
643 g_markup_parse_context_free (xml_context);
667 GString **string_return)
669 GMarkupParser xml_parser;
670 GError *error = NULL;
671 GMarkupParseContext *xml_context;
679 if (time (&last_time) == -1)
681 g_warning (
" failed to get current time: %s\n", strerror (errno));
689 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
697 if (string_return == NULL)
699 else if (*string_return == NULL)
700 string = g_string_new (
"");
702 string = *string_return;
709 xml_parser.passthrough = NULL;
713 context_data.
done = FALSE;
714 context_data.
first = NULL;
720 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
741 if ((timeout - (time (NULL) - last_time)) <= 0)
743 g_warning (
" timeout\n");
744 if (fcntl (socket, F_SETFL, 0L) < 0)
745 g_warning (
"%s :failed to set socket flag: %s",
746 __FUNCTION__, strerror (errno));
747 g_markup_parse_context_free (xml_context);
754 if (context_data.
first && context_data.
first->data)
757 g_slist_free_1 (context_data.
first);
759 if (
string && *string_return == NULL)
760 g_string_free (
string, TRUE);
762 fcntl (socket, F_SETFL, 0L);
763 g_markup_parse_context_free (xml_context);
770 g_markup_parse_context_end_parse (xml_context, &error);
773 g_warning (
" End error: %s\n", error->message);
774 g_error_free (error);
776 if (context_data.
first && context_data.
first->data)
779 g_slist_free_1 (context_data.
first);
781 if (
string && *string_return == NULL)
782 g_string_free (
string, TRUE);
785 if (fcntl (socket, F_SETFL, 0L) < 0)
786 g_warning (
"%s :failed to set socket flag: %s",
787 __FUNCTION__, strerror (errno));
789 g_markup_parse_context_free (xml_context);
796 g_debug (
"<= %.*s\n", (
int) count, buffer);
799 g_string_append_len (
string, buffer, count);
801 g_markup_parse_context_parse (xml_context, buffer, count, &error);
804 g_error_free (error);
806 if (context_data.
first && context_data.
first->data)
809 g_slist_free_1 (context_data.
first);
811 if (
string && *string_return == NULL)
812 g_string_free (
string, TRUE);
815 if (fcntl (socket, F_SETFL, 0L) < 0)
816 g_warning (
"%s :failed to set socket flag: %s", __FUNCTION__,
819 g_markup_parse_context_free (xml_context);
823 if (context_data.
done)
825 g_markup_parse_context_end_parse (xml_context, &error);
828 g_warning (
" End error: %s\n", error->message);
829 g_error_free (error);
830 if (context_data.
first && context_data.
first->data)
833 g_slist_free_1 (context_data.
first);
836 fcntl (socket, F_SETFL, 0L);
837 g_markup_parse_context_free (xml_context);
843 *string_return = string;
845 fcntl (socket, F_SETFL, 0L);
846 g_slist_free (context_data.
first);
847 g_markup_parse_context_free (xml_context);
852 if ((timeout > 0) && (time (&last_time) == -1))
854 g_warning (
" failed to get current time (1): %s\n",
856 if (fcntl (socket, F_SETFL, 0L) < 0)
857 g_warning (
"%s :failed to set server socket flag: %s", __FUNCTION__,
859 g_markup_parse_context_free (xml_context);
881 GString **string_return)
901 GString **string_return)
927 GString *
string = NULL;
932 g_string_free (
string, TRUE);
935 *text = g_string_free (
string, FALSE);
959 GString *
string = NULL;
964 g_string_free (
string, TRUE);
967 *text = g_string_free (
string, FALSE);
1042 if (connection->
tls)
1101 GMarkupParser xml_parser;
1102 GError *error = NULL;
1103 GMarkupParseContext *xml_context;
1111 xml_parser.passthrough = NULL;
1114 context_data.
done = FALSE;
1115 context_data.
first = NULL;
1121 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1125 g_markup_parse_context_parse (xml_context,
string, strlen (
string), &error);
1128 g_error_free (error);
1129 if (context_data.
first && context_data.
first->data)
1132 g_slist_free_1 (context_data.
first);
1136 if (context_data.
done)
1138 g_markup_parse_context_end_parse (xml_context, &error);
1141 g_warning (
" End error: %s\n", error->message);
1142 g_error_free (error);
1143 if (context_data.
first && context_data.
first->data)
1146 g_slist_free_1 (context_data.
first);
1151 g_slist_free_1 (context_data.
first);
1154 if (context_data.
first && context_data.
first->data)
1157 g_slist_free_1 (context_data.
first);
1185 g_string_append_printf ((GString *)
string,
" %s=\"%s\"", (
char *) name,
1199 gchar *text_escaped = NULL;
1200 g_string_append_printf (
string,
"<%s", entity->
name);
1204 g_string_append_printf (
string,
">");
1205 text_escaped = g_markup_escape_text (entity->
text, -1);
1206 g_string_append_printf (
string,
"%s", text_escaped);
1207 g_free (text_escaped);
1209 g_string_append_printf (
string,
"</%s>", entity->
name);
1234 fprintf ((FILE *) stream,
" %s=\"%s\"", (
char *) name, (
char *) value);
1246 gchar *text_escaped = NULL;
1247 fprintf (stream,
"<%s", entity->
name);
1250 fprintf (stream,
">");
1251 text_escaped = g_markup_escape_text (entity->
text, -1);
1252 fprintf (stream,
"%s", text_escaped);
1253 g_free (text_escaped);
1255 fprintf (stream,
"</%s>", entity->
name);
1272 printf (
" %s=\"%s\"", (
char *) name, (
char *) value);
1290 int indentation = GPOINTER_TO_INT (indent);
1291 gchar *text_escaped = NULL;
1293 for (i = 0; i < indentation; i++)
1296 printf (
"<%s", entity->
name);
1302 text_escaped = g_markup_escape_text (entity->
text, -1);
1303 printf (
"%s", text_escaped);
1304 g_free (text_escaped);
1310 GINT_TO_POINTER (indentation + 1));
1311 for (i = 0; i < indentation; i++)
1315 printf (
"</%s>\n", entity->
name);
1330 gchar *value2 = g_hash_table_lookup (attributes2, key);
1331 if (value2 && strcmp (value, value2) == 0)
1333 g_debug (
" compare failed attribute: %s\n", (
char *) value);
1348 if (entity1 == NULL)
1349 return entity2 == NULL ? 0 : 1;
1350 if (entity2 == NULL)
1353 if (strcmp (entity1->
name, entity2->
name))
1355 g_debug (
" compare failed name: %s vs %s\n", entity1->
name,
1359 if (strcmp (entity1->
text, entity2->
text))
1361 g_debug (
" compare failed text %s vs %s (%s)\n", entity1->
text,
1378 g_debug (
" compare failed attributes\n");
1386 while (list1 && list2)
1390 g_debug (
" compare failed subentity\n");
1393 list1 = g_slist_next (list1);
1394 list2 = g_slist_next (list2);
1399 g_debug (
" compare failed number of entities (%s)\n", entity1->
name);
1437 va_start (args, format);
1438 piece = g_markup_vprintf_escaped (format, args);
1440 g_string_append (xml, piece);
1458 const gchar *element_name,
1459 const gchar **attribute_names,
1460 const gchar **attribute_values, gpointer data,
1468 if (strcmp (element_name, search_data->
find_element) == 0
1469 && search_data->
found == 0)
1471 g_debug (
"%s: Found element <%s>", __FUNCTION__, element_name);
1477 GHashTable *found_attributes;
1479 g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
1481 while (attribute_names[index])
1483 gchar *searched_value;
1484 searched_value = g_hash_table_lookup (
1487 && strcmp (searched_value, attribute_values[index]) == 0)
1489 g_debug (
"%s: Found attribute %s=\"%s\"", __FUNCTION__,
1490 attribute_names[index], searched_value);
1491 g_hash_table_add (found_attributes, searched_value);
1495 g_debug (
"%s: Found %d of %d attributes", __FUNCTION__,
1496 g_hash_table_size (found_attributes),
1499 if (g_hash_table_size (found_attributes)
1502 search_data->
found = 1;
1505 g_hash_table_destroy (found_attributes);
1509 search_data->
found = 1;
1514 #define XML_FILE_BUFFER_SIZE 1048576
1526 GHashTable *find_attributes)
1531 GMarkupParser xml_parser;
1532 GMarkupParseContext *xml_context;
1534 GError *error = NULL;
1538 search_data.
found = 0;
1542 xml_parser.end_element = NULL;
1543 xml_parser.text = NULL;
1544 xml_parser.passthrough = NULL;
1545 xml_parser.error = NULL;
1546 xml_context = g_markup_parse_context_new (&xml_parser, 0, &search_data, NULL);
1548 file = fopen (file_path,
"r");
1551 g_markup_parse_context_free (xml_context);
1552 g_warning (
"%s: Failed to open '%s':", __FUNCTION__, strerror (errno));
1557 && g_markup_parse_context_parse (xml_context, buffer, read_len, &error)
1561 g_markup_parse_context_end_parse (xml_context, &error);
1565 g_markup_parse_context_free (xml_context);
1566 return search_data.
found;
1568 #undef XML_FILE_BUFFER_SIZE