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