Country selection convenience stuff.

This commit is contained in:
gyboth 2008-12-17 14:39:10 +00:00
parent 822deda761
commit bdd9d97e54
9 changed files with 113 additions and 153 deletions

View File

@ -517,9 +517,16 @@ file_get_country_files(void)
elem = elem->next; elem = elem->next;
} }
free_gchar_array(&country_files);
return country_files_full_path; free_gchar_array(&country_files);
country_files = g_ptr_array_new();
for(i = country_files_full_path->len - 1; i >= 0; i--)
g_ptr_array_add(country_files, g_strdup(g_ptr_array_index(country_files_full_path, i)));
free_gchar_array(&country_files_full_path);
return country_files;
} }
/** Read the file until the next line that's not a comment or /** Read the file until the next line that's not a comment or
@ -935,3 +942,77 @@ file_get_bygfoot_dir(gchar *dir)
g_free(pwd); g_free(pwd);
} }
/** Store text information in a text file in the saves directory.
*/
void
file_store_text_in_saves(const gchar *filename, const gchar *text)
{
#ifdef DEBUG
printf("file_store_text_in_saves\n");
#endif
gchar buf[SMALL];
const gchar *home = g_get_home_dir();
FILE *fil = NULL;
if(os_is_unix)
sprintf(buf, "%s%s%s%ssaves%s%s", home, G_DIR_SEPARATOR_S,
HOMEDIRNAME, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S,
filename);
else
{
gchar *pwd = g_get_current_dir();
sprintf(buf, "%s%ssaves%s%s", pwd, G_DIR_SEPARATOR_S,
G_DIR_SEPARATOR_S, filename);
g_free(pwd);
}
if(!file_my_fopen(buf, "w", &fil, FALSE))
{
g_warning("file_store_text_in_saves: failed to store '%s' in file '%s'\n", text, buf);
return;
}
fprintf(fil, "%s", text);
fclose(fil);
}
/** Load the text stored in the file in the saves directory. */
gchar*
file_load_text_from_saves(const gchar *filename)
{
#ifdef DEBUG
printf("file_load_text_from_saves\n");
#endif
gchar buf[SMALL];
const gchar *home = g_get_home_dir();
FILE *fil = NULL;
gint i = 0, c;
if(os_is_unix)
sprintf(buf, "%s%s%s%ssaves%s%s", home, G_DIR_SEPARATOR_S,
HOMEDIRNAME, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S,
filename);
else
{
gchar *pwd = g_get_current_dir();
sprintf(buf, "%s%ssaves%s%s", pwd, G_DIR_SEPARATOR_S,
G_DIR_SEPARATOR_S, filename);
g_free(pwd);
}
fil = fopen(buf, "r");
if(fil == NULL)
return NULL;
while ((c = (gchar)fgetc(fil)) != EOF)
buf[i++] = (gchar)c;
buf[i] = 0;
fclose(fil);
return g_strdup(buf);
}

View File

@ -109,4 +109,10 @@ file_get_bygfoot_dir(gchar *dir);
void void
file_load_hints_file(void); file_load_hints_file(void);
gchar*
file_load_text_from_saves(const gchar *filename);
void
file_store_text_in_saves(const gchar *filename, const gchar *text);
#endif #endif

View File

