From c69552ccff1b6921881bea701e89979c89316136 Mon Sep 17 00:00:00 2001 From: BearOso Date: Sat, 7 May 2022 19:29:40 -0500 Subject: [PATCH] More cleaning. --- display.h | 20 +---- fscompat.cpp | 30 +++++++- gtk/src/gtk_file.cpp | 18 ----- gtk/src/gtk_s9xwindow.cpp | 8 +- memmap.cpp | 150 ++++---------------------------------- memmap.h | 7 +- movie.cpp | 1 - msu1.cpp | 5 +- netplay.cpp | 2 +- server.cpp | 6 +- snapshot.cpp | 2 +- 11 files changed, 55 insertions(+), 194 deletions(-) diff --git a/display.h b/display.h index 3a47f04f..b13a3dfd 100644 --- a/display.h +++ b/display.h @@ -9,24 +9,6 @@ #include "snes9x.h" -enum s9x_getdirtype -{ - DEFAULT_DIR = 0, - HOME_DIR, - ROMFILENAME_DIR, - ROM_DIR, - SRAM_DIR, - SNAPSHOT_DIR, - SCREENSHOT_DIR, - SPC_DIR, - CHEAT_DIR, - PATCH_DIR, - BIOS_DIR, - LOG_DIR, - SAT_DIR, - LAST_DIR -}; - void S9xUsage (void); char * S9xParseArgs (char **, int); void S9xParseArgsForCheats (char **, int); @@ -45,9 +27,9 @@ bool8 S9xOpenSnapshotFile (const char *, bool8, STREAM *); void S9xCloseSnapshotFile (STREAM); const char * S9xStringInput (const char *); std::string S9xGetDirectory (enum s9x_getdirtype); -std::string S9xGetFilename (std::string, enum s9x_getdirtype); std::string S9xGetFilenameInc (std::string, enum s9x_getdirtype); std::string S9xBasename (std::string); +std::string S9xBasenameNoExt (std::string); // Routines the port has to implement if it uses command-line diff --git a/fscompat.cpp b/fscompat.cpp index fc6175dc..542c88ce 100644 --- a/fscompat.cpp +++ b/fscompat.cpp @@ -1,7 +1,10 @@ #include -#include "port.h" #include -#include + +#include "display.h" +#include "fscompat.h" +#include "port.h" +#include "memmap.h" using std::string; @@ -19,6 +22,29 @@ std::string makepath(const SplitPath &path) return makepath(path.drive, path.dir, path.stem, path.ext); } +std::string S9xGetFilename(string filename, string ext, enum s9x_getdirtype dirtype) +{ + auto path = splitpath(filename); + auto dir = S9xGetDirectory(dirtype); + return makepath(path.drive, dir, path.stem, ext); +} + +std::string S9xGetFilename(string ext, enum s9x_getdirtype dirtype) +{ + return S9xGetFilename(Memory.ROMFilename, ext, dirtype); +} + +std::string S9xBasename(std::string filename) +{ + auto path = splitpath(filename); + return path.stem + path.ext; +} + +std::string S9xBasenameNoExt(std::string filename) +{ + return splitpath(filename).stem; +} + #if __cplusplus >= 201703L #include namespace fs = std::filesystem; diff --git a/gtk/src/gtk_file.cpp b/gtk/src/gtk_file.cpp index 94c8a17d..eeb805fb 100644 --- a/gtk/src/gtk_file.cpp +++ b/gtk/src/gtk_file.cpp @@ -113,24 +113,6 @@ std::string S9xGetDirectory(enum s9x_getdirtype dirtype) return dirname; } -std::string S9xGetFilename(std::string ex, enum s9x_getdirtype dirtype) -{ - fs::path path(S9xGetDirectory(dirtype)); - path /= fs::path(Memory.ROMFilename).filename(); - path.replace_extension(ex); - return path.string(); -} - -std::string S9xBasename(std::string filename) -{ - return fs::path(filename).filename().string(); -} - -std::string S9xBasenameNoExt(std::string f) -{ - return fs::path(f).stem(); -} - bool8 S9xOpenSnapshotFile(const char *filename, bool8 read_only, STREAM *file) { if (read_only) diff --git a/gtk/src/gtk_s9xwindow.cpp b/gtk/src/gtk_s9xwindow.cpp index 15c8d88d..cce711be 100644 --- a/gtk/src/gtk_s9xwindow.cpp +++ b/gtk/src/gtk_s9xwindow.cpp @@ -903,7 +903,7 @@ const char *markup = _(R"(Information for %s char output[2048]; snprintf(output, 2048, markup, - Memory.ROMFilename, + Memory.ROMFilename.c_str(), Memory.ROMName, Memory.ROMSpeed, ((Memory.ROMSpeed & 0x10) != 0) ? "FastROM" : "SlowROM", @@ -1020,12 +1020,6 @@ void Snes9xWindow::reset_screensaver() XResetScreenSaver(GDK_DISPLAY_XDISPLAY(gdk_display)); } #endif -#ifdef GDK_WINDOWING_WAYLAND - if (GDK_IS_WAYLAND_WINDOW(gdk_window)) - { - // TODO screensaver for wayland - } -#endif config->screensaver_needs_reset = false; } diff --git a/memmap.cpp b/memmap.cpp index da0eb090..955a6f8b 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -49,7 +49,6 @@ #endif static bool8 stopMovie = TRUE; -static char LastRomFilename[PATH_MAX + 1] = ""; // from NSRT static const char *nintendo_licensees[] = @@ -1025,9 +1024,6 @@ void CMemory::Deinit (void) IPPU.TileCached[t] = NULL; } } - - Safe(NULL); - SafeANK(NULL); } // file management and ROM detection @@ -1273,7 +1269,7 @@ uint32 CMemory::FileLoader (uint8 *buffer, const char *filename, uint32 maxsize) return (0); } - strcpy(ROMFilename, filename); + ROMFilename = filename; #else S9xMessage(S9X_ERROR, S9X_ROM_INFO, "This binary was not created with Zip support."); return (0); @@ -1293,7 +1289,7 @@ uint32 CMemory::FileLoader (uint8 *buffer, const char *filename, uint32 maxsize) totalSize = HeaderRemove(size, buffer); - strcpy(ROMFilename, filename); + ROMFilename = filename; #else S9xMessage(S9X_ERROR, S9X_ROM_INFO, "This binary was not created with JMA support."); return (0); @@ -1308,7 +1304,7 @@ uint32 CMemory::FileLoader (uint8 *buffer, const char *filename, uint32 maxsize) if (!fp) return (0); - strcpy(ROMFilename, filename); + ROMFilename = filename; uint32 size = 0; @@ -1336,7 +1332,7 @@ bool8 CMemory::LoadROMMem (const uint8 *source, uint32 sourceSize) if(!source || sourceSize > MAX_ROM_SIZE) return FALSE; - strcpy(ROMFilename,"MemoryROM"); + ROMFilename = "MemoryROM"; do { @@ -1573,12 +1569,6 @@ bool8 CMemory::LoadROMInt (int32 ROMfillSize) } } - if (strncmp(LastRomFilename, ROMFilename, PATH_MAX + 1)) - { - strncpy(LastRomFilename, ROMFilename, PATH_MAX + 1); - LastRomFilename[PATH_MAX] = 0; - } - memset(&SNESGameFixes, 0, sizeof(SNESGameFixes)); SNESGameFixes.SRAMInitialValue = 0x60; @@ -1711,7 +1701,7 @@ bool8 CMemory::LoadMultiCartInt () else return (FALSE); - strcpy(ROMFilename, path.c_str()); + ROMFilename = path; } switch (Multi.cartType) @@ -1736,9 +1726,9 @@ bool8 CMemory::LoadMultiCartInt () } if (Multi.cartSizeA) - strcpy(ROMFilename, Multi.fileNameA); + ROMFilename = Multi.fileNameA; else if (Multi.cartSizeB) - strcpy(ROMFilename, Multi.fileNameB); + ROMFilename = Multi.fileNameB; memset(&SNESGameFixes, 0, sizeof(SNESGameFixes)); SNESGameFixes.SRAMInitialValue = 0x60; @@ -1879,22 +1869,14 @@ bool8 CMemory::LoadSRAM (const char *filename) { FILE *file; int size, len; - char sramName[PATH_MAX + 1]; - - strcpy(sramName, filename); ClearSRAM(); if (Multi.cartType && Multi.sramSizeB) { - char temp[PATH_MAX + 1]; - - strcpy(temp, ROMFilename); - strcpy(ROMFilename, Multi.fileNameB); - size = (1 << (Multi.sramSizeB + 3)) * 128; - file = fopen(S9xGetFilename(".srm", SRAM_DIR).c_str(), "rb"); + file = fopen(S9xGetFilename(Multi.fileNameB, ".srm", SRAM_DIR).c_str(), "rb"); if (file) { len = fread((char *) Multi.sramB, 1, 0x10000, file); @@ -1902,8 +1884,6 @@ bool8 CMemory::LoadSRAM (const char *filename) if (len - size == 512) memmove(Multi.sramB, Multi.sramB + 512, size); } - - strcpy(ROMFilename, temp); } size = SRAMSize ? (1 << (SRAMSize + 3)) * 128 : 0; @@ -1914,7 +1894,7 @@ bool8 CMemory::LoadSRAM (const char *filename) if (size) { - file = fopen(sramName, "rb"); + file = fopen(filename, "rb"); if (file) { len = fread((char *) SRAM, 1, size, file); @@ -1967,29 +1947,19 @@ bool8 CMemory::SaveSRAM (const char *filename) FILE *file; int size; - char sramName[PATH_MAX + 1]; - - strcpy(sramName, filename); if (Multi.cartType && Multi.sramSizeB) { - char name[PATH_MAX + 1], temp[PATH_MAX + 1]; - - strcpy(temp, ROMFilename); - strcpy(ROMFilename, Multi.fileNameB); - strcpy(name, S9xGetFilename(".srm", SRAM_DIR).c_str()); - + std::string name = S9xGetFilename(Multi.fileNameB, ".srm", SRAM_DIR); size = (1 << (Multi.sramSizeB + 3)) * 128; - file = fopen(name, "wb"); + file = fopen(name.c_str(), "wb"); if (file) { if (!fwrite((char *) Multi.sramB, size, 1, file)) printf ("Couldn't write to subcart SRAM file.\n"); fclose(file); } - - strcpy(ROMFilename, temp); } size = SRAMSize ? (1 << (SRAMSize + 3)) * 128 : 0; @@ -2000,7 +1970,7 @@ bool8 CMemory::SaveSRAM (const char *filename) if (size) { - file = fopen(sramName, "wb"); + file = fopen(filename, "wb"); if (file) { if (!fwrite((char *) SRAM, size, 1, file)) @@ -2023,13 +1993,11 @@ bool8 CMemory::SaveMPAK (const char *filename) { FILE *file; int size; - char mempakName[PATH_MAX + 1]; - strcpy(mempakName, filename); size = 0x100000; if (size) { - file = fopen(mempakName, "wb"); + file = fopen(filename, "wb"); if (file) { size_t written; @@ -2053,87 +2021,6 @@ static uint32 caCRC32 (uint8 *array, uint32 size, uint32 crc32) return (~crc32); } -char * CMemory::Safe (const char *s) -{ - static char *safe = NULL; - static int safe_len = 0; - - if (s == NULL) - { - if (safe) - { - free(safe); - safe = NULL; - } - - return (NULL); - } - - int len = strlen(s); - if (!safe || len + 1 > safe_len) - { - if (safe) - free(safe); - - safe_len = len + 1; - safe = (char *) malloc(safe_len); - } - - for (int i = 0; i < len; i++) - { - if (s[i] >= 32 && s[i] < 127) - safe[i] = s[i]; - else - safe[i] = '_'; - } - - safe[len] = 0; - - return (safe); -} - -char * CMemory::SafeANK (const char *s) -{ - static char *safe = NULL; - static int safe_len = 0; - - if (s == NULL) - { - if (safe) - { - free(safe); - safe = NULL; - } - - return (NULL); - } - - int len = strlen(s); - if (!safe || len + 1 > safe_len) - { - if (safe) - free(safe); - - safe_len = len + 1; - safe = (char *) malloc(safe_len); - } - - for (int i = 0; i < len; i++) - { - if (s[i] >= 32 && s[i] < 127) // ASCII - safe [i] = s[i]; - else - if (ROMRegion == 0 && ((uint8) s[i] >= 0xa0 && (uint8) s[i] < 0xe0)) // JIS X 201 - Katakana - safe [i] = s[i]; - else - safe [i] = '_'; - } - - safe [len] = 0; - - return (safe); -} - void CMemory::ParseSNESHeader (uint8 *RomHeader) { bool8 bs = Settings.BS & !Settings.BSXItself; @@ -2563,15 +2450,10 @@ void CMemory::InitROM (void) ApplyROMFixes(); //// Show ROM information - char displayName[ROM_NAME_LEN]; - - strcpy(RawROMName, ROMName); - sprintf(displayName, "%s", SafeANK(ROMName)); - sprintf(ROMName, "%s", Safe(ROMName)); - sprintf(ROMId, "%s", Safe(ROMId)); + ROMId[4] = 0; sprintf(String, "\"%s\" [%s] %s, %s, %s, %s, SRAM:%s, ID:%s, CRC32:%08X", - displayName, isChecksumOK ? "checksum ok" : ((Multi.cartType == 4) ? "no checksum" : "bad checksum"), + ROMName, isChecksumOK ? "checksum ok" : ((Multi.cartType == 4) ? "no checksum" : "bad checksum"), MapType(), Size(), KartContents(), Settings.PAL ? "PAL" : "NTSC", StaticRAMSize(), ROMId, ROMCRC32); S9xMessage(S9X_INFO, S9X_ROM_INFO, String); @@ -4058,8 +3940,6 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r do { snprintf(ips, 8, "ips%d", i); - if (strlen(ips) > _MAX_EXT) - break; if (unzFindExtension(file, ips) != UNZ_OK) break; diff --git a/memmap.h b/memmap.h index aaa46dec..30f4e0ca 100644 --- a/memmap.h +++ b/memmap.h @@ -12,6 +12,8 @@ #define MEMMAP_SHIFT (12) #define MEMMAP_MASK (MEMMAP_BLOCK_SIZE - 1) +#include + struct CMemory { enum @@ -70,9 +72,8 @@ struct CMemory uint8 BlockIsROM[MEMMAP_NUM_BLOCKS]; uint8 ExtendedFormat; - char ROMFilename[PATH_MAX + 1]; + std::string ROMFilename; char ROMName[ROM_NAME_LEN]; - char RawROMName[ROM_NAME_LEN]; char ROMId[5]; int32 CompanyId; uint8 ROMRegion; @@ -120,8 +121,6 @@ struct CMemory bool8 SaveSRTC (void); bool8 SaveMPAK (const char *); - char * Safe (const char *); - char * SafeANK (const char *); void ParseSNESHeader (uint8 *); void InitROM (void); diff --git a/movie.cpp b/movie.cpp index fe41f622..99905fd4 100644 --- a/movie.cpp +++ b/movie.cpp @@ -740,7 +740,6 @@ int S9xMovieCreate (const char *filename, uint8 controllers_mask, uint8 opts, co } Movie.ROMCRC32 = Memory.ROMCRC32; - strncpy(Movie.ROMName, Memory.RawROMName, 23); write_movie_extrarominfo(fd, &Movie); diff --git a/msu1.cpp b/msu1.cpp index 649385ec..71ce88a5 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -112,10 +112,9 @@ static bool AudioOpen() AudioClose(); - char ext[_MAX_EXT]; - snprintf(ext, _MAX_EXT, "-%d.pcm", MSU1.MSU1_CURRENT_TRACK); + std::string extension = "-" + std::to_string(MSU1.MSU1_CURRENT_TRACK) + ".pcm"; - audioStream = S9xMSU1OpenFile(ext); + audioStream = S9xMSU1OpenFile(extension.c_str()); if (audioStream) { if (GETC_STREAM(audioStream) != 'M') diff --git a/netplay.cpp b/netplay.cpp index f166cd0f..a1a986db 100644 --- a/netplay.cpp +++ b/netplay.cpp @@ -647,7 +647,7 @@ bool8 S9xNPGetROMImage (uint32 len) S9xNPSetAction ("Receiving ROM filename..."); uint32 filename_len = len - Memory.CalculatedSize - 5; if (filename_len > PATH_MAX || - !S9xNPGetData (NetPlay.Socket, (uint8 *) Memory.ROMFilename, filename_len)) + !S9xNPGetData (NetPlay.Socket, (uint8 *) Memory.ROMFilename.c_str(), filename_len)) { S9xNPSetError ("Error while receiving ROM filename from server."); S9xNPDisconnect (); diff --git a/server.cpp b/server.cpp index a56ed0d4..6b40f9ae 100644 --- a/server.cpp +++ b/server.cpp @@ -960,7 +960,7 @@ bool8 S9xNPSendROMImageToClient (int c) uint8 header [7 + 1 + 4]; uint8 *ptr = header; int len = sizeof (header) + Memory.CalculatedSize + - strlen (Memory.ROMFilename) + 1; + Memory.ROMFilename.length() + 1; *ptr++ = NP_SERV_MAGIC; *ptr++ = NPServer.Clients [c].SendSequenceNum++; *ptr++ = NP_SERV_ROM_IMAGE; @@ -972,8 +972,8 @@ bool8 S9xNPSendROMImageToClient (int c) if (!S9xNPSSendData (NPServer.Clients [c].Socket, header, sizeof (header)) || !S9xNPSSendData (NPServer.Clients [c].Socket, Memory.ROM, Memory.CalculatedSize) || - !S9xNPSSendData (NPServer.Clients [c].Socket, (uint8 *) Memory.ROMFilename, - strlen (Memory.ROMFilename) + 1)) + !S9xNPSSendData (NPServer.Clients [c].Socket, (uint8 *) Memory.ROMFilename.c_str(), + Memory.ROMFilename.length() + 1)) { S9xNPShutdownClient (c, TRUE); return (FALSE); diff --git a/snapshot.cpp b/snapshot.cpp index 14b02aa6..3175c7d6 100644 --- a/snapshot.cpp +++ b/snapshot.cpp @@ -1175,7 +1175,7 @@ void S9xFreezeToStream (STREAM stream) sprintf(buffer, "%s:%04d\n", SNAPSHOT_MAGIC, SNAPSHOT_VERSION); WRITE_STREAM(buffer, strlen(buffer), stream); - sprintf(buffer, "NAM:%06d:%s%c", (int) strlen(Memory.ROMFilename) + 1, Memory.ROMFilename, 0); + sprintf(buffer, "NAM:%06d:%s%c", (int) Memory.ROMFilename.length() + 1, Memory.ROMFilename.c_str(), 0); WRITE_STREAM(buffer, strlen(buffer) + 1, stream); FreezeStruct(stream, "CPU", &CPU, SnapCPU, COUNT(SnapCPU));