Cleanup eye-gouging madness around io.read calls (#7149)

* Don't reinvent the wheel when reading a one-line int or string from sysfs

* Simplify a whole other bunch of read calls
pull/7150/head
NiLuJe 3 years ago committed by GitHub
parent f52fffd467
commit dffe86dfe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -290,11 +290,10 @@ local footerTextGeneratorMap = {
local prefix = symbol_prefix[symbol_type].mem_usage
local statm = io.open("/proc/self/statm", "r")
if statm then
local infos = statm:read("*all")
local dummy, rss = statm:read("*number", "*number")
statm:close()
local rss = infos:match("^%S+ (%S+) ")
-- we got the nb of 4Kb-pages used, that we convert to Mb
rss = math.floor(tonumber(rss) * 4096 / 1024 / 1024)
rss = math.floor(rss * 4096 / 1024 / 1024)
return (prefix .. " %d"):format(rss)
end
return ""

@ -7,7 +7,7 @@ local function no() return false end
local function getProductId()
local ntxinfo_pcb = io.popen("/usr/bin/ntxinfo /dev/mmcblk0 | grep pcb | cut -d ':' -f2", "r")
if not ntxinfo_pcb then return 0 end
local product_id = tonumber(ntxinfo_pcb:read()) or 0
local product_id = ntxinfo_pcb:read("*number") or 0
ntxinfo_pcb:close()
return product_id
end
@ -20,23 +20,10 @@ local function isConnected()
if not file then return 0 end
-- 0 means not connected, 1 connected
local out = file:read("*all")
local out = file:read("*number")
file:close()
-- strip NaN from file read (ie: line endings, error messages)
local carrier
if type(out) ~= "number" then
carrier = tonumber(out)
else
carrier = out
end
-- finally return if we're connected or not
if type(carrier) == "number" then
return carrier
else
return 0
end
return out or 0
end
local function isMassStorageSupported()

@ -192,7 +192,7 @@ function CervantesPowerD:getCapacityHW()
end
function CervantesPowerD:isChargingHW()
return self:read_str_file(self.status_file) == "Charging\n"
return self:read_str_file(self.status_file) == "Charging"
end
function CervantesPowerD:beforeSuspend()

@ -95,20 +95,20 @@ function BasePowerD:turnOnFrontlight()
end
function BasePowerD:read_int_file(file)
local fd = io.open(file, "r")
local fd = io.open(file, "r")
if fd then
local int = fd:read("*all"):match("%d+")
local int = fd:read("*number")
fd:close()
return int and tonumber(int) or 0
return int or 0
else
return 0
end
end
function BasePowerD:read_str_file(file)
local fd = io.open(file, "r")
local fd = io.open(file, "r")
if fd then
local str = fd:read("*all")
local str = fd:read("*line")
fd:close()
return str
else

@ -678,7 +678,7 @@ function KindleOasis:init()
-- get rotate dev by EV=d
local std_out = io.popen("grep -e 'Handlers\\|EV=' /proc/bus/input/devices | grep -B1 'EV=d' | grep -o 'event[0-9]'", "r")
if std_out then
local rotation_dev = std_out:read()
local rotation_dev = std_out:read("*line")
std_out:close()
if rotation_dev then
self.input.open("/dev/input/"..rotation_dev)
@ -752,7 +752,7 @@ function KindleOasis2:init()
-- Get accelerometer device by looking for EV=d
local std_out = io.popen("grep -e 'Handlers\\|EV=' /proc/bus/input/devices | grep -B1 'EV=d' | grep -o 'event[0-9]\\{1,2\\}'", "r")
if std_out then
local rotation_dev = std_out:read()
local rotation_dev = std_out:read("*line")
std_out:close()
if rotation_dev then
self.input.open("/dev/input/"..rotation_dev)
@ -790,7 +790,7 @@ function KindlePaperWhite4:init()
-- So, look for a goodix TS input device (c.f., #5110)...
local std_out = io.popen("grep -e 'Handlers\\|Name=' /proc/bus/input/devices | grep -A1 'goodix-ts' | grep -o 'event[0-9]'", "r")
if std_out then
local goodix_dev = std_out:read()
local goodix_dev = std_out:read("*line")
std_out:close()
if goodix_dev then
self.touch_dev = "/dev/input/" .. goodix_dev
@ -866,7 +866,7 @@ end
local kindle_sn_fd = io.open("/proc/usid", "r")
if not kindle_sn_fd then return end
local kindle_sn = kindle_sn_fd:read()
local kindle_sn = kindle_sn_fd:read("*line")
kindle_sn_fd:close()
-- NOTE: Attempt to sanely differentiate v1 from v2,
-- c.f., https://github.com/NiLuJe/FBInk/commit/8a1161734b3f5b4461247af461d26987f6f1632e

@ -106,9 +106,9 @@ function KindlePowerD:getCapacityHW()
else
local std_out = io.popen("gasgauge-info -c 2>/dev/null", "r")
if std_out then
local result = std_out:read("*all"):match("%d+")
local result = std_out:read("*number")
std_out:close()
return result and tonumber(result) or 0
return result or 0
else
return 0
end

@ -495,7 +495,7 @@ function Kobo:getCodeName()
-- If that fails, run the script ourselves
if not codename then
local std_out = io.popen("/bin/kobo_config.sh 2>/dev/null", "r")
codename = std_out:read()
codename = std_out:read("*line")
std_out:close()
end
return codename
@ -506,7 +506,7 @@ function Kobo:getFirmwareVersion()
if not version_file then
self.firmware_rev = "none"
end
local version_str = version_file:read()
local version_str = version_file:read("*line")
version_file:close()
local i = 0
@ -527,7 +527,7 @@ local function getProductId()
if not version_file then
return "000"
end
local version_str = version_file:read()
local version_str = version_file:read("*line")
version_file:close()
product_id = string.sub(version_str, -3, -1)

@ -318,7 +318,7 @@ function KoboPowerD:getCapacityHW()
end
function KoboPowerD:isChargingHW()
return self:read_str_file(self.is_charging_file) == "Charging\n"
return self:read_str_file(self.is_charging_file) == "Charging"
end
function KoboPowerD:turnOffFrontlightHW()

@ -19,7 +19,7 @@ function Remarkable_PowerD:getCapacityHW()
end
function Remarkable_PowerD:isChargingHW()
return self:read_str_file(self.status_file) == "Charging\n"
return self:read_str_file(self.status_file) == "Charging"
end
return Remarkable_PowerD

@ -25,7 +25,7 @@ function SonyPRSTUX_PowerD:getCapacityHW()
end
function SonyPRSTUX_PowerD:isChargingHW()
return self:read_str_file(self.status_file) == "Charging\n"
return self:read_str_file(self.status_file) == "Charging"
end
return SonyPRSTUX_PowerD

@ -98,23 +98,11 @@ function NetworkListener:_getTxPackets()
-- file exists only when Wi-Fi module is loaded.
if not file then return nil end
local out = file:read("*all")
local tx_packets = file:read("*number")
file:close()
-- strip NaN from file read (i.e.,: line endings, error messages)
local tx_packets
if type(out) ~= "number" then
tx_packets = tonumber(out)
else
tx_packets = out
end
-- finally return it
if type(tx_packets) == "number" then
return tx_packets
else
return nil
end
-- Will be nil if NaN, just like we want it
return tx_packets
end
function NetworkListener:_unscheduleActivityCheck()

@ -97,7 +97,7 @@ end
-- Say who we are to Wikipedia (see https://meta.wikimedia.org/wiki/User-Agent_policy)
local USER_AGENT = T("KOReader/%1 (https://koreader.rocks/) %2",
(lfs.attributes("git-rev", "mode") == "file" and io.open("git-rev", "r"):read() or "devel"),
(lfs.attributes("git-rev", "mode") == "file" and io.open("git-rev", "r"):read("*line") or "devel"),
require('socket.http').USERAGENT:gsub(" ", "/") )
-- Codes that getUrlContent may get from requester.request()
@ -895,7 +895,7 @@ function Wikipedia:createEpub(epub_path, page, lang, with_images)
-- </guide>
local koreader_version = "KOReader"
if lfs.attributes("git-rev", "mode") == "file" then
koreader_version = "KOReader "..io.open("git-rev", "r"):read()
koreader_version = "KOReader "..io.open("git-rev", "r"):read("*line")
end
local content_opf_parts = {}
-- head

@ -622,13 +622,8 @@ function util.getFilesystemType(path)
local mounts = io.open("/proc/mounts", "r")
if not mounts then return nil end
local type
while true do
local line
for line in mounts:lines() do
local mount = {}
line = mounts:read()
if line == nil then
break
end
for param in line:gmatch("%S+") do table.insert(mount, param) end
if string.match(path, mount[2]) then
type = mount[3]

@ -10,7 +10,7 @@ function Version:getCurrentRevision()
if not self.rev then
local rev_file = io.open("git-rev", "r")
if rev_file then
self.rev = rev_file:read()
self.rev = rev_file:read("*line")
rev_file:close()
end
-- sanity check in case `git describe` failed

@ -54,7 +54,7 @@ local function systemInfo()
do
local stat = io.open("/proc/stat", "r")
if stat ~= nil then
for line in util.gsplit(stat:read("*all"), "\n", false) do
for line in stat:lines() do
local t = util.splitToArray(line, " ")
if #t >= 5 and string.lower(t[1]) == "cpu" then
local n1, n2, n3, n4
@ -82,7 +82,7 @@ local function systemInfo()
local meminfo = io.open("/proc/meminfo", "r")
if meminfo ~= nil then
result.memory = {}
for line in util.gsplit(meminfo:read("*all"), "\n", false) do
for line in meminfo:lines() do
local t = util.splitToArray(line, " ")
if #t >= 2 then
if string.lower(t[1]) == "memtotal:" then
@ -142,7 +142,7 @@ function SystemStat:appendProcessInfo()
local stat = io.open("/proc/self/stat", "r")
if stat == nil then return end
local t = util.splitToArray(stat:read("*all"), " ")
local t = util.splitToArray(stat:read("*line"), " ")
stat:close()
local n1, n2
@ -197,7 +197,7 @@ function SystemStat:appendStorageInfo()
if not std_out then return end
self:put({_("Storage information"), ""})
for line in util.gsplit(std_out:read("*all"), "\n", false) do
for line in std_out:lines() do
local t = util.splitToArray(line, "\t")
if #t ~= 4 then
self:put({_(" Unexpected"), line})

@ -24,10 +24,9 @@ local TimeSync = WidgetContainer:new{
local function currentTime()
local std_out = io.popen("date")
if std_out then
local result = std_out:read("*all")
local result = std_out:read("*line")
std_out:close()
if result ~= nil then
result = result:gsub("\n", "")
return T(_("New time is %1."), result)
end
end

@ -227,7 +227,7 @@ describe("device module", function()
}
elseif filename == "/sys/class/backlight/max77696-bl/brightness" then
return {
read = function() return "12" end,
read = function() return 12 end,
close = function() end
}
else

Loading…
Cancel
Save