loudmouth/lm-parser.c
author Till Maas <opensource@till.name>
Sat, 31 Oct 2015 08:37:51 +0100
changeset 664 f57b1b61e1fe
parent 623 d20813955632
child 690 7ccf2113ec5f
permissions -rw-r--r--
Remove trailing whitespace and mixed indenting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
516
4dd3aa6b83e5 Change the tab width to 4 steps in the emacs headers
Mikael Hallendal <micke@imendio.com>
parents: 515
diff changeset
     1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     2
/*
99
81e9d035de02 2004-10-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 84
diff changeset
     3
 * Copyright (C) 2003 Imendio AB
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     4
 *
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public License as
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     7
 * published by the Free Software Foundation; either version 2 of the
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     8
 * License, or (at your option) any later version.
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
     9
 *
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    13
 * Lesser General Public License for more details.
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    14
 *
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    16
 * License along with this program; if not, write to the
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    18
 * Boston, MA 02111-1307, USA.
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    19
 */
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    20
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    21
#include <config.h>
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    22
#include <string.h>
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    23
168
ac1affcd5d22 2006-09-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 153
diff changeset
    24
#include <glib.h>
ac1affcd5d22 2006-09-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 153
diff changeset
    25
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    26
#include "lm-debug.h"
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    27
#include "lm-internals.h"
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    28
#include "lm-message-node.h"
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    29
#include "lm-parser.h"
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    30
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    31
#define SHORT_END_TAG "/>"
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    32
#define XML_MAX_DEPTH 5
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    33
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    34
#define LM_PARSER(o) ((LmParser *) o)
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    35
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    36
struct LmParser {
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    37
    LmParserMessageFunction  function;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    38
    gpointer                 user_data;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    39
    GDestroyNotify           notify;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
    40
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    41
    LmMessageNode           *cur_root;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    42
    LmMessageNode           *cur_node;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
    43
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    44
    GMarkupParser           *m_parser;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    45
    GMarkupParseContext     *context;
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
    46
    gchar                   *incomplete; /* incomplete utf-8 character
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
    47
                                            found at the end of buffer */
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    48
};
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    49
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    50
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    51
/* Used while parsing */
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    52
static void    parser_start_node_cb (GMarkupParseContext  *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    53
                                     const gchar          *node_name,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    54
                                     const gchar         **attribute_names,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    55
                                     const gchar         **attribute_values,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    56
                                     gpointer              user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    57
                                     GError              **error);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    58
static void    parser_end_node_cb   (GMarkupParseContext  *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    59
                                     const gchar          *node_name,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    60
                                     gpointer              user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    61
                                     GError              **error);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    62
static void    parser_text_cb       (GMarkupParseContext  *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    63
                                     const gchar          *text,
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
    64
                                     gsize                 text_len,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    65
                                     gpointer              user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    66
                                     GError              **error);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    67
static void    parser_error_cb      (GMarkupParseContext  *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    68
                                     GError               *error,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    69
                                     gpointer              user_data);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    70
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    71
static void
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    72
parser_start_node_cb (GMarkupParseContext  *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    73
                      const gchar          *node_name,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    74
                      const gchar         **attribute_names,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    75
                      const gchar         **attribute_values,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    76
                      gpointer              user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    77
                      GError              **error)
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
    78
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    79
    LmParser     *parser;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    80
    gint          i;
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    81
    const gchar  *node_name_unq;
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    82
    const gchar  *xmlns = NULL;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
    83
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    84
    parser = LM_PARSER (user_data);;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
    85
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    86
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    87
/*  parser->cur_depth++; */
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
    88
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    89
    //strip namespace prefix other than "stream:" from node_name
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    90
    node_name_unq = strrchr(node_name, ':');
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    91
    if (!node_name_unq || !strncmp(node_name, "stream:", 7))
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    92
        node_name_unq = node_name;
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    93
    else
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    94
        ++node_name_unq;
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    95
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    96
    if (!parser->cur_root) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    97
        /* New toplevel element */
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
    98
        parser->cur_root = _lm_message_node_new (node_name_unq);
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    99
        parser->cur_node = parser->cur_root;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   100
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   101
        LmMessageNode *parent_node;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   102
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   103
        parent_node = parser->cur_node;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   104
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   105
        parser->cur_node = _lm_message_node_new (node_name_unq);
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   106
        _lm_message_node_add_child_node (parent_node,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   107
                                         parser->cur_node);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   108
    }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   109
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   110
    for (i = 0; attribute_names[i]; ++i) {
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   111
        g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER,
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   112
               "ATTRIBUTE: %s = %s\n",
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   113
               attribute_names[i],
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   114
               attribute_values[i]);
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   115
        //FIXME: strip namespace suffix from xmlns: attribute if exists
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   116
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   117
        lm_message_node_set_attributes (parser->cur_node,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   118
                                        attribute_names[i],
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   119
                                        attribute_values[i],
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   120
                                        NULL);
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   121
        if (!strncmp(attribute_names[i], "xmlns:", 6))
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   122
            xmlns = attribute_values[i];
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   123
    }
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   124
    if (xmlns && !lm_message_node_get_attribute(parser->cur_node, "xmlns")) {
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   125
        lm_message_node_set_attribute (parser->cur_node, "xmlns", xmlns);
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   126
        g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER,
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   127
               "ATTRIBUTE: %s = %s\n",
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   128
               "xmlns", xmlns);
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   129
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   130
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   131
    if (strcmp ("stream:stream", node_name) == 0) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   132
        parser_end_node_cb (context,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   133
                            "stream:stream",
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   134
                            user_data,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   135
                            error);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   136
    }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   137
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   138
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   139
static void
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   140
parser_end_node_cb (GMarkupParseContext  *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   141
                    const gchar          *node_name,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   142
                    gpointer              user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   143
                    GError              **error)
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   144
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   145
    LmParser     *parser;
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   146
    const gchar  *node_name_unq;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   147
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   148
    parser = LM_PARSER (user_data);
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   149
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   150
    node_name_unq = strrchr(node_name, ':');
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   151
    if (!node_name_unq || !strncmp(node_name, "stream:", 7))
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   152
        node_name_unq = node_name;
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   153
    else
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   154
        ++node_name_unq;
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   155
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   156
    g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER,
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   157
           "Trying to close node: %s\n", node_name_unq);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   158
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   159
    if (!parser->cur_node) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   160
        /* FIXME: LM-1 should look at this */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   161
        return;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   162
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   163
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   164
    //cur_node->name doesn't have namespace prefix anymore, node_name does.
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   165
    if (strcmp (parser->cur_node->name, node_name_unq) != 0) {
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   166
        if (strcmp (node_name, "stream:stream")) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   167
            g_print ("Got an stream:stream end\n");
34
c09b43b3b697 2003-08-15 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 7
diff changeset
   168
        }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   169
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   170
        g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   171
               "Trying to close node that isn't open: %s",
