From 5c3fbf67403400bd6dd30828f0221a0577efb542 Mon Sep 17 00:00:00 2001 From: Brandon Wright Date: Sat, 20 Jun 2020 10:44:11 -0500 Subject: [PATCH] Fix some warnings. Removed sstrncpy function. Changed bindings in GTK port to std::array and fixed packing nonsense. --- gtk/src/gtk_binding.cpp | 3 +-- gtk/src/gtk_binding.h | 2 +- gtk/src/gtk_config.cpp | 22 ++++++++++-------- gtk/src/gtk_config.h | 5 ++-- gtk/src/gtk_control.h | 3 ++- gtk/src/gtk_file.cpp | 46 ++++++++++++++++++------------------- gtk/src/gtk_preferences.cpp | 10 ++++---- gtk/src/gtk_preferences.h | 4 ++-- memmap.cpp | 2 +- movie.cpp | 2 +- port.h | 6 ----- 11 files changed, 52 insertions(+), 53 deletions(-) diff --git a/gtk/src/gtk_binding.cpp b/gtk/src/gtk_binding.cpp index d1efffe4..63b2d153 100644 --- a/gtk/src/gtk_binding.cpp +++ b/gtk/src/gtk_binding.cpp @@ -81,8 +81,7 @@ Binding &Binding::operator=(const Binding &binding) return *this; } -bool -Binding::matches (Binding &binding) +bool Binding::operator==(const Binding &binding) { if ((value & ~BINDING_THRESHOLD_MASK) == (binding.value & ~BINDING_THRESHOLD_MASK)) diff --git a/gtk/src/gtk_binding.h b/gtk/src/gtk_binding.h index c4129bff..a5a2706e 100644 --- a/gtk/src/gtk_binding.h +++ b/gtk/src/gtk_binding.h @@ -38,6 +38,7 @@ class Binding Binding (unsigned int device, unsigned int button, unsigned int threshold); Binding (const Binding &binding); Binding &operator=(const Binding &binding); + bool operator==(const Binding &binding); Binding (GdkEventKey *event); Binding (unsigned int); Binding (); @@ -45,7 +46,6 @@ class Binding void to_string (char *str, bool translate = true); unsigned int hex (); unsigned int base_hex (); - bool matches (Binding &binding); void clear (); bool is_joy (); bool is_key (); diff --git a/gtk/src/gtk_config.cpp b/gtk/src/gtk_config.cpp index 0995f779..7aa31558 100644 --- a/gtk/src/gtk_config.cpp +++ b/gtk/src/gtk_config.cpp @@ -194,8 +194,12 @@ int Snes9xConfig::load_defaults () Settings.TwoClockCycles = 12; #endif - memset (pad, 0, sizeof (JoypadBinding) * NUM_JOYPADS); - memset (shortcut, 0, sizeof (Binding) * NUM_EMU_LINKS); + for (auto &joypad : pad) + { + joypad.data.fill(Binding()); + } + + shortcut.fill(Binding()); return 0; } @@ -399,12 +403,12 @@ int Snes9xConfig::save_config_file () for (int i = 0; i < NUM_JOYPADS; i++) { - Binding *joypad = (Binding *) &pad[i]; + auto &joypad = pad[i]; for (int j = 0; j < NUM_JOYPAD_LINKS; j++) { snprintf (key, PATH_MAX, "Joypad %d::%s", i, b_links[j].snes9x_name); - joypad[j].to_string (buffer, false); + joypad.data[j].to_string (buffer, false); cf.SetString (key, std::string (buffer)); } } @@ -620,13 +624,13 @@ int Snes9xConfig::load_config_file () for (int i = 0; i < NUM_JOYPADS; i++) { - Binding *joypad = (Binding *) &pad[i]; + auto &joypad = pad[i]; for (int j = 0; j < NUM_JOYPAD_LINKS; j++) { snprintf (key, PATH_MAX, "Joypad %d::%s", i, b_links[j].snes9x_name); instr (key, buffer); - joypad[j] = Binding (buffer.c_str ()); + joypad.data[j] = Binding (buffer.c_str ()); } } @@ -714,14 +718,14 @@ void Snes9xConfig::rebind_keys() for (int joypad_i = 0; joypad_i < NUM_JOYPADS; joypad_i++) { - Binding *bin = (Binding *)&pad[joypad_i]; + auto &bin = pad[joypad_i].data; for (int button_i = 0; button_i < NUM_JOYPAD_LINKS; button_i++) { int dupe; for (dupe = button_i + 1; dupe < NUM_JOYPAD_LINKS; dupe++) { - if (bin[button_i].matches(bin[dupe]) && bin[button_i].hex() != 0) + if (bin[button_i] == bin[dupe] && bin[button_i].hex() != 0) break; } if (dupe < NUM_JOYPAD_LINKS || bin[button_i].hex() == 0) @@ -733,7 +737,7 @@ void Snes9xConfig::rebind_keys() bool ismulti = false; for (dupe = button_i - 1; dupe > 0; dupe--) { - if (bin[button_i].matches(bin[dupe])) + if (bin[button_i] == bin[dupe]) { ismulti = true; string += ",Joypad" + std::to_string((joypad_i % 5) + 1) + " "; diff --git a/gtk/src/gtk_config.h b/gtk/src/gtk_config.h index d93b2a59..cef3520d 100644 --- a/gtk/src/gtk_config.h +++ b/gtk/src/gtk_config.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "gtk_control.h" #include "filter/snes_ntsc.h" @@ -110,8 +111,8 @@ class Snes9xConfig std::string last_shader_directory; /* Controls */ - JoypadBinding pad[NUM_JOYPADS]; - Binding shortcut[NUM_EMU_LINKS]; + std::array pad; + std::array shortcut; /* Netplay */ bool netplay_is_server; diff --git a/gtk/src/gtk_control.h b/gtk/src/gtk_control.h index 95ef733b..05e0b18e 100644 --- a/gtk/src/gtk_control.h +++ b/gtk/src/gtk_control.h @@ -8,6 +8,7 @@ #define __GTK_CONTROL_H #include +#include #include "gtk_binding.h" #include "SDL.h" @@ -73,7 +74,7 @@ const int NUM_EMU_LINKS = 62; typedef struct JoypadBinding { - Binding data[NUM_JOYPAD_LINKS]; /* Avoid packing issues */ + std::array data; } JoypadBinding; bool S9xGrabJoysticks (); diff --git a/gtk/src/gtk_file.cpp b/gtk/src/gtk_file.cpp index d458acfd..ed92ccd0 100644 --- a/gtk/src/gtk_file.cpp +++ b/gtk/src/gtk_file.cpp @@ -139,33 +139,33 @@ S9xGetDirectory (enum s9x_getdirtype dirtype) switch (dirtype) { - case HOME_DIR: - sstrncpy (path, get_config_dir ().c_str (), PATH_MAX + 1); - break; + case HOME_DIR: + snprintf(path, PATH_MAX + 1, get_config_dir().c_str()); + break; - case SNAPSHOT_DIR: - sstrncpy (path, gui_config->savestate_directory.c_str (), PATH_MAX + 1); - break; + case SNAPSHOT_DIR: + snprintf(path, PATH_MAX + 1, gui_config->savestate_directory.c_str()); + break; - case PATCH_DIR: - sstrncpy (path, gui_config->patch_directory.c_str (), PATH_MAX + 1); - break; + case PATCH_DIR: + snprintf(path, PATH_MAX + 1, gui_config->patch_directory.c_str()); + break; - case CHEAT_DIR: - sstrncpy (path, gui_config->cheat_directory.c_str (), PATH_MAX + 1); - break; + case CHEAT_DIR: + snprintf(path, PATH_MAX + 1, gui_config->cheat_directory.c_str()); + break; - case SRAM_DIR: - sstrncpy (path, gui_config->sram_directory.c_str (), PATH_MAX + 1); - break; + case SRAM_DIR: + snprintf(path, PATH_MAX + 1, gui_config->sram_directory.c_str()); + break; - case SCREENSHOT_DIR: - case SPC_DIR: - sstrncpy (path, gui_config->export_directory.c_str (), PATH_MAX + 1); - break; + case SCREENSHOT_DIR: + case SPC_DIR: + snprintf(path, PATH_MAX + 1, gui_config->export_directory.c_str()); + break; - default: - path[0] = '\0'; + default: + path[0] = '\0'; } /* Check if directory exists, make it and/or set correct permissions */ @@ -249,7 +249,7 @@ S9xBasenameNoExt (const char *f) ext = strrchr (f, '.'); if (!ext) - sstrncpy (filename, base, PATH_MAX); + snprintf (filename, PATH_MAX, base); else { int len = ext - base; @@ -288,7 +288,7 @@ S9xOpenSnapshotFile (const char *fname, bool8 read_only, STREAM *file) if (*drive || *dir == '/' || (*dir == '.' && (*(dir + 1) == '/'))) { - sstrncpy (filename, fname, PATH_MAX + 1); + snprintf (filename, PATH_MAX + 1, fname); if (!file_exists (filename)) { diff --git a/gtk/src/gtk_preferences.cpp b/gtk/src/gtk_preferences.cpp index 30565c74..09014f96 100644 --- a/gtk/src/gtk_preferences.cpp +++ b/gtk/src/gtk_preferences.cpp @@ -733,8 +733,8 @@ Snes9xPreferences::move_settings_to_dialog () set_spin ("joystick_threshold", config->joystick_threshold); /* Control bindings */ - memcpy (pad, config->pad, (sizeof (JoypadBinding)) * NUM_JOYPADS); - memcpy (shortcut, config->shortcut, (sizeof (Binding)) * NUM_EMU_LINKS); + pad = config->pad; + shortcut = config->shortcut; bindings_to_dialog (0); set_combo ("joypad_to_swap_with", 0); @@ -966,8 +966,8 @@ Snes9xPreferences::get_settings_from_dialog () config->sram_directory = new_sram_directory; } - memcpy (config->pad, pad, (sizeof (JoypadBinding)) * NUM_JOYPADS); - memcpy (config->shortcut, shortcut, (sizeof (Binding)) * NUM_EMU_LINKS); + config->pad = pad; + config->shortcut = shortcut; if (sound_needs_restart) { @@ -1266,7 +1266,7 @@ Snes9xPreferences::store_binding (const char *string, Binding binding) } else { - if (shortcut[i - NUM_JOYPAD_LINKS].matches (binding)) + if (shortcut[i - NUM_JOYPAD_LINKS] == binding) { shortcut[i - NUM_JOYPAD_LINKS].clear (); } diff --git a/gtk/src/gtk_preferences.h b/gtk/src/gtk_preferences.h index 71e619b4..9deee112 100644 --- a/gtk/src/gtk_preferences.h +++ b/gtk/src/gtk_preferences.h @@ -38,8 +38,8 @@ class Snes9xPreferences : public GtkBuilderWindow GtkToggleButton *last_toggled; bool awaiting_key; bool polling_joystick; - JoypadBinding pad[NUM_JOYPADS]; - Binding shortcut[NUM_EMU_LINKS]; + std::array pad; + std::array shortcut; private: void get_settings_from_dialog (); diff --git a/memmap.cpp b/memmap.cpp index 1178e889..bd714e68 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -2531,7 +2531,7 @@ void CMemory::InitROM (void) if (Settings.ForcePAL) Settings.PAL = TRUE; else - if (!Settings.BS && ((ROMRegion >= 2) && (ROMRegion <= 12) || ROMRegion == 18)) // 18 is used by "Tintin in Tibet (Europe) (En,Es,Sv)" + if (!Settings.BS && (((ROMRegion >= 2) && (ROMRegion <= 12)) || ROMRegion == 18)) // 18 is used by "Tintin in Tibet (Europe) (En,Es,Sv)" Settings.PAL = TRUE; else Settings.PAL = FALSE; diff --git a/movie.cpp b/movie.cpp index c0a1afff..fe41f622 100644 --- a/movie.cpp +++ b/movie.cpp @@ -434,7 +434,7 @@ static int read_movie_extrarominfo (FILE *fd, SMovie *movie) ptr += 3; // zero bytes movie->ROMCRC32 = Read32(ptr); - sstrncpy(movie->ROMName, (char *) ptr, 23); + memcpy(movie->ROMName, (char *) ptr, 23); return (SUCCESS); } diff --git a/port.h b/port.h index 73ea7cb1..cb010c92 100644 --- a/port.h +++ b/port.h @@ -151,12 +151,6 @@ void SetInfoDlgColor(unsigned char, unsigned char, unsigned char); #endif // __LIBRETRO__ #endif // __WIN32__ -inline void sstrncpy(char *dst, const char *src, size_t size) -{ - strncpy(dst, src, size - 1); - dst[size - 1] = '\0'; -} - #if defined(__DJGPP) || defined(__WIN32__) #define SLASH_STR "\\" #define SLASH_CHAR '\\'