File: xml_news.c

package info (click to toggle)
bygfoot 2.3.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 12,620 kB
  • ctags: 3,441
  • sloc: xml: 113,203; ansic: 40,378; makefile: 2,345; sh: 1,816
file content (304 lines) | stat: -rw-r--r-- 9,999 bytes parent folder | download | duplicates (3)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
  xml_news.c

  Bygfoot Football Manager -- a small and simple GTK2-based
  football management game.

  http://bygfoot.sourceforge.net

  Copyright (C) 2005  Gyözö Both (gyboth@bygfoot.com)

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "free.h"
#include "news.h"
#include "misc.h"
#include "variables.h"
#include "xml_news.h"

#define TAG_NEWS "news"
#define TAG_ARTICLE "news_article"
#define TAG_ARTICLE_TYPE "type"
#define TAG_ARTICLE_CONDITION "condition"
#define TAG_ARTICLE_PRIORITY "priority"
#define TAG_ARTICLE_TITLE "title"
#define TAG_ARTICLE_SUBTITLE "subtitle"

#define ATT_NAME_TEXT_PRIORITY "priority"
#define ATT_NAME_TEXT_CONDITION "condition"

#define ARTICLE_TYPE_NAME_MATCH "match"
#define ARTICLE_TYPE_NAME_FINANCES "finances"
#define ARTICLE_TYPE_NAME_STAR_PLAYER_TRANSFER "star_player_transfer"
#define ARTICLE_TYPE_NAME_LEAGUE_CHAMPION "league_champion"
#define ARTICLE_TYPE_NAME_CUP_QUALIFICATION "cup_qualification"
#define ARTICLE_TYPE_NAME_RELEGATION "relegation"

enum XmlNewsStates
{
    STATE_NEWS = 0,
    STATE_ARTICLE,
    STATE_ARTICLE_TYPE,
    STATE_ARTICLE_CONDITION,
    STATE_ARTICLE_PRIORITY,
    STATE_ARTICLE_TITLE,
    STATE_ARTICLE_SUBTITLE,
    STATE_END
};

gint state, article_idx, priority;
gchar *condition;
NewsArticle new_article;
NewsText new_title, new_subtitle;

/** Return the appropriate enum integer going with the type string. */
gint
xml_news_article_type_to_int(const gchar *type_string)
{
#ifdef DEBUG
    printf("xml_news_type_to_int\n");
#endif

    gint return_value = -1;

    if(strcmp(type_string, ARTICLE_TYPE_NAME_MATCH) == 0)
	return_value = NEWS_ARTICLE_TYPE_MATCH;
    else if(strcmp(type_string, ARTICLE_TYPE_NAME_FINANCES) == 0)
	return_value = NEWS_ARTICLE_TYPE_FINANCES;
    else if(strcmp(type_string, ARTICLE_TYPE_NAME_STAR_PLAYER_TRANSFER) == 0)
	return_value = NEWS_ARTICLE_TYPE_STAR_PLAYER_TRANSFER;
    else if(strcmp(type_string, ARTICLE_TYPE_NAME_LEAGUE_CHAMPION) == 0)
	return_value = NEWS_ARTICLE_TYPE_LEAGUE_CHAMPION;
    else if(strcmp(type_string, ARTICLE_TYPE_NAME_CUP_QUALIFICATION) == 0)
	return_value = NEWS_ARTICLE_TYPE_CUP_QUALIFICATION;
    else if(strcmp(type_string, ARTICLE_TYPE_NAME_RELEGATION) == 0)
	return_value = NEWS_ARTICLE_TYPE_RELEGATION;
    else
	debug_print_message("xml_news_type_to_int: unknown type name %s \n", 
		  type_string);

    return return_value;
}

/**
 * The function called by the parser when an opening tag is read.
 * The state variable is changed in this function and
 * sometimes memory allocated for the information that's going to be read.
 * @see The GLib manual (Simple XML parser).
 */
void
xml_news_read_start_element (GMarkupParseContext *context,
                             const gchar         *element_name,
                             const gchar        **attribute_names,
                             const gchar        **attribute_values,
                             gpointer             user_data,
                             GError             **error)
{
#ifdef DEBUG
    printf("xml_news_read_start_element\n");
#endif
    gint atidx;
    
    atidx = 0;

    if(strcmp(element_name, TAG_NEWS) == 0)
	state = STATE_NEWS;
    else if(strcmp(element_name, TAG_ARTICLE) == 0)
    {
	state = STATE_ARTICLE;
        new_article.titles = g_array_new(FALSE, FALSE, sizeof(NewsText));
        new_article.subtitles = g_array_new(FALSE, FALSE, sizeof(NewsText));
        new_article.condition = g_strdup("0");
	new_article.priority = 1;
        new_article.id = news_article_id_new;
    }
    else if(strcmp(element_name, TAG_ARTICLE_TYPE) == 0)
	state = STATE_ARTICLE_TYPE;
    else if(strcmp(element_name, TAG_ARTICLE_CONDITION) == 0)
	state = STATE_ARTICLE_CONDITION;
    else if(strcmp(element_name, TAG_ARTICLE_PRIORITY) == 0)
	state = STATE_ARTICLE_PRIORITY;
    else if(strcmp(element_name, TAG_ARTICLE_TITLE) == 0)
    {
	state = STATE_ARTICLE_TITLE;
        new_title.id = news_title_id_new;
        new_title.priority = 1;
        new_title.condition = g_strdup("1");

        while(attribute_names[atidx] != NULL)
        {
            if(strcmp(attribute_names[atidx], ATT_NAME_TEXT_PRIORITY) == 0)
                new_title.priority = (gint)g_ascii_strtod(attribute_values[atidx], NULL);
            else if(strcmp(attribute_names[atidx], ATT_NAME_TEXT_CONDITION) == 0)
                misc_string_assign(&new_title.condition, attribute_values[atidx]);
            atidx++;
        }
    }
    else if(strcmp(element_name, TAG_ARTICLE_SUBTITLE) == 0)
    {
	state = STATE_ARTICLE_SUBTITLE;
        new_subtitle.id = news_subtitle_id_new;
        new_subtitle.priority = 1;
        new_subtitle.condition = g_strdup("1");

        while(attribute_names[atidx] != NULL)
        {
            if(strcmp(attribute_names[atidx], ATT_NAME_TEXT_PRIORITY) == 0)
                new_subtitle.priority = (gint)g_ascii_strtod(attribute_values[atidx], NULL);
            else if(strcmp(attribute_names[atidx], ATT_NAME_TEXT_CONDITION) == 0)
                misc_string_assign(&new_subtitle.condition, attribute_values[atidx]);                            
            atidx++;
        }
    }
    else
	debug_print_message("xml_news_read_start_element: unknown tag: %s; I'm in state %d\n",
		  element_name, state);
}