623
d20813955632 Workaround for parser to support prefixed tag names
Hermitifier <quantifier666@gmail.com>
parents: 622
diff changeset
   172
               node_name_unq);
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   173
        return;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   174
    }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   175
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   176
    if (parser->cur_node == parser->cur_root) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   177
        LmMessage *m;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   178
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   179
        m = _lm_message_new_from_node (parser->cur_root);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   180
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   181
        if (!m) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   182
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   183
                   "Couldn't create message: %s\n",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   184
                   parser->cur_root->name);
602
3bcd644fe1ba [PATCH] Drop stanzas when failing to convert them to LmMessages.
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 518
diff changeset
   185
        } else {
3bcd644fe1ba [PATCH] Drop stanzas when failing to convert them to LmMessages.
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 518
diff changeset
   186
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   187
               "Have a new message\n");
602
3bcd644fe1ba [PATCH] Drop stanzas when failing to convert them to LmMessages.
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 518
diff changeset
   188
            if (parser->function) {
3bcd644fe1ba [PATCH] Drop stanzas when failing to convert them to LmMessages.
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 518
diff changeset
   189
                (* parser->function) (parser, m, parser->user_data);
3bcd644fe1ba [PATCH] Drop stanzas when failing to convert them to LmMessages.
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 518
diff changeset
   190
            }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 623
diff changeset
   191
            lm_message_unref (m);
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 623
diff changeset
   192
        }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   193
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   194
        lm_message_node_unref (parser->cur_root);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   195
        parser->cur_node = parser->cur_root = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   196
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   197
        LmMessageNode *tmp_node;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   198
        tmp_node = parser->cur_node;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   199
        parser->cur_node = parser->cur_node->parent;
