#!/usr/bin/perl -s
# - Copyright (c) 2005,2008 by Mikkel Holm Olsen
use XML::Parser;

sub usage {
    print($err."\n\n") if $err=shift;
    print <<"EOF"; v2.05  Copyright (c) 2005,2008 by Mikkel Holm Olsen

Generate a list of the most used video-modes for MAME/AdvanceMAME

USAGE [args] list.xml

    list.xml - input-file (XML) generated from MAME/AdvanceMAME
               with the --listxml option.

    -r         Don\'t swap hor/ver resolution for vertical games
    -p=pct     Stop after listing pct % (1-100) of the games
    -n=num     Stop after listing num video modes (resolutions)
    -c         Don\'t count clones (only parents)
    -f         Don\'t diffrentiate on refresh frequency
    -o=?       Only list horizontal (h) or vertical (v) games


# Handle arguments
$norotate=1 if ${r};
$fraction=$p/100 if ($p)&&($p>0)&&($p<=100);
$maxmodes=$n if $n;
$noclones=1 if $c;
$refresh=1 if !$r;
$filter='horizontal' if ($o eq 'v');
$filter='vertical' if ($o eq 'h');
usage() if $h || ${-help};
usage("Missing filename (list.xml)") if $#ARGV<0;
usage("Only one filename can be given") if $#ARGV>0;


sub start_tag {
my %attrs;
    return if ($elem=~/(rom|chip|dipswitch|dipvalue)/); # x2 speed-up
    while (($key,$val)=splice(@_,0,2)) {
    if ($elem eq 'game'){
	if ($attrs{'runnable'} eq 'yes') {
	} else {
    } elsif (!$thisruns) {
    } elsif (($elem eq 'video')||($elem eq 'display')) {
        if ($attrs{'orientation'}) { # AdvanceMAME style
        } else { # Regular MAME style
        if ($filter eq $orient) {
	if ($attrs{'type'}.$attrs{'screen'} eq 'raster') {
	    if ($norotate || ($orient eq 'horizontal')) {
	    } else {
	} else { # must be vector then
	$key.="@".sprintf("%.2f",int($attrs{'refresh'}*100)/100) if $refresh;

my $file = shift;
die "Can't find file \"$file\"" unless -f $file;

my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers(Start => \&start_tag);
$runnable++ if ($thisruns);
$norunnable++ if ($no_runner);
sub by_count { $res_count{$b} <=> $res_count{$a}; }
foreach $r (sort by_count keys %res_count) {
    printf("%4d %-15s %s\n",$res_count{$r},$r,$example{$r});
    last if $fraction && ($numgames/$runnable)>=$fraction;
    last if (($maxmodes)&&($nummodes>=$maxmodes));

print "\n$nummodes modes for ".$numgames."/".$runnable." ROMs (".
    (int($numgames/$runnable*1000)/10)."%). ".$norunnable." additional non-runnable.\n";

Last updated: 2008.02.26