src/joystick/sort_controllers.py
changeset 7422 172b0c6e51d6
child 8053 495f4a047be1
equal deleted inserted replaced
7421:4086afac6a7e 7422:172b0c6e51d6
       
     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         if (entry[1] in controller_guids):
       
    33             print "Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2])
       
    34         controller_guids[entry[1]] = entry
       
    35 
       
    36         output.write(line)
       
    37     controllers = []
       
    38     controller_guids = {}
       
    39 
       
    40 for line in input:
       
    41     if ( parsing_controllers ):
       
    42         if (line.startswith("{")):
       
    43             output.write(line)
       
    44         elif (line.startswith("#endif")):
       
    45             parsing_controllers = False
       
    46             write_controllers()
       
    47             output.write(line)
       
    48         elif (line.startswith("#")):
       
    49             print "Parsing " + line.strip()
       
    50             write_controllers()
       
    51             output.write(line)
       
    52         else:
       
    53             save_controller(line)
       
    54     else:
       
    55         if (line.startswith("static const char *s_ControllerMappings")):
       
    56             parsing_controllers = True
       
    57 
       
    58         output.write(line)
       
    59 
       
    60 output.close()
       
    61 print "Finished writing %s.new" % filename