Changed menu_file_selector to use std::string for its filename buffer

This commit is contained in:
Nathan Woods 2016-07-02 13:12:10 -04:00
parent d67695e724
commit 1d08c0c6fe
2 changed files with 13 additions and 15 deletions

View File

@ -679,34 +679,33 @@ void menu_file_selector::handle()
} }
// reset the char buffer when pressing IPT_UI_SELECT // reset the char buffer when pressing IPT_UI_SELECT
if (m_filename_buffer[0] != '\0') m_filename.clear();
memset(m_filename_buffer, '\0', ARRAY_LENGTH(m_filename_buffer));
} }
else if (event->iptkey == IPT_SPECIAL) else if (event->iptkey == IPT_SPECIAL)
{ {
auto const buflen = std::strlen(m_filename_buffer);
bool update_selected = false; bool update_selected = false;
if ((event->unichar == 8) || (event->unichar == 0x7f)) if ((event->unichar == 8) || (event->unichar == 0x7f))
{ {
// if it's a backspace and we can handle it, do so // if it's a backspace and we can handle it, do so
auto const buflen = m_filename.size();
if (0 < buflen) if (0 < buflen)
{ {
*const_cast<char *>(utf8_previous_char(&m_filename_buffer[buflen])) = 0; auto buffer_oldend = m_filename.c_str() + buflen;
auto buffer_newend = utf8_previous_char(buffer_oldend);
m_filename.resize(buffer_newend - m_filename.c_str());
update_selected = true; update_selected = true;
ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename_buffer); ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename.c_str());
} }
} }
else if (event->is_char_printable()) else if (event->is_char_printable())
{ {
// if it's any other key and we're not maxed out, update // if it's any other key and we're not maxed out, update
if (event->append_char(m_filename_buffer, buflen)) m_filename += utf8_from_uchar(event->unichar);
{ update_selected = true;
update_selected = true;
ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename_buffer); ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename.c_str());
}
} }
if (update_selected) if (update_selected)
@ -719,9 +718,9 @@ void menu_file_selector::handle()
if (cur_selected != &entry) if (cur_selected != &entry)
{ {
int match = 0; int match = 0;
for (int i = 0; i < ARRAY_LENGTH(m_filename_buffer); i++) for (int i = 0; i < m_filename.size(); i++)
{ {
if (core_strnicmp(entry.basename.c_str(), m_filename_buffer, i) == 0) if (core_strnicmp(entry.basename.c_str(), m_filename.c_str(), i) == 0)
match = i; match = i;
} }
@ -743,8 +742,7 @@ void menu_file_selector::handle()
else if (event->iptkey == IPT_UI_CANCEL) else if (event->iptkey == IPT_UI_CANCEL)
{ {
// reset the char buffer also in this case // reset the char buffer also in this case
if (m_filename_buffer[0] != '\0') m_filename.clear();
memset(m_filename_buffer, '\0', ARRAY_LENGTH(m_filename_buffer));
} }
} }
} }

View File

@ -99,7 +99,7 @@ private:
int * m_result; int * m_result;
std::vector<file_selector_entry> m_entrylist; std::vector<file_selector_entry> m_entrylist;
std::string m_hover_directory; std::string m_hover_directory;
char m_filename_buffer[1024]; std::string m_filename;
// methods // methods
int compare_entries(const file_selector_entry *e1, const file_selector_entry *e2); int compare_entries(const file_selector_entry *e1, const file_selector_entry *e2);