Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/pandoc/datadir/luacov/stats.lua
12923 views
1
-----------------------------------------------------
2
-- Manages the file with statistics (being) collected.
3
-- @class module
4
-- @name luacov.stats
5
local stats = {}
6
7
-----------------------------------------------------
8
-- Loads the stats file.
9
-- @param statsfile path to the stats file.
10
-- @return table with data or nil if couldn't load.
11
-- The table maps filenames to stats tables.
12
-- Per-file tables map line numbers to hits or nils when there are no hits.
13
-- Additionally, .max field contains maximum line number
14
-- and .max_hits contains maximum number of hits in the file.
15
function stats.load(statsfile)
16
local data = {}
17
local fd = io.open(statsfile, "r")
18
if not fd then
19
return nil
20
end
21
while true do
22
local max = fd:read("*n")
23
if not max then
24
break
25
end
26
if fd:read(1) ~= ":" then
27
break
28
end
29
local filename = fd:read("*l")
30
if not filename then
31
break
32
end
33
data[filename] = {
34
max = max,
35
max_hits = 0
36
}
37
for i = 1, max do
38
local hits = fd:read("*n")
39
if not hits then
40
break
41
end
42
if fd:read(1) ~= " " then
43
break
44
end
45
if hits > 0 then
46
data[filename][i] = hits
47
data[filename].max_hits = math.max(data[filename].max_hits, hits)
48
end
49
end
50
end
51
fd:close()
52
return data
53
end
54
55
-----------------------------------------------------
56
-- Saves data to the stats file.
57
-- @param statsfile path to the stats file.
58
-- @param data data to store.
59
function stats.save(statsfile, data)
60
local fd = assert(io.open(statsfile, "w"))
61
62
local filenames = {}
63
for filename in pairs(data) do
64
table.insert(filenames, filename)
65
end
66
table.sort(filenames)
67
68
for _, filename in ipairs(filenames) do
69
local filedata = data[filename]
70
fd:write(filedata.max, ":", filename, "\n")
71
72
for i = 1, filedata.max do
73
fd:write(tostring(filedata[i] or 0), " ")
74
end
75
fd:write("\n")
76
end
77
fd:close()
78
end
79
80
return stats
81
82