-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetree.cc
132 lines (107 loc) · 2.69 KB
/
filetree.cc
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-License-Identifier: GPL-2.0+
/*
* Fast Lines of Code Counter
*
* Copyright (C) 2021 SUSE
*
* Author: Jörg Rödel <[email protected]>
*/
#include <experimental/filesystem>
#include <iostream>
#include <iomanip>
#include "classifier.h"
#include "filetree.h"
namespace fs = std::experimental::filesystem;
loc_result::loc_result()
: code(0), comment(0), whitespace(0), files(0)
{
}
loc_result &loc_result::operator+=(const loc_result &r)
{
code += r.code;
comment += r.comment;
whitespace += r.whitespace;
files += r.files;
return *this;
}
file_entry::file_entry()
: m_type(file_type::directory)
{
}
file_entry *file_entry::get_entry(std::string name, file_type type)
{
auto p = m_entries.find(name);
if (m_type != file_type::directory)
throw std::invalid_argument("get_entry() called on regular file");
if (p == m_entries.end()) {
file_entry entry;
entry.m_type = type;
auto i = m_entries.emplace(std::make_pair(name, entry));
p = i.first;
}
return &p->second;
}
void file_entry::add_results(file_type type, const loc_result& r)
{
m_results[type] += r;
}
void file_entry::jsonize(std::ostream& os, std::string arg)
{
bool first = true;
// Open Object
os << "{";
// Print Argument, if given
if (arg.length() > 0)
os << "\"Source\":\"" << arg << "\",";
// Print type
os << "\"Type\":\"" << get_file_type_cstr(m_type) << "\"";
os << ",\"Results\":[";
for (auto &pr : m_results) {
if (!first)
os << ",";
first = false;
os << "{";
os << "\"Type\":\"" << get_file_type_cstr(pr.first) << "\",";
os << "\"Files\":" << pr.second.files << ",";
os << "\"Code\":" << pr.second.code << ",";
os << "\"Comment\":" << pr.second.comment << ",";
os << "\"Blank\":" << pr.second.whitespace;
os << "}";
}
os << "]";
if (m_type == file_type::directory) {
os << ",\"Entries\":{";
first = true;
for (auto &pe : m_entries) {
if (!first)
os << ",";
first = false;
os << "\"" << pe.first << "\":";
pe.second.jsonize(os, std::string());
}
os << "}";
}
// Close Object
os << "}";
}
void insert_file_result(file_entry *root, const struct file_result &r)
{
fs::path fpath = r.name;
fs::path ppath = fpath.parent_path();
std::string filename = fpath.filename();
struct file_entry *entry = root;
loc_result result;
result.code = r.code;
result.comment = r.comment;
result.whitespace = r.whitespace;
result.files = 1;
if (!r.duplicate)
root->add_results(r.type, result);
for (auto &de : ppath) {
entry = entry->get_entry(de, file_type::directory);
if (!r.duplicate)
entry->add_results(r.type, result);
}
entry = entry->get_entry(filename, r.type);
entry->add_results(r.type, result);
}