bygfoot/src/xml_loadsave_misc.c

281 lines
7.5 KiB
C
Raw Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
xml_loadsave_misc.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-10-27 23:10:15 +02:00
#include "bet_struct.h"
2005-04-14 21:07:25 +02:00
#include "cup.h"
2005-04-06 00:14:39 +02:00
#include "file.h"
2005-10-27 23:10:15 +02:00
#include "fixture.h"
#include "free.h"
2005-04-06 00:14:39 +02:00
#include "misc.h"
#include "xml.h"
#include "variables.h"
#include "xml_loadsave_misc.h"
#include "xml_loadsave_cup.h"
#include "xml_loadsave_league.h"
enum XmlLoadSaveCountryTags
{
TAG_MISC = TAG_START_MISC,
TAG_MISC_RATING,
2005-04-06 00:14:39 +02:00
TAG_MISC_SEASON,
TAG_MISC_WEEK,
TAG_MISC_WEEK_ROUND,
2005-04-07 18:44:33 +02:00
TAG_MISC_COUNTER,
2005-04-14 21:07:25 +02:00
TAG_MISC_ALLCUP,
2005-10-27 23:10:15 +02:00
TAG_MISC_BET0,
TAG_MISC_BET1,
TAG_MISC_BET_FIX_ID,
TAG_MISC_BET_ODD,
2006-04-09 17:13:42 +02:00
TAG_MISC_VERSION,
2008-11-06 09:03:09 +01:00
TAG_MISC_CURRENT_INTEREST,
2005-04-06 00:14:39 +02:00
TAG_END
};
2005-10-27 23:10:15 +02:00
gint state, countidx, oddidx, betidx;
BetMatch new_bet;
2005-04-06 00:14:39 +02:00
void
xml_loadsave_misc_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
gint i;
gint tag = xml_get_tag_from_name(element_name);
gboolean valid_tag = FALSE;
for(i=TAG_MISC;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;
}
2005-04-07 18:44:33 +02:00
if(tag == TAG_MISC)
countidx = 0;
2005-10-27 23:10:15 +02:00
if(tag == TAG_MISC_BET0 ||
tag == TAG_MISC_BET1)
{
oddidx = 0;
betidx = (tag == TAG_MISC_BET1);
}
2005-04-06 00:14:39 +02:00
if(!valid_tag)
g_warning("xml_loadsave_misc_start_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
}
void
xml_loadsave_misc_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
gint tag = xml_get_tag_from_name(element_name);
if(tag == TAG_NAME ||
tag == TAG_SYMBOL ||
tag == TAG_SID ||
2006-04-08 21:25:48 +02:00
tag == TAG_MISC_VERSION ||
2008-11-06 09:03:09 +01:00
tag == TAG_MISC_CURRENT_INTEREST ||
tag == TAG_MISC_RATING ||
2005-04-14 21:07:25 +02:00
tag == TAG_MISC_ALLCUP ||
2005-04-07 18:44:33 +02:00
tag == TAG_MISC_COUNTER ||
2005-04-06 00:14:39 +02:00
tag == TAG_MISC_SEASON ||
tag == TAG_MISC_WEEK ||
2005-10-27 23:10:15 +02:00
tag == TAG_MISC_WEEK_ROUND ||
tag == TAG_MISC_BET0 ||
tag == TAG_MISC_BET1)
2005-04-07 18:44:33 +02:00
{
2005-04-06 00:14:39 +02:00
state = TAG_MISC;
2005-04-07 18:44:33 +02:00
if(tag == TAG_MISC_COUNTER)
countidx++;
2005-10-27 23:10:15 +02:00
else if(tag == TAG_MISC_BET0 ||
tag == TAG_MISC_BET1)
g_array_append_val(bets[betidx], new_bet);
}
else if(tag == TAG_MISC_BET_ODD ||
tag == TAG_MISC_BET_FIX_ID)
{
state = (betidx == 0) ? TAG_MISC_BET0 : TAG_MISC_BET1;
if(tag == TAG_MISC_BET_ODD)
oddidx++;
2005-04-07 18:44:33 +02:00
}
2005-04-06 00:14:39 +02:00
else if(tag != TAG_MISC)
g_warning("xml_loadsave_misc_end_element: unknown tag: %s; I'm in state %d\n",
element_name, state);
}
void
xml_loadsave_misc_text (GMarkupParseContext *context,
2005-10-27 23:10:15 +02:00
const gchar *text,
2006-04-09 17:13:42 +02:00
gsize text_len,
2005-10-27 23:10:15 +02:00
gpointer user_data,
GError **error)
2005-04-06 00:14:39 +02:00
{
gchar buf[SMALL];
gint int_value = -1;
2005-10-27 23:10:15 +02:00
gfloat float_value = -1;
2005-04-06 00:14:39 +02:00
strncpy(buf, text, text_len);
buf[text_len] = '\0';
int_value = (gint)g_ascii_strtod(buf, NULL);
2005-10-27 23:10:15 +02:00
float_value = (gfloat)g_ascii_strtod(buf, NULL) / 10000;
2005-04-06 00:14:39 +02:00
if(state == TAG_NAME)
2005-10-09 11:35:44 +02:00
misc_string_assign(&country.name, buf);
else if(state == TAG_MISC_RATING)
country.rating = int_value;
2005-04-06 00:14:39 +02:00
else if(state == TAG_SYMBOL)
2005-10-09 11:35:44 +02:00
misc_string_assign(&country.symbol, buf);
2005-04-06 00:14:39 +02:00
else if(state == TAG_SID)
2005-10-09 11:35:44 +02:00
misc_string_assign(&country.sid, buf);
2005-04-06 00:14:39 +02:00
else if(state == TAG_MISC_SEASON)
season = int_value;
else if(state == TAG_MISC_WEEK)
week = int_value;
else if(state == TAG_MISC_WEEK_ROUND)
week_round = int_value;
2005-04-07 18:44:33 +02:00
else if(state == TAG_MISC_COUNTER)
counters[countidx] = int_value;
2005-04-14 21:07:25 +02:00
else if(state == TAG_MISC_ALLCUP)
g_ptr_array_add(acps, cup_from_clid(int_value));
2005-10-27 23:10:15 +02:00
else if(state == TAG_MISC_BET_ODD)
new_bet.odds[oddidx] = float_value;
else if(state == TAG_MISC_BET_FIX_ID)
2005-10-31 17:19:14 +01:00
new_bet.fix_id = int_value;
2008-11-06 09:03:09 +01:00
else if(state == TAG_MISC_CURRENT_INTEREST)
current_interest = float_value;
2005-04-06 00:14:39 +02:00
}
/** Read a country xml file. */
void
xml_loadsave_misc_read(const gchar *dirname, const gchar *basename)
{
gchar file[SMALL];
GMarkupParser parser = {xml_loadsave_misc_start_element,
xml_loadsave_misc_end_element,
xml_loadsave_misc_text, NULL, NULL};
GMarkupParseContext *context;
gchar *file_contents;
2006-06-04 19:10:08 +02:00
gsize length;
2005-04-06 00:14:39 +02:00
GError *error = NULL;
2005-10-24 22:50:48 +02:00
sprintf(file, "%s%s%s___misc.xml", dirname, G_DIR_SEPARATOR_S, basename);
2005-04-06 00:14:39 +02:00
2006-04-09 17:13:42 +02:00
context =
2005-04-06 00:14:39 +02:00
g_markup_parse_context_new(&parser, 0, NULL, NULL);
if(!g_file_get_contents(file, &file_contents, &length, &error))
{
g_warning("xml_loadsave_misc_read: error reading file %s\n", file);
misc_print_error(&error, TRUE);
}
2005-04-14 21:07:25 +02:00
g_ptr_array_free(acps, TRUE);
acps = g_ptr_array_new();
2005-10-27 23:10:15 +02:00
free_bets(TRUE);
2005-04-06 00:14:39 +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
{
g_warning("xml_loadsave_misc_read: error parsing file %s\n", file);
misc_print_error(&error, TRUE);
}
}
/** Save the country variable into the file. */
void
xml_loadsave_misc_write(const gchar *prefix)
{
2005-04-07 18:44:33 +02:00
gint i;
2005-04-06 00:14:39 +02:00
gchar buf[SMALL];
FILE *fil = NULL;
sprintf(buf, "%s___misc.xml", prefix);
file_my_fopen(buf, "w", &fil, TRUE);
fprintf(fil, "<_%d>\n", TAG_MISC);
2006-04-08 21:25:48 +02:00
xml_write_string(fil, VERS, TAG_MISC_VERSION, I0);
2008-11-06 09:03:09 +01:00
xml_write_float(fil, current_interest, TAG_MISC_CURRENT_INTEREST, I0);
2005-10-09 11:35:44 +02:00
xml_write_string(fil, country.name, TAG_NAME, I0);
xml_write_string(fil, country.symbol, TAG_SYMBOL, I0);
xml_write_string(fil, country.sid, TAG_SID, I0);
xml_write_int(fil, country.rating, TAG_MISC_RATING, I0);
2005-04-06 00:14:39 +02:00
xml_write_int(fil, season, TAG_MISC_SEASON, I0);
xml_write_int(fil, week, TAG_MISC_WEEK, I0);
xml_write_int(fil, week_round, TAG_MISC_WEEK_ROUND, I0);
2005-04-07 18:44:33 +02:00
for(i=0;i<COUNT_END;i++)
xml_write_int(fil, counters[i], TAG_MISC_COUNTER, I0);
2005-04-14 21:07:25 +02:00
for(i=0;i<acps->len;i++)
xml_write_int(fil, acp(i)->id, TAG_MISC_ALLCUP, I0);
2005-10-27 23:10:15 +02:00
xml_loadsave_misc_write_bets(fil);
2005-04-06 00:14:39 +02:00
fprintf(fil, "</_%d>\n", TAG_MISC);
fclose(fil);
}
2005-10-27 23:10:15 +02:00
/** Write the bets arrays into the file. */
void
xml_loadsave_misc_write_bets(FILE *fil)
{
gint i, j, k;
for(i=0;i<2;i++)
for(j=0;j<bets[i]->len;j++)
{
fprintf(fil, "%s<_%d>\n", I0, (i == 0) ? TAG_MISC_BET0 : TAG_MISC_BET1);
2005-10-31 17:19:14 +01:00
xml_write_int(fil, g_array_index(bets[i], BetMatch, j).fix_id,
2005-10-27 23:10:15 +02:00
TAG_MISC_BET_FIX_ID, I1);
for(k=0;k<3;k++)
xml_write_float(fil, g_array_index(bets[i], BetMatch, j).odds[k],
TAG_MISC_BET_ODD, I1);
fprintf(fil, "%s</_%d>\n", I0, (i == 0) ? TAG_MISC_BET0 : TAG_MISC_BET1);
}
}