Advertisement
goebelmasse

Why I am not a fan of the XML formatted mameinfo.dat? Because the old format is easier parseable.

Feb 20th, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.16 KB | Gaming | 0 0
  1. #!/bin/sh
  2. ########################################################################
  3. #
  4. # mamehist
  5. # $Id: mamehist,v 1.6 2023/02/20 14:37:06 elias Exp $
  6. #
  7. ########################################################################
  8.  
  9. # Paranoia!
  10. PATH=/bin:/usr/bin
  11.  
  12. # Full path for the history.dat file in the old format
  13. HISTFILE=/usr/local/share/games/mame/history.dat
  14.  
  15. # Option parsing (not POSIX-conform)
  16. case $1 in
  17.     -h) shift
  18.     echo "Usage: `basename $0` <ROM name>"
  19.     echo "Extract information from history.dat"
  20.     echo
  21.     echo "Options"
  22.     echo "  -h             Show this help"
  23.     echo "  -l [<pattern>] List ROM names in history.dat"
  24.     echo "  -f <filename>  Use specified history.dat file"
  25.     echo
  26.     echo "Used history.dat file: $HISTFILE"
  27.     exit
  28.     ;;
  29.     -l) shift
  30.     if test -n "$1"; then filter="grep $1"; else filter="cat"; fi
  31.     sed -n '/^\$info/p' "$HISTFILE" |
  32.         sed -e 's/^.*=//' -e 's/,/\n/g' |
  33.         sed -e '/^\s*$/d' |
  34.         $filter
  35.     exit
  36.     ;;
  37.     -f) shift
  38.     if test -z "$1"
  39.     then
  40.         echo 1>&2 "Missing filename of the history.dat file"
  41.         exit 1
  42.     fi
  43.     HISTFILE=$1
  44.     shift
  45.     ;;
  46. esac
  47.  
  48. # Fail if the history.dat file does not exist
  49. if test ! -f "$HISTFILE"
  50. then
  51.     echo 1>&2 History file "$HISTFILE" does not exist
  52.     exit 1
  53. fi
  54.  
  55. # Only use less in terminals, allowing output redirection
  56. if test -t 1; then pager="less"; else pager="cat"; fi
  57.  
  58. # Check for required ROM name
  59. if test -z "$1"
  60. then
  61.     echo 1>&2 No ROM name given
  62.     exit 1
  63. fi
  64.  
  65. # Extract information from mameinfo.dat
  66. for romname in $*
  67. do
  68.     sed -e '/^#/d' -e '/published.*years ago/d' "$HISTFILE" |
  69.     awk "BEGIN { status = 0; }
  70.              /^\\\$info*[=,]$romname,/ { status = 2; }
  71.              status == 1 && /^\\\$end/ { exit; }
  72.              status == 1 { print; }
  73.          status == 3 && /\S/ { status = 1; print; }
  74.              status == 2 && /^\\\$bio/ { status = 3; }
  75.             " |
  76.     fold -s
  77.     # That awk program looks rather awkward, I know.
  78.     # status == 0: scan for ROMs name
  79.     # status == 1: print lines until the $end is reached
  80.     # status == 2: wait for $bio
  81.     # status == 3: consume empty lines at the top of the entry
  82. done | $pager
  83.  
  84. # Code is prosa, not poetry.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement