bygfoot/src/xml_country.c

296 lines
8.7 KiB
C
Raw Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
xml_country.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 "file.h"
#include "free.h"
#include "misc.h"
2005-06-23 23:53:57 +02:00
#include "option.h"
#include "variables.h"
#include "xml_cup.h"
2004-12-23 13:58:39 +01:00
#include "xml_country.h"
#include "xml_league.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 countries.
*/
#define TAG_COUNTRY "country"
#define TAG_RATING "rating"
2005-06-22 19:44:33 +02:00
#define TAG_SUPERNATIONAL "supernational"
2004-12-23 13:58:39 +01:00
#define TAG_LEAGUES "leagues"
#define TAG_LEAGUE "league"
#define TAG_CUPS "cups"
#define TAG_CUP "cup"
#define TAG_RESERVE_PROMOTION_RULES "reserve_promotion_rules"
2004-12-23 13:58:39 +01:00
/**
* Enum with the states used in the XML parser functions.
*/
enum XmlCountryStates
{
STATE_COUNTRY = 0,
STATE_NAME,
STATE_RATING,
2004-12-23 13:58:39 +01:00
STATE_SYMBOL,
STATE_SID,
2005-06-22 19:44:33 +02:00
STATE_SUPERNATIONAL,
2004-12-23 13:58:39 +01:00
STATE_LEAGUES,
STATE_LEAGUE,
STATE_CUPS,
STATE_CUP,
STATE_RESERVE_PROMOTION_RULES,
2004-12-23 13:58:39 +01:00
STATE_END
};
/**
* The state variable used in the XML parsing functions.
*/
gint state;
Country *cntry;
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_country_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_country_read_start_element\n");
#endif
2008-12-26 16:59:46 +01:00
if(strcmp(element_name, TAG_DEF_NAME) == 0)
2004-12-23 13:58:39 +01:00
state = STATE_NAME;
else if(strcmp(element_name, TAG_RATING) == 0)
state = STATE_RATING;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_SYMBOL) == 0)
2004-12-23 13:58:39 +01:00
state = STATE_SYMBOL;
2008-12-26 16:59:46 +01:00
else if(strcmp(element_name, TAG_DEF_SID) == 0)
state = STATE_SID;
2005-06-22 19:44:33 +02:00
else if(strcmp(element_name, TAG_SUPERNATIONAL) == 0)
state = STATE_SUPERNATIONAL;
2004-12-23 13:58:39 +01:00
else if(strcmp(element_name, TAG_LEAGUES) == 0)
{
state = STATE_LEAGUES;
if(cntry->leagues == NULL)
cntry->leagues = g_array_new(FALSE, FALSE, sizeof(League));
2004-12-23 13:58:39 +01:00
}
else if(strcmp(element_name, TAG_LEAGUE) == 0)
state = STATE_LEAGUE;
else if(strcmp(element_name, TAG_CUPS) == 0)
{
state = STATE_CUPS;
if(cntry->cups == NULL)
cntry->cups = g_array_new(FALSE, FALSE, sizeof(Cup));
2004-12-23 13:58:39 +01:00
}
else if(strcmp(element_name, TAG_CUP) == 0)
state = STATE_CUP;
else if(strcmp(element_name, TAG_RESERVE_PROMOTION_RULES) == 0)
state = STATE_RESERVE_PROMOTION_RULES;
2004-12-23 13:58:39 +01:00
else if(strcmp(element_name, TAG_COUNTRY) != 0)
2009-04-29 19:18:54 +02:00
debug_print_message("xml_country_read_start_element: unknown tag: %s; I'm in state %d\n",
2004-12-23 13:58:39 +01:00
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_country_read_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
2008-12-26 16:59:46 +01:00
#ifdef DEBUG
printf("xml_country_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_RATING) == 0 ||
2008-12-26 16:59:46 +01:00
strcmp(element_name, TAG_DEF_SYMBOL) == 0 ||
strcmp(element_name, TAG_DEF_SID) == 0 ||
2005-06-22 19:44:33 +02:00
strcmp(element_name, TAG_SUPERNATIONAL) == 0 ||
2004-12-23 13:58:39 +01:00
strcmp(element_name, TAG_LEAGUES) == 0 ||
strcmp(element_name, TAG_RESERVE_PROMOTION_RULES) == 0 ||
2005-05-06 18:35:19 +02:00
strcmp(element_name, TAG_CUPS) == 0)
2004-12-23 13:58:39 +01:00
state = STATE_COUNTRY;
else if(strcmp(element_name, TAG_LEAGUE) == 0)
state = STATE_LEAGUES;
else if(strcmp(element_name, TAG_CUP) == 0)
2005-05-06 18:35:19 +02:00
state = STATE_CUPS;
2004-12-23 13:58:39 +01:00
else if(strcmp(element_name, TAG_COUNTRY) != 0)
2009-04-29 19:18:54 +02:00
debug_print_message("xml_country_read_end_element: unknown tag: %s; I'm in state %d\n",
2004-12-23 13:58:39 +01:00
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_country_read_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
#ifdef DEBUG
printf("xml_country_read_text\n");
#endif
2004-12-23 13:58:39 +01:00
gchar buf[text_len + 1];
2005-06-22 19:44:33 +02:00
gint int_value;
2004-12-23 13:58:39 +01:00
strncpy(buf, text, text_len);
buf[text_len] = '\0';
2005-06-22 19:44:33 +02:00
int_value = (gint)g_ascii_strtod(buf, NULL);
2004-12-23 13:58:39 +01:00
if(state == STATE_NAME)
misc_string_assign(&cntry->name, buf);
else if(state == STATE_RATING)
cntry->rating = int_value;
2004-12-23 13:58:39 +01:00
else if(state == STATE_SYMBOL)
misc_string_assign(&cntry->symbol, buf);
else if(state == STATE_SID)
misc_string_assign(&cntry->sid, buf);
else if(state == STATE_SUPERNATIONAL && cntry == &country)
2005-06-23 23:53:57 +02:00
{
2005-06-26 13:42:01 +02:00
sett_set_int("int_opt_disable_finances", 1);
sett_set_int("int_opt_disable_transfers", 1);
sett_set_int("int_opt_disable_stadium", 1);
sett_set_int("int_opt_disable_contracts", 1);
sett_set_int("int_opt_disable_boost_on", 1);
2005-09-19 23:13:36 +02:00
sett_set_int("int_opt_disable_ya", 1);
2007-01-18 18:42:48 +01:00
sett_set_int("int_opt_disable_training_camp", 1);//***ML***
2005-06-23 23:53:57 +02:00
}
2004-12-23 13:58:39 +01:00
else if(state == STATE_LEAGUE)
xml_league_read(buf, cntry->leagues);
2004-12-23 13:58:39 +01:00
else if(state == STATE_CUP)
xml_cup_read(buf, cntry->cups);
else if(state == STATE_RESERVE_PROMOTION_RULES) {
if (!strcmp(buf, "none"))
cntry->reserve_promotion_rules = RESERVE_PROM_RULES_NONE;
else if (!strcmp(buf, "default"))
cntry->reserve_promotion_rules = RESERVE_PROM_RULES_DEFAULT;
else {
g_warning("Unknown value for <reserve_promotion_rules>: %s\n", buf);
cntry->reserve_promotion_rules = RESERVE_PROM_RULES_NONE;
}
}
2004-12-23 13:58:39 +01:00
}
/**
Function reading an XML file specifying a country.
@param country_name name of the xml file (e.g. 'country_england.xml')
to be read. Full path is not necessary, if the file is located in
one of the suppport directories; neither are the prefix 'country_'
or the suffix '.xml'.
@param cntry The country variable to write.
2004-12-23 13:58:39 +01:00
*/
void
xml_country_read(const gchar *country_name, Country *cntry_arg)
2004-12-23 13:58:39 +01:00
{
#ifdef DEBUG
printf("xml_country_read\n");
#endif
2005-04-09 21:18:28 +02:00
gchar *file_name = file_find_support_file(country_name, FALSE);
2004-12-23 13:58:39 +01:00
GMarkupParser parser = {xml_country_read_start_element,
xml_country_read_end_element,
xml_country_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];
2005-05-24 20:31:07 +02: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, "country_%s.xml", country_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_country_read: error reading file %s\n", file_name);
2005-06-17 14:57:05 +02:00
misc_print_error(&error, TRUE);
2004-12-23 13:58:39 +01:00
return;
}
cntry = (cntry_arg == NULL) ? &country : cntry_arg;
2004-12-23 13:58:39 +01:00
state = STATE_COUNTRY;
strcpy(buf, file_name);
g_free(file_name);
free_country(cntry, TRUE);
2005-06-23 23:53:57 +02:00
if(cntry_arg == NULL)
{
sett_set_int("int_opt_disable_finances", 0);
sett_set_int("int_opt_disable_transfers", 0);
sett_set_int("int_opt_disable_stadium", 0);
sett_set_int("int_opt_disable_contracts", 0);
sett_set_int("int_opt_disable_boost_on", 0);
sett_set_int("int_opt_disable_ya", 0);
2007-01-18 18:42:48 +01:00
sett_set_int("int_opt_disable_training_camp", 0); //***ML***
}
2004-12-23 13:58:39 +01:00
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_country_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-05-24 20:31:07 +02:00
for(i=0;i<cntry->leagues->len;i++)
if(g_array_index(cntry->leagues, League, i).layer == -1)
g_array_index(cntry->leagues, League, i).layer = i + 1;
2004-12-23 13:58:39 +01:00
}