1:- module(os_lc, [lc_r/1, lc_r/2, lc_r/3, lc/3]). 2
3:- use_module(library(print/table)).
13lc_r(Extensions) :-
14 lc_r(Pairs0, [extensions(Extensions)]),
15 sort(2, @>=, Pairs0, Pairs),
16 maplist(arg(2), Pairs, Counts),
17 sum_list(Counts, Sum),
18 print_table(member(_-_, [(*)-Sum|Pairs])).
24lc_r(Pairs, Options) :- lc_r(., Pairs, Options).
30lc_r(Directory, Pairs, Options) :-
31 lc(Directory, Pairs, [recursive(true)|Options]).
43lc(Directory, Pairs, Options) :-
44 findall(Member0, directory_member(Directory, Member0,
45 [ access(read)|Options
46 ]), Members),
47 concurrent_maplist(member_pair, Members, Pairs).
48
49:- public member_pair/2. 50
51member_pair(Member, Member-Count) :-
52 setup_call_cleanup(
53 open(Member, read, Stream),
54 stream_count(Stream, Count),
55 close(Stream)).
64stream_count(Stream, Count) :-
65 read_line_to_codes(Stream, Codes),
66 stream_count_(Codes, Stream, 0, Count).
67
68stream_count_(end_of_file, _, Count, Count) :- !.
69stream_count_(_, Stream, Count0, Count) :-
70 Count_ is Count0 + 1,
71 read_line_to_codes(Stream, Codes),
72 stream_count_(Codes, Stream, Count_, Count)