GTK+: Use std::string here.

This commit is contained in:
Brandon Wright 2018-11-18 13:08:42 -06:00
parent 870bda8efb
commit a48703796a
5 changed files with 39 additions and 78 deletions

View File

@ -432,9 +432,7 @@ Snes9xCheats::search_database ()
if (result < reason) if (result < reason)
reason = result; reason = result;
char *config_dir = get_config_dir (); filename = get_config_dir () + "/cheats.bml";
filename = std::string (config_dir) + "/cheats.bml";
free (config_dir);
if (!(result = S9xImportCheatsFromDatabase (filename.c_str ()))) if (!(result = S9xImportCheatsFromDatabase (filename.c_str ())))
{ {
refresh_tree_view (); refresh_tree_view ();
@ -444,7 +442,6 @@ Snes9xCheats::search_database ()
if (result < reason) if (result < reason)
reason = result; reason = result;
filename = std::string (DATADIR) + "/cheats.bml"; filename = std::string (DATADIR) + "/cheats.bml";
if (!(result = S9xImportCheatsFromDatabase (filename.c_str ()))) if (!(result = S9xImportCheatsFromDatabase (filename.c_str ())))
{ {

View File

@ -30,62 +30,38 @@ static int directory_exists (const char *directory)
return FALSE; return FALSE;
} }
char *get_config_dir () std::string get_config_dir ()
{ {
char *home_dir = NULL, // Find config directory
*classic_config_dir = NULL, char *env_home = getenv ("HOME");
*xdg_config_dir = NULL, char *env_xdg_config_home = getenv ("XDG_CONFIG_HOME");
*xdg_snes9x_dir = NULL;
/* Find config directory */ if (!env_home && !env_xdg_config_home)
home_dir = getenv ("HOME");
xdg_config_dir = getenv ("XDG_CONFIG_HOME");
if (!home_dir && !xdg_config_dir)
{ {
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); (config += env_home) += "/.config/snes9x";
sprintf (xdg_snes9x_dir, "%s/.config/snes9x", home_dir); (legacy += env_home) += "/.snes9x";
} }
else else
{ config = std::string (env_xdg_config_home) + "/snes9x";
xdg_snes9x_dir = (char *) malloc (strlen (xdg_config_dir) + 9);
sprintf (xdg_snes9x_dir, "%s/snes9x", xdg_config_dir);
}
classic_config_dir = (char *) malloc (strlen (home_dir) + 9); if (directory_exists (legacy.c_str ()) && !directory_exists(config.c_str ()))
sprintf (classic_config_dir, "%s/.snes9x", home_dir); return legacy;
char *config_dir; return config;
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;
} }
char *get_config_file_name () std::string get_config_file_name ()
{ {
char *filename; return get_config_dir () + "/snes9x.conf";
filename = get_config_dir ();
filename = (char *) realloc (filename, strlen (filename) + 16);
strcat (filename, "/snes9x.conf");
return filename;
} }
void S9xParsePortConfig (ConfigFile &conf, int pass) 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 () int Snes9xConfig::save_config_file ()
{ {
char *filename;
char key[PATH_MAX]; char key[PATH_MAX];
char buffer[PATH_MAX]; char buffer[PATH_MAX];
ConfigFile cf; ConfigFile cf;
@ -443,11 +418,9 @@ int Snes9xConfig::save_config_file ()
cf.SetString (key, std::string (buffer)); cf.SetString (key, std::string (buffer));
} }
filename = get_config_file_name ();
cf.SetNiceAlignment (true); cf.SetNiceAlignment (true);
cf.SetShowComments (true); cf.SetShowComments (true);
cf.SaveTo (filename); cf.SaveTo (get_config_file_name ().c_str ());
free (filename);
return 0; return 0;
} }
@ -455,44 +428,40 @@ int Snes9xConfig::save_config_file ()
int Snes9xConfig::load_config_file () int Snes9xConfig::load_config_file ()
{ {
struct stat file_info; struct stat file_info;
char *pathname; std::string path;
ConfigFile cf; ConfigFile cf;
char key[PATH_MAX]; char key[PATH_MAX];
char buffer[PATH_MAX]; char buffer[PATH_MAX];
load_defaults (); 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, fprintf (stderr,
_("Couldn't create config directory: %s\n"), _("Couldn't create config directory: %s\n"),
pathname); path.c_str ());
return -1; return -1;
} }
} }
else else
{ {
chmod (pathname, 0755); chmod (path.c_str (), 0755);
} }
free (pathname); path = get_config_file_name ();
pathname = get_config_file_name (); if (stat (path.c_str (), &file_info))
if (stat (pathname, &file_info))
{ {
save_config_file (); save_config_file ();
} }
if (!cf.LoadFile (pathname)) if (!cf.LoadFile (path.c_str ()))
return -1; return -1;
free (pathname);
std::string none; std::string none;
#define inbool(key, var) var = cf.GetBool (key) #define inbool(key, var) var = cf.GetBool (key)
#define inint(key, var) var = cf.GetInt (key) #define inint(key, var) var = cf.GetInt (key)

