mirror of
https://github.com/tstellar/bygfoot.git
synced 2025-01-28 14:39:22 +01:00
Newspaper progressing.
This commit is contained in:
parent
669aca19fa
commit
3be6a075a3
231
src/xml_loadsave_newspaper.c
Normal file
231
src/xml_loadsave_newspaper.c
Normal file
@ -0,0 +1,231 @@
|
||||
/*
|
||||
xml_loadsave_newspaper.c
|
||||
|
||||
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"
|
||||
#include "xml.h"
|
||||
#include "xml_loadsave_newspaper.h"
|
||||
#include "variables.h"
|
||||
|
||||
enum
|
||||
{
|
||||
TAG_NEWS_PAPER = TAG_START_NEWS_PAPER,
|
||||
TAG_NEWS_PAPER_NAME,
|
||||
TAG_NEWS_PAPER_ARTICLE,
|
||||
TAG_NEWS_PAPER_ARTICLE_WEEK,
|
||||
TAG_NEWS_PAPER_ARTICLE_WEEK_ROUND,
|
||||
TAG_NEWS_PAPER_ARTICLE_TITLE_ID,
|
||||
TAG_NEWS_PAPER_ARTICLE_SUBTITLE_ID,
|
||||
TAG_NEWS_PAPER_ARTICLE_TITLE,
|
||||
TAG_NEWS_PAPER_ARTICLE_SUBTITLE,
|
||||
TAG_END
|
||||
};
|
||||
|
||||
gint state;
|
||||
NewsPaperArticle new_article;
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_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_newspaper_start_element\n");
|
||||
#endif
|
||||
|
||||
gint i;
|
||||
gint tag = xml_get_tag_from_name(element_name);
|
||||
gboolean valid_tag = FALSE;
|
||||
|
||||
for(i=TAG_NEWS_PAPER;i<TAG_END;i++)
|
||||
if(tag == i)
|
||||
{
|
||||
state = i;
|
||||
valid_tag = TRUE;
|
||||
}
|
||||
|
||||
if(!valid_tag)
|
||||
g_warning("xml_loadsave_newspaper_start_element: unknown tag: %s; I'm in state %d\n",
|
||||
element_name, state);
|
||||
}
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_end_element (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("xml_loadsave_newspaper_end_element\n");
|
||||
#endif
|
||||
|
||||
gint tag = xml_get_tag_from_name(element_name);
|
||||
|
||||
if(tag == TAG_NEWS_PAPER_NAME ||
|
||||
tag == TAG_NEWS_PAPER_ARTICLE)
|
||||
{
|
||||
state = TAG_NEWS_PAPER;
|
||||
if(tag == TAG_NEWS_PAPER_ARTICLE)
|
||||
g_array_append_val(newspaper.articles, new_article);
|
||||
}
|
||||
else if(tag == TAG_NEWS_PAPER_ARTICLE_WEEK ||
|
||||
tag == TAG_NEWS_PAPER_ARTICLE_WEEK_ROUND ||
|
||||
tag == TAG_NEWS_PAPER_ARTICLE_TITLE ||
|
||||
tag == TAG_NEWS_PAPER_ARTICLE_TITLE_ID ||
|
||||
tag == TAG_NEWS_PAPER_ARTICLE_SUBTITLE ||
|
||||
tag == TAG_NEWS_PAPER_ARTICLE_SUBTITLE_ID)
|
||||
{
|
||||
state = TAG_NEWS_PAPER_ARTICLE;
|
||||
}
|
||||
else if(tag != TAG_NEWS_PAPER)
|
||||
g_warning("xml_loadsave_newspaper_end_element: unknown tag: %s; I'm in state %d\n",
|
||||
element_name, state);
|
||||
}
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_text (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gsize text_len,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("xml_loadsave_newspaper_text\n");
|
||||
#endif
|
||||
|
||||
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_NEWS_PAPER_NAME)
|
||||
g_ptr_array_add(newspaper.names, g_strdup(buf));
|
||||
else if(state == TAG_NEWS_PAPER_ARTICLE_WEEK)
|
||||
new_article.week_number = int_value;
|
||||
else if(state == TAG_NEWS_PAPER_ARTICLE_WEEK_ROUND)
|
||||
new_article.week_round_number = int_value;
|
||||
else if(state == TAG_NEWS_PAPER_ARTICLE_TITLE)
|
||||
new_article.title = g_strdup(buf);
|
||||
else if(state == TAG_NEWS_PAPER_ARTICLE_SUBTITLE)
|
||||
new_article.subtitle = g_strdup(buf);
|
||||
else if(state == TAG_NEWS_PAPER_ARTICLE_TITLE_ID)
|
||||
new_article.title_id = int_value;
|
||||
else if(state == TAG_NEWS_PAPER_ARTICLE_SUBTITLE_ID)
|
||||
new_article.subtitle_id = int_value;
|
||||
}
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_read(const gchar *dirname, const gchar *prefix)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("xml_loadsave_newspaper_read\n");
|
||||
#endif
|
||||
|
||||
gchar filename[SMALL];
|
||||
GMarkupParser parser = {xml_loadsave_newspaper_start_element,
|
||||
xml_loadsave_newspaper_end_element,
|
||||
xml_loadsave_newspaper_text, NULL, NULL};
|
||||
GMarkupParseContext *context;
|
||||
gchar *file_contents;
|
||||
gsize length;
|
||||
GError *error = NULL;
|
||||
|
||||
free_newspaper(TRUE);
|
||||
|
||||
sprintf(filename, "%s%s%s___newspaper.xml", dirname, G_DIR_SEPARATOR_S, prefix);
|
||||
|
||||
context =
|
||||
g_markup_parse_context_new(&parser, 0, NULL, NULL);
|
||||
|
||||
if(!g_file_get_contents(filename, &file_contents, &length, &error))
|
||||
{
|
||||
g_warning("xml_loadsave_news_read: error reading file %s\n", filename);
|
||||
misc_print_error(&error, TRUE);
|
||||
}
|
||||
|
||||
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_news_read: error parsing file %s\n", filename);
|
||||
misc_print_error(&error, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_write(const gchar *prefix)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("xml_loadsave_newspaper_write\n");
|
||||
#endif
|
||||
|
||||
gint i;
|
||||
FILE *fil = NULL;
|
||||
gchar buf[SMALL];
|
||||
|
||||
sprintf(buf, "%s___newspaper.xml", prefix);
|
||||
|
||||
file_my_fopen(buf, "w", &fil, TRUE);
|
||||
|
||||
fprintf(fil, "<_%d>\n", TAG_NEWS_PAPER);
|
||||
|
||||
for(i = 0; i < newspaper.names->len; i++)
|
||||
xml_write_string(fil, (gchar*)g_ptr_array_index(newspaper.names, i), TAG_NEWS_PAPER_NAME, I1);
|
||||
|
||||
for(i=0;i<newspaper.articles->len;i++)
|
||||
{
|
||||
fprintf(fil, "%s<_%d>\n", I0, TAG_NEWS_PAPER_ARTICLE);
|
||||
|
||||
xml_write_int(fil, g_array_index(newspaper.articles, NewsPaperArticle, i).week_number,
|
||||
TAG_NEWS_PAPER_ARTICLE_WEEK, I1);
|
||||
xml_write_int(fil, g_array_index(newspaper.articles, NewsPaperArticle, i).week_round_number,
|
||||
TAG_NEWS_PAPER_ARTICLE_WEEK_ROUND, I1);
|
||||
xml_write_string(fil, g_array_index(newspaper.articles, NewsPaperArticle, i).title,
|
||||
TAG_NEWS_PAPER_ARTICLE_TITLE, I1);
|
||||
xml_write_string(fil, g_array_index(newspaper.articles, NewsPaperArticle, i).subtitle,
|
||||
TAG_NEWS_PAPER_ARTICLE_SUBTITLE, I1);
|
||||
xml_write_int(fil, g_array_index(newspaper.articles, NewsPaperArticle, i).title_id,
|
||||
TAG_NEWS_PAPER_ARTICLE_TITLE_ID, I1);
|
||||
xml_write_int(fil, g_array_index(newspaper.articles, NewsPaperArticle, i).subtitle_id,
|
||||
TAG_NEWS_PAPER_ARTICLE_SUBTITLE_ID, I1);
|
||||
|
||||
fprintf(fil, "%s</_%d>\n", I0, TAG_NEWS_PAPER_ARTICLE);
|
||||
}
|
||||
|
||||
fprintf(fil, "</_%d>\n", TAG_NEWS_PAPER);
|
||||
|
||||
fclose(fil);
|
||||
}
|
59
src/xml_loadsave_newspaper.h
Normal file
59
src/xml_loadsave_newspaper.h
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
xml_loadsave_newspaper.h
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef XML_LOADSAVE_NEWSPAPER_H
|
||||
#define XML_LOADSAVE_NEWSPAPER_H
|
||||
|
||||
#include "bygfoot.h"
|
||||
#include "news_struct.h"
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_start_element (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
const gchar **attribute_names,
|
||||
const gchar **attribute_values,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_end_element (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_text (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gsize text_len,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_read(const gchar *dirname, const gchar *prefix);
|
||||
|
||||
void
|
||||
xml_loadsave_newspaper_write(const gchar *prefix);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user