From 6eedb3f46b1d8d474c15298dbd6ea97e3d614e7f Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Mon, 20 Jan 2025 20:44:16 -0800 Subject: [PATCH] Create save and sram dirs when needed --- src/compat.c | 4 +++- src/compat.h | 8 ++++++++ src/compat_impl.h | 24 ++++++++++++++++++++++++ src/save.c | 4 ++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/compat_impl.h diff --git a/src/compat.c b/src/compat.c index 3247801..c18a7d1 100644 --- a/src/compat.c +++ b/src/compat.c @@ -1,4 +1,6 @@ -#include "compat.h" +#include + +#include "compat_impl.h" #if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__)) diff --git a/src/compat.h b/src/compat.h index 08b1ada..c1a8c24 100644 --- a/src/compat.h +++ b/src/compat.h @@ -15,4 +15,12 @@ ssize_t getline(char **restrict lineptr, size_t *restrict n, #endif + +int mkdir_impl(const char*); + +static inline int mkdir(const char* dir) { + return mkdir_impl(dir); +} + + #endif // NESE_COMPAT_H_ diff --git a/src/compat_impl.h b/src/compat_impl.h new file mode 100644 index 0000000..2021f9c --- /dev/null +++ b/src/compat_impl.h @@ -0,0 +1,24 @@ +#ifndef NESE_COMPAT_IMPL_H_ +#define NESE_COMPAT_IMPL_H_ + + +#ifdef _WIN32 + +#include + +int mkdir_impl(const char* dir) { + return _mkdir(dir); +} + +#else // !_WIN32 + +#include + +int mkdir_impl(const char* dir) { + return mkdir(dir, 0777); +} + +#endif // _WIN32 + + +#endif // NESE_COMPAT_IMPL_H_ diff --git a/src/save.c b/src/save.c index deb09ae..adfb76f 100644 --- a/src/save.c +++ b/src/save.c @@ -5,6 +5,7 @@ #include "file.h" #include "mapper.h" #include "filemap.h" +#include "compat.h" /* SRAM */ @@ -44,6 +45,7 @@ int save_sram(const nes_cart* cart, const char* cart_filename) { cart->mapper->sram(cart->map_data) : NULL; if (sram_size > 0 && NULL != sram) { + mkdir("sram"); char sram_filename[FILENAME_MAX] = {0}; make_sram_filename(sram_filename, FILENAME_MAX - 1, cart_filename); @@ -91,6 +93,8 @@ int load_state(nes* sys, const char* cart_filename) { int save_state(const nes* sys, const char* cart_filename) { int size = -1; + mkdir("save"); + char state_filename[FILENAME_MAX] = {0}; make_state_filename(state_filename, FILENAME_MAX - 1, cart_filename);