sort_controllers.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. line = line.replace("\t", " ")
  28. if not line.endswith(",\n") and not line.endswith("*/\n"):
  29. print("Warning: '%s' is missing a comma at the end of the line" % (line))
  30. if (entry[1] in controller_guids):
  31. print("Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2]))
  32. controller_guids[entry[1]] = entry
  33. output.write(line)
  34. controllers = []
  35. controller_guids = {}
  36. for line in input:
  37. if (parsing_controllers):
  38. if (line.startswith("{")):
  39. output.write(line)
  40. elif (line.startswith(" NULL")):
  41. parsing_controllers = False
  42. write_controllers()
  43. output.write(line)
  44. elif (line.startswith("#if")):
  45. print("Parsing " + line.strip())
  46. output.write(line)
  47. elif (line.startswith("#endif")):
  48. write_controllers()
  49. output.write(line)
  50. else:
  51. save_controller(line)
  52. else:
  53. if (line.startswith("static const char *s_ControllerMappings")):
  54. parsing_controllers = True
  55. output.write(line)
  56. output.close()
  57. print("Finished writing %s.new" % filename)