XDG related cleanups #94

* Add migration support for 'vbam.cfg' to 'vbam.ini' on MacOS and Windows.

* Cleanup from XDG Base Dir code.

* Set home to NULL after using free().
This commit is contained in:
Edênis Freindorfer Azevedo 2019-03-14 02:05:02 -03:00 committed by Rafael Kitover
parent 513b0559ce
commit 3fd444da91
4 changed files with 31 additions and 41 deletions

View File

@ -66,62 +66,58 @@ bool FileExists(const char *filename)
// Get user-specific config dir manually. // Get user-specific config dir manually.
// apple: ~/Library/Application Support/ // apple: ~/Library/Application Support/
// windows: %APPDATA% // windows: %APPDATA%\
// unix: ${XDG_CONFIG_HOME:-~/.config} // unix: ${XDG_CONFIG_HOME:-~/.config}/
std::string get_xdg_user_config_home() std::string get_xdg_user_config_home()
{ {
std::string path; std::string path;
#ifdef __APPLE__ #ifdef __APPLE__
std::string home(getenv("HOME")); std::string home(getenv("HOME"));
path = home + "/Library/Application Support/"; path = home + "/Library/Application Support";
#elif _WIN32 #elif _WIN32
std::string app_data(getenv("LOCALAPPDATA")); std::string app_data(getenv("LOCALAPPDATA"));
path = app_data + '\\'; path = app_data;
#else // Unix #else // Unix
char *xdg_var = getenv("XDG_CONFIG_HOME"); char *xdg_var = getenv("XDG_CONFIG_HOME");
if (!xdg_var || !*xdg_var) if (!xdg_var || !*xdg_var)
{ {
std::string xdg_default(getenv("HOME")); std::string xdg_default(getenv("HOME"));
xdg_default += "/.config"; path = xdg_default + "/.config";
path = xdg_default;
} }
else else
{ {
path = xdg_var; path = xdg_var;
} }
path += '/';
#endif #endif
return path; return path + FILE_SEP;
} }
// Get user-specific data dir manually. // Get user-specific data dir manually.
// apple: ~/Library/Application Support/ // apple: ~/Library/Application Support/
// windows: %APPDATA% // windows: %APPDATA%\
// unix: ${XDG_DATA_HOME:-~/.local/share} // unix: ${XDG_DATA_HOME:-~/.local/share}/
std::string get_xdg_user_data_home() std::string get_xdg_user_data_home()
{ {
std::string path; std::string path;
#ifdef __APPLE__ #ifdef __APPLE__
std::string home(getenv("HOME")); std::string home(getenv("HOME"));
path = home + "/Library/Application Support/"; path = home + "/Library/Application Support";
#elif _WIN32 #elif _WIN32
std::string app_data(getenv("LOCALAPPDATA")); std::string app_data(getenv("LOCALAPPDATA"));
path = app_data + '\\'; path = app_data;
#else // Unix #else // Unix
char *xdg_var = getenv("XDG_DATA_HOME"); char *xdg_var = getenv("XDG_DATA_HOME");
if (!xdg_var || !*xdg_var) if (!xdg_var || !*xdg_var)
{ {
std::string xdg_default(getenv("HOME")); std::string xdg_default(getenv("HOME"));
xdg_default += "/.local/share"; path = xdg_default + "/.local/share";
path = xdg_default;
} }
else else
{ {
path = xdg_var; path = xdg_var;
} }
path += '/';
#endif #endif
return path; return path + FILE_SEP;
} }
void utilReadScreenPixels(uint8_t *dest, int w, int h) void utilReadScreenPixels(uint8_t *dest, int w, int h)

View File

@ -4,6 +4,12 @@
#include <string> #include <string>
#include "System.h" #include "System.h"
#ifdef _WIN32
#define FILE_SEP '\\'
#else // MacOS, Unix
#define FILE_SEP '/'
#endif
enum IMAGE_TYPE { IMAGE_UNKNOWN = -1, IMAGE_GBA = 0, IMAGE_GB = 1 }; enum IMAGE_TYPE { IMAGE_UNKNOWN = -1, IMAGE_GBA = 0, IMAGE_GB = 1 };
// save game // save game

View File

