Merge pull request #553 from h0tw1r3/master

common osd path environment var expansion [Jeffrey Clark]
This commit is contained in:
Miodrag Milanović 2016-01-06 09:51:32 +01:00
commit 6db3ac040c
5 changed files with 91 additions and 39 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -9,9 +9,11 @@
// standard windows headers
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
// 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
//-------------------------------------------------