@ -123,60 +123,6 @@ language_get_code_index(const gchar *code)
return return_value; return return_value;
} }
/** Compare country file names based on a preferred one (which
should get moved to the start). */
gint
language_compare_country_files(gconstpointer a, gconstpointer b, gpointer data)
{
#ifdef DEBUG
printf("language_compare_country_files\n");
#endif
gint i, j;
const gchar *prefdef = (const gchar*)data;
const gchar *def1 = *(const gchar**)a;
const gchar *def2 = *(const gchar**)b;
gint len1 = strlen(def1),
len2 = strlen(def2), lenmin = MIN(len1, len2);
gchar alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
gint return_value = 0;
if(strcmp(def1, def2) == 0)
return_value = 0;
else if(prefdef != NULL && strcmp(prefdef, def1) == 0)
return_value = -1;
else if(prefdef != NULL && strcmp(prefdef, def2) == 0)
return_value = 1;
else
{
for(i=0;i<lenmin;i++)
if(def1[i] != def2[i])
break;
if(i == lenmin)
return_value = (len2 < len1);
else
{
for(j=0;j<26;j++)
if(def1[i] == alphabet[j] ||
def2[i] == alphabet[j])
break;
if(j == 26)
{
g_warning("language_compare_country_files: chars %c and %c not comparable",
def1[i], def2[i]);
return_value = 0;
}
else
return_value = (def1[i] == alphabet[j]) ? -1 : 1;
}
}
return return_value;
}
/** Put the country matching the local language to the /** Put the country matching the local language to the
beginning of the array if possible. */ beginning of the array if possible. */
void void
@ -200,27 +146,23 @@ language_pick_country(GPtrArray *country_files)
if(lang != NULL) if(lang != NULL)
for(i=0;i<codes->len;i++) for(i=0;i<codes->len;i++)
{ {
if(((g_str_has_prefix(lang, "en") && if(((g_str_has_prefix(lang, "en") && strcmp((gchar*)g_ptr_array_index(codes, i), "C") == 0) ||
strcmp((gchar*)g_ptr_array_index(codes, i), "C") == 0) ||
g_str_has_prefix(lang, (gchar*)g_ptr_array_index(codes, i))) && g_str_has_prefix(lang, (gchar*)g_ptr_array_index(codes, i))) &&
strcmp((gchar*)g_ptr_array_index(defs, i), "NONE") != 0) strcmp((gchar*)g_ptr_array_index(defs, i), "NONE") != 0)
for(j=0;j<country_files->len;j++) {
if(strcmp((gchar*)g_ptr_array_index(country_files, j), for(j=0;j<country_files->len;j++)
(gchar*)g_ptr_array_index(defs, i)) == 0) if(g_str_has_suffix((gchar*)g_ptr_array_index(country_files, j),
(gchar*)g_ptr_array_index(defs, i)))
{ {
prefdef = g_ptr_array_index(country_files, j); prefdef = g_ptr_array_index(country_files, j);
g_ptr_array_remove_index(country_files, j);
g_ptr_array_add(country_files, prefdef);
break; break;
} }
break;
if(prefdef != NULL) }
break;
} }
g_ptr_array_sort_with_data(
country_files,
(GCompareDataFunc)language_compare_country_files,
prefdef);
free_gchar_array(&codes); free_gchar_array(&codes);
free_gchar_array(&defs); free_gchar_array(&defs);
} }

View File

@ -39,9 +39,6 @@ language_get_code_index(const gchar *code);
void void
language_set(gint index); language_set(gint index);
gint
language_compare_country_files(gconstpointer a, gconstpointer b, gpointer data);
void void
language_pick_country(GPtrArray *country_files); language_pick_country(GPtrArray *country_files);

View File

