Cleanups to the software list menu

This commit is contained in:
Nathan Woods 2016-06-30 17:53:16 -04:00
parent 1f0dc8903f
commit 270336bf19
4 changed files with 34 additions and 104 deletions

View File

@ -462,17 +462,6 @@ void *menu::m_pool_alloc(size_t size)
} }
//-------------------------------------------------
// pool_strdup - make a temporary string
// copy in the menu's memory pool
//-------------------------------------------------
const char *menu::pool_strdup(const char *string)
{
return strcpy((char *)m_pool_alloc(strlen(string) + 1), string);
}
//------------------------------------------------- //-------------------------------------------------
// get_selection - retrieves the index // get_selection - retrieves the index
// of the currently selected menu item // of the currently selected menu item

View File

@ -74,9 +74,6 @@ public:
// allocate temporary memory from the menu's memory pool // allocate temporary memory from the menu's memory pool
void *m_pool_alloc(size_t size); void *m_pool_alloc(size_t size);
// make a temporary string copy in the menu's memory pool
const char *pool_strdup(const char *string);
// retrieves the index of the currently selected menu item // retrieves the index of the currently selected menu item
void *get_selection(); void *get_selection();

View File

@ -128,7 +128,6 @@ menu_software_list::menu_software_list(mame_ui_manager &mui, render_container *c
{ {
m_swlist = swlist; m_swlist = swlist;
m_interface = interface; m_interface = interface;
m_entrylist = nullptr;
m_ordered_by_shortname = true; m_ordered_by_shortname = true;
} }
@ -146,34 +145,16 @@ menu_software_list::~menu_software_list()
// compare_entries // compare_entries
//------------------------------------------------- //-------------------------------------------------
int menu_software_list::compare_entries(const entry_info *e1, const entry_info *e2, bool shortname) int menu_software_list::compare_entries(const entry_info &e1, const entry_info &e2, bool shortname)
{ {
int result; int result;
const char *e1_basename; const char *e1_basename = shortname ? e1.short_name.c_str() : e1.long_name.c_str();
const char *e2_basename; const char *e2_basename = shortname ? e2.short_name.c_str() : e2.long_name.c_str();
if (shortname)
{
e1_basename = (e1->short_name != nullptr) ? e1->short_name : "";
e2_basename = (e2->short_name != nullptr) ? e2->short_name : "";
}
else
{
e1_basename = (e1->long_name != nullptr) ? e1->long_name : "";
e2_basename = (e2->long_name != nullptr) ? e2->long_name : "";
}
result = core_stricmp(e1_basename, e2_basename); result = core_stricmp(e1_basename, e2_basename);
if (result == 0) if (result == 0)
{ {
result = strcmp(e1_basename, e2_basename); result = strcmp(e1_basename, e2_basename);
if (result == 0)
{
if (e1 < e2)
result = -1;
else if (e1 > e2)
result = 1;
}
} }
return result; return result;
@ -184,24 +165,19 @@ int menu_software_list::compare_entries(const entry_info *e1, const entry_info *
// append_software_entry - populate a specific list // append_software_entry - populate a specific list
//------------------------------------------------- //-------------------------------------------------
menu_software_list::entry_info *menu_software_list::append_software_entry(const software_info &swinfo) void menu_software_list::append_software_entry(const software_info &swinfo)
{ {
entry_info *entry = nullptr; entry_info entry;
entry_info **entryptr; bool entry_updated = false;
bool entry_updated = FALSE;
// check if at least one of the parts has the correct interface and add a menu entry only in this case // check if at least one of the parts has the correct interface and add a menu entry only in this case
for (const software_part &swpart : swinfo.parts()) for (const software_part &swpart : swinfo.parts())
{ {
if (swpart.matches_interface(m_interface) && swpart.is_compatible(*m_swlist) == SOFTWARE_IS_COMPATIBLE) if (swpart.matches_interface(m_interface) && swpart.is_compatible(*m_swlist) == SOFTWARE_IS_COMPATIBLE)
{ {
entry_updated = TRUE; entry_updated = true;
// allocate a new entry entry.short_name.assign(swinfo.shortname());
entry = (entry_info *) m_pool_alloc(sizeof(*entry)); entry.long_name.assign(swinfo.longname());
memset(entry, 0, sizeof(*entry));
entry->short_name = pool_strdup(swinfo.shortname());
entry->long_name = pool_strdup(swinfo.longname());
break; break;
} }
} }
@ -210,16 +186,12 @@ menu_software_list::entry_info *menu_software_list::append_software_entry(const
if (entry_updated) if (entry_updated)
{ {
// find the end of the list // find the end of the list
entryptr = &m_entrylist; auto iter = m_entrylist.begin();
while ((*entryptr != nullptr) && (compare_entries(entry, *entryptr, m_ordered_by_shortname) >= 0)) while (iter != m_entrylist.end() && compare_entries(entry, *iter, m_ordered_by_shortname) >= 0)
entryptr = &(*entryptr)->next; iter++;
// insert the entry m_entrylist.insert(iter, entry);
entry->next = *entryptr;
*entryptr = entry;
} }
return entry;
} }
@ -237,8 +209,8 @@ void menu_software_list::populate()
item_append(_("Switch Item Ordering"), "", 0, (void *)1); item_append(_("Switch Item Ordering"), "", 0, (void *)1);
// append all of the menu entries // append all of the menu entries
for (entry_info *entry = m_entrylist; entry != nullptr; entry = entry->next) for (auto &entry : m_entrylist)
item_append(entry->short_name, entry->long_name, 0, entry); item_append(entry.short_name, entry.long_name, 0, &entry);
} }
@ -248,7 +220,6 @@ void menu_software_list::populate()
void menu_software_list::handle() void menu_software_list::handle()
{ {
const entry_info *entry;
const entry_info *selected_entry = nullptr; const entry_info *selected_entry = nullptr;
int bestmatch = 0; int bestmatch = 0;
@ -260,7 +231,6 @@ void menu_software_list::handle()
if ((FPTR)event->itemref == 1 && event->iptkey == IPT_UI_SELECT) if ((FPTR)event->itemref == 1 && event->iptkey == IPT_UI_SELECT)
{ {
m_ordered_by_shortname = !m_ordered_by_shortname; m_ordered_by_shortname = !m_ordered_by_shortname;
m_entrylist = nullptr;
// reset the char buffer if we change ordering criterion // reset the char buffer if we change ordering criterion
memset(m_filename_buffer, '\0', ARRAY_LENGTH(m_filename_buffer)); memset(m_filename_buffer, '\0', ARRAY_LENGTH(m_filename_buffer));
@ -305,55 +275,30 @@ void menu_software_list::handle()
if (update_selected) if (update_selected)
{ {
const entry_info *cur_selected; // identify the selected entry
const entry_info *cur_selected = ((FPTR)event->itemref != 1)
? (const entry_info *)get_selection()
: nullptr;
// if the current selection is a software entry, start search from here // loop through all entries
if ((FPTR)event->itemref != 1) for (auto &entry : m_entrylist)
cur_selected= (const entry_info *)get_selection();
// else (if we are on the 'Switch Order' entry) start from the beginning
else
cur_selected = m_entrylist;
// check for entries which matches our filename_buffer:
// from current entry to the end
for (entry = cur_selected; entry != nullptr; entry = entry->next)
{ {
const char *compare_name = m_ordered_by_shortname ? entry->short_name : entry->long_name; // is this entry the selected entry?
if (cur_selected != &entry)
if (compare_name != nullptr && m_filename_buffer != nullptr)
{ {
auto &compare_name = m_ordered_by_shortname ? entry.short_name : entry.long_name;
int match = 0; int match = 0;
for (int i = 0; i < ARRAY_LENGTH(m_filename_buffer); i++) for (int i = 0; i < ARRAY_LENGTH(m_filename_buffer); i++)
{ {
if (core_strnicmp(compare_name, m_filename_buffer, i) == 0) if (core_strnicmp(compare_name.c_str(), m_filename_buffer, i) == 0)
match = i; match = i;
} }
if (match > bestmatch) if (match > bestmatch)
{ {
bestmatch = match; bestmatch = match;
selected_entry = entry; selected_entry = &entry;
}
}
}
// and from the first entry to current one
for (entry = m_entrylist; entry != cur_selected; entry = entry->next)
{
const char *compare_name = m_ordered_by_shortname ? entry->short_name : entry->long_name;
if (compare_name != nullptr && m_filename_buffer != nullptr)
{
int match = 0;
for (int i = 0; i < ARRAY_LENGTH(m_filename_buffer); i++)
{
if (core_strnicmp(compare_name, m_filename_buffer, i) == 0)
match = i;
}
if (match > bestmatch)
{
bestmatch = match;
selected_entry = entry;
} }
} }
} }

View File

@ -52,24 +52,23 @@ public:
virtual void handle() override; virtual void handle() override;
private: private:
struct entry_info { struct entry_info
entry_info *next; {
std::string short_name;
const char *short_name; std::string long_name;
const char *long_name;
}; };
// variables // variables
software_list_device * m_swlist; // currently selected list software_list_device * m_swlist; // currently selected list
const char * m_interface; const char * m_interface;
std::string & m_result; std::string & m_result;
entry_info * m_entrylist; std::vector<entry_info> m_entrylist;
char m_filename_buffer[1024]; char m_filename_buffer[1024];
bool m_ordered_by_shortname; bool m_ordered_by_shortname;
// functions // functions
int compare_entries(const entry_info *e1, const entry_info *e2, bool shortname); int compare_entries(const entry_info &e1, const entry_info &e2, bool shortname);
entry_info *append_software_entry(const software_info &swinfo); void append_software_entry(const software_info &swinfo);
}; };