Converted menu_file_create to use std::string for its filename buffer

This commit is contained in:
Nathan Woods 2016-07-02 12:55:06 -04:00
parent 60c81498b0
commit d67695e724
4 changed files with 39 additions and 20 deletions

View File

@ -53,24 +53,25 @@ namespace ui {
// into a buffer
//-------------------------------------------------
template <std::size_t N, typename F>
static void input_character(char (&buffer)[N], unicode_char unichar, F &&filter)
template <typename F>
static void input_character(std::string &buffer, unicode_char unichar, F &&filter)
{
auto buflen = std::strlen(buffer);
auto buflen = buffer.size();
if ((unichar == 8) || (unichar == 0x7f))
{
// backspace
if (0 < buflen)
*const_cast<char *>(utf8_previous_char(&buffer[buflen])) = 0;
{
auto buffer_oldend = buffer.c_str() + buflen;
auto buffer_newend = utf8_previous_char(buffer_oldend);
buffer.resize(buffer_newend - buffer.c_str());
}
}
else if ((unichar >= ' ') && (!filter || filter(unichar)))
{
auto const chlen = utf8_from_uchar(&buffer[buflen], N - buflen - 1, unichar);
if (0 <= chlen)
{
buflen += chlen;
buffer[buflen] = 0;
}
// append this character
buffer += utf8_from_uchar(unichar);
}
}
@ -175,7 +176,11 @@ menu_file_create::menu_file_create(mame_ui_manager &mui, render_container *conta
m_ok = ok;
*m_ok = true;
auto const sep = current_file.rfind(PATH_SEPARATOR);
std::strncpy(m_filename_buffer, current_file.c_str() + ((std::string::npos == sep) ? 0 : (sep + 1)), sizeof(m_filename_buffer));
m_filename.reserve(1024);
m_filename = sep != std::string::npos
? current_file.substr(sep + strlen(PATH_SEPARATOR), current_file.size() - sep - strlen(PATH_SEPARATOR))
: current_file;
}
@ -208,19 +213,19 @@ void menu_file_create::populate()
{
std::string buffer;
const image_device_format *format;
const char *new_image_name;
const std::string *new_image_name;
// append the "New Image Name" item
if (get_selection() == ITEMREF_NEW_IMAGE_NAME)
{
buffer.append(m_filename_buffer).append("_");
new_image_name = buffer.c_str();
buffer = m_filename + "_";
new_image_name = &buffer;
}
else
{
new_image_name = m_filename_buffer;
new_image_name = &m_filename;
}
item_append(_("New Image Name:"), new_image_name, 0, ITEMREF_NEW_IMAGE_NAME);
item_append(_("New Image Name:"), *new_image_name, 0, ITEMREF_NEW_IMAGE_NAME);
// do we support multiple formats?
if (ENABLE_FORMATS) format = m_image->formatlist().front().get();
@ -256,10 +261,10 @@ void menu_file_create::handle()
case IPT_UI_SELECT:
if ((event->itemref == ITEMREF_CREATE) || (event->itemref == ITEMREF_NEW_IMAGE_NAME))
{
std::string tmp_file(m_filename_buffer);
std::string tmp_file(m_filename);
if (tmp_file.find(".") != -1 && tmp_file.find(".") < tmp_file.length() - 1)
{
m_current_file = m_filename_buffer;
m_current_file = m_filename;
menu::stack_pop(machine());
}
else
@ -270,7 +275,7 @@ void menu_file_create::handle()
case IPT_SPECIAL:
if (get_selection() == ITEMREF_NEW_IMAGE_NAME)
{
input_character(m_filename_buffer,event->unichar, &is_valid_filename_char);
input_character(m_filename, event->unichar, &is_valid_filename_char);
reset(reset_options::REMEMBER_POSITION);
}
break;

View File

@ -49,7 +49,7 @@ private:
std::string & m_current_directory;
std::string & m_current_file;
const image_device_format * m_current_format;
char m_filename_buffer[1024];
std::string m_filename;
protected:
bool * m_ok;

View File

@ -246,6 +246,19 @@ int utf8_from_uchar(char *utf8string, size_t count, unicode_char uchar)
}
//-------------------------------------------------
// utf8_from_uchar - convert a unicode character
// into a UTF-8 sequence
//-------------------------------------------------
std::string utf8_from_uchar(unicode_char uchar)
{
char buffer[UTF8_CHAR_MAX];
auto len = utf8_from_uchar(buffer, ARRAY_LENGTH(buffer), uchar);
return std::string(buffer, len);
}
//-------------------------------------------------
// utf16_from_uchar - convert a unicode character
// into a UTF-16 sequence

View File

@ -97,6 +97,7 @@ int uchar_from_utf16f(unicode_char *uchar, const utf16_char *utf16char, size_t c
// converting 32-bit Unicode chars to strings
int utf8_from_uchar(char *utf8string, size_t count, unicode_char uchar);
std::string utf8_from_uchar(unicode_char uchar);
int utf16_from_uchar(utf16_char *utf16string, size_t count, unicode_char uchar);
int utf16f_from_uchar(utf16_char *utf16string, size_t count, unicode_char uchar);