src/joystick/sort_controllers.py
author Ryan C. Gordon <icculus@icculus.org>
Mon, 29 Oct 2018 20:18:50 -0400
changeset 12370 86e097182d16
parent 12014 8af8c17eaf82
child 12554 66498ac4cff7
permissions -rwxr-xr-x
winmain: Patched to compile on C89 compilers.
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@12014
    30
    # Check for duplicates
slouken@12014
    31
    for entry in controllers:
slouken@12014
    32
        if (entry[1] in controller_guids):
slouken@12014
    33
            current_name = entry[2]
slouken@12014
    34
            existing_name = controller_guids[entry[1]][2]
slouken@12014
    35
            print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
slouken@12014
    36
slouken@12014
    37
            if (not current_name.startswith("(DUPE)")):
slouken@12014
    38
                entry[2] = "(DUPE) " + current_name
slouken@12014
    39
slouken@12014
    40
            if (not existing_name.startswith("(DUPE)")):
slouken@12014
    41
                controller_guids[entry[1]][2] = "(DUPE) " + existing_name
slouken@12014
    42
slouken@12014
    43
        controller_guids[entry[1]] = entry
slouken@12014
    44
slouken@12014
    45
    for entry in sorted(controllers, key=lambda entry: entry[2]+"-"+entry[1]):
slouken@7422
    46
        line = "".join(entry) + "\n"
slouken@11430
    47
        line = line.replace("\t", "    ")
slouken@8053
    48
        if not line.endswith(",\n") and not line.endswith("*/\n"):
philipp@9203
    49
            print("Warning: '%s' is missing a comma at the end of the line" % (line))
slouken@12014
    50
        output.write(line)
slouken@7422
    51
slouken@7422
    52
    controllers = []
slouken@7422
    53
    controller_guids = {}
slouken@7422
    54
slouken@7422
    55
for line in input:
philipp@9205
    56
    if (parsing_controllers):
slouken@7422
    57
        if (line.startswith("{")):
slouken@7422
    58
            output.write(line)
philipp@9205
    59
        elif (line.startswith("    NULL")):
slouken@7422
    60
            parsing_controllers = False
slouken@7422
    61
            write_controllers()
slouken@7422
    62
            output.write(line)
philipp@9205
    63
        elif (line.startswith("#if")):
philipp@9203
    64
            print("Parsing " + line.strip())
philipp@9205
    65
            output.write(line)
philipp@9205
    66
        elif (line.startswith("#endif")):
slouken@7422
    67
            write_controllers()
slouken@7422
    68
            output.write(line)
slouken@7422
    69
        else:
slouken@7422
    70
            save_controller(line)
slouken@7422
    71
    else:
slouken@7422
    72
        if (line.startswith("static const char *s_ControllerMappings")):
slouken@7422
    73
            parsing_controllers = True
slouken@7422
    74
slouken@7422
    75
        output.write(line)
slouken@7422
    76
slouken@7422
    77
output.close()
philipp@9203
    78
print("Finished writing %s.new" % filename)