From 81a7c7b20cfc98a3b2319b403bda55beaca38108 Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Thu, 3 Sep 2015 09:52:28 -0500 Subject: [PATCH] common osd path environment var expansion --- src/osd/osdcore.h | 11 +++++ src/osd/osdmini/minimisc.cpp | 12 +++++ src/osd/sdl/sdlfile.cpp | 86 ++++++++++++++++++++---------------- src/osd/windows/winfile.cpp | 7 ++- src/osd/windows/winutil.cpp | 14 ++++++ 5 files changed, 91 insertions(+), 39 deletions(-) diff --git a/src/osd/osdcore.h b/src/osd/osdcore.h index b7180accac8..0a4f351e5e9 100644 --- a/src/osd/osdcore.h +++ b/src/osd/osdcore.h @@ -945,6 +945,17 @@ void osd_list_network_adapters(void); -----------------------------------------------------------------------------*/ const char *osd_get_volume_name(int idx); +/*----------------------------------------------------------------------------- + osd_subst_env: substitute environment variables with values + + Parameters: + + dst - result pointer + src - source string + +-----------------------------------------------------------------------------*/ +void osd_subst_env(char **dst, const char *src); + /* ----- output management ----- */ // output channels diff --git a/src/osd/osdmini/minimisc.cpp b/src/osd/osdmini/minimisc.cpp index c6bb413b292..2bf613b0b7c 100644 --- a/src/osd/osdmini/minimisc.cpp +++ b/src/osd/osdmini/minimisc.cpp @@ -99,3 +99,15 @@ int osd_setenv(const char *name, const char *value, int overwrite) { return 0; } + +//============================================================ +// osd_subst_env +//============================================================ +int osd_subst_env(char **dst, const char *src) +{ + *dst = (char *)osd_malloc_array(strlen(src) + 1); + if (*dst != nullptr) + strcpy(*dst, src); + + return 0; +} diff --git a/src/osd/sdl/sdlfile.cpp b/src/osd/sdl/sdlfile.cpp index a0a49801a95..9cf18f30b9d 100644 --- a/src/osd/sdl/sdlfile.cpp +++ b/src/osd/sdl/sdlfile.cpp @@ -117,8 +117,7 @@ file_error osd_open(const char *path, UINT32 openflags, osd_file **file, UINT64 #else struct stat64 st; #endif - char *tmpstr, *envstr; - int i, j; + char *tmpstr; file_error filerr = FILERR_NONE; tmpstr = NULL; @@ -169,42 +168,7 @@ file_error osd_open(const char *path, UINT32 openflags, osd_file **file, UINT64 goto error; } - tmpstr = (char *) osd_malloc_array(strlen((*file)->filename)+1); - strcpy(tmpstr, (*file)->filename); - - // does path start with an environment variable? - if (tmpstr[0] == '$') - { - envstr = (char *) osd_malloc_array(strlen(tmpstr)+1); - - strcpy(envstr, tmpstr); - - i = 0; - while (envstr[i] != PATHSEPCH && envstr[i] != 0 && envstr[i] != '.') - { - i++; - } - - envstr[i] = '\0'; - - const char *envval = osd_getenv(&envstr[1]); - if (envval != NULL) - { - j = strlen(envval) + strlen(tmpstr) + 1; - osd_free(tmpstr); - tmpstr = (char *) osd_malloc_array(j); - - // start with the value of $HOME - strcpy(tmpstr, envval); - // replace the null with a path separator again - envstr[i] = PATHSEPCH; - // append it - strcat(tmpstr, &envstr[i]); - } - else - fprintf(stderr, "Warning: osd_open environment variable %s not found.\n", envstr); - osd_free(envstr); - } + osd_subst_env(&tmpstr, (*file)->filename); #if defined(SDLMAME_WIN32) || defined(SDLMAME_OS2) access |= O_BINARY; @@ -597,4 +561,50 @@ const char *osd_get_volume_name(int idx) return "/"; } +//============================================================ +// osd_subst_env +//============================================================ +void osd_subst_env(char **dst, const char *src) +{ + int i, j; + char *envstr; + + osd_free(*dst); + *dst = (char *) osd_malloc_array(strlen(src)+1); + strcpy(*dst, src); + + // start with an environment variable? + if (*dst[0] == '$') + { + + envstr = (char *) osd_malloc_array(strlen(src)+1); + strcpy(envstr, src); + + i = 0; + while (!osd_is_path_separator(envstr[i]) && envstr[i] != 0 && envstr[i] != '.') + { + i++; + } + + envstr[i] = '\0'; + + const char *envval = osd_getenv(&envstr[1]); + if (envval != NULL) + { + j = strlen(envval) + strlen(*dst) + 1; + osd_free(*dst); + *dst = (char *) osd_malloc_array(j); + + // start with the value of $HOME + strcpy(*dst, envval); + // replace the null with a path separator again + envstr[i] = PATHSEPCH; + // append it + strcat(*dst, &envstr[i]); + } + else + fprintf(stderr, "Warning: osd_subst_env variable %s not found.\n", envstr); + osd_free(envstr); + } +} #endif diff --git a/src/osd/windows/winfile.cpp b/src/osd/windows/winfile.cpp index da4b0361591..b83f0e041d9 100644 --- a/src/osd/windows/winfile.cpp +++ b/src/osd/windows/winfile.cpp @@ -20,6 +20,7 @@ // MAMEOS headers #include "strconv.h" #include "winutil.h" +#include "winutf8.h" #include "winfile.h" @@ -45,9 +46,12 @@ extern const char *winfile_ptty_identifier; // osd_open //============================================================ -file_error osd_open(const char *path, UINT32 openflags, osd_file **file, UINT64 *filesize) +file_error osd_open(const char *orig_path, UINT32 openflags, osd_file **file, UINT64 *filesize) { file_error filerr = FILERR_NONE; + char *path = nullptr; + + osd_subst_env(&path, orig_path); // convert path to TCHAR TCHAR *t_path = tstring_from_utf8(path); @@ -153,6 +157,7 @@ error: *file = NULL; } osd_free(t_path); + osd_free(path); return filerr; } diff --git a/src/osd/windows/winutil.cpp b/src/osd/windows/winutil.cpp index 3d32bf92e6d..f7d120e6ea2 100644 --- a/src/osd/windows/winutil.cpp +++ b/src/osd/windows/winutil.cpp @@ -9,9 +9,11 @@ // standard windows headers #define WIN32_LEAN_AND_MEAN #include +#include // MAMEOS headers #include "winutil.h" +#include "strconv.h" //============================================================ // win_error_to_file_error @@ -122,6 +124,18 @@ BOOL win_is_gui_application(void) return is_gui_frontend; } +//============================================================ +// osd_subst_env +//============================================================ +void osd_subst_env(char **dst, const char *src) +{ + TCHAR buffer[MAX_PATH]; + + TCHAR *t_src = tstring_from_utf8(src); + ExpandEnvironmentStrings(t_src, buffer, ARRAY_LENGTH(buffer)); + *dst = utf8_from_tstring(buffer); +} + //------------------------------------------------- // Universal way to get module handle //-------------------------------------------------