bygfoot/src/option.c

161 lines
4.4 KiB
C
Raw Normal View History

2005-06-15 18:44:53 +02:00
#include "main.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)
{
2005-03-23 20:03:26 +01:00
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
if(element == NULL)
g_warning("option_string: option named %s not found\n", name);
else
return ((Option*)element)->string_value->str;
2005-06-17 14:57:05 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND, NULL);
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. */
GString*
option_string_pointer(const gchar *name, OptionList *optionlist)
{
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
if(element == NULL)
g_warning("option_string: option named %s not found\n", name);
else
return ((Option*)element)->string_value;
2005-06-17 14:57:05 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND, NULL);
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)
{
2005-03-23 20:03:26 +01:00
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
if(element == NULL)
g_warning("option_int: option named %s not found\n", name);
else
return ((Option*)element)->value;
2005-06-17 14:57:05 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND, NULL);
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)
{
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
if(element == NULL)
g_warning("option_int: option named %s not found\n", name);
else
return &((Option*)element)->value;
2005-06-17 14:57:05 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND, NULL);
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)
{
2005-03-23 20:03:26 +01:00
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
2005-03-23 20:03:26 +01:00
if(element == NULL)
g_warning("option_float: option named %s not found\n", name);
else
return (gfloat)((Option*)element)->value / 10000;
2005-06-17 14:57:05 +02:00
main_exit_program(EXIT_OPTION_NOT_FOUND, NULL);
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)
{
2005-03-23 20:03:26 +01:00
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
if(element == NULL)
g_warning("option_set_string: option named %s not found\n", name);
else
g_string_printf(((Option*)element)->string_value, "%s", 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)
{
2005-03-23 20:03:26 +01:00
gpointer element = g_datalist_get_data(&optionlist->datalist, name);
2005-03-23 20:03:26 +01:00
if(element == NULL)
g_warning("option_set_int: option named %s not found\n", name);
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
option_add(OptionList *optionlist, const gchar *name, gint int_value, const gchar *string_value)
{
gint i;
Option new;
2005-06-26 13:42:01 +02:00
gpointer element = NULL;
if(optionlist->list != NULL)
g_datalist_get_data(&optionlist->datalist, name);
2005-06-23 23:53:57 +02:00
if(element != NULL)
{
g_warning("Option named '%s' already contained in optionlist.", name);
main_exit_program(EXIT_GENERAL, NULL);
}
new.name = g_string_new(name);
new.value = int_value;
new.string_value = (string_value == NULL) ? NULL : g_string_new(string_value);
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++)
g_datalist_set_data(&optionlist->datalist, g_array_index(optionlist->list, Option, i).name->str,
&g_array_index(optionlist->list, Option, i));
}