src/joystick/sort_controllers.py
author Ryan C. Gordon <icculus@icculus.org>
Wed, 24 Dec 2014 22:22:35 -0500
changeset 9289 5f857c00d25d
parent 9205 5302aee2916a
child 11430 884105e0943f
permissions -rwxr-xr-x
Maybe actually patched to compile this time?
slouken@7422
     1
#!/usr/bin/env python
slouken@7422
     2
#
slouken@7422
     3
# Script to sort the game controller database entries in SDL_gamecontroller.c
slouken@7422
     4
slouken@7422
     5
import re
slouken@7422
     6
slouken@7422
     7
slouken@7422
     8
filename = "SDL_gamecontrollerdb.h"
slouken@7422
     9
input = open(filename)
slouken@7422
    10
output = open(filename + ".new", "w")
slouken@7422
    11
parsing_controllers = False
slouken@7422
    12
controllers = []
slouken@7422
    13
controller_guids = {}
slouken@7422
    14
split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
slouken@7422
    15
slouken@7422
    16
def save_controller(line):
slouken@7422
    17
    global controllers
slouken@7422
    18
    match = split_pattern.match(line)
slouken@7422
    19
    entry = [ match.group(1), match.group(2), match.group(3) ]
slouken@7422
    20
    bindings = sorted(match.group(4).split(","))
slouken@7422
    21
    if (bindings[0] == ""):
slouken@7422
    22
        bindings.pop(0)
slouken@7422
    23
    entry.extend(",".join(bindings) + ",")
slouken@7422
    24
    entry.append(match.group(5))
slouken@7422
    25
    controllers.append(entry)
slouken@7422
    26
slouken@7422
    27
def write_controllers():
slouken@7422
    28
    global controllers
slouken@7422
    29
    global controller_guids
slouken@7422
    30
    for entry in sorted(controllers, key=lambda entry: entry[2]):
slouken@7422
    31
        line = "".join(entry) + "\n"
slouken@8053
    32
        if not line.endswith(",\n") and not line.endswith("*/\n"):
philipp@9203
    33
            print("Warning: '%s' is missing a comma at the end of the line" % (line))
slouken@7422
    34
        if (entry[1] in controller_guids):
philipp@9203
    35
            print("Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2]))
slouken@7422
    36
        controller_guids[entry[1]] = entry
slouken@7422
    37
slouken@7422
    38
        output.write(line)
slouken@7422
    39
    controllers = []
slouken@7422
    40
    controller_guids = {}
slouken@7422
    41
slouken@7422
    42
for line in input:
philipp@9205
    43
    if (parsing_controllers):
slouken@7422
    44
        if (line.startswith("{")):
slouken@7422
    45
            output.write(line)
philipp@9205
    46
        elif (line.startswith("    NULL")):
slouken@7422
    47
            parsing_controllers = False
slouken@7422
    48
            write_controllers()
slouken@7422
    49
            output.write(line)
philipp@9205
    50
        elif (line.startswith("#if")):
philipp@9203
    51
            print("Parsing " + line.strip())
philipp@9205
    52
            output.write(line)
philipp@9205
    53
        elif (line.startswith("#endif")):
slouken@7422
    54
            write_controllers()
slouken@7422
    55
            output.write(line)
slouken@7422
    56
        else:
slouken@7422
    57
            save_controller(line)
slouken@7422
    58
    else:
slouken@7422
    59
        if (line.startswith("static const char *s_ControllerMappings")):
slouken@7422
    60
            parsing_controllers = True
slouken@7422
    61
slouken@7422
    62
        output.write(line)
slouken@7422
    63
slouken@7422
    64
output.close()
philipp@9203
    65
print("Finished writing %s.new" % filename)