2004-12-23 13:58:39 +01:00
|
|
|
#include "file.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "free.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "misc.h"
|
2005-03-03 13:46:48 +01:00
|
|
|
#include "option.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
#include "support.h"
|
2005-01-09 21:21:22 +01:00
|
|
|
#include "variables.h"
|
2004-12-30 17:48:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
Add the specified directory to the list of directories file_find_support_file()
|
|
|
|
searches for support files.
|
|
|
|
Any subdirectories are added recursively.
|
|
|
|
@param directory The full path of the directory to be added.
|
|
|
|
@see file_find_support_file()
|
|
|
|
@see #support_directories
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
file_add_support_directory_recursive (const gchar *directory)
|
|
|
|
{
|
|
|
|
GDir *newdir =
|
|
|
|
g_dir_open(directory, 0, NULL);
|
|
|
|
const gchar *file;
|
|
|
|
gchar *fullpath;
|
|
|
|
|
|
|
|
if(newdir == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
add_pixmap_directory(directory);
|
|
|
|
support_directories = g_list_prepend (support_directories,
|
|
|
|
g_strdup (directory));
|
2005-04-04 12:36:04 +02:00
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
while(TRUE)
|
|
|
|
{
|
|
|
|
file = g_dir_read_name(newdir);
|
|
|
|
|
|
|
|
if(file == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
fullpath = g_strdup_printf ("%s%s%s", directory,
|
|
|
|
G_DIR_SEPARATOR_S, file);
|
|
|
|
|
|
|
|
if(g_file_test(fullpath, G_FILE_TEST_IS_DIR))
|
|
|
|
file_add_support_directory_recursive(fullpath);
|
|
|
|
|
|
|
|
g_free(fullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dir_close(newdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Search the list of support directories for a given file and return
|
|
|
|
the full path name.
|
|
|
|
The return value must be freed.
|
|
|
|
@param filename The name of the file we look for (without path).
|
2005-04-09 21:18:28 +02:00
|
|
|
@param warning Whether to show a warning if we don't find the file.
|
2004-12-30 17:48:19 +01:00
|
|
|
@return A pointer to the full path string of the file or NULL if
|
|
|
|
we didn't find the file. The gchar* must be freed.
|
|
|
|
@see #support_directories
|
|
|
|
@see file_add_support_directory_recursive()
|
|
|
|
*/
|
|
|
|
gchar*
|
2005-04-09 21:18:28 +02:00
|
|
|
file_find_support_file (const gchar *filename, gboolean warning)
|
2004-12-30 17:48:19 +01:00
|
|
|
{
|
2005-04-04 12:36:04 +02:00
|
|
|
GList *elem = support_directories;
|
2004-12-30 17:48:19 +01:00
|
|
|
|
|
|
|
while (elem)
|
|
|
|
{
|
|
|
|
gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
|
|
|
|
G_DIR_SEPARATOR_S, filename);
|
2005-04-20 19:56:31 +02:00
|
|
|
if (g_file_test (pathname, G_FILE_TEST_EXISTS) &&
|
|
|
|
!g_file_test(pathname, G_FILE_TEST_IS_DIR))
|
2004-12-30 17:48:19 +01:00
|
|
|
return pathname;
|
2005-04-20 19:56:31 +02:00
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
g_free (pathname);
|
|
|
|
elem = elem->next;
|
|
|
|
}
|
2005-04-20 19:56:31 +02:00
|
|
|
|
2005-04-09 21:18:28 +02:00
|
|
|
if(warning)
|
|
|
|
g_warning("file_find_support_file: file '%s' not found.", filename);
|
2005-04-04 12:36:04 +02:00
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
/** Execute command with 'system' and give a warning if return value is -1.
|
|
|
|
@return TRUE on success, FALSE, otherwise. */
|
|
|
|
gboolean
|
|
|
|
file_my_system(const gchar *command)
|
|
|
|
{
|
|
|
|
if(system(command) == -1)
|
|
|
|
{
|
|
|
|
g_warning("file_my_system: system returned -1 when executing '%s'.", command);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
/** A custom function opening files.
|
|
|
|
@param filename The full path to the file or a file name from the support files.
|
|
|
|
@param bits The mode we use, e.g. "r" or "w". @see fopen()
|
|
|
|
@param fil The file pointer that will point to the opened stream.
|
|
|
|
@param abort_program Whether to abort the program if we encounter an error.
|
|
|
|
@return TRUE on success, FALSE otherwise. */
|
|
|
|
gboolean
|
2005-01-09 21:21:22 +01:00
|
|
|
file_my_fopen(const gchar *filename, gchar *bits, FILE **fil, gboolean abort_program)
|
2004-12-30 17:48:19 +01:00
|
|
|
{
|
|
|
|
gchar buf[SMALL];
|
2005-04-09 21:18:28 +02:00
|
|
|
gchar *support_file = file_find_support_file(filename, FALSE);
|
2004-12-30 17:48:19 +01:00
|
|
|
*fil = fopen(filename, bits);
|
|
|
|
|
|
|
|
if(*fil != NULL)
|
|
|
|
{
|
|
|
|
g_free(support_file);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fil = fopen(support_file, bits);
|
|
|
|
if(*fil != NULL)
|
|
|
|
{
|
|
|
|
g_free(support_file);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(buf, "Could not open file '%s' in mode '%s'.\n", filename, bits);
|
|
|
|
|
|
|
|
g_warning(buf);
|
|
|
|
|
|
|
|
if(abort_program)
|
2005-01-09 21:21:22 +01:00
|
|
|
main_exit_program(EXIT_FILE_OPEN_FAILED, NULL);
|
2004-12-30 17:48:19 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-12-23 13:58:39 +01:00
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
/** Create a $HOME/.bygfoot dir and other stuff if necessary. */
|
|
|
|
void
|
|
|
|
file_check_home_dir_create_dirs(void)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gchar *dirs[3] =
|
|
|
|
{HOMEDIRNAME,
|
|
|
|
HOMEDIRNAME"/definitions",
|
|
|
|
HOMEDIRNAME"/saves"};
|
|
|
|
const gchar *home = g_get_home_dir();
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
|
|
|
for(i=0;i<3;i++)
|
|
|
|
{
|
|
|
|
sprintf(buf, "%s/%s", home, dirs[i]);
|
|
|
|
if(!g_file_test(buf, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
sprintf(buf, "mkdir -v %s/%s", home, dirs[i]);
|
|
|
|
file_my_system(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy the basic config files into the user home dir. */
|
|
|
|
void
|
|
|
|
file_check_home_dir_copy_conf_files(void)
|
|
|
|
{
|
|
|
|
gint i;
|
2005-05-18 18:00:49 +02:00
|
|
|
gchar *conf_files[4] =
|
2005-04-04 12:36:04 +02:00
|
|
|
{"bygfoot.conf",
|
|
|
|
"bygfoot_user.conf",
|
2005-05-18 18:00:49 +02:00
|
|
|
"bygfoot_constants",
|
|
|
|
"bygfoot_app"};
|
2005-04-04 12:36:04 +02:00
|
|
|
const gchar *home = g_get_home_dir();
|
|
|
|
gchar *conf_file = NULL;
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
2005-05-18 18:00:49 +02:00
|
|
|
for(i=0;i<4;i++)
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
|
|
|
sprintf(buf, "%s/%s/%s", home, HOMEDIRNAME, conf_files[i]);
|
|
|
|
if(!g_file_test(buf, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
2005-04-09 21:18:28 +02:00
|
|
|
conf_file = file_find_support_file(conf_files[i], TRUE);
|
2005-04-04 12:36:04 +02:00
|
|
|
sprintf(buf, "cp -v %s %s/%s/%s", conf_file, home, HOMEDIRNAME, conf_files[i]);
|
|
|
|
file_my_system(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy the xml definition files into the home dir. */
|
|
|
|
void
|
2005-04-20 19:56:31 +02:00
|
|
|
file_check_home_dir_copy_definition_dir(const gchar *dirname, const gchar *basename)
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
|
|
|
gint i;
|
2005-04-20 19:56:31 +02:00
|
|
|
gchar buf[SMALL], buf2[SMALL];
|
2005-04-04 12:36:04 +02:00
|
|
|
const gchar *home = g_get_home_dir();
|
2005-04-20 19:56:31 +02:00
|
|
|
GPtrArray *dir_contents = NULL;
|
|
|
|
|
|
|
|
sprintf(buf, "%s/%s/%s", home, HOMEDIRNAME, basename);
|
|
|
|
|
|
|
|
if(!g_file_test(buf, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
sprintf(buf2, "mkdir -v %s", buf);
|
|
|
|
file_my_system(buf2);
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_contents = file_dir_get_contents(dirname, "", "");
|
|
|
|
|
|
|
|
for(i=0;i<dir_contents->len;i++)
|
|
|
|
{
|
|
|
|
sprintf(buf, "%s/%s/%s/%s", home, HOMEDIRNAME, basename,
|
|
|
|
((GString*)g_ptr_array_index(dir_contents, i))->str);
|
|
|
|
|
|
|
|
if(g_str_has_suffix(((GString*)g_ptr_array_index(dir_contents, i))->str, ".xml") &&
|
|
|
|
!g_file_test(buf, G_FILE_TEST_EXISTS))
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
|
|
|
|
2005-04-20 19:56:31 +02:00
|
|
|
sprintf(buf2, "cp -v %s/%s %s", dirname,
|
|
|
|
((GString*)g_ptr_array_index(dir_contents, i))->str,
|
|
|
|
buf);
|
|
|
|
file_my_system(buf2);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(buf, "%s/%s", dirname, ((GString*)g_ptr_array_index(dir_contents, i))->str);
|
|
|
|
|
|
|
|
if(g_file_test(buf, G_FILE_TEST_IS_DIR))
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
2005-04-20 19:56:31 +02:00
|
|
|
sprintf(buf2, "%s/%s", basename,
|
2005-04-04 12:36:04 +02:00
|
|
|
((GString*)g_ptr_array_index(dir_contents, i))->str);
|
2005-04-20 19:56:31 +02:00
|
|
|
file_check_home_dir_copy_definition_dir(buf, buf2);
|
2005-04-04 12:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
2005-04-20 19:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free_g_string_array(&dir_contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy the xml definition files into the home dir. */
|
|
|
|
void
|
|
|
|
file_check_home_dir_copy_definition_files(void)
|
|
|
|
{
|
|
|
|
GList *elem = support_directories;
|
|
|
|
|
|
|
|
while(elem != NULL)
|
|
|
|
{
|
|
|
|
if(g_str_has_suffix((gchar*)elem->data, "definitions"))
|
|
|
|
file_check_home_dir_copy_definition_dir((const gchar*)elem->data, "definitions");
|
2005-04-04 12:36:04 +02:00
|
|
|
|
|
|
|
elem = elem->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy some files into the user's home directory. */
|
|
|
|
void
|
|
|
|
file_check_home_dir(void)
|
|
|
|
{
|
|
|
|
file_check_home_dir_create_dirs();
|
|
|
|
file_check_home_dir_copy_conf_files();
|
|
|
|
file_check_home_dir_copy_definition_files();
|
|
|
|
}
|
|
|
|
|
2004-12-23 13:58:39 +01:00
|
|
|
/**
|
|
|
|
Retrieve those files in the given directory
|
2005-04-04 12:36:04 +02:00
|
|
|
that start with the given prefix and suffix. The file names are stored
|
2004-12-23 13:58:39 +01:00
|
|
|
in an array of GStrings.
|
|
|
|
@param dir_name The full path to the directory.
|
|
|
|
@param prefix The prefix that files must have to be included.
|
2005-04-04 12:36:04 +02:00
|
|
|
@param suffix The suffix that files must have to be included.
|
2004-12-23 13:58:39 +01:00
|
|
|
@return A GPtrArray with pointers to the GStrings of the file
|
2005-04-04 12:36:04 +02:00
|
|
|
names. The GStrings and the array must be freed with free_g_string_array().
|
|
|
|
@see free_g_string_array()
|
2004-12-23 13:58:39 +01:00
|
|
|
*/
|
|
|
|
GPtrArray*
|
2005-04-04 12:36:04 +02:00
|
|
|
file_dir_get_contents(const gchar *dir_name, const gchar *prefix, const gchar *suffix)
|
2004-12-23 13:58:39 +01:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GDir *dir = g_dir_open(dir_name, 0, &error);
|
2005-04-09 21:18:28 +02:00
|
|
|
GPtrArray *contents = g_ptr_array_new();
|
2004-12-23 13:58:39 +01:00
|
|
|
GString *new = NULL;
|
|
|
|
const gchar *file = NULL;
|
|
|
|
|
2005-01-09 21:21:22 +01:00
|
|
|
misc_print_error(&error, FALSE);
|
2004-12-23 13:58:39 +01:00
|
|
|
|
|
|
|
if(dir == NULL)
|
2005-04-09 21:18:28 +02:00
|
|
|
return contents;
|
2004-12-23 13:58:39 +01:00
|
|
|
|
|
|
|
file = g_dir_read_name(dir);
|
|
|
|
|
2005-04-09 21:18:28 +02:00
|
|
|
if(file == NULL)
|
|
|
|
return contents;
|
2004-12-23 13:58:39 +01:00
|
|
|
|
|
|
|
while(file != NULL)
|
|
|
|
{
|
2005-04-04 12:36:04 +02:00
|
|
|
if(g_str_has_prefix(file, prefix) &&
|
|
|
|
g_str_has_suffix(file, suffix))
|
2004-12-23 13:58:39 +01:00
|
|
|
{
|
|
|
|
new = g_string_new(file);
|
|
|
|
g_ptr_array_add(contents, (gpointer)new);
|
|
|
|
}
|
|
|
|
file = g_dir_read_name(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dir_close(dir);
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
2005-01-09 21:21:22 +01:00
|
|
|
|
2005-04-09 21:18:28 +02:00
|
|
|
/** Return the country definition files found in the support dirs. */
|
|
|
|
GPtrArray*
|
|
|
|
file_get_country_files(void)
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-04-09 21:18:28 +02:00
|
|
|
gint i;
|
2005-04-04 12:36:04 +02:00
|
|
|
GList *elem = support_directories;
|
2005-04-09 21:18:28 +02:00
|
|
|
GPtrArray *country_files = g_ptr_array_new();
|
|
|
|
GPtrArray *dir_contents = NULL;
|
|
|
|
GString *new_string = NULL;
|
|
|
|
|
2005-01-09 21:21:22 +01:00
|
|
|
while(elem != NULL)
|
|
|
|
{
|
2005-04-09 21:18:28 +02:00
|
|
|
dir_contents = file_dir_get_contents((gchar*)elem->data, "country_", ".xml");
|
|
|
|
|
|
|
|
for(i=0;i<dir_contents->len;i++)
|
|
|
|
if(!query_misc_string_in_array(((GString*)g_ptr_array_index(dir_contents, i))->str,
|
|
|
|
country_files))
|
|
|
|
{
|
|
|
|
new_string = g_string_new(((GString*)g_ptr_array_index(dir_contents, i))->str);
|
|
|
|
g_ptr_array_add(country_files, new_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_g_string_array(&dir_contents);
|
|
|
|
|
2005-01-09 21:21:22 +01:00
|
|
|
elem = elem->next;
|
|
|
|
}
|
|
|
|
|
2005-04-09 21:18:28 +02:00
|
|
|
return country_files;
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Read the file until the next line that's not a comment or
|
2005-03-03 13:46:48 +01:00
|
|
|
a blank line. Split the line into the part before and after
|
|
|
|
the first white space and copy them into the char arrays.
|
|
|
|
Trailing and leading white spaces and trailing comments are stripped.
|
|
|
|
@param fil The file stream.
|
|
|
|
@param opt_name The first char array (an option name, mostly).
|
|
|
|
@param opt_value The second array (an option value, mostly).
|
|
|
|
@return TRUE if the file still contains lines to read, FALSE otherwise. */
|
2005-01-09 21:21:22 +01:00
|
|
|
gboolean
|
2005-03-03 13:46:48 +01:00
|
|
|
file_get_next_opt_line(FILE *fil, gchar *opt_name, gchar *opt_value)
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-03-03 13:46:48 +01:00
|
|
|
gint i;
|
|
|
|
gchar trash[SMALL];
|
|
|
|
gchar buf[BIG];
|
2005-01-09 21:21:22 +01:00
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
strcpy(buf, "");
|
2005-03-08 10:39:54 +01:00
|
|
|
strcpy(opt_name, "");
|
|
|
|
strcpy(opt_value, "");
|
2005-01-09 21:21:22 +01:00
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
while( (buf[0] == '#' || strlen(buf) == 0) &&
|
2005-01-09 21:21:22 +01:00
|
|
|
feof(fil) == 0)
|
|
|
|
{
|
2005-03-03 13:46:48 +01:00
|
|
|
fscanf(fil, "%[\n \t]*", buf);
|
|
|
|
fscanf(fil, "%[^\n]", buf);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
if(buf[0] != '#' && strlen(buf) != 0)
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-05-22 15:33:24 +02:00
|
|
|
if(strlen(buf) > 1000)
|
|
|
|
g_warning("\n the text file I'm reading contains a line longer than 1000 chars.\n\n");
|
2005-01-09 21:21:22 +01:00
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
for(i=0;i<strlen(buf);i++)
|
|
|
|
if(buf[i] == '#')
|
|
|
|
{
|
|
|
|
buf[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=strlen(buf) - 1;i>0;i--)
|
|
|
|
if(buf[i] == '\t' || buf[i] == ' ')
|
|
|
|
buf[i] = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
sscanf(buf, "%[^ \t]%[ \t]%[^\n]", opt_name, trash, opt_value);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (feof(fil) == 0);
|
|
|
|
}
|
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
/** Save an optionlist to a file. */
|
|
|
|
void
|
|
|
|
file_save_opt_file(const gchar *filename, OptionList *optionlist)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
FILE *fil = NULL;
|
|
|
|
|
|
|
|
file_my_fopen(filename, "w", &fil, TRUE);
|
|
|
|
|
|
|
|
for(i=0;i<optionlist->list->len;i++)
|
|
|
|
if(g_str_has_prefix(g_array_index(optionlist->list, Option, i).name->str, "string_"))
|
|
|
|
fprintf(fil, "%s %s\n", g_array_index(optionlist->list, Option, i).name->str,
|
|
|
|
g_array_index(optionlist->list, Option, i).string_value->str);
|
|
|
|
else
|
|
|
|
fprintf(fil, "%s %d\n", g_array_index(optionlist->list, Option, i).name->str,
|
|
|
|
g_array_index(optionlist->list, Option, i).value);
|
|
|
|
|
|
|
|
fclose(fil);
|
|
|
|
}
|
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
/** Load a file containing name - value pairs into
|
|
|
|
the specified array. */
|
2005-01-09 21:21:22 +01:00
|
|
|
void
|
2005-04-04 12:36:04 +02:00
|
|
|
file_load_opt_file(const gchar *filename, OptionList *optionlist)
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-03-23 20:03:26 +01:00
|
|
|
gint i;
|
2005-03-03 13:46:48 +01:00
|
|
|
gchar opt_name[SMALL], opt_value[SMALL];
|
|
|
|
Option new;
|
2005-04-04 12:36:04 +02:00
|
|
|
FILE *fil = NULL;
|
2005-03-03 13:46:48 +01:00
|
|
|
|
2005-03-24 08:42:24 +01:00
|
|
|
free_option_list(optionlist, TRUE);
|
2005-03-03 13:46:48 +01:00
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
file_my_fopen(filename, "r", &fil, TRUE);
|
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
while(file_get_next_opt_line(fil, opt_name, opt_value))
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-03-03 13:46:48 +01:00
|
|
|
new.name = g_string_new(opt_name);
|
|
|
|
if(g_str_has_prefix(opt_name, "string_"))
|
|
|
|
{
|
|
|
|
new.string_value = g_string_new(opt_value);
|
|
|
|
new.value = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new.string_value = NULL;
|
|
|
|
sscanf(opt_value, "%d", &new.value);
|
|
|
|
}
|
|
|
|
|
2005-03-23 20:03:26 +01:00
|
|
|
g_array_append_val(optionlist->list, new);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
2005-03-03 13:46:48 +01:00
|
|
|
|
2005-03-23 20:03:26 +01: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));
|
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
fclose(fil);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Load the options at the beginning of a new game from
|
2005-03-03 13:46:48 +01:00
|
|
|
the configuration files. */
|
2005-01-09 21:21:22 +01:00
|
|
|
void
|
2005-03-03 13:46:48 +01:00
|
|
|
file_load_conf_files(void)
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-04-09 21:18:28 +02:00
|
|
|
gchar *conf_file = file_find_support_file("bygfoot.conf", TRUE);
|
2005-01-09 21:21:22 +01:00
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
file_load_opt_file(conf_file, &options);
|
2005-01-09 21:21:22 +01:00
|
|
|
g_free(conf_file);
|
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
file_load_opt_file(opt_str("string_opt_constants_file"), &constants);
|
2005-05-06 18:35:19 +02:00
|
|
|
file_load_opt_file(opt_str("string_opt_appearance_file"), &constants_app);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
/** Load a user-specific conf file.
|
|
|
|
@param user The user we load the file for. */
|
2005-01-09 21:21:22 +01:00
|
|
|
void
|
2005-03-03 13:46:48 +01:00
|
|
|
file_load_user_conf_file(User *user)
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
|
|
|
FILE *fil = NULL;
|
2005-03-03 13:46:48 +01:00
|
|
|
gchar *conf_file = NULL, buf[SMALL];
|
2005-01-09 21:21:22 +01:00
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
sprintf(buf, "bygfoot_%s.conf", user->name->str);
|
2005-04-09 21:18:28 +02:00
|
|
|
conf_file = file_find_support_file(buf, FALSE);
|
2005-04-06 00:10:18 +02:00
|
|
|
|
2005-03-03 13:46:48 +01:00
|
|
|
if(conf_file == NULL ||
|
|
|
|
!file_my_fopen(conf_file, "r", &fil, FALSE))
|
2005-01-09 21:21:22 +01:00
|
|
|
{
|
2005-03-03 13:46:48 +01:00
|
|
|
g_free(conf_file);
|
2005-04-09 21:18:28 +02:00
|
|
|
conf_file = file_find_support_file(opt_str("string_opt_default_user_conf_file"), TRUE);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
|
|
|
|
2005-04-04 12:36:04 +02:00
|
|
|
file_load_opt_file(conf_file, &user->options);
|
|
|
|
g_free(conf_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the primary support dir (probably './support_files' or
|
|
|
|
the Bygfoot dir in $HOME). */
|
|
|
|
const gchar*
|
|
|
|
file_get_first_support_dir(void)
|
|
|
|
{
|
|
|
|
GList *elem = support_directories;
|
|
|
|
|
|
|
|
while (elem)
|
|
|
|
{
|
|
|
|
if(g_str_has_suffix((gchar*)elem->data, HOMEDIRNAME) ||
|
|
|
|
g_str_has_suffix((gchar*)elem->data, "support_files"))
|
|
|
|
return (const gchar*)elem->data;
|
|
|
|
|
|
|
|
elem = elem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_warning("file_get_first_support_dir: no primary support dir found.");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compress the files starting with the prefix.
|
|
|
|
@param destfile The name of the file to create. */
|
|
|
|
void
|
|
|
|
file_compress_files(const gchar *destfile, const gchar *prefix)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gchar buf[SMALL];
|
|
|
|
gchar *basename = g_path_get_basename(prefix),
|
|
|
|
*dirname = g_path_get_dirname(prefix),
|
2005-04-07 23:10:31 +02:00
|
|
|
*zipbasename = g_path_get_basename(destfile),
|
|
|
|
*pwd = g_get_current_dir();
|
2005-04-04 12:36:04 +02:00
|
|
|
GPtrArray *files = file_dir_get_contents(dirname, basename, "");
|
|
|
|
|
2005-04-07 23:10:31 +02:00
|
|
|
sprintf(buf, "cd 1> /dev/null %s; %s %s", dirname,
|
2005-04-04 12:36:04 +02:00
|
|
|
const_str("string_save_compress_command"), zipbasename);
|
|
|
|
|
|
|
|
for(i=0;i<files->len;i++)
|
|
|
|
{
|
|
|
|
strcat(buf, " ");
|
|
|
|
strcat(buf, ((GString*)g_ptr_array_index(files, i))->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_my_system(buf);
|
|
|
|
|
2005-04-07 23:10:31 +02:00
|
|
|
sprintf(buf, "cd %s; rm -rf %s/%s*", pwd, dirname, basename);
|
2005-04-04 12:36:04 +02:00
|
|
|
file_my_system(buf);
|
|
|
|
|
|
|
|
free_g_string_array(&files);
|
|
|
|
|
|
|
|
g_free(basename);
|
|
|
|
g_free(dirname);
|
|
|
|
g_free(zipbasename);
|
2005-04-07 23:10:31 +02:00
|
|
|
g_free(pwd);
|
2005-04-04 12:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Decompress the specified file. */
|
|
|
|
void
|
|
|
|
file_decompress(const gchar *filename)
|
|
|
|
{
|
|
|
|
gchar buf[SMALL];
|
|
|
|
gchar *dirname = g_path_get_dirname(filename),
|
|
|
|
*basename = g_path_get_basename(filename);
|
|
|
|
|
2005-04-07 23:10:31 +02:00
|
|
|
sprintf(buf, "cd %s 1> /dev/null; %s %s", dirname,
|
2005-04-04 12:36:04 +02:00
|
|
|
const_str("string_save_uncompress_command"), basename);
|
|
|
|
|
|
|
|
file_my_system(buf);
|
|
|
|
|
|
|
|
g_free(dirname);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|