bygfoot/src/xml_loadsave_league_stat.c

289 lines
8.0 KiB
C
Raw Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
xml_loadsave_league_stat.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.
*/
2005-04-30 15:57:58 +02:00
#include "file.h"
#include "misc.h"
2005-06-05 12:39:29 +02:00
#include "stat.h"
2005-04-30 15:57:58 +02:00
#include "team.h"
#include "xml.h"
#include "xml_loadsave_league_stat.h"
enum
{
TAG_LEAGUE_STAT = TAG_START_LEAGUE_STAT,
TAG_STAT_LEAGUE_NAME,
TAG_STAT_LEAGUE_SYMBOL,
2005-04-30 15:57:58 +02:00
TAG_STAT_TEAMS_OFF,
TAG_STAT_TEAMS_DEF,
TAG_STAT_PLAYER_SCORERS,
TAG_STAT_PLAYER_GOALIES,
TAG_STAT,
TAG_STAT_TEAM_NAME,
2005-04-30 15:57:58 +02:00
TAG_STAT_VALUE,
TAG_STAT_VALUE_STRING,
TAG_END
};
gint state, in_state, valueidx;
Stat new_stat;
2005-10-09 11:35:44 +02:00
LeagueStat *lig_stat;
2005-04-30 15:57:58 +02:00
void
xml_loadsave_league_stat_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_league_stat_start_element\n");
#endif
2005-04-30 15:57:58 +02:00
gint i;
gint tag = xml_get_tag_from_name(element_name);
gboolean valid_tag = FALSE;
for(i=TAG_LEAGUE_STAT;i<TAG_END;i++)
if(tag == i)
{
state = i;
valid_tag = TRUE;
}
if(tag == TAG_STAT)
2005-10-15 18:49:04 +02:00
{
2005-04-30 15:57:58 +02:00
valueidx = 0;
new_stat.value_string =
new_stat.team_name = NULL;
2005-10-15 18:49:04 +02:00
}
2005-04-30 15:57:58 +02:00
else if(tag == TAG_STAT_TEAMS_OFF ||
tag == TAG_STAT_TEAMS_DEF ||
tag == TAG_STAT_PLAYER_SCORERS ||
tag == TAG_STAT_PLAYER_GOALIES)
in_state = tag;
if(!valid_tag)
2009-04-29 19:18:54 +02:00
debug_print_message(
"xml_loadsave_league_stat_start_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
2005-04-30 15:57:58 +02:00
}
void
xml_loadsave_league_stat_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
#ifdef DEBUG
printf("xml_loadsave_league_stat_end_element\n");
#endif
2005-04-30 15:57:58 +02:00
GArray *stat_array = NULL;
gint tag = xml_get_tag_from_name(element_name);
if(tag == TAG_STAT_LEAGUE_NAME ||
tag == TAG_STAT_LEAGUE_SYMBOL ||
2005-04-30 15:57:58 +02:00
tag == TAG_STAT_TEAMS_OFF ||
tag == TAG_STAT_TEAMS_DEF ||
tag == TAG_STAT_PLAYER_SCORERS ||
tag == TAG_STAT_PLAYER_GOALIES)
state = TAG_LEAGUE_STAT;
else if(tag == TAG_STAT)
{
state = in_state;
if(in_state == TAG_STAT_TEAMS_OFF)
2005-10-09 11:35:44 +02:00
stat_array = lig_stat->teams_off;
2005-04-30 15:57:58 +02:00
else if(in_state == TAG_STAT_TEAMS_DEF)
2005-10-09 11:35:44 +02:00
stat_array = lig_stat->teams_def;
2005-04-30 15:57:58 +02:00
else if(in_state == TAG_STAT_PLAYER_SCORERS)
2005-10-09 11:35:44 +02:00
stat_array = lig_stat->player_scorers;
2005-04-30 15:57:58 +02:00
else if(in_state == TAG_STAT_PLAYER_GOALIES)
2005-10-09 11:35:44 +02:00
stat_array = lig_stat->player_goalies;
2005-04-30 15:57:58 +02:00
else
2005-06-17 14:57:05 +02:00
{
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_league_stat_end_element: unknown in_state %d \n",
2005-04-30 15:57:58 +02:00
in_state);
2005-06-17 14:57:05 +02:00
return;
}
2005-04-30 15:57:58 +02:00
g_array_append_val(stat_array, new_stat);
}
else if(tag == TAG_STAT_VALUE_STRING ||
tag == TAG_STAT_VALUE ||
tag == TAG_STAT_VALUE_STRING ||
tag == TAG_STAT_TEAM_NAME)
2005-04-30 15:57:58 +02:00
{
state = TAG_STAT;
if(tag == TAG_STAT_VALUE)
valueidx++;
}
else if(tag != TAG_LEAGUE_STAT)
2009-04-29 19:18:54 +02:00
debug_print_message(
"xml_loadsave_league_stat_end_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
2005-04-30 15:57:58 +02:00
}
void
xml_loadsave_league_stat_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
#ifdef DEBUG
printf("xml_loadsave_league_stat_text\n");
#endif
2005-04-30 15:57:58 +02:00
gchar buf[SMALL];
gint int_value = -1;
strncpy(buf, text, text_len);
buf[text_len] = '\0';
int_value = (gint)g_ascii_strtod(buf, NULL);
if(state == TAG_STAT_LEAGUE_NAME)
lig_stat->league_name = g_strdup(buf);
else if(state == TAG_STAT_LEAGUE_SYMBOL)
lig_stat->league_symbol = g_strdup(buf);
else if(state == TAG_STAT_TEAM_NAME)
new_stat.team_name = g_strdup(buf);
2005-04-30 15:57:58 +02:00
else if(state == TAG_STAT_VALUE)
{
if(valueidx == 0)
new_stat.value1 = int_value;
else if(valueidx == 1)
new_stat.value2 = int_value;
else
new_stat.value3 = int_value;
}
else if(state == TAG_STAT_VALUE_STRING)
2005-10-09 11:35:44 +02:00
new_stat.value_string = g_strdup(buf);
2005-04-30 15:57:58 +02:00
}
void
xml_loadsave_league_stat_read(const gchar *filename, LeagueStat *league_stat)
{
#ifdef DEBUG
printf("xml_loadsave_league_stat_read\n");
#endif
2005-04-30 15:57:58 +02:00
GMarkupParser parser = {xml_loadsave_league_stat_start_element,
xml_loadsave_league_stat_end_element,
xml_loadsave_league_stat_text, NULL, NULL};
GMarkupParseContext *context;
gchar *file_contents;
2006-06-04 19:10:08 +02:00
gsize length;
2005-04-30 15:57:58 +02:00
GError *error = NULL;
context =
g_markup_parse_context_new(&parser, 0, NULL, NULL);
if(!g_file_get_contents(filename, &file_contents, &length, &error))
{
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_league_stat_read: error reading file %s\n", filename);
2005-04-30 15:57:58 +02:00
misc_print_error(&error, TRUE);
}
2005-10-09 11:35:44 +02:00
lig_stat = league_stat;
2005-04-30 15:57:58 +02: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
{
2009-04-29 19:18:54 +02:00
debug_print_message("xml_loadsave_league_stat_read: error parsing file %s\n", filename);
2005-04-30 15:57:58 +02:00
misc_print_error(&error, TRUE);
}
}
void
xml_loadsave_league_stat_write(const gchar *filename, const LeagueStat *league_stat)
{
#ifdef DEBUG
printf("xml_loadsave_league_stat_write\n");
#endif
2005-04-30 15:57:58 +02:00
gint i;
FILE *fil = NULL;
file_my_fopen(filename, "w", &fil, TRUE);
fprintf(fil, "<_%d>\n", TAG_LEAGUE_STAT);
xml_write_string(fil, league_stat->league_name, TAG_STAT_LEAGUE_NAME, I0);
xml_write_string(fil, league_stat->league_symbol, TAG_STAT_LEAGUE_SYMBOL, I0);
2005-04-30 15:57:58 +02:00
fprintf(fil, "<_%d>\n", TAG_STAT_TEAMS_OFF);
for(i=0;i<league_stat->teams_off->len;i++)
xml_loadsave_league_stat_write_stat(
fil, &g_array_index(league_stat->teams_off, Stat, i));
fprintf(fil, "</_%d>\n", TAG_STAT_TEAMS_OFF);
fprintf(fil, "<_%d>\n", TAG_STAT_TEAMS_DEF);
for(i=0;i<league_stat->teams_def->len;i++)
xml_loadsave_league_stat_write_stat(
fil, &g_array_index(league_stat->teams_def, Stat, i));
fprintf(fil, "</_%d>\n", TAG_STAT_TEAMS_DEF);
fprintf(fil, "<_%d>\n", TAG_STAT_PLAYER_SCORERS);
for(i=0;i<league_stat->player_scorers->len;i++)
xml_loadsave_league_stat_write_stat(
fil, &g_array_index(league_stat->player_scorers, Stat, i));
fprintf(fil, "</_%d>\n", TAG_STAT_PLAYER_SCORERS);
fprintf(fil, "<_%d>\n", TAG_STAT_PLAYER_GOALIES);
for(i=0;i<league_stat->player_goalies->len;i++)
xml_loadsave_league_stat_write_stat(
fil, &g_array_index(league_stat->player_goalies, Stat, i));
fprintf(fil, "</_%d>\n", TAG_STAT_PLAYER_GOALIES);
fprintf(fil, "</_%d>\n", TAG_LEAGUE_STAT);
fclose(fil);
}
void
xml_loadsave_league_stat_write_stat(FILE *fil, const Stat *stat)
{
#ifdef DEBUG
printf("xml_loadsave_league_stat_write_stat\n");
#endif
2005-04-30 15:57:58 +02:00
fprintf(fil, "%s<_%d>\n", I1, TAG_STAT);
xml_write_string(fil, stat->team_name, TAG_STAT_TEAM_NAME, I1);
2005-04-30 15:57:58 +02:00
xml_write_int(fil, stat->value1, TAG_STAT_VALUE, I1);
xml_write_int(fil, stat->value2, TAG_STAT_VALUE, I1);
xml_write_int(fil, stat->value3, TAG_STAT_VALUE, I1);
2005-10-09 11:35:44 +02:00
xml_write_string(fil, stat->value_string, TAG_STAT_VALUE_STRING, I1);
2005-04-30 15:57:58 +02:00
fprintf(fil, "%s</_%d>\n", I1, TAG_STAT);
}