-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.erl
66 lines (55 loc) · 2.06 KB
/
misc.erl
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
-module(misc).
-compile(export_all).
remove_extension(Filename) ->
hd(string:split(Filename, ".", trailing)).
remove_directory(Path) ->
case string:find(Path, "/", trailing) of
nomatch -> Path;
Matching -> string:substr(Matching, 2)
end.
write_to_file(Filename, Content) ->
write_to_file(Filename, Content, write).
write_to_file(Filename, Content, Mode) ->
case file:open(Filename, [Mode]) of
{ok, Fd} ->
file:write(Fd, Content),
file:close(Fd);
{Status, Msg} ->
io:format("Error opening file ~s: ~s", [Status, Msg])
end.
parse(Expression) ->
{ok, Tokens, _} = erl_scan:string(Expression++"."),
{ok, Parsed} = erl_parse:parse_exprs(Tokens),
{value, Result, _} = erl_eval:exprs(Parsed, []),
Result.
report_coverage_to_csv(Map, Filename) ->
StatLine = maps:fold(fun(_, V, Acc) -> integer_to_list(V) ++ ";" ++ Acc end, "\n", Map), % "~n" does not work here, only "\n"
case filelib:is_regular(Filename) of
%% No header needed
true -> misc:write_to_file(Filename, StatLine, append);
%% header needed
false ->
begin
HeaderLine = maps:fold(fun(K, _, Acc) -> if is_atom(K) -> atom_to_list(K);
true -> K
end ++ ";" ++ Acc end, "\n", Map),
misc:write_to_file(Filename, HeaderLine ++ StatLine, append)
end
end
.
%% FILL UP INITIAL MAP WITH KEY-0 PAIRS
init_stat_map(List) ->
lists:foldr(fun(Elem, Acc) -> maps:put(Elem, 0, Acc) end, #{}, List).
hline() ->
io:format("------------------------------------------------------------------------~n").
update_map(K, V, Acc) ->
case maps:is_key(K, Acc) of
true -> maps:update_with(K, fun(X) -> X + V end, Acc);
false -> Acc
end.
process_trace(Trace, Loc) ->
ReportMap = get(Loc),
UpdatedReportMap = maps:fold(fun(K, V, Acc) ->
update_map(K, V, Acc)
end, ReportMap, Trace),
put(Loc, UpdatedReportMap).