sort_controllers.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 (entry[1] in controller_guids):
  28. print "Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2])
  29. controller_guids[entry[1]] = entry
  30. output.write(line)
  31. controllers = []
  32. controller_guids = {}
  33. for line in input:
  34. if ( parsing_controllers ):
  35. if (line.startswith("{")):
  36. output.write(line)
  37. elif (line.startswith("#endif")):
  38. parsing_controllers = False
  39. write_controllers()
  40. output.write(line)
  41. elif (line.startswith("#")):
  42. print "Parsing " + line.strip()
  43. write_controllers()
  44. output.write(line)
  45. else:
  46. save_controller(line)
  47. else:
  48. if (line.startswith("static const char *s_ControllerMappings")):
  49. parsing_controllers = True
  50. output.write(line)
  51. output.close()
  52. print "Finished writing %s.new" % filename