bygfoot/src/xml_loadsave_leagues_cups.c

245 lines
6.6 KiB
C
Raw Normal View History

2005-10-21 15:45:58 +02:00
/*
2005-11-26 17:52:51 +01:00
xml_loadsave_leagues_cups.c
2005-10-21 15:45:58 +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"
2009-01-11 16:24:26 +01:00
#include "fixture.h"
2005-10-21 15:45:58 +02:00
#include "free.h"
#include "league_struct.h"
#include "misc.h"
2009-01-11 16:24:26 +01:00
#include "table.h"
2009-01-10 11:41:27 +01:00
#include "team.h"
2005-10-21 15:45:58 +02:00
#include "xml.h"
#include "xml_loadsave_cup.h"
#include "xml_loadsave_league.h"
#include "xml_loadsave_leagues_cups.h"
enum
{
TAG_LEAGUES_CUPS = TAG_START_LEAGUES_CUPS,
TAG_LEAGUE_FILE,
TAG_CUP_FILE,
TAG_END
};
gint state;
const gchar *dir;
void
xml_loadsave_leagues_cups_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_loadsave_leagues_cups_start_element\n");
#endif
2005-10-21 15:45:58 +02:00
gint i;
gint tag = xml_get_tag_from_name(element_name);
gboolean valid_tag = FALSE;
for(i=TAG_LEAGUES_CUPS;i<TAG_END;i++)
if(tag == i)
{
state = i;
valid_tag = TRUE;
}
if(!valid_tag)
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_leagues_cups_start_element: unknown tag: %s; I'm in state %d\n",
2005-10-21 15:45:58 +02:00
element_name, state);
}
void
xml_loadsave_leagues_cups_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
#ifdef DEBUG
printf("xml_loadsave_leagues_cups_end_element\n");
#endif
2005-10-21 15:45:58 +02:00
gint tag = xml_get_tag_from_name(element_name);
if(tag == TAG_LEAGUE_FILE ||
tag == TAG_CUP_FILE)
state = TAG_LEAGUES_CUPS;
else if(tag != TAG_LEAGUES_CUPS)
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_leagues_cups_end_element: unknown tag: %s; I'm in state %d\n",
2005-10-21 15:45:58 +02:00
element_name, state);
}
void
xml_loadsave_leagues_cups_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
Bygfoot *bygfoot = (Bygfoot*)user_data;
#ifdef DEBUG
printf("xml_loadsave_leagues_cups_text\n");
#endif
2005-10-21 15:45:58 +02:00
gchar buf[SMALL];
Cup new_cup;
strncpy(buf, text, text_len);
buf[text_len] = '\0';
if(state == TAG_LEAGUE_FILE)
xml_load_league(bygfoot, ligs, dir, buf);
2005-10-21 15:45:58 +02:00
else if(state == TAG_CUP_FILE)
{
new_cup = cup_new(FALSE);
g_array_append_val(cps, new_cup);
xml_load_cup(bygfoot, &g_array_index(cps, Cup, cps->len - 1), dir, buf);
2005-10-21 15:45:58 +02:00
}
}
/** Load the leagues and cups given in the leagues_cups.xml file. */
void
xml_loadsave_leagues_cups_read(Bygfoot *bygfoot, const gchar *dirname, const gchar *prefix)
2005-10-21 15:45:58 +02:00
{
#ifdef DEBUG
printf("xml_loadsave_leagues_cups_read\n");
#endif
2005-10-21 15:45:58 +02:00
gchar file[SMALL];
GMarkupParser parser = {xml_loadsave_leagues_cups_start_element,
xml_loadsave_leagues_cups_end_element,
xml_loadsave_leagues_cups_text, NULL, NULL};
GMarkupParseContext *context;
gchar *file_contents;
2006-06-04 19:10:08 +02:00
gsize length;
2005-10-21 15:45:58 +02:00
GError *error = NULL;
2005-10-24 22:50:48 +02:00
sprintf(file, "%s%s%s___leagues_cups.xml", dirname, G_DIR_SEPARATOR_S, prefix);
2005-10-21 15:45:58 +02:00
context =
g_markup_parse_context_new(&parser, 0, bygfoot, NULL);
2005-10-21 15:45:58 +02:00
if(!g_file_get_contents(file, &file_contents, &length, &error))
{
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_misc_read: error reading file %s\n", file);
2005-10-21 15:45:58 +02:00
misc_print_error(&error, TRUE);
}
free_leagues_array(&ligs, TRUE);
free_cups_array(&cps, TRUE);
dir = dirname;
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
{
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_misc_read: error parsing file %s\n", file);
2005-10-21 15:45:58 +02:00
misc_print_error(&error, TRUE);
}
2009-01-10 11:41:27 +01:00
xml_loadsave_leagues_cups_adjust_team_ptrs();
2005-10-21 15:45:58 +02:00
}
/** Write the leagues into xml files with the given prefix. */
void
xml_loadsave_leagues_cups_write(const gchar *prefix)
{
#ifdef DEBUG
printf("xml_loadsave_leagues_cups_write\n");
#endif
2005-10-21 15:45:58 +02:00
gint i;
gchar buf[SMALL],
*basename = g_path_get_basename(prefix);
FILE *fil = NULL;
sprintf(buf, "%s___leagues_cups.xml", prefix);
file_my_fopen(buf, "w", &fil, TRUE);
fprintf(fil, "%s<_%d>\n", I0, TAG_LEAGUES_CUPS);
for(i=0;i<ligs->len;i++)
{
xml_loadsave_league_write(prefix, &lig(i));
sprintf(buf, "%s___league_%d.xml", basename, lig(i).id);
xml_write_string(fil, buf, TAG_LEAGUE_FILE, I1);
}
for(i=0;i<cps->len;i++)
{
xml_loadsave_cup_write(prefix, &cp(i));
sprintf(buf, "%s___cup_%d.xml", basename, cp(i).id);
xml_write_string(fil, buf, TAG_CUP_FILE, I1);
}
fprintf(fil, "%s</_%d>\n", I0, TAG_LEAGUES_CUPS);
fclose(fil);
g_free(basename);
}
2009-01-10 11:41:27 +01:00
void
xml_loadsave_leagues_cups_adjust_team_ptrs(void)
{
2009-01-11 16:24:26 +01:00
gint i, j, k;
2009-01-10 11:41:27 +01:00
GPtrArray *team_ptrs;
for(i = 0; i < ligs->len; i++)
{
2009-01-11 15:51:12 +01:00
fixture_refresh_team_pointers(lig(i).fixtures);
2009-01-11 11:23:55 +01:00
for(j = 0; j < lig(i).tables->len; j++)
2009-01-11 15:51:12 +01:00
table_refresh_team_pointers(&g_array_index(lig(i).tables, Table, j));
2009-01-10 11:41:27 +01:00
}
for(i = 0; i < cps->len; i++)
{
2009-01-11 15:51:12 +01:00
fixture_refresh_team_pointers(cp(i).fixtures);
2009-01-10 11:41:27 +01:00
for(j = 0; j < cp(i).rounds->len; j++)
{
team_ptrs = g_ptr_array_new();
for(k = 0; k < g_array_index(cp(i).rounds, CupRound, j).team_ptrs->len; k++)
g_ptr_array_add(team_ptrs, team_of_id(GPOINTER_TO_INT(g_ptr_array_index(g_array_index(cp(i).rounds, CupRound, j).team_ptrs, k))));
g_ptr_array_free(g_array_index(cp(i).rounds, CupRound, j).team_ptrs, TRUE);
g_array_index(cp(i).rounds, CupRound, j).team_ptrs = team_ptrs;
2009-01-11 11:23:55 +01:00
for(k = 0; k < g_array_index(cp(i).rounds, CupRound, j).tables->len; k++)
2009-01-11 15:51:12 +01:00
table_refresh_team_pointers(&g_array_index(g_array_index(cp(i).rounds, CupRound, j).tables, Table, k));
2009-01-10 11:41:27 +01:00
}
}
}