summaryrefslogtreecommitdiff
path: root/src/luacov/stats.lua
blob: 30a7a48f55bb1140caf7494642d09c49660d856d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-----------------------------------------------------
-- Manages the file with statistics (being) collected.
-- In general the module requires that its property `stats.statsfile`
-- has been set to the filename of the statsfile to create, load, etc.
-- @class module
-- @name luacov.stats
local stats = {}

-----------------------------------------------------
-- Loads the stats file.
-- @return table with data. The table maps filenames to stats tables.
-- Per-file tables map line numbers to hits or nils when there are no hits.
-- Additionally, .max field contains maximum line number
-- and .max_hits contains maximum number of hits in the file.
function stats.load()
   local data = {}
   local fd = io.open(stats.statsfile, "r")
   if not fd then
      return nil
   end
   while true do
      local max = fd:read("*n")
      if not max then
         break
      end
      local skip = fd:read(1)
      if skip ~= ":" then
         break
      end
      local filename = fd:read("*l")
      if not filename then
         break
      end
      data[filename] = {
         max = max,
         max_hits = 0
      }
      for i = 1, max do
         local hits = fd:read("*n")
         if not hits then
            break
         end
         local skip = fd:read(1)
         if skip ~= " " then
            break
         end
         if hits > 0 then
            data[filename][i] = hits
            data[filename].max_hits = math.max(data[filename].max_hits, hits)
         end
      end
   end
   fd:close()
   return data
end

--------------------------------
-- Opens the statfile
-- @return filehandle
function stats.start()
   return io.open(stats.statsfile, "w")
end

--------------------------------
-- Closes the statfile
-- @param fd filehandle to the statsfile
function stats.stop(fd)
   fd:close()
end

--------------------------------
-- Saves data to the statfile
-- @param data data to store
-- @param fd filehandle where to store
function stats.save(data, fd)
   fd:seek("set")
   for filename, filedata in pairs(data) do
      local max = filedata.max
      fd:write(max, ":", filename, "\n")
      for i = 1, max do
         local hits = filedata[i]
         if not hits then
            hits = 0
         end
         fd:write(hits, " ")
      end
      fd:write("\n")
   end
   fd:flush()
end

return stats