Make path shell-style reading part of config reader.

This commit is contained in:
Themaister 2012-09-08 00:31:30 +02:00
parent 6a169513c9
commit bca1694183
6 changed files with 61 additions and 56 deletions

View File

@ -565,16 +565,48 @@ bool config_get_array(config_file_t *conf, const char *key, char *buf, size_t si
{
struct entry_list *list = conf->entries;
while (list)
{
if (strcmp(key, list->key) == 0)
return strlcpy(buf, list->value, size) < size;
list = list->next;
}
return false;
}
bool config_get_path(config_file_t *conf, const char *key, char *buf, size_t size)
{
#if defined(_WIN32) || defined(RARCH_CONSOLE)
return config_get_array(conf, key, buf, size);
#else
struct entry_list *list = conf->entries;
while (list)
{
if (strcmp(key, list->key) == 0)
{
strlcpy(buf, list->value, size);
return true;
const char *value = list->value;
if (*value == '~')
{
const char *home = getenv("HOME");
if (home)
{
size_t src_size = strlcpy(buf, home, size);
if (src_size >= size)
return false;
buf += src_size;
size -= src_size;
value++;
}
}
return strlcpy(buf, value, size) < size;
}
list = list->next;
}
return false;
#endif
}
bool config_get_bool(config_file_t *conf, const char *key, bool *in)

View File

@ -63,6 +63,8 @@ bool config_get_char(config_file_t *conf, const char *entry, char *in);
bool config_get_string(config_file_t *conf, const char *entry, char **in);
// Extracts a string to a preallocated buffer. Avoid memory allocation.
bool config_get_array(config_file_t *conf, const char *entry, char *in, size_t size);
// Extracts a string to a preallocated buffer. Avoid memory allocation. Recognized magic like ~/. Similar to config_get_array() otherwise.
bool config_get_path(config_file_t *conf, const char *entry, char *in, size_t size);
// Extracts a boolean from config. Valid boolean true are "true" and "1". Valid false are "false" and "0". Other values will be treated as an error.
bool config_get_bool(config_file_t *conf, const char *entry, bool *in);

View File

@ -47,16 +47,21 @@
#define CONFIG_GET_STRING_BASE(conf, base, var, key) \
config_get_array(conf, key, base.var, sizeof(base.var))
#define CONFIG_GET_PATH_BASE(conf, base, var, key) \
config_get_path(conf, key, base.var, sizeof(base.var))
#define CONFIG_GET_BOOL(var, key) CONFIG_GET_BOOL_BASE(conf, g_settings, var, key)
#define CONFIG_GET_INT(var, key) CONFIG_GET_INT_BASE(conf, g_settings, var, key)
#define CONFIG_GET_UINT64(var, key) CONFIG_GET_UINT64_BASE(conf, g_settings, var, key)
#define CONFIG_GET_FLOAT(var, key) CONFIG_GET_FLOAT_BASE(conf, g_settings, var, key)
#define CONFIG_GET_STRING(var, key) CONFIG_GET_STRING_BASE(conf, g_settings, var, key)
#define CONFIG_GET_PATH(var, key) CONFIG_GET_PATH_BASE(conf, g_settings, var, key)
#define CONFIG_GET_BOOL_EXTERN(var, key) CONFIG_GET_BOOL_BASE(conf, g_extern, var, key)
#define CONFIG_GET_INT_EXTERN(var, key) CONFIG_GET_INT_BASE(conf, g_extern, var, key)
#define CONFIG_GET_FLOAT_EXTERN(var, key) CONFIG_GET_FLOAT_BASE(conf, g_extern, var, key)
#define CONFIG_GET_STRING_EXTERN(var, key) CONFIG_GET_STRING_BASE(conf, g_extern, var, key)
#define CONFIG_GET_PATH_EXTERN(var, key) CONFIG_GET_PATH_BASE(conf, g_extern, var, key)
#ifdef RARCH_CONSOLE
#define CONFIG_GET_BOOL_CONSOLE(var, key) CONFIG_GET_BOOL_BASE(conf, g_console, var, key)

3
file.h
View File

@ -99,9 +99,6 @@ void fill_pathname_base(char *out_path, const char *in_path, size_t size);
// If in_path is a path without any slashes (relative current directory), out_path will get path ".".
void fill_pathname_basedir(char *out_path, const char *in_path, size_t size);
// Copies string, and attempts to replace magic like ~/, etc with proper paths, like a shell would.
void fill_pathname_shell(char *out_path, const char *in_path, size_t size);
size_t convert_char_to_wchar(wchar_t *out_wchar, const char *in_char, size_t size);
size_t convert_wchar_to_char(char *out_char, const wchar_t *in_wchar, size_t size);

View File

@ -424,27 +424,6 @@ void fill_pathname_basedir(char *out_dir, const char *in_path, size_t size)
}
}
void fill_pathname_shell(char *out_path, const char *in_path, size_t size)
{
#if !defined(_WIN32) && !defined(RARCH_CONSOLE)
if (*in_path == '~')
{
const char *home = getenv("HOME");
if (home)
{
size_t src_size = strlcpy(out_path, home, size);
rarch_assert(src_size < size);
out_path += src_size;
size -= src_size;
in_path++;
}
}
#endif
rarch_assert(strlcpy(out_path, in_path, size) < size);
}
size_t convert_char_to_wchar(wchar_t *out_wchar, const char *in_char, size_t size)
{
return mbstowcs(out_wchar, in_char, size / sizeof(wchar_t));

View File

@ -363,9 +363,9 @@ bool config_load_file(const char *path)
CONFIG_GET_BOOL(video.aspect_ratio_auto, "video_aspect_ratio_auto");
CONFIG_GET_FLOAT(video.refresh_rate, "video_refresh_rate");
CONFIG_GET_STRING(video.cg_shader_path, "video_cg_shader");
CONFIG_GET_STRING(video.bsnes_shader_path, "video_bsnes_shader");
CONFIG_GET_STRING(video.second_pass_shader, "video_second_pass_shader");
CONFIG_GET_PATH(video.cg_shader_path, "video_cg_shader");
CONFIG_GET_PATH(video.bsnes_shader_path, "video_bsnes_shader");
CONFIG_GET_PATH(video.second_pass_shader, "video_second_pass_shader");
CONFIG_GET_BOOL(video.render_to_texture, "video_render_to_texture");
CONFIG_GET_FLOAT(video.fbo_scale_x, "video_fbo_scale_x");
CONFIG_GET_FLOAT(video.fbo_scale_y, "video_fbo_scale_y");
@ -373,7 +373,7 @@ bool config_load_file(const char *path)
CONFIG_GET_BOOL(video.allow_rotate, "video_allow_rotate");
#ifdef HAVE_FREETYPE
CONFIG_GET_STRING(video.font_path, "video_font_path");
CONFIG_GET_PATH(video.font_path, "video_font_path");
CONFIG_GET_INT(video.font_size, "video_font_size");
CONFIG_GET_BOOL(video.font_enable, "video_font_enable");
CONFIG_GET_BOOL(video.font_scale, "video_font_scale");
@ -396,9 +396,9 @@ bool config_load_file(const char *path)
CONFIG_GET_BOOL(video.gpu_screenshot, "video_gpu_screenshot");
#ifdef HAVE_DYLIB
CONFIG_GET_STRING(video.filter_path, "video_filter");
CONFIG_GET_STRING(video.external_driver, "video_external_driver");
CONFIG_GET_STRING(audio.external_driver, "audio_external_driver");
CONFIG_GET_PATH(video.filter_path, "video_filter");
CONFIG_GET_PATH(video.external_driver, "video_external_driver");
CONFIG_GET_PATH(audio.external_driver, "audio_external_driver");
#endif
#if defined(HAVE_CG) || defined(HAVE_XML)
@ -416,8 +416,7 @@ bool config_load_file(const char *path)
#endif
#if defined(HAVE_XML)
if (config_get_array(conf, "video_shader_dir", tmp_str, sizeof(tmp_str)))
fill_pathname_shell(g_settings.video.shader_dir, tmp_str, sizeof(g_settings.video.shader_dir));
CONFIG_GET_PATH(video.shader_dir, "video_shader_dir");
#endif
CONFIG_GET_FLOAT(input.axis_threshold, "input_axis_threshold");
@ -442,15 +441,13 @@ bool config_load_file(const char *path)
CONFIG_GET_STRING(video.driver, "video_driver");
CONFIG_GET_STRING(audio.driver, "audio_driver");
CONFIG_GET_STRING(audio.dsp_plugin, "audio_dsp_plugin");
CONFIG_GET_PATH(audio.dsp_plugin, "audio_dsp_plugin");
CONFIG_GET_STRING(input.driver, "input_driver");
if (!*g_settings.libretro)
CONFIG_GET_STRING(libretro, "libretro_path");
if (config_get_array(conf, "screenshot_directory", tmp_str, sizeof(tmp_str)))
fill_pathname_shell(g_settings.screenshot_directory, tmp_str, sizeof(g_settings.screenshot_directory));
CONFIG_GET_PATH(libretro, "libretro_path");
CONFIG_GET_PATH(screenshot_directory, "screenshot_directory");
if (*g_settings.screenshot_directory && !path_is_directory(g_settings.screenshot_directory))
{
RARCH_WARN("screenshot_directory is not an existing directory, ignoring ...\n");
@ -471,8 +468,8 @@ bool config_load_file(const char *path)
CONFIG_GET_BOOL(pause_nonactive, "pause_nonactive");
CONFIG_GET_INT(autosave_interval, "autosave_interval");
CONFIG_GET_STRING(cheat_database, "cheat_database_path");
CONFIG_GET_STRING(cheat_settings_path, "cheat_settings_path");
CONFIG_GET_PATH(cheat_database, "cheat_database_path");
CONFIG_GET_PATH(cheat_settings_path, "cheat_settings_path");
CONFIG_GET_BOOL(block_sram_overwrite, "block_sram_overwrite");
CONFIG_GET_BOOL(savestate_auto_index, "savestate_auto_index");
@ -494,36 +491,29 @@ bool config_load_file(const char *path)
}
}
if (!g_extern.has_set_save_path && config_get_array(conf, "savefile_directory", tmp_str, sizeof(tmp_str)))
if (!g_extern.has_set_save_path && config_get_path(conf, "savefile_directory", tmp_str, sizeof(tmp_str)))
{
char tmp[PATH_MAX];
fill_pathname_shell(tmp, tmp_str, sizeof(tmp));
if (path_is_directory(tmp))
if (path_is_directory(tmp_str))
{
strlcpy(g_extern.savefile_name_srm, tmp, sizeof(g_extern.savefile_name_srm));
strlcpy(g_extern.savefile_name_srm, tmp_str, sizeof(g_extern.savefile_name_srm));
fill_pathname_dir(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
}
else
RARCH_WARN("savefile_directory is not a directory, ignoring ....\n");
}
if (!g_extern.has_set_state_path && config_get_array(conf, "savestate_directory", tmp_str, sizeof(tmp_str)))
if (!g_extern.has_set_state_path && config_get_path(conf, "savestate_directory", tmp_str, sizeof(tmp_str)))
{
char tmp[PATH_MAX];
fill_pathname_shell(tmp, tmp_str, sizeof(tmp));
if (path_is_directory(tmp))
if (path_is_directory(tmp_str))
{
strlcpy(g_extern.savestate_name, tmp, sizeof(g_extern.savestate_name));
strlcpy(g_extern.savestate_name, tmp_str, sizeof(g_extern.savestate_name));
fill_pathname_dir(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
}
else
RARCH_WARN("savestate_directory is not a directory, ignoring ...\n");
}
if (config_get_array(conf, "system_directory", tmp_str, sizeof(tmp_str)))
fill_pathname_shell(g_settings.system_directory, tmp_str, sizeof(g_settings.system_directory));
CONFIG_GET_PATH(system_directory, "system_directory");
config_read_keybinds_conf(conf);