File: multibin2c.lua

package info (click to toggle)
wordgrinder 0.8-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,100 kB
  • sloc: ansic: 3,880; makefile: 194; xml: 13; sh: 10
file content (41 lines) | stat: -rw-r--r-- 888 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- © 2008 David Given.
-- WordGrinder is licensed under the MIT open source license. See the COPYING
-- file in this distribution for the full text.

local function write(...)
	io.stdout:write(...)
end

local function multibin2c(pattern, ...)
	local files = {...}
	local id = 1
	
	write('#include "globals.h"\n')
	for _, f in ipairs(files) do
		write("\n/* This is ", f, " */\n")
		write("static const char file_", id, "[] = {\n")
		
		local fp = io.open(f, "rb")
		local data = fp:read("*a")
		for i = 1, data:len() do
			write(data:byte(i), ", ")
			if ((i % 16) == 0) then
				write("\n")
			end
		end
		fp:close()
		
		write("\n};\n")
		id = id + 1
	end
	
	write("const FileDescriptor ", pattern, "[] = {\n")
	for i = 1, id-1 do
		local id = "file_"..i
		write("{ ", id, ", sizeof(", id, "), \"", files[i], "\" },\n")
	end
	write("{ NULL, 0 }\n")
	write("};\n")
end

multibin2c(...)