summaryrefslogtreecommitdiffstats
path: root/src/lib/event_xml_parser.c
blob: 6b5629571dba2adb53e17aff8242a70be6816680 (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
//#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "event_xml_parser.h"

#define EVENT_ELEMENT       "event"
#define LABEL_ELEMENT       "label"
#define DESCRIPTION_ELEMENT "description"
#define ALLOW_EMPTY_ELEMENT "allow-empty"
#define OPTION_ELEMENT      "option"
#define ACTION_ELEMENT      "action"
#define NAME_ELEMENT        "name"

int in_option = 0;

static const char *option_types[] =
{
    "text",
    "bool",
    "password",
    "number",
    NULL
};

// Called for open tags <foo bar="baz">
void start_element(GMarkupParseContext *context,
                  const gchar *element_name,
                  const gchar **attribute_names,
                  const gchar **attribute_values,
                  gpointer user_data,
                  GError **error)
{
    //g_print("start: %s\n", element_name);

    event_obj_t *ui = (event_obj_t *)user_data;

    if(strcmp(element_name, OPTION_ELEMENT) == 0)
    {
        if(in_option == 0)
        {
            in_option = 1;
            event_option_obj_t *option = (event_option_obj_t *)malloc(sizeof(event_option_obj_t));
            //we need to prepend, so ui->options always points to the last created option
            ui->options = g_list_prepend(ui->options, option);

            int i;
            for(i = 0; attribute_names[i] != NULL; ++i)
            {
                //g_print("attr: %s:%s\n", attribute_names[i], attribute_values[i]);
                if(strcmp(attribute_names[i], "name") == 0)
                    option->name = strdup(attribute_values[i]);
                if(strcmp(attribute_names[i], "type") == 0)
                {
                    option_type_t type = 0;
                    for(type = OPTION_TYPE_TEXT; type < OPTION_TYPE_INVALID; ++type)
                    {
                        if(strcmp(option_types[type], attribute_values[i]) == 0)
                            option->type = type;
                    }
                }
            }
        }
        else
        {
            g_print("error, option nested in option!\n");
            exit(-127);
        }
    }

}

 // Called for close tags </foo>
void end_element(GMarkupParseContext *context,
                          const gchar         *element_name,
                          gpointer             user_data,
                          GError             **error)
{
    event_obj_t *ui = (event_obj_t *)user_data;
    if(strcmp(element_name, OPTION_ELEMENT) == 0)
    {
        in_option = 0;
    }
    if(strcmp(element_name, EVENT_ELEMENT) == 0)
    {
        //we need to revers the list, because we we're prepending
        ui->options = g_list_reverse(ui->options);
        in_option = 0;
    }
}

  // Called for character data
  // text is not nul-terminated
void text(GMarkupParseContext *context,
         const gchar         *text,
         gsize                text_len,
         gpointer             user_data,
         GError             **error)
{
    event_obj_t *ui = (event_obj_t *)user_data;
    const gchar * inner_element = g_markup_parse_context_get_element(context);
    char *_text = (char *)malloc(text_len+1);
    strncpy(_text, text, text_len);
    _text[text_len] = '\0';
    if(in_option == 1)
    {
        event_option_obj_t *option = (event_option_obj_t *)((ui->options)->data);
        if(strcmp(inner_element, LABEL_ELEMENT) == 0)
        {
            //g_print("\tnew label: \t\t%s\n", _text);
            option->label = _text;
        }
        if(strcmp(inner_element, DESCRIPTION_ELEMENT) == 0)
        {
            //g_print("\ttooltip: \t\t%s\n", _text);
            option->description = _text;
        }
    }
    else
    {
        /* we're not in option, so the description is for the event */
        if(strcmp(inner_element, ACTION_ELEMENT) == 0)
        {
            //g_print("\taction description: \t%s\n", _text);
            ui->action = _text;
        }
        if(strcmp(inner_element, NAME_ELEMENT) == 0)
        {
            //g_print("\tevent name: \t\t%s\n", _text);
            ui->name = _text;
        }
    }
    //will be freed when the event_option is freed
    //free(_text);
}

  // Called for strings that should be re-saved verbatim in this same
  // position, but are not otherwise interpretable.  At the moment
  // this includes comments and processing instructions.
  // text is not nul-terminated
void passthrough(GMarkupParseContext *context,
                const gchar *passthrough_text,
                gsize text_len,
                gpointer user_data,
                GError **error)
{
    g_print("passthrough\n");
}

// Called on error, including one set by other
// methods in the vtable. The GError should not be freed.
void error(GMarkupParseContext *context,
          GError *error,
          gpointer user_data)
{
    g_print("error\n");
}

/* this function takes 2 parameters
  * ui -> pointer to event_obj_t
  * filename -> filename to read
 * event_obj_t contains list of options, which is malloced by hits function
 * and must be freed by the caller
 */

void load_event_from_file(event_obj_t *ui, const char* filename)
{
    GMarkupParser parser;
    parser.start_element = &start_element;
    parser.end_element = &end_element;
    parser.text = &text;
    parser.passthrough = &passthrough;
    parser.error = &error;
    GMarkupParseContext *context = g_markup_parse_context_new(
                    &parser, G_MARKUP_TREAT_CDATA_AS_TEXT,
                    ui, NULL);
    FILE* fin = fopen(filename, "r");
    size_t read_bytes = 0;
    char buff[1024] = {'\0'};
    while((read_bytes = fread(buff, 1, 1024, fin)))
    {
        g_markup_parse_context_parse(context, buff, read_bytes, NULL);
    }
    fclose(fin);
    free(context);
}