Browse Source

Add subdir for SRAM saves

master
Nathaniel Walizer 11 months ago
parent
commit
3b1d8ff709
3 changed files with 40 additions and 9 deletions
  1. +33
    -5
      src/file.c
  2. +4
    -2
      src/file.h
  3. +3
    -2
      src/save.c

+ 33
- 5
src/file.c View File

@@ -14,19 +14,45 @@ const char* basename(const char* filename) {
return &slash[1];
}

int replace_extension(char* filename, int max_len,
const char* orig_name, const char* ext) {
int make_filename(char* filename, int max_len,
const char* orig_name,
const char* subdir, const char* ext) {
int status = 0;
int remain = max_len;

const char* orig_base = basename(orig_name);
const char* orig_dot = strrchr(orig_base, '.');

int orig_path_len = orig_base - orig_name;

int orig_base_len = (NULL == orig_dot) ?
strlen(orig_name) :
(orig_dot - orig_name);
strlen(orig_base) :
(orig_dot - orig_base);

// Part 1/4: Leading path
if (0 == status && orig_path_len <= remain) {
strncpy(filename, orig_name, orig_path_len);
remain -= orig_path_len;
filename += orig_path_len;
} else {
status = -1;
}

// Part 2/4: Subdirectory
if (0 == status && NULL != subdir) {
int subdir_len = strlen(subdir);
if ((subdir_len + 1) <= remain) {
strncpy(filename, subdir, subdir_len);
filename += subdir_len;
*filename++ = '/';
remain -= (subdir_len + 1);
} else {
status = -1;
}
}

if (orig_base_len <= remain) {
// Part 3/4: Basename
if (0 == status && orig_base_len <= remain) {
strncpy(filename, orig_name, orig_base_len);
remain -= orig_base_len;
filename += orig_base_len;
@@ -34,12 +60,14 @@ int replace_extension(char* filename, int max_len,
status = -1;
}

// Part 4/4: Extension
if (0 == status && NULL != ext) {
int ext_len = strlen(ext);
if ((ext_len + 1) <= remain) {
*filename++ = '.';
strncpy(filename, ext, ext_len);
remain -= (ext_len + 1);
filename += ext_len;
} else {
status = -1;
}


+ 4
- 2
src/file.h View File

@@ -5,10 +5,12 @@
// Return pointer to filename omitting path
const char* basename(const char* filename);

int replace_extension(char* filename, int max_len,
const char* orig_name, const char* ext);
int make_filename(char* filename, int max_len,
const char* orig_name,
const char* subdir, const char* ext);

int write_file(const char* filename, const void* data, int len);
int read_file(const char* filename, void* data, int len);


#endif // NESE_FILE_H_

+ 3
- 2
src/save.c View File

@@ -5,8 +5,9 @@

static int make_sram_filename(char* sram_filename, int max_len,
const char* cart_filename) {
return replace_extension( sram_filename, max_len,
basename(cart_filename), "sram");
return make_filename( sram_filename, max_len,
basename(cart_filename),
"sram", "sram");
}

int load_sram(nes_cart* cart, const char* cart_filename) {


Loading…
Cancel
Save