From 43cbcbf319f28b7afb0c01caaa2d03853c098a93 Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Mon, 24 Sep 2012 20:07:23 +0100 Subject: [PATCH] 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() --- crereader.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/crereader.lua b/crereader.lua index eb9a245cf..4a55200c4 100644 --- a/crereader.lua +++ b/crereader.lua @@ -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