bin2cpp.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function convertFile(filenameIn, filenameOut, stringname)
  2. print("\nfilenameOut = " .. filenameOut)
  3. local f = assert(io.open(filenameIn, "rb"))
  4. local fw = io.open(filenameOut,"w")
  5. fw:write(string.format("char %s[]={", stringname))
  6. local block = 10
  7. while true do
  8. local bytes = f:read(block)
  9. if not bytes then break end
  10. for b in string.gfind(bytes, ".") do
  11. fw:write(string.format("%u,", string.byte(b)))
  12. end
  13. --io.write(string.rep(" ", block - string.len(bytes) + 1))
  14. --io.write(string.gsub(bytes, "%c", "."), "\n")
  15. fw:write(string.format("\n"))
  16. end
  17. fw:write(string.format("\n};"))
  18. end
  19. newoption {
  20. trigger = "binfile",
  21. value = "binpath",
  22. description = "full path to the binary input file"
  23. }
  24. newoption {
  25. trigger = "cppfile",
  26. value = "path",
  27. description = "full path to the cpp output file"
  28. }
  29. newoption {
  30. trigger = "stringname",
  31. value = "var",
  32. description = "name of the variable name in the cppfile that contains the binary data"
  33. }
  34. newaction {
  35. trigger = "bin2cpp",
  36. description = "convert binary file into cpp source",
  37. execute = function ()
  38. convertFile( _OPTIONS["binfile"] , _OPTIONS["cppfile"], _OPTIONS["stringname"])
  39. end
  40. }