From bdd9d97e54edb6fd001d290f516bea3e33e24c6c Mon Sep 17 00:00:00 2001 From: gyboth Date: Wed, 17 Dec 2008 14:39:10 +0000 Subject: [PATCH] Country selection convenience stuff. --- src/file.c | 85 ++++++++++++++++++++++++++++++++- src/file.h | 6 +++ src/language.c | 76 ++++------------------------- src/language.h | 3 -- src/load_save.c | 76 ++--------------------------- src/load_save.h | 6 --- src/misc_callback_func.c | 2 + src/window.c | 10 +++- support_files/bygfoot_constants | 2 +- 9 files changed, 113 insertions(+), 153 deletions(-) diff --git a/src/file.c b/src/file.c index 0181f52e..ad811f6f 100644 --- a/src/file.c +++ b/src/file.c @@ -517,9 +517,16 @@ file_get_country_files(void) 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 @@ -935,3 +942,77 @@ file_get_bygfoot_dir(gchar *dir) 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); +} diff --git a/src/file.h b/src/file.h index 4f22efc0..85406aa8 100644 --- a/src/file.h +++ b/src/file.h @@ -109,4 +109,10 @@ file_get_bygfoot_dir(gchar *dir); 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 diff --git a/src/language.c b/src/language.c index f5efee10..82ef21eb 100644 --- a/src/language.c +++ b/src/language.c @@ -123,60 +123,6 @@ language_get_code_index(const gchar *code) 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;ilen;i++) { - if(((g_str_has_prefix(lang, "en") && - strcmp((gchar*)g_ptr_array_index(codes, i), "C") == 0) || + if(((g_str_has_prefix(lang, "en") && strcmp((gchar*)g_ptr_array_index(codes, i), "C") == 0) || g_str_has_prefix(lang, (gchar*)g_ptr_array_index(codes, i))) && strcmp((gchar*)g_ptr_array_index(defs, i), "NONE") != 0) - for(j=0;jlen;j++) - if(strcmp((gchar*)g_ptr_array_index(country_files, j), - (gchar*)g_ptr_array_index(defs, i)) == 0) + { + for(j=0;jlen;j++) + 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); + g_ptr_array_remove_index(country_files, j); + g_ptr_array_add(country_files, prefdef); break; } - - if(prefdef != NULL) - break; + break; + } } - g_ptr_array_sort_with_data( - country_files, - (GCompareDataFunc)language_compare_country_files, - prefdef); - free_gchar_array(&codes); free_gchar_array(&defs); } diff --git a/src/language.h b/src/language.h index 69208cb5..fcfb2cae 100644 --- a/src/language.h +++ b/src/language.h @@ -39,9 +39,6 @@ language_get_code_index(const gchar *code); void language_set(gint index); -gint -language_compare_country_files(gconstpointer a, gconstpointer b, gpointer data); - void language_pick_country(GPtrArray *country_files); diff --git a/src/load_save.c b/src/load_save.c index 1e9c1f55..499b6567 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -178,7 +178,7 @@ load_save_save_game(const gchar *filename) gui_show_progress(1, _("Done."), PIC_TYPE_SAVE); - load_save_last_save_set(fullname->str); + file_store_text_in_saves("last_save", fullname->str); g_free(prefix); 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(fullname); - basename = load_save_last_save_get(); + basename = file_load_text_from_saves("last_save"); if(basename != NULL) { @@ -343,7 +343,7 @@ load_save_load_game(const gchar* filename, gboolean create_main_window) misc_string_assign(&save_file, fullname); - load_save_last_save_set(fullname); + file_store_text_in_saves("last_save", fullname); gui_show_progress(-1, "", PIC_TYPE_LOAD); @@ -369,76 +369,6 @@ load_save_load_game(const gchar* filename, gboolean create_main_window) 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. */ void diff --git a/src/load_save.h b/src/load_save.h index dbd1f5da..a0757112 100644 --- a/src/load_save.h +++ b/src/load_save.h @@ -34,12 +34,6 @@ load_save_save_game(const gchar* filename); gboolean 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 load_save_autosave(void); diff --git a/src/misc_callback_func.c b/src/misc_callback_func.c index 65b30fff..cfcdfe56 100644 --- a/src/misc_callback_func.c +++ b/src/misc_callback_func.c @@ -25,6 +25,7 @@ #include "callbacks.h" #include "debug.h" +#include "file.h" #include "finance.h" #include "free.h" #include "game.h" @@ -98,6 +99,7 @@ misc_callback_start_game(void) start_new_game(); window_destroy(&window.startup); + file_store_text_in_saves("last_country", country.sid); if(!opt_int("int_opt_calodds")) { diff --git a/src/window.c b/src/window.c index 4cc6f0e6..ab96820d 100644 --- a/src/window.c +++ b/src/window.c @@ -309,6 +309,7 @@ window_show_startup(void) GPtrArray *country_files = NULL; GtkTreeModel *model = NULL; GtkCellRenderer *renderer = NULL; + gchar *last_country = file_load_text_from_saves("last_country"); country_files = file_get_country_files(); @@ -336,7 +337,14 @@ window_show_startup(void) 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); } diff --git a/support_files/bygfoot_constants b/support_files/bygfoot_constants index f710086e..8b7e9149 100644 --- a/support_files/bygfoot_constants +++ b/support_files/bygfoot_constants @@ -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_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_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 # of the player wages a sponsor pays; the actual