Rewrite CREReader:ZipContentExt() function.

Technically, the only serious problem in this function was the code:
 local tmp = assert(io.popen('unzip -l \"'..fname..'\"', "r"))
 while tmp do
         s = tmp:read("*line")
         if i > 3 then tmp:close(); break; end
         i = i + 1
 end
It is meaningless to evaluate the truth of the return value of assert()
and the above code gives the impression that it relies on the
(undocumented and unreliable) fact that after tmp:close() tmp will be
nil, even though in actual fact it does because it breaks out of the
loop.
However, having looked at the function I saw that the whole thing
can be rewritten in a much simpler way:

 local tmp = assert(io.popen('unzip -l \"'..fname..'\" | head -4 | tail -1', "r"))
 s = tmp:read("*line")
 tmp:close()
This commit is contained in:
Tigran Aivazian
2012-09-24 20:07:23 +01:00
parent 6e0f0aef26
commit 43cbcbf319

View File

@@ -35,19 +35,16 @@ function CREReader:init()
self.default_font = default_font
end
end
-- inspect the zipfile content
function CREReader:ZipContentExt(fname)
local i, s = 1
local tmp = assert(io.popen('unzip -l \"'..fname..'\"', "r"))
while tmp do
s = tmp:read("*line")
if i > 3 then tmp:close(); break; end
i = i + 1
end
local tmp = assert(io.popen('unzip -l \"'..fname..'\" | head -4 | tail -1', "r"))
s = tmp:read("*line")
tmp:close()
return s and string.lower(string.match(s, ".+%.([^.]+)"))
end
-- open a CREngine supported file and its settings store
function CREReader:open(filename)
local ok