(libretro) handling dynamic string is easier with cpp

This commit is contained in:
barbudreadmon 2019-11-04 19:07:11 +01:00
parent 2095adbe39
commit 3dc220053c
3 changed files with 29 additions and 25 deletions

View File

@ -245,33 +245,33 @@ static int InpDIPSWInit()
dipswitch_core_option *dip_option = &dipswitch_core_options.back();
// Clean the dipswitch name to creation the core option name (removing space and equal characters)
char option_name[100];
std::string option_name;
// Some dipswitch has no name...
if (bdi.szText)
{
strcpy(option_name, bdi.szText);
option_name = bdi.szText;
}
else // ... so, to not hang, we will generate a name based on the position of the dip (DIPSWITCH 1, DIPSWITCH 2...)
{
sprintf(option_name, "DIPSWITCH %d", (int)dipswitch_core_options.size());
log_cb(RETRO_LOG_WARN, "Error in %sDIPList : The DIPSWITCH '%d' has no name. '%s' name has been generated\n", drvname, dipswitch_core_options.size(), option_name);
option_name = SSTR( "DIPSWITCH " << dipswitch_core_options.size() );
log_cb(RETRO_LOG_WARN, "Error in %sDIPList : The DIPSWITCH '%d' has no name. '%s' name has been generated\n", drvname, dipswitch_core_options.size(), option_name.c_str());
}
strncpy(dip_option->friendly_name, option_name, sizeof(dip_option->friendly_name));
dip_option->friendly_name = option_name;
str_char_replace(option_name, ' ', '_');
str_char_replace(option_name, '=', '_');
std::replace( option_name.begin(), option_name.end(), ' ', '_');
std::replace( option_name.begin(), option_name.end(), '=', '_');
snprintf(dip_option->option_name, sizeof(dip_option->option_name), "fbneo-dipswitch-%s-%s", drvname, option_name);
dip_option->option_name = SSTR( "fbneo-dipswitch-" << drvname << "-" << option_name.c_str() );
// Search for duplicate name, and add number to make them unique in the core-options file
for (int dup_idx = 0, dup_nbr = 1; dup_idx < dipswitch_core_options.size() - 1; dup_idx++) // - 1 to exclude the current one
{
if (strcmp(dip_option->option_name, dipswitch_core_options[dup_idx].option_name) == 0)
if (dip_option->option_name.compare(dipswitch_core_options[dup_idx].option_name) == 0)
{
dup_nbr++;
snprintf(dip_option->option_name, sizeof(dip_option->option_name), "fbneo-dipswitch-%s-%s_%d", drvname, option_name, dup_nbr);
dip_option->option_name = SSTR( "fbneo-dipswitch-" << drvname << "-" << option_name.c_str() << "_" << dup_nbr );
}
}
@ -315,13 +315,13 @@ static int InpDIPSWInit()
BurnDrvGetDIPInfo(&(dip_value->bdi), k + i + 1);
dip_value->pgi = pgi_value;
strncpy(dip_value->friendly_name, dip_value->bdi.szText, sizeof(dip_value->friendly_name));
dip_value->friendly_name = dip_value->bdi.szText;
bool is_default_value = (dip_value->pgi->Input.Constant.nConst & dip_value->bdi.nMask) == (dip_value->bdi.nSetting);
if (is_default_value)
{
snprintf(dip_option->default_value, sizeof(dip_option->default_value), "%s", dip_value->bdi.szText);
dip_option->default_value = dip_value->bdi.szText;
}
values_count++;
@ -366,10 +366,10 @@ static bool apply_dipswitch_from_variables()
dipswitch_core_option *dip_option = &dipswitch_core_options[dip_idx];
// Games which needs a specific bios don't handle alternative bioses very well
if (is_neogeo_game && !allow_neogeo_mode && strcasecmp(dip_option->friendly_name, "BIOS") == 0)
if (is_neogeo_game && !allow_neogeo_mode && dip_option->friendly_name.compare("BIOS") == 0)
continue;
var.key = dip_option->option_name;
var.key = dip_option->option_name.c_str();
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) == false)
continue;
@ -377,7 +377,7 @@ static bool apply_dipswitch_from_variables()
{
dipswitch_core_option_value *dip_value = &(dip_option->values[dip_value_idx]);
if (strcasecmp(var.value, dip_value->friendly_name) != 0)
if (dip_value->friendly_name.compare(var.value) != 0)
continue;
int old_nConst = dip_value->pgi->Input.Constant.nConst;

View File

@ -465,7 +465,7 @@ void evaluate_neogeo_bios_mode(const char* drvname)
// search the BIOS dipswitch
for (int dip_idx = 0; dip_idx < dipswitch_core_options.size(); dip_idx++)
{
if (strcasecmp(dipswitch_core_options[dip_idx].friendly_name, "BIOS") == 0)
if (dipswitch_core_options[dip_idx].friendly_name.compare("BIOS") == 0)
{
is_bios_dipswitch_found = true;
if (dipswitch_core_options[dip_idx].values.size() > 0)
@ -571,17 +571,17 @@ void set_environment()
for (int dip_idx = 0; dip_idx < nbr_dips; dip_idx++)
{
// Filter out the BIOS dipswitch if present while the game needs specific bios
if (!is_neogeo_game || allow_neogeo_mode || strcasecmp(dipswitch_core_options[dip_idx].friendly_name, "BIOS") != 0)
if (!is_neogeo_game || allow_neogeo_mode || dipswitch_core_options[dip_idx].friendly_name.compare("BIOS") != 0)
{
vars[idx_var].key = dipswitch_core_options[dip_idx].option_name;
vars[idx_var].desc = dipswitch_core_options[dip_idx].friendly_name;
vars[idx_var].key = dipswitch_core_options[dip_idx].option_name.c_str();
vars[idx_var].desc = dipswitch_core_options[dip_idx].friendly_name.c_str();
vars[idx_var].info = "Dipswitch setting, setting is specific to the running romset";
for (int dip_value_idx = 0; dip_value_idx < dipswitch_core_options[dip_idx].values.size(); dip_value_idx++)
{
vars[idx_var].values[dip_value_idx].value = dipswitch_core_options[dip_idx].values[dip_value_idx].friendly_name;
vars[idx_var].values[dip_value_idx].value = dipswitch_core_options[dip_idx].values[dip_value_idx].friendly_name.c_str();
}
vars[idx_var].values[dipswitch_core_options[dip_idx].values.size()].value = NULL;
vars[idx_var].default_value = dipswitch_core_options[dip_idx].default_value;
vars[idx_var].default_value = dipswitch_core_options[dip_idx].default_value.c_str();
idx_var++;
}
}

View File

@ -2,9 +2,13 @@
#define __RETRO_COMMON__
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include "burner.h"
#define SSTR( x ) static_cast< std::ostringstream & >(( std::ostringstream() << std::dec << x ) ).str()
#define RETRO_GAME_TYPE_CV 1
#define RETRO_GAME_TYPE_GG 2
#define RETRO_GAME_TYPE_MD 3
@ -60,14 +64,14 @@ struct dipswitch_core_option_value
{
struct GameInp *pgi;
BurnDIPInfo bdi;
char friendly_name[100];
std::string friendly_name;
};
struct dipswitch_core_option
{
char option_name[100];
char friendly_name[100];
char default_value[100];
std::string option_name;
std::string friendly_name;
std::string default_value;
std::vector<dipswitch_core_option_value> values;
};