2005-07-17 13:55:33 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2005-07-18 18:18:39 +02:00
|
|
|
#include <unistd.h>
|
2005-07-17 13:55:33 +02:00
|
|
|
|
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);
|
2005-07-18 18:18:39 +02:00
|
|
|
|
2004-12-30 17:48:19 +01:00
|
|
|
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-07-18 18:18:39 +02:00
|
|
|
GList *elem = support_directories;
|
2004-12-30 17:48:19 +01:00
|
|
|
|
2005-07-18 18:18:39 +02:00
|
|
|
while (elem)
|
2004-12-30 17:48:19 +01:00
|
|
|
{
|
2005-07-18 18:18:39 +02:00
|
|
|
gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
|
|
|
|
G_DIR_SEPARATOR_S, filename);
|
|
|
|
if (g_file_test (pathname, G_FILE_TEST_EXISTS) &&
|
|
|
|
!g_file_test(pathname, G_FILE_TEST_IS_DIR))
|
|
|
|
return pathname;
|
2005-04-20 19:56:31 +02:00
|
|
|
|
2005-07-18 18:18:39 +02:00
|
|
|
g_free (pathname);
|
|
|
|
elem = elem->next;
|
2004-12-30 17:48:19 +01:00
|
|
|
}
|
2005-04-20 19:56:31 +02:00
|
|
|
|
2005-07-18 18:18:39 +02:00
|
|
|
if(warning)
|
|
|
|
g_warning("file_find_support_file: file '%s' not found.", filename);
|
2005-04-04 12:36:04 +02:00
|
|
|
|
2005-07-18 18:18:39 +02:00
|
|
|
return NULL;
|
2004-12-30 17:48:19 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2005-06-21 15:33:16 +02:00
|
|
|
if(system(command) != 0)
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
|
|
|
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,
|
2005-07-17 13:55:33 +02:00
|
|
|
HOMEDIRNAME G_DIR_SEPARATOR_S "definitions",
|
|
|
|
HOMEDIRNAME G_DIR_SEPARATOR_S "saves"};
|
2005-04-04 12:36:04 +02:00
|
|
|
const gchar *home = g_get_home_dir();
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
|
|
|
for(i=0;i<3;i++)
|
|
|
|
{
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf, "%s%s%s", home, G_DIR_SEPARATOR_S, dirs[i]);
|
2005-04-04 12:36:04 +02:00
|
|
|
if(!g_file_test(buf, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf, "%s%s%s", home, G_DIR_SEPARATOR_S, dirs[i]);
|
2005-07-18 18:18:39 +02:00
|
|
|
file_mkdir(buf);
|
2005-04-04 12:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy the basic config files into the user home dir. */
|
|
|
|
void
|
|
|
|
file_check_home_dir_copy_conf_files(void)
|
|
|
|
{
|
|
|
|
gint i;
|
2005-07-01 22:51:46 +02:00
|
|
|
gchar *conf_files[2] =
|
2005-04-04 12:36:04 +02:00
|
|
|
{"bygfoot.conf",
|
2005-07-01 22:51:46 +02:00
|
|
|
"bygfoot_user.conf"};
|
2005-04-04 12:36:04 +02:00
|
|
|
const gchar *home = g_get_home_dir();
|
|
|
|
gchar *conf_file = NULL;
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
2005-07-01 22:51:46 +02:00
|
|
|
for(i=0;i<2;i++)
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf, "%s%s%s%s%s", home, G_DIR_SEPARATOR_S,
|
|
|
|
HOMEDIRNAME, G_DIR_SEPARATOR_S, conf_files[i]);
|
2005-04-04 12:36:04 +02:00
|
|
|
if(!g_file_test(buf, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
2005-07-18 18:18:39 +02:00
|
|
|
conf_file = file_find_support_file(conf_files[i], TRUE);
|
|
|
|
sprintf(buf, "%s%s%s%s%s",home, G_DIR_SEPARATOR_S,
|
2005-07-17 13:55:33 +02:00
|
|
|
HOMEDIRNAME, G_DIR_SEPARATOR_S, conf_files[i]);
|
2005-07-18 18:18:39 +02:00
|
|
|
file_copy_file(conf_file, buf);
|
2005-04-04 12:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf, "%s%s%s%s%s", home, G_DIR_SEPARATOR_S,
|
|
|
|
HOMEDIRNAME, G_DIR_SEPARATOR_S, basename);
|
2005-04-20 19:56:31 +02:00
|
|
|
|
|
|
|
if(!g_file_test(buf, G_FILE_TEST_EXISTS))
|
2005-07-18 18:18:39 +02:00
|
|
|
file_mkdir(buf);
|
2005-04-20 19:56:31 +02:00
|
|
|
|
|
|
|
dir_contents = file_dir_get_contents(dirname, "", "");
|
|
|
|
|
|
|
|
for(i=0;i<dir_contents->len;i++)
|
|
|
|
{
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf, "%s%s%s%s%s%s%s", home, G_DIR_SEPARATOR_S,
|
|
|
|
HOMEDIRNAME, G_DIR_SEPARATOR_S, basename, G_DIR_SEPARATOR_S,
|
2005-04-20 19:56:31 +02:00
|
|
|
((GString*)g_ptr_array_index(dir_contents, i))->str);
|
2005-07-18 18:18:39 +02:00
|
|
|
|
2005-04-20 19:56:31 +02:00
|
|
|
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-07-18 18:18:39 +02:00
|
|
|
|
|
|
|
sprintf(buf2, "%s%s%s", dirname, G_DIR_SEPARATOR_S,
|
|
|
|
((GString*)g_ptr_array_index(dir_contents, i))->str);
|
|
|
|
file_copy_file(buf2, buf);
|
2005-04-20 19:56:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf, "%s%s%s", dirname, G_DIR_SEPARATOR_S,
|
|
|
|
((GString*)g_ptr_array_index(dir_contents, i))->str);
|
2005-04-20 19:56:31 +02:00
|
|
|
|
|
|
|
if(g_file_test(buf, G_FILE_TEST_IS_DIR))
|
2005-04-04 12:36:04 +02:00
|
|
|
{
|
2005-07-17 13:55:33 +02:00
|
|
|
sprintf(buf2, "%s%s%s", basename, G_DIR_SEPARATOR_S,
|
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)
|
2005-07-18 18:18:39 +02:00
|
|
|
{
|
2005-04-20 19:56:31 +02:00
|
|
|
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-07-18 18:18:39 +02: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-07-18 18:18:39 +02:00
|
|
|
|
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-07-17 13:55:33 +02:00
|
|
|
new.name = g_string_new(opt_name);
|
2005-03-03 13:46:48 +01:00
|
|
|
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-07-17 13:55:33 +02:00
|
|
|
|
|
|
|
if((g_str_has_suffix(new.name->str, "_unix") && os_is_unix) ||
|
|
|
|
(g_str_has_suffix(new.name->str, "_win32") && !os_is_unix))
|
|
|
|
{
|
|
|
|
strcpy(opt_name, new.name->str);
|
2005-07-18 18:18:39 +02:00
|
|
|
opt_name[strlen(new.name->str) - (os_is_unix ? 5 : 6)] = '\0';
|
2005-07-17 13:55:33 +02:00
|
|
|
new.name = g_string_new(opt_name);
|
|
|
|
new.string_value = g_string_new(opt_value);
|
|
|
|
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-07-01 22:51:46 +02:00
|
|
|
gint i;
|
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-06-28 04:08:56 +02:00
|
|
|
file_load_opt_file("bygfoot_lg_tokens", &lg_tokens);
|
2005-07-01 22:51:46 +02:00
|
|
|
|
|
|
|
for(i=0;i<lg_tokens.list->len;i++)
|
|
|
|
g_array_index(lg_tokens.list, Option, i).value = i;
|
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;
|
|
|
|
}
|
|
|
|
|
2005-06-17 14:57:05 +02:00
|
|
|
main_exit_program(EXIT_NO_SUPPORT_DIR,
|
|
|
|
"file_get_first_support_dir: no primary support dir found.");
|
2005-04-04 12:36:04 +02:00
|
|
|
|
|
|
|
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-07-18 18:18:39 +02:00
|
|
|
|
|
|
|
chdir(dirname);
|
|
|
|
|
|
|
|
if (os_is_unix)
|
2005-07-18 20:09:38 +02:00
|
|
|
sprintf(buf, "%s %s %s",
|
|
|
|
const_str("string_fs_compress_command"),
|
|
|
|
const_str("string_fs_compress_switches"),
|
|
|
|
zipbasename);
|
2005-07-18 18:18:39 +02:00
|
|
|
else
|
2005-07-18 20:09:38 +02:00
|
|
|
sprintf(buf, "\"%s%s%s\" %s %s", pwd, G_DIR_SEPARATOR_S,
|
|
|
|
const_str("string_fs_compress_command"),
|
|
|
|
const_str("string_fs_compress_switches"),
|
|
|
|
zipbasename);
|
2005-04-04 12:36:04 +02:00
|
|
|
|
|
|
|
for(i=0;i<files->len;i++)
|
|
|
|
{
|
|
|
|
strcat(buf, " ");
|
|
|
|
strcat(buf, ((GString*)g_ptr_array_index(files, i))->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_my_system(buf);
|
|
|
|
|
2005-07-18 18:18:39 +02:00
|
|
|
chdir(pwd);
|
|
|
|
|
|
|
|
sprintf(buf, "%s%s%s*", dirname, G_DIR_SEPARATOR_S, basename);
|
|
|
|
file_remove_files(buf);
|
2005-04-04 12:36:04 +02:00
|
|
|
|
|
|
|
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),
|
2005-07-18 18:18:39 +02:00
|
|
|
*basename = g_path_get_basename(filename),
|
|
|
|
*pwd = g_get_current_dir();
|
2005-04-04 12:36:04 +02:00
|
|
|
|
2005-07-18 18:18:39 +02:00
|
|
|
chdir(dirname);
|
|
|
|
|
|
|
|
if (os_is_unix)
|
2005-07-18 20:09:38 +02:00
|
|
|
sprintf(buf, "%s %s %s",
|
|
|
|
const_str("string_fs_uncompress_command"),
|
|
|
|
const_str("string_fs_uncompress_switches"),
|
|
|
|
basename);
|
2005-07-18 18:18:39 +02:00
|
|
|
else
|
2005-07-18 20:09:38 +02:00
|
|
|
sprintf(buf, "\"%s%s%s\" %s %s", pwd, G_DIR_SEPARATOR_S,
|
|
|
|
const_str("string_fs_uncompress_command"),
|
|
|
|
const_str("string_fs_uncompress_switches"),
|
|
|
|
basename);
|
2005-04-04 12:36:04 +02:00
|
|
|
|
|
|
|
file_my_system(buf);
|
|
|
|
|
|
|
|
g_free(dirname);
|
2005-01-09 21:21:22 +01:00
|
|
|
}
|
2005-07-18 18:18:39 +02:00
|
|
|
|
|
|
|
/** Execute the appropriate remove command with 'files'
|
|
|
|
as argument (can be directories or a regexp, too). */
|
|
|
|
void
|
|
|
|
file_remove_files(const gchar *files)
|
|
|
|
{
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
|
|
|
if(os_is_unix)
|
|
|
|
sprintf(buf, "%s %s", const_str("string_fs_remove_file_command"), files);
|
|
|
|
else
|
|
|
|
sprintf(buf, "%s \"%s\"", const_str("string_fs_remove_file_command"), files);
|
|
|
|
|
|
|
|
file_my_system(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Execute the appropriate copy command. */
|
|
|
|
void
|
|
|
|
file_copy_file(const gchar *source_file, const gchar *dest_file)
|
|
|
|
{
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
|
|
|
if(os_is_unix)
|
|
|
|
sprintf(buf, "%s %s %s", const_str("string_fs_copy_file_command"),
|
|
|
|
source_file, dest_file);
|
|
|
|
else
|
|
|
|
sprintf(buf, "%s \"%s\" \"%s\"", const_str("string_fs_copy_file_command"),
|
|
|
|
source_file, dest_file);
|
|
|
|
|
|
|
|
file_my_system(buf);
|
|
|
|
}
|