SDL 2.0
sort_controllers.py
Go to the documentation of this file.
1#!/usr/bin/env python
2#
3# Script to sort the game controller database entries in SDL_gamecontroller.c
4
5import re
6
7
8filename = "SDL_gamecontrollerdb.h"
9input = open(filename)
10output = open(filename + ".new", "w")
11parsing_controllers = False
12controllers = []
13controller_guids = {}
14split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
15
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
28 global controllers
29 global controller_guids
30 # Check for duplicates
31 for entry in controllers:
32 if (entry[1] in controller_guids):
33 current_name = entry[2]
34 existing_name = controller_guids[entry[1]][2]
35 print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
36
37 if (not current_name.startswith("(DUPE)")):
38 entry[2] = "(DUPE) " + current_name
39
40 if (not existing_name.startswith("(DUPE)")):
41 controller_guids[entry[1]][2] = "(DUPE) " + existing_name
42
43 controller_guids[entry[1]] = entry
44
45 for entry in sorted(controllers, key=lambda entry: entry[2]+"-"+entry[1]):
46 line = "".join(entry) + "\n"
47 line = line.replace("\t", " ")
48 if not line.endswith(",\n") and not line.endswith("*/\n"):
49 print("Warning: '%s' is missing a comma at the end of the line" % (line))
50 output.write(line)
51
52 controllers = []
53 controller_guids = {}
54
55for line in input:
56 if (parsing_controllers):
57 if (line.startswith("{")):
58 output.write(line)
59 elif (line.startswith(" NULL")):
60 parsing_controllers = False
62 output.write(line)
63 elif (line.startswith("#if")):
64 print("Parsing " + line.strip())
65 output.write(line)
66 elif (line.startswith("#endif")):
68 output.write(line)
69 else:
70 save_controller(line)
71 else:
72 if (line.startswith("static const char *s_ControllerMappings")):
73 parsing_controllers = True
74
75 output.write(line)
76
77output.close()
78print("Finished writing %s.new" % filename)
def save_controller(line)