src/joystick/sort_controllers.py
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 29 Oct 2014 22:44:35 +0100
changeset 9205 5302aee2916a
parent 9203 874b59c43a3f
permissions -rwxr-xr-x
Fixed gamecontroller database sorting script not sorting the whole database. The script was originally written for an SDL_gamecontrollerdb.h which had groups of mappings separated by single lines starting with a "#". This was changed some time ago to #endif/#if pairs. Because the script assumed only a single #endif in the whole file it no longer worked correctly and only processed the first group.

#!/usr/bin/env python
#
# Script to sort the game controller database entries in SDL_gamecontroller.c

import re


filename = "SDL_gamecontrollerdb.h"
input = open(filename)
output = open(filename + ".new", "w")
parsing_controllers = False
controllers = []
controller_guids = {}
split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')

def save_controller(line):
    global controllers
    match = split_pattern.match(line)
    entry = [ match.group(1), match.group(2), match.group(3) ]
    bindings = sorted(match.group(4).split(","))
    if (bindings[0] == ""):
        bindings.pop(0)
    entry.extend(",".join(bindings) + ",")
    entry.append(match.group(5))
    controllers.append(entry)

def write_controllers():
    global controllers
    global controller_guids
    for entry in sorted(controllers, key=lambda entry: entry[2]):
        line = "".join(entry) + "\n"
        if not line.endswith(",\n") and not line.endswith("*/\n"):
            print("Warning: '%s' is missing a comma at the end of the line" % (line))
        if (entry[1] in controller_guids):
            print("Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2]))
        controller_guids[entry[1]] = entry

        output.write(line)
    controllers = []
    controller_guids = {}

for line in input:
    if (parsing_controllers):
        if (line.startswith("{")):
            output.write(line)
        elif (line.startswith("    NULL")):
            parsing_controllers = False
            write_controllers()
            output.write(line)
        elif (line.startswith("#if")):
            print("Parsing " + line.strip())
            output.write(line)
        elif (line.startswith("#endif")):
            write_controllers()
            output.write(line)
        else:
            save_controller(line)
    else:
        if (line.startswith("static const char *s_ControllerMappings")):
            parsing_controllers = True

        output.write(line)

output.close()
print("Finished writing %s.new" % filename)