ctodata.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # ***** BEGIN GPL LICENSE BLOCK *****
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #
  19. # The Original Code is Copyright (C) 2009 Blender Foundation.
  20. # All rights reserved.
  21. #
  22. # ***** END GPL LICENCE BLOCK *****
  23. # <pep8 compliant>
  24. import sys
  25. argv = sys.argv[:]
  26. strip_byte = False
  27. if "--strip-byte" in argv:
  28. argv.remove("--strip-byte")
  29. strip_byte = True
  30. if len(argv) < 2:
  31. sys.stdout.write("Usage: ctodata <c_file> [--strip-byte]\n")
  32. sys.exit(1)
  33. filename = argv[1]
  34. try:
  35. fpin = open(filename, "r")
  36. except:
  37. sys.stdout.write("Unable to open input %s\n" % argv[1])
  38. sys.exit(1)
  39. data = fpin.read().rsplit("{")[-1].split("}")[0]
  40. data = data.replace(",", " ")
  41. data = data.split()
  42. data = [int(v) for v in data]
  43. if strip_byte:
  44. # String data gets trailing byte.
  45. last = data.pop()
  46. assert(last == 0)
  47. data = bytes(data)
  48. dname = filename + ".ctodata"
  49. sys.stdout.write("Making DATA file <%s>\n" % dname)
  50. try:
  51. fpout = open(dname, "wb")
  52. except:
  53. sys.stdout.write("Unable to open output %s\n" % dname)
  54. sys.exit(1)
  55. size = fpout.write(data)
  56. sys.stdout.write("%d\n" % size)