8 filename =
"SDL_gamecontrollerdb.h"
10 output = open(filename +
".new",
"w")
11 parsing_controllers =
False
15 split_pattern = re.compile(
r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
19 for element
in bindings:
20 if element.startswith(prefix):
28 match = split_pattern.match(line)
29 entry = [ match.group(1), match.group(2), match.group(3) ]
30 bindings = sorted(match.group(4).split(
","))
31 if (bindings[0] ==
""):
36 bindings.append(bindings.pop(pos))
40 bindings.append(bindings.pop(pos))
42 entry.extend(
",".join(bindings) +
",")
43 entry.append(match.group(5))
44 controllers.append(entry)
46 if ',sdk' in line
or ',hint:' in line:
47 conditionals.append(entry[1])
51 global controller_guids
53 for entry
in controllers:
54 if (entry[1]
in controller_guids
and entry[1]
not in conditionals):
55 current_name = entry[2]
56 existing_name = controller_guids[entry[1]][2]
57 print(
"Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
59 if (
not current_name.startswith(
"(DUPE)")):
60 entry[2] =
"(DUPE) " + current_name
62 if (
not existing_name.startswith(
"(DUPE)")):
63 controller_guids[entry[1]][2] =
"(DUPE) " + existing_name
65 controller_guids[entry[1]] = entry
67 for entry
in sorted(controllers, key=
lambda entry: entry[2]+
"-"+entry[1]):
68 line =
"".join(entry) +
"\n"
69 line = line.replace(
"\t",
" ")
70 if not line.endswith(
",\n")
and not line.endswith(
"*/\n"):
71 print(
"Warning: '%s' is missing a comma at the end of the line" % (line))
78 if (parsing_controllers):
79 if (line.startswith(
"{")):
81 elif (line.startswith(
" NULL")):
82 parsing_controllers =
False
85 elif (line.startswith(
"#if")):
86 print(
"Parsing " + line.strip())
88 elif (line.startswith(
"#endif")):
94 if (line.startswith(
"static const char *s_ControllerMappings")):
95 parsing_controllers =
True
100 print(
"Finished writing %s.new" % filename)
def find_element(prefix, bindings)
def save_controller(line)