Move Newmenu_allowed_chars into individual newmenu_item

Allowed characters are more properly scoped to a specific text entry
field.  Move them there to eliminate a global.
This commit is contained in:
Kp 2020-12-27 22:03:09 +00:00
parent 40501fa31e
commit e6875641c9
4 changed files with 19 additions and 23 deletions

View file

@ -66,6 +66,7 @@ class newmenu_item
{
struct input_common_type
{
const char *allowed_chars;
int text_len;
/* Only used by imenu, but placing it in imenu_specific_type
* makes newmenu_item non-POD. Some users expect newmenu_item
@ -154,8 +155,6 @@ public:
ntstring<NM_MAX_TEXT_LEN> saved_text;
};
extern const char *Newmenu_allowed_chars;
enum class tab_processing_flag : uint8_t
{
ignore,
@ -478,23 +477,25 @@ static inline newmenu_item nm_item_menu(const char *text)
}
__attribute_nonnull()
static inline void nm_set_item_input(newmenu_item &ni, unsigned len, char *text)
static inline void nm_set_item_input(newmenu_item &ni, unsigned len, char *text, const char *const allowed_chars)
{
ni.type = NM_TYPE_INPUT;
ni.text = text;
ni.input().text_len = len - 1;
auto &i = ni.input();
i.text_len = len - 1;
i.allowed_chars = allowed_chars;
}
template <std::size_t len>
static inline void nm_set_item_input(newmenu_item &ni, char (&text)[len])
static inline void nm_set_item_input(newmenu_item &ni, char (&text)[len], const char *const allowed_chars = nullptr)
{
nm_set_item_input(ni, len, text);
nm_set_item_input(ni, len, text, allowed_chars);
}
template <std::size_t len>
static inline void nm_set_item_input(newmenu_item &ni, std::array<char, len> &text)
static inline void nm_set_item_input(newmenu_item &ni, std::array<char, len> &text, const char *const allowed_chars = nullptr)
{
nm_set_item_input(ni, len, text.data());
nm_set_item_input(ni, len, text.data(), allowed_chars);
}
template <typename... T>

View file

@ -413,12 +413,10 @@ static int MakeNewPlayerFile(int allow_abort)
try_again:
{
std::array<newmenu_item, 1> m{{
nm_item_input(text.buffer()),
nm_item_input(text.buffer(), playername_allowed_chars),
}};
Newmenu_allowed_chars = playername_allowed_chars;
x = newmenu_do2(menu_title{nullptr}, menu_subtitle{TXT_ENTER_PILOT_NAME}, m, unused_newmenu_subfunction, unused_newmenu_userdata);
}
Newmenu_allowed_chars = NULL;
if ( x < 0 ) {
if ( allow_abort ) return 0;

View file

@ -4107,23 +4107,21 @@ void newdemo_stop_recording()
try_again:
;
Newmenu_allowed_chars = demoname_allowed_chars;
if (guess_demo_name(filename))
{
}
else if (!nd_record_v_no_space) {
std::array<newmenu_item, 1> m{{
nm_item_input(filename),
nm_item_input(filename, demoname_allowed_chars),
}};
exit = newmenu_do2(menu_title{nullptr}, menu_subtitle{TXT_SAVE_DEMO_AS}, m, unused_newmenu_subfunction, unused_newmenu_userdata);
} else if (nd_record_v_no_space == 2) {
std::array<newmenu_item, 2> m{{
nm_item_text(TXT_DEMO_SAVE_NOSPACE),
nm_item_input(filename),
nm_item_input(filename, demoname_allowed_chars),
}};
exit = newmenu_do2(menu_title{nullptr}, menu_subtitle{nullptr}, m, unused_newmenu_subfunction, unused_newmenu_userdata);
}
Newmenu_allowed_chars = NULL;
if (exit == -2) { // got bumped out from network menu
char save_file[PATH_MAX];

View file

@ -87,8 +87,6 @@ constexpr std::integral_constant<unsigned, NM_TYPE_SLIDER> newmenu_item::slider_
namespace dcx {
const char *Newmenu_allowed_chars;
int passive_newmenu::subfunction_handler(const d_event &)
{
return 0;
@ -503,9 +501,9 @@ static void draw_item(grs_canvas &canvas, const grs_font &cv_font, newmenu_item
}
//returns true if char is allowed
static bool char_disallowed(char c)
static bool char_disallowed(char c, const char *const allowed_chars)
{
const char *p = Newmenu_allowed_chars;
const char *p = allowed_chars;
if (!p)
return false;
for (uint8_t a, b; (a = p[0]) && (b = p[1]); p += 2)
@ -516,9 +514,9 @@ static bool char_disallowed(char c)
return true;
}
static bool char_allowed(char c)
static bool char_allowed(char c, const char *const allowed_chars)
{
return !char_disallowed(c);
return !char_disallowed(c, allowed_chars);
}
static void strip_end_whitespace( char * text )
@ -1154,7 +1152,7 @@ static window_event_result newmenu_key_command(const d_event &event, newmenu *co
changed = 1;
rval = window_event_result::handled;
}
else if (citem.value < citem.input_or_menu()->text_len)
else if (const auto im = citem.input_or_menu(); citem.value < im->text_len)
{
auto ascii = key_ascii();
if (ascii < 255)
@ -1162,7 +1160,8 @@ static window_event_result newmenu_key_command(const d_event &event, newmenu *co
if (citem.value == -1) {
citem.value = 0;
}
if (char_allowed(ascii) || (ascii == ' ' && char_allowed(ascii = '_')))
const auto allowed_chars = im->allowed_chars;
if (char_allowed(ascii, allowed_chars) || (ascii == ' ' && char_allowed(ascii = '_', allowed_chars)))
{
citem.text[citem.value++] = ascii;
citem.text[citem.value] = 0;