bygfoot/src/xml_cup.c

569 lines
21 KiB
C
Raw Permalink Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
xml_cup.c
2005-10-20 17:45:00 +02:00
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 "cup.h"
#include "file.h"
#include "league.h"
2005-04-13 15:01:59 +02:00
#include "main.h"
#include "misc.h"
2005-05-06 18:35:19 +02:00
#include "option.h"
2005-04-14 21:07:25 +02:00
#include "variables.h"
2004-12-23 13:58:39 +01:00
#include "xml_cup.h"
2008-12-26 16:59:46 +01:00
#include "xml.h"
2004-12-23 13:58:39 +01:00
/**
* The tags used in the XML files defining cups.
*/
#define TAG_CUP "cup"
2005-05-06 18:35:19 +02:00
#define TAG_GROUP "group"
2004-12-23 13:58:39 +01:00
#define TAG_LAST_WEEK "last_week"
2005-05-06 18:35:19 +02:00
#define TAG_ADD_WEEK "add_week"
2005-09-14 23:16:22 +02:00
#define TAG_TALENT_DIFF "talent_diff"
2004-12-23 13:58:39 +01:00
#define TAG_CUP_ROUNDS "cup_rounds"
#define TAG_CUP_ROUND "cup_round"
2009-01-04 11:53:09 +01:00
#define TAG_CUP_ROUND_NAME "round_name"
2005-05-08 19:56:26 +02:00
#define TAG_CUP_ROUND_NEW_TEAMS "new_teams"
2005-05-30 18:22:23 +02:00
#define TAG_CUP_ROUND_BYES "byes"
2004-12-23 13:58:39 +01:00
#define TAG_CUP_ROUND_HOME_AWAY "home_away"
#define TAG_CUP_ROUND_REPLAY "replay"
#define TAG_CUP_ROUND_NEUTRAL "neutral"
2006-06-29 18:06:52 +02:00
#define TAG_CUP_ROUND_DELAY "delay"
2005-09-27 14:18:46 +02:00
#define TAG_CUP_ROUND_RANDOMISE_TEAMS "randomise_teams"
2005-09-28 18:41:32 +02:00
#define TAG_CUP_ROUND_NUMBER_OF_GROUPS "number_of_groups"
#define TAG_CUP_ROUND_NUMBER_OF_ADVANCE "number_of_advance"
#define TAG_CUP_ROUND_NUMBER_OF_BEST_ADVANCE "number_of_best_advance"
#define TAG_CUP_ROUND_ROUND_ROBINS "round_robins"
#define TAG_CUP_ROUND_BREAK "break"
2008-12-28 16:44:12 +01:00
#define TAG_CUP_ROUND_WAIT "wait_for_cup"
2007-02-18 14:04:19 +01:00
#define TAG_CUP_ROUND_TWO_MATCH_WEEK_START "two_match_week_start"
#define TAG_CUP_ROUND_TWO_MATCH_WEEK_END "two_match_week_end"
#define TAG_CUP_ROUND_TWO_MATCH_WEEK "two_match_week"
2004-12-23 13:58:39 +01:00
#define TAG_CHOOSE_TEAMS "choose_teams"
#define TAG_CHOOSE_TEAM "choose_team"
#define TAG_CHOOSE_TEAM_SID "choose_team_sid"
2004-12-23 13:58:39 +01:00
#define TAG_CHOOSE_TEAM_NUMBER_OF_TEAMS "number_of_teams"
#define TAG_CHOOSE_TEAM_START_IDX "start_idx"
#define TAG_CHOOSE_TEAM_END_IDX "end_idx"
#define TAG_CHOOSE_TEAM_RANDOMLY "randomly"
2005-05-06 18:35:19 +02:00
#define TAG_CHOOSE_TEAM_GENERATE "generate"
#define TAG_CHOOSE_TEAM_SKIP_GROUP_CHECK "skip_group_check"
#define TAG_CHOOSE_TEAM_FROM_TABLE "from_table"
2009-01-03 12:24:00 +01:00
#define TAG_CHOOSE_TEAM_PRELOAD "preload"
2021-02-18 16:41:04 +01:00
#define TAG_CHOOSE_TEAM_OPTIONAL "optional"
#define TAG_CHOOSE_TEAM_ALTERNATIVES "alternatives"
2004-12-23 13:58:39 +01:00
2008-12-28 16:44:12 +01:00
#define ATT_NAME_CUP_ROUND_WAIT_ROUND "round"
2004-12-23 13:58:39 +01:00
/**
* Enum with the states used in the XML parser functions.
*/
enum XmlCupStates
{
STATE_CUP = 0,
STATE_NAME,
STATE_SHORT_NAME,
STATE_SYMBOL,
STATE_SID,
2005-05-06 18:35:19 +02:00
STATE_GROUP,
STATE_PROPERTY,
STATE_LAST_WEEK,
2005-05-06 18:35:19 +02:00
STATE_ADD_WEEK,
STATE_WEEK_GAP,
2008-12-26 16:59:46 +01:00
STATE_WEEK_BREAK,
2009-01-08 23:20:26 +01:00
STATE_SKIP_WEEKS_WITH,
STATE_YELLOW_RED,
2005-09-14 23:16:22 +02:00
STATE_TALENT_DIFF,
STATE_CUP_ROUNDS,
STATE_CUP_ROUND,
2009-01-04 11:53:09 +01:00
STATE_CUP_ROUND_NAME,
2005-05-08 19:56:26 +02:00
STATE_CUP_ROUND_NEW_TEAMS,
2005-05-30 18:22:23 +02:00
STATE_CUP_ROUND_BYES,
STATE_CUP_ROUND_HOME_AWAY,
STATE_CUP_ROUND_REPLAY,
STATE_CUP_ROUND_NEUTRAL,
2006-06-29 18:06:52 +02:00
STATE_CUP_ROUND_DELAY,
2005-09-27 14:18:46 +02:00
STATE_CUP_ROUND_RANDOMISE_TEAMS,
2005-09-28 18:41:32 +02:00
STATE_CUP_ROUND_NUMBER_OF_GROUPS,
STATE_CUP_ROUND_NUMBER_OF_ADVANCE,
STATE_CUP_ROUND_NUMBER_OF_BEST_ADVANCE,
STATE_CUP_ROUND_ROUND_ROBINS,
STATE_CUP_ROUND_BREAK,
2008-12-28 16:44:12 +01:00
STATE_CUP_ROUND_WAIT,
2007-02-18 14:04:19 +01:00
STATE_CUP_ROUND_TWO_MATCH_WEEK_START,
STATE_CUP_ROUND_TWO_MATCH_WEEK_END,
STATE_CUP_ROUND_TWO_MATCH_WEEK,
STATE_CHOOSE_TEAMS,
STATE_CHOOSE_TEAM,
STATE_CHOOSE_TEAM_SID,
STATE_CHOOSE_TEAM_NUMBER_OF_TEAMS,
STATE_CHOOSE_TEAM_START_IDX,
STATE_CHOOSE_TEAM_END_IDX,
STATE_CHOOSE_TEAM_RANDOMLY,
2005-05-06 18:35:19 +02:00
STATE_CHOOSE_TEAM_GENERATE,
STATE_CHOOSE_TEAM_SKIP_GROUP_CHECK,
STATE_CHOOSE_TEAM_FROM_TABLE,
2009-01-03 12:24:00 +01:00
STATE_CHOOSE_TEAM_PRELOAD,
2021-02-18 16:41:04 +01:00
STATE_CHOOSE_TEAM_OPTIONAL,
STATE_CHOOSE_TEAM_ALTERNATIVES,
2004-12-23 13:58:39 +01:00
STATE_END
};
/**
* The state variable used in the XML parsing functions.
*/
gint state;
/** The variable we will fill and append to an array. */
Cup new_cup;
2005-05-08 19:56:26 +02:00
CupRound new_round;
static CupChooseTeam *new_choose_team = NULL;
2008-12-26 16:59:46 +01:00
WeekBreak new_week_break;
2008-12-28 16:44:12 +01:00
CupRoundWait new_wait;
gboolean alternatives = FALSE;
2004-12-23 13:58:39 +01:00
/**
* 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_cup_read_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
2004-12-23 13:58:39 +01:00
{
#ifdef DEBUG
printf("xml_cup_read_start_element\n");
#endif
if(strcmp(element_name, TAG_CUP) == 0)
{
2005-04-14 21:07:25 +02:00
new_cup = cup_new(FALSE);
state = STATE_CUP;
}
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_NAME) == 0)
state = STATE_NAME;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_SHORT_NAME) == 0)
state = STATE_SHORT_NAME;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_SYMBOL) == 0)
state = STATE_SYMBOL;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_SID) == 0)
state = STATE_SID;
2005-05-06 18:35:19 +02:00
else if(strcmp(element_name, TAG_GROUP) == 0)
state = STATE_GROUP;
else if(strcmp(element_name, TAG_LAST_WEEK) == 0)
state = STATE_LAST_WEEK;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_PROPERTY) == 0)
2005-05-06 18:35:19 +02:00
state = STATE_PROPERTY;
else if(strcmp(element_name, TAG_ADD_WEEK) == 0)
state = STATE_ADD_WEEK;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_WEEK_GAP) == 0)
state = STATE_WEEK_GAP;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_WEEK_BREAK) == 0)
{
state = STATE_WEEK_BREAK;
if(attribute_names[0] != NULL && strcmp(attribute_names[0], ATT_DEF_NAME_WEEK_BREAK_LENGTH) == 0)
new_week_break.length = (gint)g_ascii_strtod(attribute_values[0], NULL);
else
2008-12-28 11:24:54 +01:00
new_week_break.length = -1000;
2008-12-26 16:59:46 +01:00
}
2009-01-08 23:20:26 +01:00
else if(strcmp(element_name, TAG_DEF_SKIP_WEEKS_WITH) == 0)
state = STATE_SKIP_WEEKS_WITH;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_YELLOW_RED) == 0)
state = STATE_YELLOW_RED;
2005-09-14 23:16:22 +02:00
else if(strcmp(element_name, TAG_TALENT_DIFF) == 0)
state = STATE_TALENT_DIFF;
else if(strcmp(element_name, TAG_CUP_ROUNDS) == 0)
state = STATE_CUP_ROUNDS;
else if(strcmp(element_name, TAG_CUP_ROUND) == 0)
{
2005-05-08 19:56:26 +02:00
new_round = cup_round_new();
state = STATE_CUP_ROUND;
}
2009-01-04 11:53:09 +01:00
else if(strcmp(element_name, TAG_CUP_ROUND_NAME) == 0)
state = STATE_CUP_ROUND_NAME;
2005-05-08 19:56:26 +02:00
else if(strcmp(element_name, TAG_CUP_ROUND_NEW_TEAMS) == 0)
state = STATE_CUP_ROUND_NEW_TEAMS;
2005-05-30 18:22:23 +02:00
else if(strcmp(element_name, TAG_CUP_ROUND_BYES) == 0)
state = STATE_CUP_ROUND_BYES;
else if(strcmp(element_name, TAG_CUP_ROUND_HOME_AWAY) == 0)
state = STATE_CUP_ROUND_HOME_AWAY;
else if(strcmp(element_name, TAG_CUP_ROUND_REPLAY) == 0)
state = STATE_CUP_ROUND_REPLAY;
else if(strcmp(element_name, TAG_CUP_ROUND_NEUTRAL) == 0)
state = STATE_CUP_ROUND_NEUTRAL;
2006-06-29 18:06:52 +02:00
else if(strcmp(element_name, TAG_CUP_ROUND_DELAY) == 0)
state = STATE_CUP_ROUND_DELAY;
2005-09-27 14:18:46 +02:00
else if(strcmp(element_name, TAG_CUP_ROUND_RANDOMISE_TEAMS) == 0)
state = STATE_CUP_ROUND_RANDOMISE_TEAMS;
2005-09-28 18:41:32 +02:00
else if(strcmp(element_name, TAG_CUP_ROUND_NUMBER_OF_GROUPS) == 0)
state = STATE_CUP_ROUND_NUMBER_OF_GROUPS;
else if(strcmp(element_name, TAG_CUP_ROUND_NUMBER_OF_ADVANCE) == 0)
state = STATE_CUP_ROUND_NUMBER_OF_ADVANCE;
else if(strcmp(element_name, TAG_CUP_ROUND_NUMBER_OF_BEST_ADVANCE) == 0)
state = STATE_CUP_ROUND_NUMBER_OF_BEST_ADVANCE;
else if(strcmp(element_name, TAG_CUP_ROUND_ROUND_ROBINS) == 0)
state = STATE_CUP_ROUND_ROUND_ROBINS;
else if(strcmp(element_name, TAG_CUP_ROUND_BREAK) == 0)
state = STATE_CUP_ROUND_BREAK;
2008-12-28 16:44:12 +01:00
else if(strcmp(element_name, TAG_CUP_ROUND_WAIT) == 0)
{
state = STATE_CUP_ROUND_WAIT;
if(attribute_names[0] != NULL && strcmp(attribute_names[0], ATT_NAME_CUP_ROUND_WAIT_ROUND) == 0)
new_wait.cup_round = (gint)g_ascii_strtod(attribute_values[0], NULL) - 1;
else
{
new_wait.cup_round = -1;
2009-04-29 19:18:54 +02:00
debug_print_message("xml_cup_read_start_element: No round number specified for cup round wait in cup %s\n", new_cup.name);
2008-12-28 16:44:12 +01:00
}
}
2007-02-18 14:04:19 +01:00
else if(strcmp(element_name, TAG_CUP_ROUND_TWO_MATCH_WEEK_START) == 0)
state = STATE_CUP_ROUND_TWO_MATCH_WEEK_START;
else if(strcmp(element_name, TAG_CUP_ROUND_TWO_MATCH_WEEK_END) == 0)
state = STATE_CUP_ROUND_TWO_MATCH_WEEK_END;
else if(strcmp(element_name, TAG_CUP_ROUND_TWO_MATCH_WEEK) == 0)
state = STATE_CUP_ROUND_TWO_MATCH_WEEK;
else if(strcmp(element_name, TAG_CHOOSE_TEAMS) == 0)
state = STATE_CHOOSE_TEAMS;
else if(strcmp(element_name, TAG_CHOOSE_TEAM) == 0)
{
if (!alternatives) {
CupChooseTeam new = cup_choose_team_new();
g_array_append_val(new_round.choose_teams, new);
new_choose_team = &g_array_index(new_round.choose_teams, CupChooseTeam,
new_round.choose_teams->len - 1);
} else {
new_choose_team->next = g_malloc(sizeof(CupChooseTeam ));
*new_choose_team->next = cup_choose_team_new();
new_choose_team = new_choose_team->next;
}
state = STATE_CHOOSE_TEAM;
}
else if(strcmp(element_name, TAG_CHOOSE_TEAM_SID) == 0)
state = STATE_CHOOSE_TEAM_SID;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_NUMBER_OF_TEAMS) == 0)
state = STATE_CHOOSE_TEAM_NUMBER_OF_TEAMS;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_START_IDX) == 0)
state = STATE_CHOOSE_TEAM_START_IDX;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_END_IDX) == 0)
state = STATE_CHOOSE_TEAM_END_IDX;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_RANDOMLY) == 0)
state = STATE_CHOOSE_TEAM_RANDOMLY;
2005-05-06 18:35:19 +02:00
else if(strcmp(element_name, TAG_CHOOSE_TEAM_GENERATE) == 0)
state = STATE_CHOOSE_TEAM_GENERATE;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_SKIP_GROUP_CHECK) == 0)
state = STATE_CHOOSE_TEAM_SKIP_GROUP_CHECK;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_FROM_TABLE) == 0)
state = STATE_CHOOSE_TEAM_FROM_TABLE;
2009-01-03 12:24:00 +01:00
else if(strcmp(element_name, TAG_CHOOSE_TEAM_PRELOAD) == 0)
state = STATE_CHOOSE_TEAM_PRELOAD;
2021-02-18 16:41:04 +01:00
else if(strcmp(element_name, TAG_CHOOSE_TEAM_OPTIONAL) == 0)
state = STATE_CHOOSE_TEAM_OPTIONAL;
else if(strcmp(element_name, TAG_CHOOSE_TEAM_ALTERNATIVES) == 0) {
state = STATE_CHOOSE_TEAM_ALTERNATIVES;
alternatives = TRUE;
} else
2009-04-29 19:18:54 +02:00
debug_print_message("xml_cup_read_start_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
2004-12-23 13:58:39 +01:00
}
/**
* 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_cup_read_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
2004-12-23 13:58:39 +01:00
{
#ifdef DEBUG
printf("xml_cup_read_end_element\n");
#endif
2008-12-26 16:59:46 +01:00
if(strcmp(element_name, TAG_DEF_NAME) == 0 ||
strcmp(element_name, TAG_DEF_SHORT_NAME) == 0 ||
strcmp(element_name, TAG_DEF_SYMBOL) == 0 ||
strcmp(element_name, TAG_DEF_SID) == 0 ||
2005-05-06 18:35:19 +02:00
strcmp(element_name, TAG_GROUP) == 0 ||
strcmp(element_name, TAG_LAST_WEEK) == 0 ||
2008-12-26 16:59:46 +01:00
strcmp(element_name, TAG_DEF_PROPERTY) == 0 ||
2005-05-06 18:35:19 +02:00
strcmp(element_name, TAG_ADD_WEEK) == 0 ||
2008-12-26 16:59:46 +01:00
strcmp(element_name, TAG_DEF_WEEK_GAP) == 0 ||
2009-01-08 23:20:26 +01:00
strcmp(element_name, TAG_DEF_SKIP_WEEKS_WITH) == 0 ||
2008-12-26 16:59:46 +01:00
strcmp(element_name, TAG_DEF_YELLOW_RED) == 0 ||
2005-09-14 23:16:22 +02:00
strcmp(element_name, TAG_TALENT_DIFF) == 0 ||
2005-05-08 19:56:26 +02:00
strcmp(element_name, TAG_CUP_ROUNDS) == 0)
state = STATE_CUP;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_WEEK_BREAK) == 0)
{
state = STATE_CUP;
g_array_append_val(new_cup.week_breaks, new_week_break);
}
else if(strcmp(element_name, TAG_CUP_ROUND) == 0)
2005-05-08 19:56:26 +02:00
{
state = STATE_CUP_ROUNDS;
if(new_round.home_away == 0)
new_round.round_robins = 1;
league_cup_adjust_rr_breaks(new_round.rr_breaks, new_round.round_robins, new_cup.week_gap);
2005-05-08 19:56:26 +02:00
g_array_append_val(new_cup.rounds, new_round);
}
else if(strcmp(element_name, TAG_CUP_ROUND_HOME_AWAY) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_REPLAY) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_NEUTRAL) == 0 ||
2006-06-29 18:06:52 +02:00
strcmp(element_name, TAG_CUP_ROUND_DELAY) == 0 ||
2005-09-27 14:18:46 +02:00
strcmp(element_name, TAG_CUP_ROUND_RANDOMISE_TEAMS) == 0 ||
2005-09-28 18:41:32 +02:00
strcmp(element_name, TAG_CUP_ROUND_NUMBER_OF_GROUPS) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_NUMBER_OF_ADVANCE) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_NUMBER_OF_BEST_ADVANCE) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_ROUND_ROBINS) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_BREAK) == 0 ||
2008-12-28 16:44:12 +01:00
strcmp(element_name, TAG_CUP_ROUND_WAIT) == 0 ||
2007-02-18 14:04:19 +01:00
strcmp(element_name, TAG_CUP_ROUND_TWO_MATCH_WEEK_START) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_TWO_MATCH_WEEK_END) == 0 ||
strcmp(element_name, TAG_CUP_ROUND_TWO_MATCH_WEEK) == 0 ||
2005-05-08 19:56:26 +02:00
strcmp(element_name, TAG_CUP_ROUND_NEW_TEAMS) == 0 ||
2009-01-04 11:53:09 +01:00
strcmp(element_name, TAG_CUP_ROUND_NAME) == 0 ||
2005-05-30 18:22:23 +02:00
strcmp(element_name, TAG_CUP_ROUND_BYES) == 0 ||
2005-05-08 19:56:26 +02:00
strcmp(element_name, TAG_CHOOSE_TEAMS) == 0)
2008-12-28 16:44:12 +01:00
{
state = STATE_CUP_ROUND;
if(strcmp(element_name, TAG_CUP_ROUND_WAIT) == 0)
g_array_append_val(new_round.waits, new_wait);
}
else if(strcmp(element_name, TAG_CHOOSE_TEAM) == 0)
2005-05-08 19:56:26 +02:00
{
state = STATE_CHOOSE_TEAMS;
}
else if (strcmp(element_name, TAG_CHOOSE_TEAM_ALTERNATIVES) == 0)
{
new_choose_team = &g_array_index(new_round.choose_teams, CupChooseTeam,
new_round.choose_teams->len - 1);
state = STATE_CHOOSE_TEAM;
alternatives = FALSE;
2005-05-08 19:56:26 +02:00
}
else if(strcmp(element_name, TAG_CHOOSE_TEAM_SID) == 0 ||
strcmp(element_name, TAG_CHOOSE_TEAM_NUMBER_OF_TEAMS) == 0 ||
strcmp(element_name, TAG_CHOOSE_TEAM_START_IDX) == 0 ||
strcmp(element_name, TAG_CHOOSE_TEAM_END_IDX) == 0 ||
2005-05-06 18:35:19 +02:00
strcmp(element_name, TAG_CHOOSE_TEAM_RANDOMLY) == 0 ||
strcmp(element_name, TAG_CHOOSE_TEAM_SKIP_GROUP_CHECK) == 0 ||
strcmp(element_name, TAG_CHOOSE_TEAM_FROM_TABLE) == 0 ||
2009-01-03 12:24:00 +01:00
strcmp(element_name, TAG_CHOOSE_TEAM_PRELOAD) == 0 ||
2021-02-18 16:41:04 +01:00
strcmp(element_name, TAG_CHOOSE_TEAM_OPTIONAL) == 0 ||
2005-05-06 18:35:19 +02:00
strcmp(element_name, TAG_CHOOSE_TEAM_GENERATE) == 0)
state = STATE_CHOOSE_TEAM;
else if(strcmp(element_name, TAG_CUP) != 0)
2009-04-29 19:18:54 +02:00
debug_print_message("xml_cup_read_end_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
2004-12-23 13:58:39 +01:00
}
/**
* 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_cup_read_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
2004-12-23 13:58:39 +01:00
{
#ifdef DEBUG
printf("xml_cup_read_text\n");
#endif
gchar buf[text_len + 1];
2005-09-21 19:42:41 +02:00
gint int_value;
gfloat float_value;
2004-12-23 13:58:39 +01:00
strncpy(buf, text, text_len);
buf[text_len] = '\0';
2005-09-21 19:42:41 +02:00
int_value = (gint)g_ascii_strtod(buf, NULL);
float_value = (gfloat)g_ascii_strtod(buf, NULL);
if(state == STATE_NAME)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_cup.name, buf);
else if(state == STATE_SHORT_NAME)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_cup.short_name, buf);
else if(state == STATE_SYMBOL)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_cup.symbol, buf);
else if(state == STATE_SID)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_cup.sid, buf);
2005-05-06 18:35:19 +02:00
else if(state == STATE_GROUP)
2005-09-21 19:42:41 +02:00
new_cup.group = int_value;
else if(state == STATE_LAST_WEEK)
2005-09-21 19:42:41 +02:00
new_cup.last_week = int_value;
2005-05-06 18:35:19 +02:00
else if(state == STATE_PROPERTY)
2005-10-05 21:59:37 +02:00
g_ptr_array_add(new_cup.properties, g_strdup(buf));
2005-05-06 18:35:19 +02:00
else if(state == STATE_ADD_WEEK)
2005-09-21 19:42:41 +02:00
new_cup.add_week = int_value;
else if(state == STATE_WEEK_GAP)
2005-09-21 19:42:41 +02:00
new_cup.week_gap = int_value;
2008-12-26 16:59:46 +01:00
else if(state == STATE_WEEK_BREAK)
new_week_break.week_number = int_value;
2009-01-08 23:20:26 +01:00
else if(state == STATE_SKIP_WEEKS_WITH)
g_ptr_array_add(new_cup.skip_weeks_with, g_strdup(buf));
else if(state == STATE_YELLOW_RED)
2005-09-21 19:42:41 +02:00
new_cup.yellow_red = int_value;
2005-09-14 23:16:22 +02:00
else if(state == STATE_TALENT_DIFF)
2005-09-21 19:42:41 +02:00
new_cup.talent_diff =
2005-10-13 10:52:33 +02:00
(float_value / 10000);
2009-01-04 11:53:09 +01:00
else if(state == STATE_CUP_ROUND_NAME)
new_round.name = g_strdup(buf);
2005-05-08 19:56:26 +02:00
else if(state == STATE_CUP_ROUND_NEW_TEAMS)
2005-09-21 19:42:41 +02:00
new_round.new_teams = int_value;
2005-05-30 18:22:23 +02:00
else if(state == STATE_CUP_ROUND_BYES)
2005-09-21 19:42:41 +02:00
new_round.byes = int_value;
else if(state == STATE_CUP_ROUND_HOME_AWAY)
2005-09-21 19:42:41 +02:00
new_round.home_away = int_value;
else if(state == STATE_CUP_ROUND_REPLAY)
2005-09-21 19:42:41 +02:00
new_round.replay = int_value;
else if(state == STATE_CUP_ROUND_NEUTRAL)
2005-09-21 19:42:41 +02:00
new_round.neutral = int_value;
2006-06-29 18:06:52 +02:00
else if(state == STATE_CUP_ROUND_DELAY)
new_round.delay = int_value;
2005-09-27 14:18:46 +02:00
else if(state == STATE_CUP_ROUND_RANDOMISE_TEAMS)
new_round.randomise_teams = int_value;
2005-09-28 18:41:32 +02:00
else if(state == STATE_CUP_ROUND_NUMBER_OF_GROUPS)
2005-09-21 19:42:41 +02:00
new_round.round_robin_number_of_groups = int_value;
2005-09-28 18:41:32 +02:00
else if(state == STATE_CUP_ROUND_NUMBER_OF_ADVANCE)
2005-09-21 19:42:41 +02:00
new_round.round_robin_number_of_advance = int_value;
2005-09-28 18:41:32 +02:00
else if(state == STATE_CUP_ROUND_NUMBER_OF_BEST_ADVANCE)
2005-09-21 19:42:41 +02:00
new_round.round_robin_number_of_best_advance = int_value;
else if(state == STATE_CUP_ROUND_ROUND_ROBINS)
new_round.round_robins = int_value;
else if(state == STATE_CUP_ROUND_BREAK)
league_cup_fill_rr_breaks(new_round.rr_breaks, buf);
2008-12-28 16:44:12 +01:00
else if(state == STATE_CUP_ROUND_WAIT)
new_wait.cup_sid = g_strdup(buf);
2007-02-18 14:04:19 +01:00
else if(state == STATE_CUP_ROUND_TWO_MATCH_WEEK_START)
g_array_append_val(new_round.two_match_weeks[0], int_value);
else if(state == STATE_CUP_ROUND_TWO_MATCH_WEEK_END)
g_array_append_val(new_round.two_match_weeks[1], int_value);
else if(state == STATE_CUP_ROUND_TWO_MATCH_WEEK)
new_round.two_match_week = int_value;
else if(state == STATE_CHOOSE_TEAM_SID)
misc_string_assign(&new_choose_team->sid, buf);
else if(state == STATE_CHOOSE_TEAM_NUMBER_OF_TEAMS)
new_choose_team->number_of_teams = int_value;
else if(state == STATE_CHOOSE_TEAM_START_IDX)
new_choose_team->start_idx = int_value;
else if(state == STATE_CHOOSE_TEAM_END_IDX)
new_choose_team->end_idx = int_value;
else if(state == STATE_CHOOSE_TEAM_RANDOMLY)
new_choose_team->randomly = int_value;
2005-05-06 18:35:19 +02:00
else if(state == STATE_CHOOSE_TEAM_GENERATE)
new_choose_team->generate = int_value;
else if(state == STATE_CHOOSE_TEAM_SKIP_GROUP_CHECK)
new_choose_team->skip_group_check = int_value;
else if(state == STATE_CHOOSE_TEAM_FROM_TABLE)
new_choose_team->from_table = int_value;
2009-01-03 12:24:00 +01:00
else if(state == STATE_CHOOSE_TEAM_PRELOAD)
new_choose_team->preload = int_value;
2021-02-18 16:41:04 +01:00
else if(state == STATE_CHOOSE_TEAM_OPTIONAL)
new_choose_team->optional = int_value;
2004-12-23 13:58:39 +01:00
}
/**
* Function reading an XML file specifying a cup.
2004-12-23 13:58:39 +01:00
* @param cup_name name of the xml file (e.g. 'cup_england_fa.xml')
* to be read. Full path is not necessary, if the file is located in
* one of the suppport directories; neither are the prefix 'cup_'
* or the suffix '.xml'.
* @param cups The array we append the new cup to.
2004-12-23 13:58:39 +01:00
*/
void
xml_cup_read(const gchar *cup_name, GArray *cups)
2004-12-23 13:58:39 +01:00
{
#ifdef DEBUG
printf("xml_cup_read\n");
#endif
2005-04-09 21:18:28 +02:00
gchar *file_name = file_find_support_file(cup_name, FALSE);
2004-12-23 13:58:39 +01:00
GMarkupParser parser = {xml_cup_read_start_element,
xml_cup_read_end_element,
xml_cup_read_text, NULL, NULL};
GMarkupParseContext *context;
gchar *file_contents;
2006-06-04 19:10:08 +02:00
gsize length;
2004-12-23 13:58:39 +01:00
GError *error = NULL;
gchar buf[SMALL];
2009-01-04 11:53:09 +01:00
gint i;
2004-12-23 13:58:39 +01:00
context =
g_markup_parse_context_new(&parser, 0, NULL, NULL);
if(file_name == NULL)
{
sprintf(buf, "cup_%s.xml", cup_name);
2005-04-09 21:18:28 +02:00
file_name = file_find_support_file(buf, TRUE);
2004-12-23 13:58:39 +01:00
}
if(!g_file_get_contents(file_name, &file_contents, &length, &error))
{
2009-04-29 19:18:54 +02:00
debug_print_message("xml_cup_read: error reading file %s\n", file_name);
2005-01-09 21:21:22 +01:00
misc_print_error(&error, FALSE);
2004-12-23 13:58:39 +01:00
return;
}
state = STATE_CUP;
strcpy(buf, file_name);
g_free(file_name);
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
{
g_critical("xml_cup_read: error parsing file %s\n", buf);
2005-01-09 21:21:22 +01:00
misc_print_error(&error, TRUE);
2004-12-23 13:58:39 +01:00
}
2005-04-13 15:01:59 +02:00
2005-05-06 18:35:19 +02:00
new_cup.id = cup_id_new;
2008-12-26 16:59:46 +01:00
league_cup_adjust_week_breaks(new_cup.week_breaks, new_cup.week_gap);
2009-01-04 11:53:09 +01:00
for(i = 0; i < new_cup.rounds->len; i++)
if(g_array_index(new_cup.rounds, CupRound, i).name == NULL)
{
cup_get_round_name(&new_cup, i, buf);
g_array_index(new_cup.rounds, CupRound, i).name = g_strdup(buf);
}
2005-04-14 21:07:25 +02:00
g_array_append_val(cups, new_cup);
2004-12-23 13:58:39 +01:00
}