Path: blob/main/src/resources/pandoc/datadir/luacov/stats.lua
12923 views
-----------------------------------------------------1-- Manages the file with statistics (being) collected.2-- @class module3-- @name luacov.stats4local stats = {}56-----------------------------------------------------7-- Loads the stats file.8-- @param statsfile path to the stats file.9-- @return table with data or nil if couldn't load.10-- The table maps filenames to stats tables.11-- Per-file tables map line numbers to hits or nils when there are no hits.12-- Additionally, .max field contains maximum line number13-- and .max_hits contains maximum number of hits in the file.14function stats.load(statsfile)15local data = {}16local fd = io.open(statsfile, "r")17if not fd then18return nil19end20while true do21local max = fd:read("*n")22if not max then23break24end25if fd:read(1) ~= ":" then26break27end28local filename = fd:read("*l")29if not filename then30break31end32data[filename] = {33max = max,34max_hits = 035}36for i = 1, max do37local hits = fd:read("*n")38if not hits then39break40end41if fd:read(1) ~= " " then42break43end44if hits > 0 then45data[filename][i] = hits46data[filename].max_hits = math.max(data[filename].max_hits, hits)47end48end49end50fd:close()51return data52end5354-----------------------------------------------------55-- Saves data to the stats file.56-- @param statsfile path to the stats file.57-- @param data data to store.58function stats.save(statsfile, data)59local fd = assert(io.open(statsfile, "w"))6061local filenames = {}62for filename in pairs(data) do63table.insert(filenames, filename)64end65table.sort(filenames)6667for _, filename in ipairs(filenames) do68local filedata = data[filename]69fd:write(filedata.max, ":", filename, "\n")7071for i = 1, filedata.max do72fd:write(tostring(filedata[i] or 0), " ")73end74fd:write("\n")75end76fd:close()77end7879return stats808182