src/joystick/sort_controllers.py
author Sam Lantinga <slouken@libsdl.org>
Fri, 12 Jul 2013 10:44:55 -0700
changeset 7422 172b0c6e51d6
child 8053 495f4a047be1
permissions -rwxr-xr-x
Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
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@7422
    32
        if (entry[1] in controller_guids):
slouken@7422
    33
            print "Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2])
slouken@7422
    34
        controller_guids[entry[1]] = entry
slouken@7422
    35
slouken@7422
    36
        output.write(line)
slouken@7422
    37
    controllers = []
slouken@7422
    38
    controller_guids = {}
slouken@7422
    39
slouken@7422
    40
for line in input:
slouken@7422
    41
    if ( parsing_controllers ):
slouken@7422
    42
        if (line.startswith("{")):
slouken@7422
    43
            output.write(line)
slouken@7422
    44
        elif (line.startswith("#endif")):
slouken@7422
    45
            parsing_controllers = False
slouken@7422
    46
            write_controllers()
slouken@7422
    47
            output.write(line)
slouken@7422
    48
        elif (line.startswith("#")):
slouken@7422
    49
            print "Parsing " + line.strip()
slouken@7422
    50
            write_controllers()
slouken@7422
    51
            output.write(line)
slouken@7422
    52
        else:
slouken@7422
    53
            save_controller(line)
slouken@7422
    54
    else:
slouken@7422
    55
        if (line.startswith("static const char *s_ControllerMappings")):
slouken@7422
    56
            parsing_controllers = True
slouken@7422
    57
slouken@7422
    58
        output.write(line)
slouken@7422
    59
slouken@7422
    60
output.close()
slouken@7422
    61
print "Finished writing %s.new" % filename