Use strcspn instead of strtok for reentrancy.

Also, trim codes so game genie and pro-action replay codes
parse properly.
This commit is contained in:
Brandon Wright 2018-12-29 12:47:15 -06:00
parent 2b0e4b557d
commit ae1477d420
1 changed files with 22 additions and 8 deletions

View File

@ -9,6 +9,17 @@
#include "cheats.h" #include "cheats.h"
#include "bml.h" #include "bml.h"
static inline char *trim (char *string)
{
int start;
int end;
for (start = 0; string[start] && isspace (string[start]); start++) {}
for (end = start; string[end] && !isspace (string[end]); end++) {}
string[end] = '\0';
return &string[start];
}
static inline uint8 S9xGetByteFree (uint32 Address) static inline uint8 S9xGetByteFree (uint32 Address)
{ {
int block = (Address & 0xffffff) >> MEMMAP_SHIFT; int block = (Address & 0xffffff) >> MEMMAP_SHIFT;
@ -394,20 +405,23 @@ SCheat S9xTextToCheat (char *text)
SCheatGroup S9xCreateCheatGroup (const char *name, const char *cheat) SCheatGroup S9xCreateCheatGroup (const char *name, const char *cheat)
{ {
SCheatGroup g; SCheatGroup g;
char *code;
char *code_string = strdup (cheat); char *code_string = strdup (cheat);
char *code_ptr = code_string;
int len;
g.name = strdup (name); g.name = strdup (name);
g.enabled = false; g.enabled = false;
for (code = strtok (code_string, "+"); code; code = strtok (NULL, "+")) for (len = strcspn (code_ptr, "+"); len; len = strcspn (code_ptr, "+"))
{ {
if (code) char *code = code_ptr;
{ code_ptr += len + (code_ptr[len] == '\0' ? 0 : 1);
SCheat c = S9xTextToCheat (code); code[len] = '\0';
if (c.address) code = trim (code);
g.c.push_back (c);
} SCheat c = S9xTextToCheat (code);
if (c.address)
g.c.push_back (c);
} }
delete[] code_string; delete[] code_string;