-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.cpp
More file actions
180 lines (149 loc) · 4.65 KB
/
Copy pathFileUtils.cpp
File metadata and controls
180 lines (149 loc) · 4.65 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "FileUtils.h"
#include <string_view>
#include <algorithm>
#include <filesystem>
#include <mutex>
#include <future>
namespace fs = std::filesystem;
std::mutex g_mutex;
constexpr LineCounts& LineCounts::operator+=(const LineCounts& rhs)
{
blank_lines += rhs.blank_lines;
code_lines += rhs.code_lines;
comment_lines += rhs.comment_lines;
return *this;
}
std::ostream& operator<<(std::ostream& os, const LineCounts& line_count)
{
return os << "Line Count Statistics\nBlank lines: " << line_count.blank_lines
<< "\nCode lines: " << line_count.code_lines
<< "\nComment lines: " << line_count.comment_lines;
}
static inline std::wstring_view TrimLeft(const std::wstring& str) {
auto begin = std::find_if_not(str.begin(), str.end(), ::isspace);
return (begin < str.end()) ?
std::wstring_view(&*begin, str.end() - begin)
: std::wstring_view();
}
void CountLinesInFile(const std::string& file_path, LineCounts& total_counts)
{
std::wifstream file(file_path);
if (file.fail())
{
std::cerr << "Couldn't open a file with path : " << file_path << std::endl;
return;
}
LineCounts counts;
std::wstring line;
bool in_multiline_comment = false;
while (std::getline(file, line))
{
std::wstring_view stripped_line = TrimLeft(line);
// Check for blank lines
if (stripped_line.empty())
{
counts.blank_lines++;
continue;
}
// Check for multiline comments
if (in_multiline_comment)
{
counts.comment_lines++;
if (stripped_line.find(L"*/") != std::string::npos)
{
in_multiline_comment = false;
}
continue;
}
if (stripped_line.find(L"/*") == 0)
{
in_multiline_comment = true;
counts.comment_lines++;
if (stripped_line.find(L"*/") != std::string::npos)
{
in_multiline_comment = false;
}
continue;
}
// Check for single-line comments
if (stripped_line.find(L"//") == 0)
{
counts.comment_lines++;
continue;
}
// Otherwise, it's a code line
counts.code_lines++;
}
// Check for last empty line
if (line.empty())
{
counts.blank_lines++;
}
std::lock_guard lg(g_mutex);
total_counts += counts;
}
static inline void ProcessFile(const fs::directory_entry& entry, LineCounts& total_counts, size_t& total_files)
{
if (entry.is_regular_file())
{
std::string extension = entry.path().extension().string();
if (extension == ".h" || extension == ".hpp" || extension == ".c" || extension == ".cpp")
{
total_files++;
CountLinesInFile(entry.path().string(), total_counts);
}
}
}
size_t ProcessDirectory(const std::string& root_folder, LineCounts& total_counts)
{
try
{
size_t total_files = 0;
for (const auto& entry : fs::recursive_directory_iterator(root_folder))
{
ProcessFile(entry, total_counts, total_files);
}
return total_files;
}
catch (std::filesystem::filesystem_error& er)
{
std::cerr << er.what() << std::endl;
return 0;
}
}
size_t ProcessDirectoryConcurrently(const std::string& root_folder, LineCounts& total_counts)
{
size_t total_files = 0;
try
{
std::vector<std::future<void>> tasks;
for (const auto& entry : fs::recursive_directory_iterator(root_folder))
{
if (entry.is_regular_file())
{
std::string extension = entry.path().extension().string();
if (extension == ".h" || extension == ".hpp" || extension == ".c" || extension == ".cpp")
{
total_files++;
tasks.push_back(std::async(std::launch::async, CountLinesInFile,
entry.path().string(), std::ref(total_counts)));
}
}
}
for (auto& task : tasks) {
task.wait();
}
return total_files;
}
catch (std::filesystem::filesystem_error& er)
{
std::cerr << er.what() << std::endl;
return total_files;
}
}
void SaveToStream(std::ostream& os, const LineCounts& lc, const size_t total_files,
std::chrono::duration<double> executionTime)
{
os << "Total number of processed files: " << total_files << '\n' << lc << '\n'
<< "Execution time: " << executionTime.count() << " seconds\n";
}