sort_controllers.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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("#endif")):
  40. parsing_controllers = False
  41. write_controllers()
  42. output.write(line)
  43. elif (line.startswith("#")):
  44. print "Parsing " + line.strip()
  45. write_controllers()
  46. output.write(line)
  47. else:
  48. save_controller(line)
  49. else:
  50. if (line.startswith("static const char *s_ControllerMappings")):
  51. parsing_controllers = True
  52. output.write(line)
  53. output.close()
  54. print "Finished writing %s.new" % filename