bygfoot/src/option.c

230 lines
6.8 KiB
C
Raw Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
option.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-06-15 18:44:53 +02:00
#include "main.h"
2005-10-09 11:35:44 +02:00
#include "misc.h"
#include "option.h"
#include "variables.h"
/** Return the string going with the option
named 'name'.
@param name The name of the option.
@return The string_value of the option.
@see #Option */
gchar*
2005-03-23 20:03:26 +01:00
option_string(const gchar *name, OptionList *optionlist)
{
#ifdef DEBUG
printf("option_string\n");
#endif
#ifdef DEBUG
printf("option_string\n");
#endif
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-03-23 20:03:26 +01:00
2005-10-24 22:50:48 +02:00
if(element != NULL)
2005-10-09 11:35:44 +02:00
return ((Option*)element)->string_value;
2005-10-24 22:50:48 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND,
"option_string: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-06-15 18:44:53 +02:00
2005-03-23 20:03:26 +01:00
return NULL;
}
2005-03-23 20:03:26 +01:00
/** Return the GString pointer going with the option. */
2005-10-09 11:35:44 +02:00
gchar**
2005-03-23 20:03:26 +01:00
option_string_pointer(const gchar *name, OptionList *optionlist)
{
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-03-23 20:03:26 +01:00
2005-10-24 22:50:48 +02:00
if(element != NULL)
2005-10-09 11:35:44 +02:00
return &((Option*)element)->string_value;
2005-10-24 22:50:48 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND,
"option_string: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-06-15 18:44:53 +02:00
return NULL;
}
/** Return the integer going with the option
named 'name'.
@param name The name of the option.
@return The value of the option.
@see #Option */
gint
2005-03-23 20:03:26 +01:00
option_int(const gchar *name, OptionList *optionlist)
{
#ifdef DEBUG
printf("option_int\n");
#endif
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-03-23 20:03:26 +01:00
2005-10-24 22:50:48 +02:00
if(element != NULL)
2005-03-23 20:03:26 +01:00
return ((Option*)element)->value;
2005-10-24 22:50:48 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND,
"option_int: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-06-15 18:44:53 +02:00
2005-03-23 20:03:26 +01:00
return -1;
}
2005-03-23 20:03:26 +01:00
/** Return the address of an options variable. */
gint*
option_int_pointer(const gchar *name, OptionList *optionlist)
{
#ifdef DEBUG
printf("option_int_pointer\n");
#endif
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-03-23 20:03:26 +01:00
2005-10-24 22:50:48 +02:00
if(element != NULL)
2005-03-23 20:03:26 +01:00
return &((Option*)element)->value;
2005-10-24 22:50:48 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND,
"option_int: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-06-15 18:44:53 +02:00
2005-03-23 20:03:26 +01:00
return NULL;
}
/** Return the int going with the option named 'name'
cast to float and divided by 1000.
@param name The name of the option.
@return The value of the option cast to float and divided by 1000.
@see #Option */
gfloat
2005-03-23 20:03:26 +01:00
option_float(const gchar *name, OptionList *optionlist)
{
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-10-24 22:50:48 +02:00
if(element != NULL)
2009-03-09 21:20:26 +01:00
return (gfloat)((Option*)element)->value / OPTION_FLOAT_DIVISOR;
2005-10-24 22:50:48 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND,
"option_float: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-06-15 18:44:53 +02:00
return -1;
}
/** Change the value of a string option in the array.
@param name The name of the option.
@param option_array The option array.
@param new_value The value we set. */
void
2005-03-23 20:03:26 +01:00
option_set_string(const gchar *name, OptionList *optionlist, const gchar *new_value)
{
#ifdef DEBUG
printf("option_set_string\n");
#endif
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-03-23 20:03:26 +01:00
if(element == NULL)
2009-04-29 19:18:54 +02:00
debug_print_message("option_set_string: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-03-23 20:03:26 +01:00
else
2005-10-09 11:35:44 +02:00
misc_string_assign(&((Option*)element)->string_value, new_value);
}
/** Change the value of an int option in the array.
@param name The name of the option.
@param option_array The option array.
@param new_value The value we set. */
void
2005-03-23 20:03:26 +01:00
option_set_int(const gchar *name, OptionList *optionlist, gint new_value)
{
#ifdef DEBUG
printf("option_set_int\n");
#endif
GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
2005-03-23 20:03:26 +01:00
if(element == NULL)
2009-04-29 19:18:54 +02:00
debug_print_message("option_set_int: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
2005-03-23 20:03:26 +01:00
else
((Option*)element)->value = new_value;
}
2005-06-23 23:53:57 +02:00
/** Add an option to the optionlist with the given values. */
void
2005-09-29 15:41:16 +02:00
option_add(OptionList *optionlist, const gchar *name,
gint int_value, const gchar *string_value)
2005-06-23 23:53:57 +02:00
{
#ifdef DEBUG
printf("option_add\n");
#endif
2005-06-23 23:53:57 +02:00
gint i;
Option new;
2005-06-26 13:42:01 +02:00
gpointer element = NULL;
if(optionlist->list != NULL) {
GQuark quark = g_quark_from_string(name);
element = g_datalist_id_get_data(&optionlist->datalist, quark);
}
2005-06-23 23:53:57 +02:00
if(element != NULL)
2009-01-09 15:32:34 +01:00
{
if(debug > 0)
2009-04-29 19:18:54 +02:00
debug_print_message("Option %s already in optionlist\n", name);
2009-01-09 15:32:34 +01:00
((Option*)element)->value = int_value;
((Option*)element)->string_value = (string_value == NULL) ? NULL : g_strdup(string_value);
return;
}
2005-06-23 23:53:57 +02:00
2005-10-09 11:35:44 +02:00
new.name = g_strdup(name);
2005-06-23 23:53:57 +02:00
new.value = int_value;
2005-10-09 11:35:44 +02:00
new.string_value = (string_value == NULL) ? NULL : g_strdup(string_value);
2005-06-23 23:53:57 +02:00
2005-06-26 13:42:01 +02:00
if(optionlist->list == NULL)
{
optionlist->list = g_array_new(FALSE, FALSE, sizeof(Option));
g_datalist_init(&optionlist->datalist);
}
2005-06-23 23:53:57 +02:00
2005-06-26 13:42:01 +02:00
g_array_append_val(optionlist->list, new);
2005-06-23 23:53:57 +02:00
for(i=0;i<optionlist->list->len;i++)
2005-09-29 15:41:16 +02:00
g_datalist_set_data(&optionlist->datalist,
2005-10-09 11:35:44 +02:00
g_array_index(optionlist->list, Option, i).name,
2005-06-23 23:53:57 +02:00
&g_array_index(optionlist->list, Option, i));
}
2009-03-10 21:58:34 +01:00
gint
option_compare_func(gconstpointer a, gconstpointer b)
{
return misc_alphabetic_compare(((const Option*)a)->name, ((const Option*)b)->name);
}