/**
 * The function called by the parser when a closing tag is read.
 * The state variable is changed in this function.
 * @see The GLib manual (Simple XML parser).
 */
void
xml_news_read_end_element    (GMarkupParseContext *context,
                              const gchar         *element_name,
                              gpointer             user_data,
                              GError             **error)
{
#ifdef DEBUG
    printf("xml_news_read_end_element\n");
#endif

    if(strcmp(element_name, TAG_ARTICLE) == 0)
    {
     	state = STATE_NEWS;
        g_array_append_val(news[article_idx], new_article);
    }
    else if(strcmp(element_name, TAG_ARTICLE_TYPE) == 0 ||
	    strcmp(element_name, TAG_ARTICLE_CONDITION) == 0 ||
	    strcmp(element_name, TAG_ARTICLE_PRIORITY) == 0 ||
	    strcmp(element_name, TAG_ARTICLE_TITLE) == 0 ||
	    strcmp(element_name, TAG_ARTICLE_SUBTITLE) == 0)
	state = STATE_ARTICLE;
    else if(strcmp(element_name, TAG_NEWS) != 0)
	debug_print_message("xml_news_read_end_element: unknown tag: %s; I'm in state %d\n",
		  element_name, state);
}

/**
 * The function called by the parser when the text between tags is read.
 * This function is responsible for filling in the variables (e.g. team names)
 * when a file gets loaded.
 * @see The GLib manual (Simple XML parser).
 */
void
xml_news_read_text         (GMarkupParseContext *context,
                            const gchar         *text,
                            gsize                text_len,  
                            gpointer             user_data,
                            GError             **error)
{
#ifdef DEBUG
    printf("xml_news_read_text\n");
#endif

    gchar buf[text_len + 1];
    gint int_value;

    strncpy(buf, text, text_len);
    buf[text_len] = '\0';

    int_value = (gint)g_ascii_strtod(buf, NULL);

    if(state == STATE_ARTICLE_TYPE)
	article_idx = xml_news_article_type_to_int(buf);
    else if(state == STATE_ARTICLE_CONDITION)
        misc_string_assign(&new_article.condition, buf);
    else if(state == STATE_ARTICLE_PRIORITY)
        new_article.priority = int_value;
    else if(state == STATE_ARTICLE_TITLE)
    {
        new_title.text = g_strdup(buf);
        g_array_append_val(new_article.titles, new_title);
    }
    else if(state == STATE_ARTICLE_SUBTITLE)
    {
        new_subtitle.text = g_strdup(buf);
        g_array_append_val(new_article.subtitles, new_subtitle);
    }
}

/**
 * Function reading an XML file specifying a country.
 * The variable #country gets freed and overwritten afterwards.
 * @param news_file Name of the xml file (e.g. 'news_en.xml')
 * to be read. Full path is not necessary, if the file is located in
 * one of the suppport directories; neither are the prefix 'news_'
 * or the suffix '.xml'.
 */
void
xml_news_read(const gchar *news_file)
{
#ifdef DEBUG
    printf("xml_news_read\n");
#endif

    GMarkupParser parser = {xml_news_read_start_element,
			    xml_news_read_end_element,
			    xml_news_read_text, NULL, NULL};
    GMarkupParseContext *context;
    gchar *file_contents;
    gsize length;
    GError *error = NULL;

    context = 
	g_markup_parse_context_new(&parser, 0, NULL, NULL);

    if(!g_file_get_contents(news_file, &file_contents, &length, &error))
    {
	debug_print_message("xml_news_read: error reading file %s\n", news_file);
	if(g_str_has_suffix(news_file, "news_en.xml"))
	    misc_print_error(&error, TRUE);
	else
	{
	    misc_print_error(&error, FALSE);
	    news_load_news_file("news_en.xml", TRUE);
	}
	return;
    }

    free_news(TRUE);

    if(g_markup_parse_context_parse(context, file_contents, length, &error))
    {
	g_markup_parse_context_end_parse(context, NULL);	
	g_markup_parse_context_free(context);
	g_free(file_contents);
    }
    else
    {
	debug_print_message("xml_news_read: error parsing file %s\n", news_file);
	if(g_str_has_suffix(news_file, "news_en.xml"))
	    misc_print_error(&error, TRUE);
	else
	{
	    misc_print_error(&error, FALSE);
	    news_load_news_file("news_en.xml", TRUE);
	}
	return;
    }
}