84
7ae7b690aa89 2004-05-28 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 57
diff changeset
   200
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   201
        lm_message_node_unref (tmp_node);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   202
    }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   203
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   204
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   205
static void
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   206
parser_text_cb (GMarkupParseContext   *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   207
                const gchar           *text,
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   208
                gsize                  text_len,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   209
                gpointer               user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   210
                GError               **error)
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   211
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   212
    LmParser *parser;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   213
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   214
    g_return_if_fail (user_data != NULL);
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   215
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   216
    parser = LM_PARSER (user_data);
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   217
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   218
    if (parser->cur_node && strcmp (text, "") != 0) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   219
        lm_message_node_set_value (parser->cur_node, text);
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   220
    }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   221
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   222
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   223
static void
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   224
parser_error_cb (GMarkupParseContext *context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   225
                 GError              *error,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   226
                 gpointer             user_data)
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   227
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   228
    g_return_if_fail (user_data != NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   229
    g_return_if_fail (error != NULL);
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   230
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   231
    g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   232
           "Parsing failed: %s\n", error->message);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   233
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   234
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   235
LmParser *
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   236
lm_parser_new (LmParserMessageFunction function,
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   237
               gpointer                user_data,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   238
               GDestroyNotify          notify)
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   239
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   240
    LmParser *parser;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   241
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   242
    parser = g_new0 (LmParser, 1);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   243
    if (!parser) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   244
        return NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   245
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   246
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   247
    parser->m_parser = g_new0 (GMarkupParser, 1);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   248
    if (!parser->m_parser) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   249
        g_free (parser);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   250
        return NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   251
    }
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   252
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   253
    parser->function  = function;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   254
    parser->user_data = user_data;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   255
    parser->notify    = notify;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   256
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   257
    parser->m_parser->start_element = parser_start_node_cb;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   258
    parser->m_parser->end_element   = parser_end_node_cb;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   259
    parser->m_parser->text          = parser_text_cb;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   260
    parser->m_parser->error         = parser_error_cb;
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   261
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   262
    parser->context = g_markup_parse_context_new (parser->m_parser, 0,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   263
                                                  parser, NULL);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   264
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   265
    parser->cur_root = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   266
    parser->cur_node = NULL;
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   267
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   268
    parser->incomplete = NULL;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   269
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   270
    return parser;
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   271
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   272
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   273
static gchar *
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   274
_lm_parser_make_valid (const gchar *buffer, gchar **incomplete)
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   275
{
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   276
    GString *string;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   277
    const gchar *remainder, *invalid;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   278
    gint remaining_bytes, valid_bytes;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   279
    gunichar code; /*error code for invalid character*/
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   280
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   281
    g_return_val_if_fail (buffer != NULL, NULL);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   282
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   283
    string = NULL;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   284
    remainder = buffer;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   285
    remaining_bytes = strlen (buffer);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   286
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   287
    while (remaining_bytes != 0)
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   288
    {
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   289
        if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   290
            break;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   291
        valid_bytes = invalid - remainder;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   292
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   293
        if (string == NULL)
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   294
            string = g_string_sized_new (remaining_bytes);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   295
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   296
        g_string_append_len (string, remainder, valid_bytes);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   297
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   298
        remainder = g_utf8_find_next_char(invalid, NULL);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   299
        remaining_bytes -= valid_bytes + (remainder - invalid);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   300
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   301
        code = g_utf8_get_char_validated (invalid, -1);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   302
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   303
        if (code == -1) {
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   304
            /* A complete but invalid codepoint */
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   305
            /* append U+FFFD REPLACEMENT CHARACTER */
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   306
            g_string_append (string, "\357\277\275");
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   307
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE, "invalid character!\n");
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   308
        } else if (code == -2) {
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   309
            /* Beginning of what could be a character */
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   310
            *incomplete = g_strdup (invalid);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   311
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_VERBOSE,
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   312
                           "incomplete character: %s\n", *incomplete);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   313
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   314
            g_assert (remaining_bytes == 0);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   315
            g_assert (*(g_utf8_find_next_char(invalid, NULL)) == '\0');
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   316
        }
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   317
    }
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   318
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   319
    if (string == NULL)
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   320
        return g_strdup (buffer);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   321
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   322
    g_string_append (string, remainder);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   323
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   324
    g_assert (g_utf8_validate (string->str, -1, NULL));
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   325
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   326
    return g_string_free (string, FALSE);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   327
}
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   328
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   329
399
b5ebdc9d8152 Added a boolean return value from LmParser::parse to be able to check whether it was successful
Mikael Hallendal <micke@imendio.com>
parents: 188
diff changeset
   330
gboolean
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   331
lm_parser_parse (LmParser *parser, const gchar *string)
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   332
{
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   333
    gboolean parsed;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   334
    gchar *valid, *completed;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   335
    g_return_val_if_fail (parser != NULL, FALSE);
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 602
diff changeset
   336
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   337
    if (!parser->context) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   338
        parser->context = g_markup_parse_context_new (parser->m_parser, 0,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   339
                                                      parser, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   340
    }
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   341
    if (parser->incomplete) {
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   342
        completed = g_strdup_printf("%s%s", parser->incomplete, string);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   343
        g_free(parser->incomplete);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   344
        parser->incomplete = NULL;
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   345
    } else {
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   346
        completed = g_strdup(string);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   347
    }
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   348
    valid = _lm_parser_make_valid (completed, &parser->incomplete);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   349
    g_free(completed);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   350
    if (g_markup_parse_context_parse (parser->context, valid,
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   351
                                      (gssize)strlen (valid), NULL)) {
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   352
        parsed = TRUE;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   353
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   354
        g_markup_parse_context_free (parser->context);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   355
        parser->context = NULL;
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   356
        parsed = FALSE;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   357
    }
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   358
    g_free(valid);
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   359
    return parsed;
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   360
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   361
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   362
void
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   363
lm_parser_free (LmParser *parser)
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   364
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   365
    if (parser->notify) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   366
        (* parser->notify) (parser->user_data);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   367
    }
153
b2324df84971 2006-06-06 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 140
diff changeset
   368
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   369
    if (parser->context) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   370
        g_markup_parse_context_free (parser->context);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   371
    }
622
2fd864a27f9b Protect GMarkup parser against invalid unicode characters
Hermitifier <quantifier666@gmail.com>
parents: 607
diff changeset
   372
    g_free (parser->incomplete);
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   373
    g_free (parser->m_parser);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   374
    g_free (parser);
1
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   375
}
50e230cf7818 Initial revision
hallski <hallski>
parents:
diff changeset
   376