-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.cpp
126 lines (113 loc) · 3.08 KB
/
snapshot.cpp
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
#include "snapshot.h"
#include <string>
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
std::ostream& operator<<(std::ostream & os, const snapshot & s) {
os << "The summary: \n"
<< "Total line: " << s.total_line << "\n"
<< "Total comment line: " << s.total_comment << "\n"
<< "Total file number: " << s.total_file << "\n"
<< std::endl;
return os;
}
bool snapshot::is_empty_string(std::string l) {
for (auto c : l) {
if (!isspace(c))
return false;
}
return true;
}
void snapshot::get_snapshot() {
walk(root_dir);
}
void snapshot::walk(boost::filesystem::path p) {
if (boost::filesystem::is_regular_file(p)) {
// cout << p.filename() << endl;
count_comment(p);
total_file ++;
return;
}
for (auto entry : boost::filesystem::directory_iterator(p)) {
walk(entry);
}
}
// This version is using boost::filesystem::recursive_directory_iterator
// void walk2(path p) {
// for (auto entry : recursive_directory_iterator(p)) {
// if (is_regular_file(entry)) {
// cout << entry.path().filename() << endl;
// }
// }
// }
void snapshot::count_comment(boost::filesystem::path file_path) {
// std::string file_path(argv[1]);
// std::string path {file_path};
std::ifstream f(file_path.string());
std::string line;
int line_number {0};
bool in_comment {false}, start_line_occupied {false};
int total_comment_line {0}, comment_start_line {0};
while(std::getline(f, line)) {
line_number++;
decltype(line.size()) i = 0;
while (i < line.size()) {
if (!in_comment && line[i] == '/') {
if (i + 1 < line.size() && line[i+1] == '*') {
in_comment = true;
comment_start_line = line_number;
if (!is_empty_string(line.substr(0, i))) {
start_line_occupied = true;
}
i++;
}
else if (i + 1 < line.size() && line[i + 1] == '/') {
if (is_empty_string(line.substr(0, i))) {
total_comment_line += 1;
break;
}
}
i++;
continue;
} else
if (in_comment && line[i] == '*') {
if (i + 1 < line.size() && line[i + 1] == '/') {
in_comment = false;
if (is_empty_string(line.substr(i + 2))) { // end line not occupied case
if (start_line_occupied) {
total_comment_line += line_number - comment_start_line;
}
else {
total_comment_line += line_number - comment_start_line + 1;
}
break;
}
else {// end line occupied case
if (start_line_occupied) {
// if both start line and end line are occupied:
int c = line_number == comment_start_line ? 0 : line_number - comment_start_line - 1;
total_comment_line += c;
}
else {
total_comment_line += line_number - comment_start_line;
}
}
start_line_occupied = false;
i++;
}
i++;
continue;
}
i++;
}
}
f.close();
// In case the code is not correct - the coder forgot to close the comment.
if (in_comment) {
total_comment_line += line_number - comment_start_line;
}
// update the class member.
total_comment += total_comment_line;
// the line_number is the total number of line of this file.
total_line += line_number;
}