-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (136 loc) · 5.07 KB
/
main.cpp
File metadata and controls
167 lines (136 loc) · 5.07 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
// ---- Count Lines of Code in Python, and JavaScript Files ----
// by Brendan Murphy - b-murphy.ca
#include <iostream>
#include "vector"
#include "string"
#include "algorithm"
#include "fstream"
void count_lines(std::string file_path) {
std::cout << "\n ---------------- count_lines -------------------------- \n";
std::cout << "File Name/Path: " << file_path;
int code_lines = 0;
int not_code_lines = 0;
int only_comments = 0;
int blank_lines = 0;
bool doc_switch = false;
std::string js = ".js";
std::vector<std::string> lines; // won't have zero but not enough slots if [] left empty
// problem solved, do not initiate with [], and = {} is not needed
std::string file_path_rev = file_path; // seems to make a copy
std::reverse(file_path_rev.begin(), file_path_rev.end()); // reverse a string
std::string sub = file_path_rev.substr(0, 3); // sub is the last 3 characters of file path (reverse here)
std::reverse(sub.begin(), sub.end()); // reverse sub back to forward order
if (sub == js) {
std::string comm_symbol = "//";
std::string line;
std::ifstream rfile;
rfile.open(file_path); // works on string or char file path
if (rfile.is_open()) {
while (std::getline(rfile, line)) {
lines.push_back(line);
}
rfile.close();
}
for (auto i: lines) {
i.erase(remove_if(i.begin(), i.end(), isspace), i.end()); //strip white space on ends
auto k = i;
// Handle doc strings as comments
std::string sub2 = k.substr(0, 2);
if (sub2 == "/*" or doc_switch) {
not_code_lines += 1;
if (!k.empty()) { // can use k != '' but this is suggested
only_comments += 1;
} else {
blank_lines += 1;
}
doc_switch = true;
if (k.find("*/") != std::string::npos) {
doc_switch = false;
}
continue;
}
if (!k.empty()) {
if (sub2 != comm_symbol) {
code_lines += 1;
} else {
not_code_lines += 1;
only_comments += 1;
}
} else {
not_code_lines += 1;
blank_lines += 1;
}
}
} else {
std::string comm_symbol = "#";
std::string line;
std::ifstream rfile;
rfile.open(file_path); // works on string or char file path
if (rfile.is_open()) {
while (std::getline(rfile, line)) {
lines.push_back(line);
}
rfile.close();
}
for (auto i: lines) {
i.erase(remove_if(i.begin(), i.end(), isspace), i.end()); //strip white space on ends
auto k = i;
// Handle doc strings as comments
// Turn off
std::string sub2 = k.substr(0, 3); // as in py k[:3]
// Have to make a sub3 for py k[3:]
std::string sub3;
if (k.length() >= 3) {
sub3 = k.substr(3, k.length() - 1); // as in py k[3:]
}
if (k.find("'''") != std::string::npos and doc_switch or
k.find(R"(""")") != std::string::npos and doc_switch) {
doc_switch = false;
not_code_lines += 1;
only_comments += 1;
continue;
}
// Turn on
if (sub2 == "'''" and !doc_switch or sub2 == R"(""")" and !doc_switch) {
if (sub3.find("'''") != std::string::npos or sub3.find(R"(""")") != std::string::npos) {
not_code_lines += 1;
only_comments += 1;
continue;
} else {
doc_switch = true;
}
}
if (doc_switch) {
not_code_lines += 1;
if (!k.empty()) {
only_comments += 1;
} else {
blank_lines += 1;
}
} else {
if (!k.empty()) {
std::string k_first = k.substr(0, 1);
if (k_first != comm_symbol) {
code_lines += 1;
} else {
not_code_lines += 1;
only_comments += 1;
}
} else {
not_code_lines += 1;
blank_lines += 1;
}
}
}
}
std::cout << "\nLines of Code: " << code_lines;
std::cout << "\nLines that are not code: " << not_code_lines;
std::cout << "\nTotal Lines: " << lines.size();
std::cout << "\n\nBlank Lines: " << blank_lines;
std::cout << "\nLines that are just comments: " << only_comments;
std::cout << "\n------------------------------------------";
}
int main() {
count_lines("path_to_file");
return 0;
}