View File

@ -9,6 +9,7 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h> #include <X11/extensions/Xrandr.h>
#include <string>
#include "gtk_control.h" #include "gtk_control.h"
#include "filter/snes_ntsc.h" #include "filter/snes_ntsc.h"
@ -164,7 +165,7 @@ class Snes9xConfig
}; };
char *get_config_dir (); std::string get_config_dir ();
char *get_config_file_name (); std::string get_config_file_name ();
#endif /* __GTK_CONFIG_H */ #endif /* __GTK_CONFIG_H */

View File

@ -136,14 +136,11 @@ const char *
S9xGetDirectory (enum s9x_getdirtype dirtype) S9xGetDirectory (enum s9x_getdirtype dirtype)
{ {
static char path[PATH_MAX + 1]; static char path[PATH_MAX + 1];
char *config_dir;
switch (dirtype) switch (dirtype)
{ {
case HOME_DIR: case HOME_DIR:
config_dir = get_config_dir (); sstrncpy (path, get_config_dir ().c_str (), PATH_MAX + 1);
strcpy (path, config_dir);
free (config_dir);
break; break;
case SNAPSHOT_DIR: case SNAPSHOT_DIR:

View File

@ -57,23 +57,20 @@ static void toggled (GtkToggleButton *togglebutton, gpointer user_data)
static void dialog_response (GtkDialog *pdialog, gint response_id, gpointer user_data) static void dialog_response (GtkDialog *pdialog, gint response_id, gpointer user_data)
{ {
std::vector<GLSLParam> * params = (std::vector<GLSLParam> *) user_data; std::vector<GLSLParam> * params = (std::vector<GLSLParam> *) user_data;
char *config_dir;
char *config_file;
switch (response_id) switch (response_id)
{ {
case GTK_RESPONSE_OK: case GTK_RESPONSE_OK:
config_dir = get_config_dir(); {
config_file = new char[strlen (config_dir) + 14]; std::string config_file = get_config_dir() + "/snes9x.glslp";
sprintf(config_file, "%s/snes9x.glslp", config_dir); S9xDisplayGetDriver ()->save (config_file.c_str ());
delete[] config_dir; realpath (config_file.c_str (), gui_config->fragment_shader);
S9xDisplayGetDriver ()->save (config_file);
realpath (config_file, gui_config->fragment_shader);
if (dialog) if (dialog)
gtk_widget_destroy (GTK_WIDGET (dialog)); gtk_widget_destroy (GTK_WIDGET (dialog));
dialog = NULL; dialog = NULL;
break; break;
}
case GTK_RESPONSE_CANCEL: case GTK_RESPONSE_CANCEL:
case GTK_RESPONSE_DELETE_EVENT: case GTK_RESPONSE_DELETE_EVENT: