Feature request; allow loading prefs.bin relatively

This commit is contained in:
Lior Halphon 2020-03-28 22:56:19 +03:00
parent 9f3bffd4dd
commit d75b7c0023
3 changed files with 23 additions and 5 deletions

View File

@ -11,6 +11,7 @@
#ifndef _WIN32 #ifndef _WIN32
#define AUDIO_FREQUENCY 96000 #define AUDIO_FREQUENCY 96000
#include <unistd.h>
#else #else
#include <Windows.h> #include <Windows.h>
/* Windows (well, at least my VM) can't handle 96KHz sound well :( */ /* Windows (well, at least my VM) can't handle 96KHz sound well :( */
@ -686,9 +687,12 @@ int main(int argc, char **argv)
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
char *prefs_dir = SDL_GetPrefPath("", "SameBoy"); strcpy(prefs_path, resource_path("prefs.bin"));
snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir); if (access(prefs_path, R_OK | W_OK) != 0) {
SDL_free(prefs_dir); char *prefs_dir = SDL_GetPrefPath("", "SameBoy");
snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir);
SDL_free(prefs_dir);
}
FILE *prefs_file = fopen(prefs_path, "rb"); FILE *prefs_file = fopen(prefs_path, "rb");
if (prefs_file) { if (prefs_file) {

View File

@ -2,6 +2,10 @@
#include_next <stdio.h> #include_next <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int access(const char *filename, int mode);
#define R_OK 2
#define W_OK 4
#ifndef __MINGW32__ #ifndef __MINGW32__
#ifndef __LIBRETRO__ #ifndef __LIBRETRO__
static inline int vasprintf(char **str, const char *fmt, va_list args) static inline int vasprintf(char **str, const char *fmt, va_list args)
@ -72,4 +76,4 @@ static inline size_t getline(char **lineptr, size_t *n, FILE *stream) {
return p - bufptr - 1; return p - bufptr - 1;
} }
#define snprintf _snprintf #define snprintf _snprintf

View File

@ -1,6 +1,7 @@
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <winnls.h> #include <winnls.h>
#include <io.h>
FILE *fopen(const char *filename, const char *mode) FILE *fopen(const char *filename, const char *mode)
{ {
@ -11,4 +12,13 @@ FILE *fopen(const char *filename, const char *mode)
MultiByteToWideChar(CP_UTF8, 0, mode, -1, w_mode, sizeof(w_mode) / sizeof(w_mode[0])); MultiByteToWideChar(CP_UTF8, 0, mode, -1, w_mode, sizeof(w_mode) / sizeof(w_mode[0]));
return _wfopen(w_filename, w_mode); return _wfopen(w_filename, w_mode);
} }
int access(const char *filename, int mode)
{
wchar_t w_filename[MAX_PATH] = {0,};
MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename) / sizeof(w_filename[0]));
return _waccess(w_filename, mode);
}