bygfoot/src/xml_loadsave_teams.c

285 lines
8.4 KiB
C
Raw Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
xml_loadsave_teams.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-06 00:14:39 +02:00
#include "file.h"
#include "misc.h"
#include "player.h"
#include "team.h"
#include "xml.h"
2005-07-08 19:34:10 +02:00
#include "xml_loadsave_players.h"
2005-04-06 00:14:39 +02:00
#include "xml_loadsave_teams.h"
enum
{
TAG_TEAMS = TAG_START_TEAMS,
TAG_TEAM,
2005-06-15 10:22:40 +02:00
TAG_TEAM_NAMES_FILE,
2005-04-06 00:14:39 +02:00
TAG_TEAM_STRUCTURE,
TAG_TEAM_STYLE,
TAG_TEAM_BOOST,
2005-04-13 15:01:59 +02:00
TAG_TEAM_CLID,
2005-10-24 22:50:48 +02:00
TAG_TEAM_STRATEGY_SID,
2005-04-06 00:14:39 +02:00
TAG_TEAM_STADIUM,
2005-06-21 15:33:16 +02:00
TAG_TEAM_STADIUM_NAME,
2005-04-06 00:14:39 +02:00
TAG_TEAM_STADIUM_CAPACITY,
TAG_TEAM_STADIUM_AVERAGE_ATTENDANCE,
TAG_TEAM_STADIUM_POSSIBLE_ATTENDANCE,
TAG_TEAM_STADIUM_GAMES,
TAG_TEAM_STADIUM_SAFETY,
TAG_END
};
2005-05-19 21:43:22 +02:00
gint state, etalidx, careeridx;
2005-04-06 00:14:39 +02:00
GArray *teams_array;
Team new_team;
Player new_player;
PlayerGamesGoals new_games_goals;
PlayerCard new_card;
void
xml_loadsave_teams_start_element (GMarkupParseContext *context,
2005-07-08 19:34:10 +02:00
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
2005-04-06 00:14:39 +02:00
{
gint i;
gint tag = xml_get_tag_from_name(element_name);
gboolean valid_tag = FALSE;
for(i=TAG_TEAMS;i<TAG_END;i++)
if(tag == i)
{
state = i;
valid_tag = TRUE;
}
for(i=TAG_NAME;i<=TAG_ROUND;i++)
if(tag == i)
{
state = i;
valid_tag = TRUE;
}
if(tag == TAG_TEAM)
2005-04-14 21:07:25 +02:00
new_team = team_new(FALSE);
2005-07-08 19:34:10 +02:00
else if(tag >= TAG_START_PLAYERS && tag <= TAG_END_PLAYERS)
2005-04-06 00:14:39 +02:00
{
2005-07-08 19:34:10 +02:00
valid_tag = TRUE;
state = TAG_START_PLAYERS;
xml_loadsave_players_start_element(tag, &new_team);
2005-04-06 00:14:39 +02:00
}
if(!valid_tag)
g_warning("xml_loadsave_teams_start_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
}
void
xml_loadsave_teams_end_element (GMarkupParseContext *context,
2005-07-08 19:34:10 +02:00
const gchar *element_name,
gpointer user_data,
GError **error)
2005-04-06 00:14:39 +02:00
{
gint tag = xml_get_tag_from_name(element_name);
if(tag == TAG_TEAM)
{
state = TAG_TEAMS;
g_array_append_val(teams_array, new_team);
}
else if(tag == TAG_NAME ||
tag == TAG_SYMBOL ||
2005-06-15 10:22:40 +02:00
tag == TAG_TEAM_NAMES_FILE ||
2005-04-06 00:14:39 +02:00
tag == TAG_TEAM_ID ||
tag == TAG_TEAM_CLID ||
2005-10-24 22:50:48 +02:00
tag == TAG_TEAM_STRATEGY_SID ||
2005-04-06 00:14:39 +02:00
tag == TAG_TEAM_STRUCTURE ||
tag == TAG_TEAM_STYLE ||
tag == TAG_TEAM_BOOST ||
2005-07-08 19:34:10 +02:00
tag == TAG_TEAM_STADIUM)
2005-04-06 00:14:39 +02:00
state = TAG_TEAM;
2005-06-21 15:33:16 +02:00
else if(tag == TAG_TEAM_STADIUM_NAME ||
tag == TAG_TEAM_STADIUM_CAPACITY ||
2005-04-06 00:14:39 +02:00
tag == TAG_TEAM_STADIUM_AVERAGE_ATTENDANCE ||
tag == TAG_TEAM_STADIUM_POSSIBLE_ATTENDANCE ||
tag == TAG_TEAM_STADIUM_GAMES ||
tag == TAG_TEAM_STADIUM_SAFETY)
state = TAG_TEAM_STADIUM;
2005-07-08 19:34:10 +02:00
else if(tag >= TAG_START_PLAYERS && tag <= TAG_END_PLAYERS)
2005-04-06 00:14:39 +02:00
{
2005-07-08 19:34:10 +02:00
xml_loadsave_players_end_element(tag, new_team.players);
if(tag == TAG_START_PLAYERS)
state = TAG_TEAM;
2005-04-06 00:14:39 +02:00
}
else if(tag != TAG_TEAMS)
g_warning("xml_loadsave_teams_end_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
}
void
xml_loadsave_teams_text (GMarkupParseContext *context,
2005-07-08 19:34:10 +02:00
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
2005-04-06 00:14:39 +02:00
{
gchar buf[SMALL];
gint int_value = -1;
gfloat float_value = -1;
strncpy(buf, text, text_len);
buf[text_len] = '\0';
int_value = (gint)g_ascii_strtod(buf, NULL);
float_value = (gfloat)g_ascii_strtod(buf, NULL) / 10000;
if(state == TAG_NAME)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_team.name, buf);
2005-04-06 00:14:39 +02:00
else if(state == TAG_SYMBOL)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_team.symbol, buf);
2005-06-15 10:22:40 +02:00
else if(state == TAG_TEAM_NAMES_FILE)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_team.names_file, buf);
2005-04-06 00:14:39 +02:00
else if(state == TAG_TEAM_CLID)
new_team.clid = int_value;
2005-10-24 22:50:48 +02:00
else if(state == TAG_TEAM_STRATEGY_SID)
misc_string_assign(&new_team.strategy_sid, buf);
2005-04-06 00:14:39 +02:00
else if(state == TAG_TEAM_ID)
new_team.id = int_value;
else if(state == TAG_TEAM_STRUCTURE)
new_team.structure = int_value;
else if(state == TAG_TEAM_STYLE)
new_team.style = int_value;
else if(state == TAG_TEAM_BOOST)
new_team.boost = int_value;
2005-06-21 15:33:16 +02:00
else if(state == TAG_TEAM_STADIUM_NAME)
2005-10-09 11:35:44 +02:00
misc_string_assign(&new_team.stadium.name, buf);
2005-04-06 00:14:39 +02:00
else if(state == TAG_TEAM_STADIUM_CAPACITY)
new_team.stadium.capacity = int_value;
else if(state == TAG_TEAM_STADIUM_AVERAGE_ATTENDANCE)
new_team.stadium.average_attendance = int_value;
else if(state == TAG_TEAM_STADIUM_POSSIBLE_ATTENDANCE)
new_team.stadium.possible_attendance = int_value;
else if(state == TAG_TEAM_STADIUM_GAMES)
new_team.stadium.games = int_value;
else if(state == TAG_TEAM_STADIUM_SAFETY)
new_team.stadium.safety = float_value;
2005-07-08 19:34:10 +02:00
else if(state >= TAG_START_PLAYERS && state <= TAG_END_PLAYERS)
xml_loadsave_players_text(buf);
2005-04-06 00:14:39 +02:00
}
void
xml_loadsave_teams_read(const gchar *filename, GArray *teams)
{
gint i, j;
GMarkupParser parser = {xml_loadsave_teams_start_element,
xml_loadsave_teams_end_element,
xml_loadsave_teams_text, NULL, NULL};
GMarkupParseContext *context;
gchar *file_contents;
guint length;
GError *error = NULL;
context =
g_markup_parse_context_new(&parser, 0, NULL, NULL);
if(!g_file_get_contents(filename, &file_contents, &length, &error))
{
g_warning("xml_loadsave_teams_read: error reading file %s\n", filename);
misc_print_error(&error, TRUE);
}
teams_array = teams;
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_warning("xml_loadsave_teams_read: error parsing file %s\n", filename);
misc_print_error(&error, TRUE);
}
for(i=0;i<teams->len;i++)
for(j=0;j<g_array_index(teams, Team, i).players->len;j++)
g_array_index(g_array_index(teams, Team, i).players, Player, j).team =
&g_array_index(teams, Team, i);
}
void
xml_loadsave_teams_write(const gchar *filename, const GArray *teams)
{
gint i;
FILE *fil = NULL;
file_my_fopen(filename, "w", &fil, TRUE);
fprintf(fil, "<_%d>\n", TAG_TEAMS);
for(i=0;i<teams->len;i++)
xml_loadsave_teams_write_team(fil, &g_array_index(teams, Team, i));
fprintf(fil, "</_%d>\n", TAG_TEAMS);
fclose(fil);
}
void
xml_loadsave_teams_write_team(FILE *fil, const Team* team)
{
fprintf(fil, "<_%d>\n", TAG_TEAM);
2005-10-09 11:35:44 +02:00
xml_write_string(fil, team->name, TAG_NAME, I1);
xml_write_string(fil, team->symbol, TAG_SYMBOL, I1);
xml_write_string(fil, team->names_file, TAG_TEAM_NAMES_FILE, I1);
2005-10-24 22:50:48 +02:00
xml_write_string(fil, team->strategy_sid, TAG_TEAM_STRATEGY_SID, I1);
2005-04-06 00:14:39 +02:00
xml_write_int(fil, team->clid, TAG_TEAM_CLID, I1);
xml_write_int(fil, team->id, TAG_TEAM_ID, I1);
xml_write_int(fil, team->structure, TAG_TEAM_STRUCTURE, I1);
xml_write_int(fil, team->style, TAG_TEAM_STYLE, I1);
xml_write_int(fil, team->boost, TAG_TEAM_BOOST, I1);
2005-10-24 22:50:48 +02:00
2005-04-06 00:14:39 +02:00
fprintf(fil, "%s<_%d>\n", I1, TAG_TEAM_STADIUM);
2005-06-21 15:33:16 +02:00
if(team->stadium.name != NULL)
2005-10-09 11:35:44 +02:00
xml_write_string(fil, team->stadium.name, TAG_TEAM_STADIUM_NAME, I2);
2005-06-21 15:33:16 +02:00
2005-04-06 00:14:39 +02:00
xml_write_int(fil, team->stadium.capacity, TAG_TEAM_STADIUM_CAPACITY, I2);
xml_write_int(fil, team->stadium.average_attendance, TAG_TEAM_STADIUM_AVERAGE_ATTENDANCE, I2);
xml_write_int(fil, team->stadium.possible_attendance, TAG_TEAM_STADIUM_POSSIBLE_ATTENDANCE, I2);
xml_write_int(fil, team->stadium.games, TAG_TEAM_STADIUM_GAMES, I2);
xml_write_float(fil, team->stadium.safety, TAG_TEAM_STADIUM_SAFETY, I2);
fprintf(fil, "%s</_%d>\n", I1, TAG_TEAM_STADIUM);
2005-07-08 19:34:10 +02:00
xml_loadsave_players_write(fil, team->players);
2005-04-06 00:14:39 +02:00
fprintf(fil, "</_%d>\n", TAG_TEAM);
}