@ -644,11 +644,9 @@ const char* FindConfigFile(const char *name)
#ifdef _WIN32 #ifdef _WIN32
#define PATH_SEP ";" #define PATH_SEP ";"
#define FILE_SEP '\\'
#define EXE_NAME "vbam.exe" #define EXE_NAME "vbam.exe"
#else // ! _WIN32 #else // ! _WIN32
#define PATH_SEP ":" #define PATH_SEP ":"
#define FILE_SEP '/'
#define EXE_NAME "vbam" #define EXE_NAME "vbam"
#endif // ! _WIN32 #endif // ! _WIN32
@ -732,7 +730,7 @@ const char* FindConfigFile(const char *name)
void LoadConfigFile() void LoadConfigFile()
{ {
struct stat s; struct stat s;
std::string homeDirTmp = get_xdg_user_config_home() + FILE_SEP + DOT_DIR; std::string homeDirTmp = get_xdg_user_config_home() + DOT_DIR;
homeDir = (char *)homeDirTmp.c_str(); homeDir = (char *)homeDirTmp.c_str();
if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode)) if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode))
mkdir(homeDir, 0755); mkdir(homeDir, 0755);
@ -742,29 +740,18 @@ void LoadConfigFile()
const char* configFile = FindConfigFile("vbam.ini"); const char* configFile = FindConfigFile("vbam.ini");
OpenPreferences(configFile); OpenPreferences(configFile);
} }
if (preferences == NULL)
{
const char* configFile = FindConfigFile("vbam.cfg");
OpenPreferences(configFile);
}
} }
void SaveConfigFile() void SaveConfigFile()
{ {
struct stat s; struct stat s;
std::string homeDirTmp = get_xdg_user_config_home() + FILE_SEP + DOT_DIR; std::string homeDirTmp = get_xdg_user_config_home() + DOT_DIR;
homeDir = (char *)homeDirTmp.c_str(); homeDir = (char *)homeDirTmp.c_str();
if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode)) if (stat(homeDir, &s) == -1 || !S_ISDIR(s.st_mode))
mkdir(homeDir, 0755); mkdir(homeDir, 0755);
const char* configFile = FindConfigFile("vbam.ini"); const char* configFile = FindConfigFile("vbam.ini");
if (configFile == NULL)
{
configFile = FindConfigFile("vbam.cfg");
}
if (configFile != NULL) if (configFile != NULL)
{ {
FILE *f = fopen(configFile, "w"); FILE *f = fopen(configFile, "w");

View File

@ -76,7 +76,7 @@ static void get_config_path(wxPathList& path, bool exists = true)
#if defined(__WXGTK__) #if defined(__WXGTK__)
// XDG spec manual support // XDG spec manual support
// ${XDG_CONFIG_HOME:-$HOME/.config}/`appname` // ${XDG_CONFIG_HOME:-$HOME/.config}/`appname`
wxString old_config = wxString(getenv("HOME")) + "/.vbam"; wxString old_config = wxString(getenv("HOME")) + FILE_SEP + ".vbam";
wxString new_config(get_xdg_user_config_home()); wxString new_config(get_xdg_user_config_home());
if (!wxDirExists(old_config) && wxIsWritable(new_config)) if (!wxDirExists(old_config) && wxIsWritable(new_config))
{ {
@ -88,7 +88,7 @@ static void get_config_path(wxPathList& path, bool exists = true)
} }
else else
{ {
// config is in $HOME/.vbam/vbam.conf // config is in $HOME/.vbam/
add_nonstandard_path(old_config); add_nonstandard_path(old_config);
} }
#endif #endif
@ -120,11 +120,7 @@ const wxString wxvbamApp::GetPluginsDir()
wxString wxvbamApp::GetConfigurationPath() wxString wxvbamApp::GetConfigurationPath()
{ {
#if defined(__WXMSW__) || defined(__APPLE__)
wxString config("vbam.ini"); wxString config("vbam.ini");
#else
wxString config("vbam.conf");
#endif
// first check if config files exists in reverse order // first check if config files exists in reverse order
// (from system paths to more local paths.) // (from system paths to more local paths.)
if (data_path.empty()) { if (data_path.empty()) {
@ -251,16 +247,18 @@ bool wxvbamApp::OnInit()
wxString confname("vbam.ini"); wxString confname("vbam.ini");
wxFileName vbamconf(GetConfigurationPath(), confname); wxFileName vbamconf(GetConfigurationPath(), confname);
// /MIGRATION // /MIGRATION
// migrate from 'vbam.conf' to 'vbam.ini' to manage a single config // migrate from 'vbam.{cfg,conf}' to 'vbam.ini' to manage a single config
// file for all platforms. // file for all platforms.
#if !defined(__WXMSW__) && !defined(__APPLE__) #if !defined(__WXMSW__) && !defined(__APPLE__)
wxString oldConf(GetConfigurationPath() + "/vbam.conf"); wxString oldConf(GetConfigurationPath() + FILE_SEP + "vbam.conf");
wxString newConf(GetConfigurationPath() + "/vbam.ini"); #else
wxString oldConf(GetConfigurationPath() + FILE_SEP + "vbam.cfg");
#endif
wxString newConf(GetConfigurationPath() + FILE_SEP + "vbam.ini");
if (wxFileExists(oldConf)) if (wxFileExists(oldConf))
{ {
wxRenameFile(oldConf, newConf, false); wxRenameFile(oldConf, newConf, false);
} }
#endif
// /END_MIGRATION // /END_MIGRATION
cfg = new wxFileConfig(wxT("vbam"), wxEmptyString, cfg = new wxFileConfig(wxT("vbam"), wxEmptyString,
vbamconf.GetFullPath(), vbamconf.GetFullPath(),
@ -616,7 +614,10 @@ bool wxvbamApp::OnCmdLineParsed(wxCmdLineParser& cl)
wxvbamApp::~wxvbamApp() { wxvbamApp::~wxvbamApp() {
if (home != NULL) if (home != NULL)
{
free(home); free(home);
home = NULL;
}
} }
MainFrame::MainFrame() MainFrame::MainFrame()