Previous 15994 Revisions Next

r10018 Thursday 13th January, 2011 at 08:00:27 UTC by Fabio Priuli
added best match suggestions to software lists
[src/emu]softlist.c

src/emu/softlist.c
r10017r10018
5353   int rom_entries;
5454   int current_rom_entry;
5555   void (*error_proc)(const char *message);
56   int list_entries;
5657};
5758
5859
r10017r10018
803804
804805
805806/*-------------------------------------------------
807 software_list_get_count
808 -------------------------------------------------*/
809
810static int software_list_get_count(software_list *swlist)
811{
812   int count = 0;
813   
814   for (software_info *swinfo = software_list_find(swlist, "*", NULL); swinfo != NULL; swinfo = software_list_find(swlist, "*", swinfo))
815      count++;
816   
817   return count;
818}
819
820
821/*-------------------------------------------------
806822    software_list_parse
807823-------------------------------------------------*/
808824
r10017r10018
851867      XML_ParserFree(swlist->state.parser);
852868   swlist->state.parser = NULL;
853869   swlist->current_software_info = swlist->software_info_list;
870   swlist->list_entries = software_list_get_count(swlist);
854871}
855872
856873
r10017r10018
930947
931948
932949/*-------------------------------------------------
950 software_list_find_by_number
951 -------------------------------------------------*/
952
953INLINE software_info *software_list_find_by_number(software_list *swlist, int number)
954{
955   int length = swlist->list_entries;
956   if (number > length)
957      return NULL;
958   
959   software_info *cur_info = software_list_find(swlist, "*", NULL);
960   
961   for (int count = 0; count < number; count++)
962      cur_info = software_list_find(swlist, "*", cur_info);
963   
964   return cur_info;
965}
966
967
968/*-------------------------------------------------
969 softlist_penalty_compare (borrowed from driver.c)
970 -------------------------------------------------*/
971
972static int softlist_penalty_compare(const char *source, const char *target)
973{
974   int gaps = 1;
975   int last = TRUE;
976   
977   /* scan the strings */
978   for ( ; *source && *target; target++)
979   {
980      /* do a case insensitive match */
981      int match = (tolower((UINT8)*source) == tolower((UINT8)*target));
982     
983      /* if we matched, advance the source */
984      if (match)
985         source++;
986     
987      /* if the match state changed, count gaps */
988      if (match != last)
989      {
990         last = match;
991         if (!match)
992            gaps++;
993      }
994   }
995   
996   /* penalty if short string does not completely fit in */
997   for ( ; *source; source++)
998      gaps++;
999   
1000   /* if we matched perfectly, gaps == 0 */
1001   if (gaps == 1 && *source == 0 && *target == 0)
1002      gaps = 0;
1003   
1004   return gaps;
1005}
1006
1007
1008/*-------------------------------------------------
1009 software_list_find_approx_matches
1010 -------------------------------------------------*/
1011
1012void software_list_find_approx_matches(software_list *swlist, const char *name, int matches, software_info **list)
1013{
1014#undef rand
1015   
1016   int matchnum;
1017   int *penalty;
1018   
1019   /* if no name, return */
1020   if (name == NULL || name[0] == 0)
1021      return;
1022
1023   /* allocate some temp memory */
1024   penalty = global_alloc_array(int, matches);
1025   
1026   /* initialize everyone's states */
1027   for (matchnum = 0; matchnum < matches; matchnum++)
1028   {
1029      penalty[matchnum] = 9999;
1030      list[matchnum] = NULL;
1031   }
1032
1033   for (software_info *swinfo = software_list_find(swlist, "*", NULL); swinfo != NULL; swinfo = software_list_find(swlist, "*", swinfo))
1034   {
1035      int curpenalty, tmp;
1036      software_info *candidate = swinfo;
1037     
1038      /* pick the best match between driver name and description */
1039      curpenalty = softlist_penalty_compare(name, candidate->longname);
1040      tmp = softlist_penalty_compare(name, candidate->shortname);
1041      curpenalty = MIN(curpenalty, tmp);
1042     
1043      /* insert into the sorted table of matches */
1044      for (matchnum = matches - 1; matchnum >= 0; matchnum--)
1045      {
1046         /* stop if we're worse than the current entry */
1047         if (curpenalty >= penalty[matchnum])
1048            break;
1049         
1050         /* as long as this isn't the last entry, bump this one down */
1051         if (matchnum < matches - 1)
1052         {
1053            penalty[matchnum + 1] = penalty[matchnum];
1054            list[matchnum + 1] = list[matchnum];
1055         }
1056         list[matchnum] = candidate;
1057         penalty[matchnum] = curpenalty;
1058      }
1059   }
1060}
1061
1062
1063/*-------------------------------------------------
9331064    software_list_find
9341065-------------------------------------------------*/
9351066
r10017r10018
10371168
10381169bool load_software_part(device_image_interface *image, const char *path, software_info **sw_info, software_part **sw_part, char **full_sw_name)
10391170{
1040   char *swlist_name, *swname, *swpart;
1171   char *swlist_name, *swname, *swpart, *swname_bckp;
10411172   bool result = false;
10421173   software_list *software_list_ptr = NULL;
10431174   software_info *software_info_ptr = NULL;
r10017r10018
10481179
10491180   /* Split full software name into software list name and short software name */
10501181   software_name_split( image->device().machine, path, &swlist_name, &swname, &swpart );
1182   swname_bckp = swname;
10511183
10521184   const char *interface = image->image_config().image_interface();
10531185
r10017r10018
11611293      }
11621294   }
11631295
1296   if (software_info_ptr == NULL)
1297   {
1298
1299      mame_printf_error("\n\"%s\" approximately matches the following\n"
1300            "supported software items (best match first):\n\n", swname_bckp);
1301
1302      for (device_t *swlists = image->device().machine->m_devicelist.first(SOFTWARE_LIST); swlists != NULL; swlists = swlists->typenext())
1303      {
1304         software_list_config *swlist = (software_list_config *)downcast<const legacy_device_config_base *>(&swlists->baseconfig())->inline_config();
1305         
1306         for (int i = 0; i < DEVINFO_STR_SWLIST_MAX - DEVINFO_STR_SWLIST_0; i++)
1307         {
1308            if (swlist->list_name[i] && *swlist->list_name[i] && (swlist->list_type == SOFTWARE_LIST_ORIGINAL_SYSTEM))
1309            {
1310               software_list *list = software_list_open(image->device().machine->options(), swlist->list_name[i], FALSE, NULL);
1311               
1312               if (list)
1313               {
1314                  software_info *matches[5] = { 0 };
1315                  int softnum;
1316         
1317                  software_list_parse(list, list->error_proc, NULL);
1318                  mame_printf_error("* Software list \"%s\" matches: \n", software_list_get_description(list));
1319                  /* get the top 5 approximate matches */
1320                  software_list_find_approx_matches(list, swname_bckp, ARRAY_LENGTH(matches), matches);
1321         
1322                  /* print them out */
1323                  for (softnum = 0; softnum < ARRAY_LENGTH(matches); softnum++)
1324                     if (matches[softnum] != NULL)
1325                        mame_printf_error("%-18s%s\n", matches[softnum]->shortname, matches[softnum]->longname);
1326
1327                  mame_printf_error("\n");
1328               }
1329               software_list_close(list);
1330            }
1331         }
1332      }
1333     
1334   }
1335
11641336   if ( software_part_ptr )
11651337   {
11661338      /* Load the software part */

Previous 15994 Revisions Next


© 1998-2018 The MESS Team