diff --git a/src/osd/osdcore.h b/src/osd/osdcore.h index 795b30a5fed..83b52d26571 100644 --- a/src/osd/osdcore.h +++ b/src/osd/osdcore.h @@ -50,6 +50,8 @@ #include "osdcomm.h" + + /*************************************************************************** FILE I/O INTERFACES ***************************************************************************/ @@ -84,18 +86,6 @@ typedef enum _file_error file_error; /* osd_file is an opaque type which represents an open file */ typedef struct _osd_file osd_file; -enum -{ - OSD_FOPEN_READ, - OSD_FOPEN_WRITE, - OSD_FOPEN_RW, - OSD_FOPEN_RW_CREATE -}; - - -struct _mame_file; - - /*----------------------------------------------------------------------------- osd_open: open a new file. @@ -819,46 +809,6 @@ void osd_free_executable(void *ptr, size_t size); -----------------------------------------------------------------------------*/ void osd_break_into_debugger(const char *message); -/*************************************************************************** - FILE I/O INTERFACES -***************************************************************************/ - -/*----------------------------------------------------------------------------- - osd_copyfile: copies a file - - Parameters: - - destfile - path to destination - - srcfile - path to source file - - Return value: - - a file_error describing any error that occurred while copying - the file, or FILERR_NONE if no error occurred ------------------------------------------------------------------------------*/ -file_error osd_copyfile(const char *destfile, const char *srcfile); - - -/*----------------------------------------------------------------------------- - osd_get_temp_filename: given a filename, return a full path with that - filename but in a temporary directory - - Parameters: - - buffer - pointer to buffer - - buffer_len - length of buffer - - basename - basename of file to create - - Return value: - - a file_error describing any error that occurred while copying - the file, or FILERR_NONE if no error occurred ------------------------------------------------------------------------------*/ -file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename); - /*************************************************************************** DIRECTORY INTERFACES @@ -879,69 +829,6 @@ file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *ba -----------------------------------------------------------------------------*/ osd_directory_entry *osd_stat(const char *path); - -/*----------------------------------------------------------------------------- - osd_mkdir: creates a directory - - Parameters: - - dir - path to directory to create - - Return value: - - a file_error describing any error that occurred while creating - the directory, or FILERR_NONE if no error occurred ------------------------------------------------------------------------------*/ -file_error osd_mkdir(const char *dir); - - -/*----------------------------------------------------------------------------- - osd_rmdir: removes a directory - - Parameters: - - dir - path to directory to removes - - Return value: - - a file_error describing any error that occurred while deleting - the directory, or FILERR_NONE if no error occurred ------------------------------------------------------------------------------*/ -file_error osd_rmdir(const char *dir); - - -/*----------------------------------------------------------------------------- - osd_getcurdir: retrieves the current working directory - - Parameters: - - buffer - place to store current working directory - - buffer_len - length of buffer - - Return value: - - a file_error describing any error that occurred while deleting - the directory, or FILERR_NONE if no error occurred ------------------------------------------------------------------------------*/ -file_error osd_getcurdir(char *buffer, size_t buffer_len); - - -/*----------------------------------------------------------------------------- - osd_setcurdir: sets the current working directory - - Parameters: - - dir - path to directory to which to change - - Return value: - - a file_error describing any error that occurred while deleting - the directory, or FILERR_NONE if no error occurred ------------------------------------------------------------------------------*/ -file_error osd_setcurdir(const char *dir); - - /*************************************************************************** PATH INTERFACES ***************************************************************************/ @@ -958,51 +845,6 @@ file_error osd_setcurdir(const char *dir); void osd_get_emulator_directory(char *dir, size_t dir_size); -/*----------------------------------------------------------------------------- - osd_is_path_separator: returns whether a character is a path separator - - Parameters: - - c - the character in question - - Return value: - - non-zero if the character is a path separator, zero otherwise - ------------------------------------------------------------------------------*/ -int osd_is_path_separator(char c); - - -/*----------------------------------------------------------------------------- - osd_dirname: returns the base directory of a file path - - Parameters: - - filename - the path in question - - Return value: - - an allocated path to the directory containing this file - ------------------------------------------------------------------------------*/ -char *osd_dirname(const char *filename); - - -/*----------------------------------------------------------------------------- - osd_basename: returns the file or directory name from a full path - - Parameters: - - filename - the path in question - - Return value: - - a pointer to the base name of the file - ------------------------------------------------------------------------------*/ -char *osd_basename(char *filename); - - /*----------------------------------------------------------------------------- osd_get_full_path: retrieves the full path @@ -1019,25 +861,32 @@ char *osd_basename(char *filename); file_error osd_get_full_path(char **dst, const char *path); - /*************************************************************************** UNCATEGORIZED INTERFACES ***************************************************************************/ +/*----------------------------------------------------------------------------- + osd_get_clipboard_text: retrieves text from the clipboard -/* reads text from the clipboard - the returned string needs to be free()-ed! */ + Return value: + + the returned string needs to be free()-ed! + +-----------------------------------------------------------------------------*/ char *osd_get_clipboard_text(void); +/*----------------------------------------------------------------------------- + osd_get_volume_name: retrieves the volume name + Parameters: -/****************************************************************************** + idx - order number of volume - Device and file browsing + Return value: -******************************************************************************/ - -int osd_num_devices(void); -const char *osd_get_device_name(int idx); + pointer to volume name +-----------------------------------------------------------------------------*/ +const char *osd_get_volume_name(int idx); #endif /* __OSDEPEND_H__ */ diff --git a/src/osd/sdl/sdlfile.c b/src/osd/sdl/sdlfile.c index eecbf316e21..3ecd9b39b09 100644 --- a/src/osd/sdl/sdlfile.c +++ b/src/osd/sdl/sdlfile.c @@ -395,7 +395,7 @@ int osd_get_physical_drive_geometry(const char *filename, UINT32 *cylinders, UIN /* osd_is_path_separator */ /*============================================================ */ -int osd_is_path_separator(char c) +static int osd_is_path_separator(char c) { return (c == '/') || (c == '\\'); } @@ -422,267 +422,3 @@ int osd_is_absolute_path(const char *path) result = FALSE; return result; } - -//============================================================ -// MESS - specific code below .... -//============================================================ - -// these are MESS specific - DO NOT TOUCH!!@! -#ifdef MESS - -//============================================================ -// osd_copyfile -// FIXME: this will not work with blanks in filename ... -//============================================================ - -file_error osd_copyfile(const char *destfile, const char *srcfile) -{ - char command[1024]; - - sprintf(command, "cp %s %s\n", srcfile, destfile); - system(command); - - return FILERR_NONE; -} - -//============================================================ -// osd_stat -//============================================================ - -#ifndef SDLMAME_WIN32 -osd_directory_entry *osd_stat(const char *path) -{ - int err; - osd_directory_entry *result = NULL; - #if defined(SDLMAME_DARWIN) || defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) || defined(SDLMAME_OS2) - struct stat st; - #else - struct stat64 st; - #endif - - #if defined(SDLMAME_DARWIN) || defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) || defined(SDLMAME_OS2) - err = stat(path, &st); - #else - err = stat64(path, &st); - #endif - - if( err == -1) return NULL; - - // create an osd_directory_entry; be sure to make sure that the caller can - // free all resources by just freeing the resulting osd_directory_entry - result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1); - strcpy(((char *) result) + sizeof(*result), path); - result->name = ((char *) result) + sizeof(*result); - result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE; - result->size = (UINT64)st.st_size; - - return result; -} -#endif -#endif - -//============================================================ -// osd_getcurdir -//============================================================ - -file_error osd_getcurdir(char *buffer, size_t buffer_len) -{ - file_error filerr = FILERR_NONE; - - if (getcwd(buffer, buffer_len) == NULL) - { - filerr = FILERR_FAILURE; - } - - if( filerr != FILERR_FAILURE ) - { - if( strcmp( &buffer[strlen(buffer)-1], PATH_SEPARATOR ) != 0 ) - { - strncat( buffer, PATH_SEPARATOR, buffer_len ); - } - } - - return filerr; -} - -//============================================================ -// osd_get_full_path -//============================================================ - -file_error osd_get_full_path(char **dst, const char *path) -{ - file_error err; - char path_buffer[512]; - - err = FILERR_NONE; - - if (getcwd(path_buffer, 511) == NULL) - { - printf("osd_get_full_path: failed!\n"); - err = FILERR_FAILURE; - } - else - { - *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3); - - // if it's already a full path, just pass it through - if (path[0] == '/') - { - strcpy(*dst, path); - } - else - { - sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path); - } - } - - return err; -} - -//============================================================ -// osd_setcurdir -//============================================================ - -file_error osd_setcurdir(const char *dir) -{ - file_error filerr = FILERR_NONE; - - if (chdir(dir) != 0) - { - filerr = FILERR_FAILURE; - } - - return filerr; -} - -//============================================================ -// osd_basename -//============================================================ - -char *osd_basename(char *filename) -{ - char *c; - - // NULL begets NULL - if (!filename) - return NULL; - - // start at the end and return when we hit a slash or colon - for (c = filename + strlen(filename) - 1; c >= filename; c--) - if (*c == '\\' || *c == '/' || *c == ':') - return c + 1; - - // otherwise, return the whole thing - return filename; -} - -//============================================================ -// osd_dirname -//============================================================ - -char *osd_dirname(const char *filename) -{ - char *dirname; - char *c; - - // NULL begets NULL - if (!filename) - return NULL; - - // allocate space for it - dirname = (char*)malloc(strlen(filename) + 1); - if (!dirname) - return NULL; - - // copy in the name - strcpy(dirname, filename); - - // search backward for a slash or a colon - for (c = dirname + strlen(dirname) - 1; c >= dirname; c--) - if (*c == '\\' || *c == '/' || *c == ':') - { - // found it: NULL terminate and return - *(c + 1) = 0; - return dirname; - } - - // otherwise, return an empty string - dirname[0] = 0; - return dirname; -} - -// no idea what these are for, just cheese them -int osd_num_devices(void) -{ - return 1; -} - -const char *osd_get_device_name(int idx) -{ - return "/"; -} - -//============================================================ -// osd_get_temp_filename -//============================================================ - -file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename) -{ - char tempbuf[512]; - - if (!basename) - basename = "tempfile"; - - sprintf(tempbuf, "/tmp/%s", basename); - unlink(tempbuf); - - strncpy(buffer, tempbuf, buffer_len); - - return FILERR_NONE; -} - -//============================================================ -// osd_mkdir -//============================================================ - -file_error osd_mkdir(const char *dir) -{ - file_error filerr = FILERR_NONE; - - #ifdef SDLMAME_WIN32 - if (mkdir(dir) != 0) - #else - if (mkdir(dir, 0700) != 0) - #endif - { - filerr = FILERR_FAILURE; - } - - return filerr; -} - -//============================================================ -// osd_rmdir -//============================================================ - -file_error osd_rmdir(const char *dir) -{ - file_error filerr = FILERR_NONE; - - if (rmdir(dir) != 0) - { - filerr = FILERR_FAILURE; - } - - return filerr; -} - - -//============================================================ -// osd_get_emulator_directory -//============================================================ - -void osd_get_emulator_directory(char *dir, size_t dir_size) -{ - strncpy(dir, sdl_cwd, dir_size); -} diff --git a/src/osd/sdl/sdlos_macosx.c b/src/osd/sdl/sdlos_macosx.c index d0044324da2..f094cf0cd14 100644 --- a/src/osd/sdl/sdlos_macosx.c +++ b/src/osd/sdl/sdlos_macosx.c @@ -305,3 +305,89 @@ char *osd_get_clipboard_text(void) return result; } + +//============================================================ +// osd_stat +//============================================================ + +osd_directory_entry *osd_stat(const char *path) +{ + int err; + osd_directory_entry *result = NULL; + #if defined(SDLMAME_DARWIN) || defined(SDLMAME_NO64BITIO) + struct stat st; + #else + struct stat64 st; + #endif + + #if defined(SDLMAME_DARWIN) || defined(SDLMAME_NO64BITIO) + err = stat(path, &st); + #else + err = stat64(path, &st); + #endif + + if( err == -1) return NULL; + + // create an osd_directory_entry; be sure to make sure that the caller can + // free all resources by just freeing the resulting osd_directory_entry + result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1); + strcpy(((char *) result) + sizeof(*result), path); + result->name = ((char *) result) + sizeof(*result); + result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE; + result->size = (UINT64)st.st_size; + + return result; +} + +//============================================================ +// osd_get_volume_name +//============================================================ + +const char *osd_get_volume_name(int idx) +{ + if (idx!=0) return NULL; + return "/"; +} + +//============================================================ +// osd_get_full_path +//============================================================ + +file_error osd_get_full_path(char **dst, const char *path) +{ + file_error err; + char path_buffer[512]; + + err = FILERR_NONE; + + if (getcwd(path_buffer, 511) == NULL) + { + printf("osd_get_full_path: failed!\n"); + err = FILERR_FAILURE; + } + else + { + *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3); + + // if it's already a full path, just pass it through + if (path[0] == '/') + { + strcpy(*dst, path); + } + else + { + sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path); + } + } + + return err; +} + +//============================================================ +// osd_get_emulator_directory +//============================================================ + +void osd_get_emulator_directory(char *dir, size_t dir_size) +{ + strncpy(dir, sdl_cwd, dir_size); +} diff --git a/src/osd/sdl/sdlos_unix.c b/src/osd/sdl/sdlos_unix.c index cb1d619f492..8d78fee7c8b 100644 --- a/src/osd/sdl/sdlos_unix.c +++ b/src/osd/sdl/sdlos_unix.c @@ -225,3 +225,89 @@ char *osd_get_clipboard_text(void) return result; } #endif + +//============================================================ +// osd_stat +//============================================================ + +osd_directory_entry *osd_stat(const char *path) +{ + int err; + osd_directory_entry *result = NULL; + #if defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) + struct stat st; + #else + struct stat64 st; + #endif + + #if defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) + err = stat(path, &st); + #else + err = stat64(path, &st); + #endif + + if( err == -1) return NULL; + + // create an osd_directory_entry; be sure to make sure that the caller can + // free all resources by just freeing the resulting osd_directory_entry + result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1); + strcpy(((char *) result) + sizeof(*result), path); + result->name = ((char *) result) + sizeof(*result); + result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE; + result->size = (UINT64)st.st_size; + + return result; +} + +//============================================================ +// osd_get_volume_name +//============================================================ + +const char *osd_get_volume_name(int idx) +{ + if (idx!=0) return NULL; + return "/"; +} + +//============================================================ +// osd_get_full_path +//============================================================ + +file_error osd_get_full_path(char **dst, const char *path) +{ + file_error err; + char path_buffer[512]; + + err = FILERR_NONE; + + if (getcwd(path_buffer, 511) == NULL) + { + printf("osd_get_full_path: failed!\n"); + err = FILERR_FAILURE; + } + else + { + *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3); + + // if it's already a full path, just pass it through + if (path[0] == '/') + { + strcpy(*dst, path); + } + else + { + sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path); + } + } + + return err; +} + +//============================================================ +// osd_get_emulator_directory +//============================================================ + +void osd_get_emulator_directory(char *dir, size_t dir_size) +{ + strncpy(dir, sdl_cwd, dir_size); +} diff --git a/src/osd/sdl/sdlos_win32.c b/src/osd/sdl/sdlos_win32.c index 6cad82b5c1d..efe51ffaa1e 100644 --- a/src/osd/sdl/sdlos_win32.c +++ b/src/osd/sdl/sdlos_win32.c @@ -355,3 +355,230 @@ char *osd_get_clipboard_text(void) return result; } + +//============================================================ +// astring_from_utf8 +//============================================================ + +CHAR *astring_from_utf8(const char *utf8string) +{ + WCHAR *wstring; + int char_count; + CHAR *result; + + // convert MAME string (UTF-8) to UTF-16 + char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); + wstring = (WCHAR *)alloca(char_count * sizeof(*wstring)); + MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count); + + // convert UTF-16 to "ANSI code page" string + char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL); + result = (CHAR *)malloc(char_count * sizeof(*result)); + if (result != NULL) + WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL); + + return result; +} + +//============================================================ +// wstring_from_utf8 +//============================================================ + +WCHAR *wstring_from_utf8(const char *utf8string) +{ + int char_count; + WCHAR *result; + + // convert MAME string (UTF-8) to UTF-16 + char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0); + result = (WCHAR *)malloc(char_count * sizeof(*result)); + if (result != NULL) + MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count); + + return result; +} + +//============================================================ +// win_attributes_to_entry_type +//============================================================ + +static osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes) +{ + if (attributes == 0xFFFFFFFF) + return ENTTYPE_NONE; + else if (attributes & FILE_ATTRIBUTE_DIRECTORY) + return ENTTYPE_DIR; + else + return ENTTYPE_FILE; +} + +//============================================================ +// osd_stat +//============================================================ + +osd_directory_entry *osd_stat(const char *path) +{ + osd_directory_entry *result = NULL; + TCHAR *t_path; + HANDLE find = INVALID_HANDLE_VALUE; + WIN32_FIND_DATA find_data; + + // convert the path to TCHARs + t_path = tstring_from_utf8(path); + if (t_path == NULL) + goto done; + + // attempt to find the first file + find = FindFirstFile(t_path, &find_data); + if (find == INVALID_HANDLE_VALUE) + goto done; + + // create an osd_directory_entry; be sure to make sure that the caller can + // free all resources by just freeing the resulting osd_directory_entry + result = (osd_directory_entry *) malloc(sizeof(*result) + strlen(path) + 1); + if (!result) + goto done; + strcpy(((char *) result) + sizeof(*result), path); + result->name = ((char *) result) + sizeof(*result); + result->type = win_attributes_to_entry_type(find_data.dwFileAttributes); + result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32); + +done: + if (t_path) + free(t_path); + return result; +} + +//============================================================ +// osd_get_volume_name +//============================================================ + +const char *osd_get_volume_name(int idx) +{ + static char szBuffer[128]; + const char *p; + + GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer); + + p = szBuffer; + while(idx--) { + p += strlen(p) + 1; + if (!*p) return NULL; + } + + return p; +} + +//============================================================ +// win_error_to_mame_file_error +//============================================================ + +static file_error win_error_to_mame_file_error(DWORD error) +{ + file_error filerr; + + // convert a Windows error to a file_error + switch (error) + { + case ERROR_SUCCESS: + filerr = FILERR_NONE; + break; + + case ERROR_OUTOFMEMORY: + filerr = FILERR_OUT_OF_MEMORY; + break; + + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + filerr = FILERR_NOT_FOUND; + break; + + case ERROR_ACCESS_DENIED: + filerr = FILERR_ACCESS_DENIED; + break; + + case ERROR_SHARING_VIOLATION: + filerr = FILERR_ALREADY_OPEN; + break; + + default: + filerr = FILERR_FAILURE; + break; + } + return filerr; +} + +//============================================================ +// osd_get_full_path +//============================================================ + +file_error osd_get_full_path(char **dst, const char *path) +{ + file_error err; + TCHAR *t_path; + TCHAR buffer[MAX_PATH]; + + // convert the path to TCHARs + t_path = tstring_from_utf8(path); + if (t_path == NULL) + { + err = FILERR_OUT_OF_MEMORY; + goto done; + } + + // cannonicalize the path + if (!GetFullPathName(t_path, ARRAY_LENGTH(buffer), buffer, NULL)) + { + err = win_error_to_mame_file_error(GetLastError()); + goto done; + } + + // convert the result back to UTF-8 + *dst = utf8_from_tstring(buffer); + if (!*dst) + { + err = FILERR_OUT_OF_MEMORY; + goto done; + } + + err = FILERR_NONE; + +done: + if (t_path != NULL) + free(t_path); + return err; +} + +//============================================================ +// win_get_module_file_name_utf8 +//============================================================ + +static DWORD win_get_module_file_name_utf8(HMODULE module, char *filename, DWORD size) +{ + TCHAR t_filename[MAX_PATH]; + char *utf8_filename; + + if (GetModuleFileName(module, t_filename, ARRAY_LENGTH(t_filename)) == 0) + return 0; + + utf8_filename = utf8_from_tstring(t_filename); + if (!utf8_filename) + return 0; + + size = (DWORD) snprintf(filename, size, "%s", utf8_filename); + free(utf8_filename); + return size; +} + +//============================================================ +// osd_get_emulator_directory +//============================================================ + +void osd_get_emulator_directory(char *dir, size_t dir_size) +{ + char *s; + win_get_module_file_name_utf8(NULL, dir, dir_size); + s = strrchr(dir, '\\'); + if (s) + s[1] = '\0'; +} \ No newline at end of file diff --git a/src/osd/windows/winfile.c b/src/osd/windows/winfile.c index 0e5745ec1b7..6e5eee5ee5d 100644 --- a/src/osd/windows/winfile.c +++ b/src/osd/windows/winfile.c @@ -413,48 +413,6 @@ static file_error win_error_to_mame_file_error(DWORD error) return filerr; } - -//============================================================ -// osd_get_temp_filename -//============================================================ - -file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename) -{ - TCHAR tempbuf[MAX_PATH]; - TCHAR *t_filename; - char *u_tempbuf; - - if (!basename) - basename = "tempfile.tmp"; - - GetTempPath(ARRAY_LENGTH(tempbuf), tempbuf); - t_filename = tstring_from_utf8(basename); - _tcscat(tempbuf, t_filename); - free(t_filename); - DeleteFile(tempbuf); - - u_tempbuf = utf8_from_tstring(tempbuf); - snprintf(buffer, buffer_len, "%s", u_tempbuf); - free(u_tempbuf); - - return FILERR_NONE; -} - - -//============================================================ -// osd_copyfile -//============================================================ - -file_error osd_copyfile(const char *destfile, const char *srcfile) -{ - // wrapper for win_copy_file_utf8() - if (!win_copy_file_utf8(srcfile, destfile, TRUE)) - return win_error_to_mame_file_error(GetLastError()); - - return FILERR_NONE; -} - - //============================================================ // osd_stat //============================================================ @@ -502,77 +460,6 @@ done: return result; } - - -//============================================================ -// osd_is_path_separator -//============================================================ - -int osd_is_path_separator(char c) -{ - return (c == '/') || (c == '\\'); -} - - - -//============================================================ -// osd_dirname -//============================================================ - -char *osd_dirname(const char *filename) -{ - char *dirname; - char *c; - - // NULL begets NULL - if (!filename) - return NULL; - - // allocate space for it - dirname = (char*)malloc(strlen(filename) + 1); - if (!dirname) - return NULL; - - // copy in the name - strcpy(dirname, filename); - - // search backward for a slash or a colon - for (c = dirname + strlen(dirname) - 1; c >= dirname; c--) - if (*c == '\\' || *c == '/' || *c == ':') - { - // found it: NULL terminate and return - *(c + 1) = 0; - return dirname; - } - - // otherwise, return an empty string - dirname[0] = 0; - return dirname; -} - - -//============================================================ -// osd_basename -//============================================================ - -char *osd_basename(char *filename) -{ - char *c; - - // NULL begets NULL - if (!filename) - return NULL; - - // start at the end and return when we hit a slash or colon - for (c = filename + strlen(filename) - 1; c >= filename; c--) - if (*c == '\\' || *c == '/' || *c == ':') - return c + 1; - - // otherwise, return the whole thing - return filename; -} - - //============================================================ // osd_get_full_path //============================================================ @@ -614,141 +501,11 @@ done: return err; } - //============================================================ -// osd_mkdir +// osd_get_volume_name //============================================================ -file_error osd_mkdir(const char *dir) -{ - file_error filerr = FILERR_NONE; - - TCHAR *tempstr = tstring_from_utf8(dir); - if (!tempstr) - { - filerr = FILERR_OUT_OF_MEMORY; - goto done; - } - - if (!CreateDirectory(tempstr, NULL)) - { - filerr = win_error_to_mame_file_error(GetLastError()); - goto done; - } - -done: - if (tempstr) - free(tempstr); - return filerr; -} - - -//============================================================ -// osd_rmdir -//============================================================ - -file_error osd_rmdir(const char *dir) -{ - file_error filerr = FILERR_NONE; - - TCHAR *tempstr = tstring_from_utf8(dir); - if (!tempstr) - { - filerr = FILERR_OUT_OF_MEMORY; - goto done; - } - - if (!RemoveDirectory(tempstr)) - { - filerr = win_error_to_mame_file_error(GetLastError()); - goto done; - } - -done: - if (tempstr) - free(tempstr); - return filerr; -} - - -//============================================================ -// osd_getcurdir -//============================================================ - -file_error osd_getcurdir(char *buffer, size_t buffer_len) -{ - file_error filerr = FILERR_NONE; - char *tempstr = NULL; - TCHAR path[_MAX_PATH]; - - if (!GetCurrentDirectory(ARRAY_LENGTH(path), path)) - { - filerr = win_error_to_mame_file_error(GetLastError()); - goto done; - } - - tempstr = utf8_from_tstring(path); - if (!tempstr) - { - filerr = FILERR_OUT_OF_MEMORY; - goto done; - } - snprintf(buffer, buffer_len, "%s", tempstr); - -done: - if (tempstr) - free(tempstr); - return filerr; -} - - -//============================================================ -// osd_setcurdir -//============================================================ - -file_error osd_setcurdir(const char *dir) -{ - file_error filerr = FILERR_NONE; - - TCHAR *tempstr = tstring_from_utf8(dir); - if (!tempstr) - { - filerr = FILERR_OUT_OF_MEMORY; - goto done; - } - - if (!SetCurrentDirectory(tempstr)) - { - filerr = win_error_to_mame_file_error(GetLastError()); - goto done; - } - -done: - if (tempstr) - free(tempstr); - return filerr; -} - - -//============================================================ -// DIRECTORIES -//============================================================ - -int osd_num_devices(void) -{ - char szBuffer[128]; - char *p; - int i; - - GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer); - - i = 0; - for (p = szBuffer; *p; p += (strlen(p) + 1)) - i++; - return i; -} - -const char *osd_get_device_name(int idx) +const char *osd_get_volume_name(int idx) { static char szBuffer[128]; const char *p; @@ -756,12 +513,18 @@ const char *osd_get_device_name(int idx) GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer); p = szBuffer; - while(idx--) + while(idx--) { p += strlen(p) + 1; + if (!*p) return NULL; + } return p; } +//============================================================ +// osd_get_emulator_directory +//============================================================ + void osd_get_emulator_directory(char *dir, size_t dir_size) { char *s;