@ -178,7 +178,7 @@ load_save_save_game(const gchar *filename)
gui_show_progress(1, _("Done."), gui_show_progress(1, _("Done."),
PIC_TYPE_SAVE); PIC_TYPE_SAVE);
load_save_last_save_set(fullname->str); file_store_text_in_saves("last_save", fullname->str);
g_free(prefix); g_free(prefix);
g_string_free(fullname, TRUE); g_string_free(fullname, TRUE);
@ -214,7 +214,7 @@ load_save_load_game(const gchar* filename, gboolean create_main_window)
g_free(prefix); g_free(prefix);
g_free(fullname); g_free(fullname);
basename = load_save_last_save_get(); basename = file_load_text_from_saves("last_save");
if(basename != NULL) if(basename != NULL)
{ {
@ -343,7 +343,7 @@ load_save_load_game(const gchar* filename, gboolean create_main_window)
misc_string_assign(&save_file, fullname); misc_string_assign(&save_file, fullname);
load_save_last_save_set(fullname); file_store_text_in_saves("last_save", fullname);
gui_show_progress(-1, "", gui_show_progress(-1, "",
PIC_TYPE_LOAD); PIC_TYPE_LOAD);
@ -369,76 +369,6 @@ load_save_load_game(const gchar* filename, gboolean create_main_window)
return TRUE; return TRUE;
} }
/** Store the name of the last savegame in the users home dir. */
void
load_save_last_save_set(const gchar *filename)
{
#ifdef DEBUG
printf("load_save_last_save_set\n");
#endif
gchar buf[SMALL];
const gchar *home = g_get_home_dir();
FILE *fil = NULL;
if(os_is_unix)
sprintf(buf, "%s%s%s%ssaves%slast_save", home, G_DIR_SEPARATOR_S,
HOMEDIRNAME, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
else
{
gchar *pwd = g_get_current_dir();
sprintf(buf, "%s%ssaves%slast_save", pwd, G_DIR_SEPARATOR_S,
G_DIR_SEPARATOR_S);
g_free(pwd);
}
if(!file_my_fopen(buf, "w", &fil, FALSE))
return;
fprintf(fil, "%s", filename);
fclose(fil);
}
/** Return the filename of the last savegame. */
gchar*
load_save_last_save_get(void)
{
#ifdef DEBUG
printf("load_save_last_save_get\n");
#endif
#ifdef DEBUG
printf("load_save_last_save_get\n");
#endif
gchar buf[SMALL];
const gchar *home = g_get_home_dir();
FILE *fil = NULL;
gint i = 0, c;
if(os_is_unix)
sprintf(buf, "%s%s%s%ssaves%slast_save", home, G_DIR_SEPARATOR_S,
HOMEDIRNAME, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
else
{
gchar *pwd = g_get_current_dir();
sprintf(buf, "%s%ssaves%slast_save", pwd, G_DIR_SEPARATOR_S,
G_DIR_SEPARATOR_S);
g_free(pwd);
}
if(!file_my_fopen(buf, "r", &fil, FALSE))
return NULL;
while ((c = (gchar)fgetc(fil)) != EOF)
buf[i++] = (gchar)c;
buf[i] = 0;
fclose(fil);
return g_strdup(buf);
}
/** Write an autosave. */ /** Write an autosave. */
void void

View File

@ -34,12 +34,6 @@ load_save_save_game(const gchar* filename);
gboolean gboolean
load_save_load_game(const gchar* filename, gboolean create_main_window); load_save_load_game(const gchar* filename, gboolean create_main_window);
void
load_save_last_save_set(const gchar *filename);
gchar*
load_save_last_save_get(void);
void void
load_save_autosave(void); load_save_autosave(void);

View File

@ -25,6 +25,7 @@
#include "callbacks.h" #include "callbacks.h"
#include "debug.h" #include "debug.h"
#include "file.h"
#include "finance.h" #include "finance.h"
#include "free.h" #include "free.h"
#include "game.h" #include "game.h"
@ -98,6 +99,7 @@ misc_callback_start_game(void)
start_new_game(); start_new_game();
window_destroy(&window.startup); window_destroy(&window.startup);
file_store_text_in_saves("last_country", country.sid);
if(!opt_int("int_opt_calodds")) if(!opt_int("int_opt_calodds"))
{ {

View File

@ -309,6 +309,7 @@ window_show_startup(void)
GPtrArray *country_files = NULL; GPtrArray *country_files = NULL;
GtkTreeModel *model = NULL; GtkTreeModel *model = NULL;
GtkCellRenderer *renderer = NULL; GtkCellRenderer *renderer = NULL;
gchar *last_country = file_load_text_from_saves("last_country");
country_files = file_get_country_files(); country_files = file_get_country_files();
@ -336,7 +337,14 @@ window_show_startup(void)
g_object_unref(model); g_object_unref(model);
gtk_combo_box_set_active(GTK_COMBO_BOX(combo_country), 0); if(last_country != NULL)
{
misc_callback_show_team_list(combo_country, last_country);
g_free(last_country);
}
else
misc_callback_show_team_list(combo_country, (const gchar*)g_ptr_array_index(country_files, country_files->len - 1));
//gtk_combo_box_set_active(GTK_COMBO_BOX(combo_country), country_index);
free_gchar_array(&country_files); free_gchar_array(&country_files);
} }

View File

@ -770,7 +770,7 @@ float_name_random_list_prob 20000
string_language_names English Deutsch Français Spanish Nederlands Polski Svenska Danish Romanian Bulgarian Chinese Italian string_language_names English Deutsch Français Spanish Nederlands Polski Svenska Danish Romanian Bulgarian Chinese Italian
string_language_codes C de fr es nl pl sv da ro bg zh it string_language_codes C de fr es nl pl sv da ro bg zh it
string_language_symbols flag_england.png flag_germany.png flag_france.png flag_spain.png flag_netherlands.png flag_poland.png flag_sweden.png flag_dk.png flag_romania.png flag_bulgaria.png flag_china.png flag_italy.png string_language_symbols flag_england.png flag_germany.png flag_france.png flag_spain.png flag_netherlands.png flag_poland.png flag_sweden.png flag_dk.png flag_romania.png flag_bulgaria.png flag_china.png flag_italy.png
string_language_defs country_england.xml country_germany.xml country_france.xml country_spain.xml country_netherlands.xml country_poland.xml country_sweden.xml NONE country_romania.xml country_bulgaria.xml NONE country_italy.xml string_language_defs country_england.xml country_germany.xml country_france.xml country_spain.xml country_netherlands.xml country_poland.xml country_sweden.xml country_denmark.xml country_romania.xml country_bulgaria.xml NONE country_italy.xml
# lower and upper limits of which percentage # lower and upper limits of which percentage
# of the player wages a sponsor pays; the actual # of the player wages a sponsor pays; the actual