sort_controllers.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. #
  3. # Script to sort the game controller database entries in SDL_gamecontroller.c
  4. import re
  5. filename = "SDL_gamecontrollerdb.h"
  6. input = open(filename)
  7. output = open(filename + ".new", "w")
  8. parsing_controllers = False
  9. controllers = []
  10. controller_guids = {}
  11. split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
  12. def save_controller(line):
  13. global controllers
  14. match = split_pattern.match(line)
  15. entry = [ match.group(1), match.group(2), match.group(3) ]
  16. bindings = sorted(match.group(4).split(","))
  17. if (bindings[0] == ""):
  18. bindings.pop(0)
  19. entry.extend(",".join(bindings) + ",")
  20. entry.append(match.group(5))
  21. controllers.append(entry)
  22. def write_controllers():
  23. global controllers
  24. global controller_guids
  25. for entry in sorted(controllers, key=lambda entry: entry[2]):
  26. line = "".join(entry) + "\n"
  27. if not line.endswith(",\n") and not line.endswith("*/\n"):
  28. print("Warning: '%s' is missing a comma at the end of the line" % (line))
  29. if (entry[1] in controller_guids):
  30. print("Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2]))
  31. controller_guids[entry[1]] = entry
  32. output.write(line)
  33. controllers = []
  34. controller_guids = {}
  35. for line in input:
  36. if (parsing_controllers):
  37. if (line.startswith("{")):
  38. output.write(line)
  39. elif (line.startswith(" NULL")):
  40. parsing_controllers = False
  41. write_controllers()
  42. output.write(line)
  43. elif (line.startswith("#if")):
  44. print("Parsing " + line.strip())
  45. output.write(line)
  46. elif (line.startswith("#endif")):
  47. write_controllers()
  48. output.write(line)
  49. else:
  50. save_controller(line)
  51. else:
  52. if (line.startswith("static const char *s_ControllerMappings")):
  53. parsing_controllers = True
  54. output.write(line)
  55. output.close()
  56. print("Finished writing %s.new" % filename)