src/joystick/sort_controllers.py
changeset 7422 172b0c6e51d6
child 8053 495f4a047be1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/sort_controllers.py	Fri Jul 12 10:44:55 2013 -0700
     1.3 @@ -0,0 +1,61 @@
     1.4 +#!/usr/bin/env python
     1.5 +#
     1.6 +# Script to sort the game controller database entries in SDL_gamecontroller.c
     1.7 +
     1.8 +import re
     1.9 +
    1.10 +
    1.11 +filename = "SDL_gamecontrollerdb.h"
    1.12 +input = open(filename)
    1.13 +output = open(filename + ".new", "w")
    1.14 +parsing_controllers = False
    1.15 +controllers = []
    1.16 +controller_guids = {}
    1.17 +split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
    1.18 +
    1.19 +def save_controller(line):
    1.20 +    global controllers
    1.21 +    match = split_pattern.match(line)
    1.22 +    entry = [ match.group(1), match.group(2), match.group(3) ]
    1.23 +    bindings = sorted(match.group(4).split(","))
    1.24 +    if (bindings[0] == ""):
    1.25 +        bindings.pop(0)
    1.26 +    entry.extend(",".join(bindings) + ",")
    1.27 +    entry.append(match.group(5))
    1.28 +    controllers.append(entry)
    1.29 +
    1.30 +def write_controllers():
    1.31 +    global controllers
    1.32 +    global controller_guids
    1.33 +    for entry in sorted(controllers, key=lambda entry: entry[2]):
    1.34 +        line = "".join(entry) + "\n"
    1.35 +        if (entry[1] in controller_guids):
    1.36 +            print "Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2])
    1.37 +        controller_guids[entry[1]] = entry
    1.38 +
    1.39 +        output.write(line)
    1.40 +    controllers = []
    1.41 +    controller_guids = {}
    1.42 +
    1.43 +for line in input:
    1.44 +    if ( parsing_controllers ):
    1.45 +        if (line.startswith("{")):
    1.46 +            output.write(line)
    1.47 +        elif (line.startswith("#endif")):
    1.48 +            parsing_controllers = False
    1.49 +            write_controllers()
    1.50 +            output.write(line)
    1.51 +        elif (line.startswith("#")):
    1.52 +            print "Parsing " + line.strip()
    1.53 +            write_controllers()
    1.54 +            output.write(line)
    1.55 +        else:
    1.56 +            save_controller(line)
    1.57 +    else:
    1.58 +        if (line.startswith("static const char *s_ControllerMappings")):
    1.59 +            parsing_controllers = True
    1.60 +
    1.61 +        output.write(line)
    1.62 +
    1.63 +output.close()
    1.64 +print "Finished writing %s.new" % filename