From a48703796a9d9e858e717e14bec4010499b1bec2 Mon Sep 17 00:00:00 2001 From: Brandon Wright Date: Sun, 18 Nov 2018 13:08:42 -0600 Subject: [PATCH] GTK+: Use std::string here. --- gtk/src/gtk_cheat.cpp | 5 +- gtk/src/gtk_config.cpp | 89 ++++++++++--------------------- gtk/src/gtk_config.h | 5 +- gtk/src/gtk_file.cpp | 5 +- gtk/src/gtk_shader_parameters.cpp | 13 ++--- 5 files changed, 39 insertions(+), 78 deletions(-) diff --git a/gtk/src/gtk_cheat.cpp b/gtk/src/gtk_cheat.cpp index ba6c93dc..95fe3351 100644 --- a/gtk/src/gtk_cheat.cpp +++ b/gtk/src/gtk_cheat.cpp @@ -432,9 +432,7 @@ Snes9xCheats::search_database () if (result < reason) reason = result; - char *config_dir = get_config_dir (); - filename = std::string (config_dir) + "/cheats.bml"; - free (config_dir); + filename = get_config_dir () + "/cheats.bml"; if (!(result = S9xImportCheatsFromDatabase (filename.c_str ()))) { refresh_tree_view (); @@ -444,7 +442,6 @@ Snes9xCheats::search_database () if (result < reason) reason = result; - filename = std::string (DATADIR) + "/cheats.bml"; if (!(result = S9xImportCheatsFromDatabase (filename.c_str ()))) { diff --git a/gtk/src/gtk_config.cpp b/gtk/src/gtk_config.cpp index 8d4e25cf..bf06de7b 100644 --- a/gtk/src/gtk_config.cpp +++ b/gtk/src/gtk_config.cpp @@ -30,62 +30,38 @@ static int directory_exists (const char *directory) return FALSE; } -char *get_config_dir () +std::string get_config_dir () { - char *home_dir = NULL, - *classic_config_dir = NULL, - *xdg_config_dir = NULL, - *xdg_snes9x_dir = NULL; + // Find config directory + char *env_home = getenv ("HOME"); + char *env_xdg_config_home = getenv ("XDG_CONFIG_HOME"); - /* Find config directory */ - home_dir = getenv ("HOME"); - xdg_config_dir = getenv ("XDG_CONFIG_HOME"); - - if (!home_dir && !xdg_config_dir) + if (!env_home && !env_xdg_config_home) { - return strdup (".snes9x"); + return std::string (".snes9x"); } - if (!xdg_config_dir) + std::string config; + std::string legacy; + + // If XDG_CONFIG_HOME is set, use that, otherwise guess default + if (!env_xdg_config_home) { - xdg_snes9x_dir = (char *) malloc (strlen (home_dir) + 16); - sprintf (xdg_snes9x_dir, "%s/.config/snes9x", home_dir); + (config += env_home) += "/.config/snes9x"; + (legacy += env_home) += "/.snes9x"; } else - { - xdg_snes9x_dir = (char *) malloc (strlen (xdg_config_dir) + 9); - sprintf (xdg_snes9x_dir, "%s/snes9x", xdg_config_dir); - } + config = std::string (env_xdg_config_home) + "/snes9x"; - classic_config_dir = (char *) malloc (strlen (home_dir) + 9); - sprintf (classic_config_dir, "%s/.snes9x", home_dir); + if (directory_exists (legacy.c_str ()) && !directory_exists(config.c_str ())) + return legacy; - char *config_dir; - - if (directory_exists (classic_config_dir) && !directory_exists(xdg_snes9x_dir)) - { - free (xdg_snes9x_dir); - config_dir = classic_config_dir; - } - else - { - free (classic_config_dir); - config_dir = xdg_snes9x_dir; - } - - return config_dir; + return config; } -char *get_config_file_name () +std::string get_config_file_name () { - char *filename; - - filename = get_config_dir (); - - filename = (char *) realloc (filename, strlen (filename) + 16); - strcat (filename, "/snes9x.conf"); - - return filename; + return get_config_dir () + "/snes9x.conf"; } void S9xParsePortConfig (ConfigFile &conf, int pass) @@ -258,7 +234,6 @@ static inline void outbool (ConfigFile &cf, const char *key, bool value, const c int Snes9xConfig::save_config_file () { - char *filename; char key[PATH_MAX]; char buffer[PATH_MAX]; ConfigFile cf; @@ -443,11 +418,9 @@ int Snes9xConfig::save_config_file () cf.SetString (key, std::string (buffer)); } - filename = get_config_file_name (); cf.SetNiceAlignment (true); cf.SetShowComments (true); - cf.SaveTo (filename); - free (filename); + cf.SaveTo (get_config_file_name ().c_str ()); return 0; } @@ -455,44 +428,40 @@ int Snes9xConfig::save_config_file () int Snes9xConfig::load_config_file () { struct stat file_info; - char *pathname; + std::string path; ConfigFile cf; char key[PATH_MAX]; char buffer[PATH_MAX]; load_defaults (); - pathname = get_config_dir (); + path = get_config_dir (); - if (stat (pathname, &file_info)) + if (stat (path.c_str (), &file_info)) { - if (mkdir (pathname, 0755)) + if (mkdir (path.c_str (), 0755)) { fprintf (stderr, _("Couldn't create config directory: %s\n"), - pathname); + path.c_str ()); return -1; } } else { - chmod (pathname, 0755); + chmod (path.c_str (), 0755); } - free (pathname); + path = get_config_file_name (); - pathname = get_config_file_name (); - - if (stat (pathname, &file_info)) + if (stat (path.c_str (), &file_info)) { save_config_file (); } - if (!cf.LoadFile (pathname)) + if (!cf.LoadFile (path.c_str ())) return -1; - free (pathname); - std::string none; #define inbool(key, var) var = cf.GetBool (key) #define inint(key, var) var = cf.GetInt (key) diff --git a/gtk/src/gtk_config.h b/gtk/src/gtk_config.h index 524a01f4..2168cf8f 100644 --- a/gtk/src/gtk_config.h +++ b/gtk/src/gtk_config.h @@ -9,6 +9,7 @@ #include #include +#include #include "gtk_control.h" #include "filter/snes_ntsc.h" @@ -164,7 +165,7 @@ class Snes9xConfig }; -char *get_config_dir (); -char *get_config_file_name (); +std::string get_config_dir (); +std::string get_config_file_name (); #endif /* __GTK_CONFIG_H */ diff --git a/gtk/src/gtk_file.cpp b/gtk/src/gtk_file.cpp index d86376be..156b2b16 100644 --- a/gtk/src/gtk_file.cpp +++ b/gtk/src/gtk_file.cpp @@ -136,14 +136,11 @@ const char * S9xGetDirectory (enum s9x_getdirtype dirtype) { static char path[PATH_MAX + 1]; - char *config_dir; switch (dirtype) { case HOME_DIR: - config_dir = get_config_dir (); - strcpy (path, config_dir); - free (config_dir); + sstrncpy (path, get_config_dir ().c_str (), PATH_MAX + 1); break; case SNAPSHOT_DIR: diff --git a/gtk/src/gtk_shader_parameters.cpp b/gtk/src/gtk_shader_parameters.cpp index fa27bdd4..14fbfbad 100644 --- a/gtk/src/gtk_shader_parameters.cpp +++ b/gtk/src/gtk_shader_parameters.cpp @@ -57,23 +57,20 @@ static void toggled (GtkToggleButton *togglebutton, gpointer user_data) static void dialog_response (GtkDialog *pdialog, gint response_id, gpointer user_data) { std::vector * params = (std::vector *) user_data; - char *config_dir; - char *config_file; switch (response_id) { case GTK_RESPONSE_OK: - config_dir = get_config_dir(); - config_file = new char[strlen (config_dir) + 14]; - sprintf(config_file, "%s/snes9x.glslp", config_dir); - delete[] config_dir; - S9xDisplayGetDriver ()->save (config_file); - realpath (config_file, gui_config->fragment_shader); + { + std::string config_file = get_config_dir() + "/snes9x.glslp"; + S9xDisplayGetDriver ()->save (config_file.c_str ()); + realpath (config_file.c_str (), gui_config->fragment_shader); if (dialog) gtk_widget_destroy (GTK_WIDGET (dialog)); dialog = NULL; break; + } case GTK_RESPONSE_CANCEL: case GTK_RESPONSE_DELETE_EVENT: