Change auto.lua to create plaintext instead of hex code.

This commit is contained in:
vrld
2011-02-20 23:34:48 +01:00
parent 8581efae07
commit 08259a4a0e
7 changed files with 17 additions and 8054 deletions
+11 -52
View File
@@ -13,7 +13,7 @@ function auto(name)
local cpp_name = name .. "_lua"
-- Read source Lua file
local src_file = io.open(src, "rb")
local src_file = assert(io.open(src, "rb"))
local src_data = src_file:read("*a")
-- remove comments
src_data = src_data:gsub("%-%-%[%[.-%-%-%]%]", ""):gsub("%-%-.-\n", "")
@@ -22,64 +22,23 @@ function auto(name)
local src_len = #src_data
src_file:close()
local lines = {}
local line = {}
table.insert(lines, line)
for i=1,src_len do
table.insert(line, string.byte(src_data, i))
if math.mod(i, max_width) == 0 then
line = {}
table.insert(lines, line)
end
end
local src_output = "const unsigned char "..cpp_name.."[] = \n{\n"
for i,line in ipairs(lines) do
local concat = {}
for j,b in ipairs(line) do
table.insert(concat, string.format("0x%02X,", b))
end
src_output = src_output .. "\t" .. table.concat(concat, "").."\n"
end
src_output = src_output .. "};"
local include = true
-- Read dst
local dst_lines = {}
for line in io.lines(dst) do
if line == "// ["..src.."]" then
include = false
table.insert(dst_lines, line)
table.insert(dst_lines, src_output)
end
if line == "// [/"..src.."]" then
include = true
end
if include then
table.insert(dst_lines, line)
end
end
local tmp_data = table.concat(dst_lines, "\n")
src_data = string.format("const std::string %s = %q;", cpp_name, src_data)
-- Overwrite the old file only if they are different.
local dst_file = io.open(dst, "rb")
local dst_file = assert(io.open(dst, "r"))
dst_data = dst_file:read("*a")
dst_file:close()
if tmp_data == dst_data then
local header, content, footer = dst_data:match('^(.-)(const.-%b"";)(.-)$')
assert(header and content and footer, "error parsing header/content/footer")
if content == src_data then
print(name .. ": no change")
else
local dst_file = io.open(dst, "wb")
dst_file:write(tmp_data)
local dst_file = assert(io.open(dst, "wb"))
dst_file:write(header)
dst_file:write(src_data)
dst_file:write(footer)
dst_file:close()
print(name .. ": updated")