summaryrefslogtreecommitdiff
path: root/src/luacov/stats.lua
blob: fbf1b6a34ac9cc9b08414d1db9c789f9d63763a7 (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

module("luacov.stats", package.seeall)

local statsfile = "luacov.stats.out"

function load_stats()
   local data = {}
   local stats = io.open(statsfile, "r")
   if not stats then return data end
   while true do
      local nlines = stats:read("*n")
      if not nlines then break end
      local skip = stats:read(1)
      if skip ~= ":" then break end
      local filename = stats:read("*l")
      if not filename then break end
      data[filename] = {}
      data[filename].max = nlines
      for i = 1, nlines do
         local hits = stats:read("*n")
         if not hits then break end
         local skip = stats:read(1)
         if skip ~= " " then break end
         if hits > 0 then
            data[filename][i] = hits
         end
      end
   end
   stats:close()
   return data
end

function start_stats()
   return io.open(statsfile, "w")
end

function stop_stats(stats)
   stats:close()
end

function save_stats(data, stats)
   stats:seek("set")
   for filename, filedata in pairs(data) do
      local max = filedata.max
      stats:write(filedata.max, ":", filename, "\n")
      for i = 1, max do
         local hits = filedata[i]
         if not hits then
            hits = 0
         end
         stats:write(hits, " ")
      end
      stats:write("\n")
   end
   stats:flush()
end