-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasysloc.cpp
242 lines (200 loc) · 6.2 KB
/
easysloc.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* easysloc is a C/C++ logical lines of code counter (LLOC).
We chose an intuitive and clear definition of a logical line of code for C/C++ :
the number of instructions with the semantic delimiter ";" (see Fenton 1991, p246-253)
Additionnally easysloc gives you the number of comments.
// is one comment
/* *foo* */ is one logical and physical comment
/* this is
another comment */ is one logical comment and two physical comments
**[[http://www.monperrus.net/martin/easysloc.c.txt|download easysloc]]**
=====Related tools=====
CodeCount / CCCC / LOCC / SLOCCount
Compared to these tools, easysloc satisfies all the following conditions:
* easysloc gives the number of _logical lines of code_;
* easysloc provides the definition used for a logical line of code;
* easysloc's source code can be easily reviewed;
* easysloc is designed to be integrated via command line interface, see below.
easysloc is able to measure the linux kernel:
* linux kernel 2.0.27 : 206531 logical lines of code ([[http://www.kernel.org/pub/linux/kernel/v2.0/linux-2.0.27.tar.bz2]])
* linux kernel 2.6.19 : 1987933 logical lines of code (x 9.6 !)
=====Usage=====
Compilation :
$ gcc -o easysloc easysloc.c
$ cat myfile.c | easysloc
$ cat myfile.cpp | easysloc
$ cat myfile1.c myfile2.c myfile3.c| easysloc
$ for i in *.cpp; do echo -n "$i "; cat $i | easysloc ; done
$ for i in `find . -iname "*.c"`; do echo -n "$i "; cat $i | easysloc ; done
Warning: you should not use these two below because a syntactic error in one file could have hard consequences on others, please use the command line above instead.
$ cat *.cpp | easysloc
$ cat `find . -iname "*c"` | easysloc
= A powerful usage =
# this prints the result for each file and computes the total
$ for i in `find . -iname "*.c"`;
do
echo -n "$i ";
cat $i | easysloc ;
done \
| awk 'BEGIN{n1=0;n2=0;} {print $0;n1=n1+$2; n2=n2+$3; } END{print "Total",n1,n2;}' \
| sort -n -k 2
Tailed output for coreutils-6.7:
./src/stty.c 464 151
./src/csplit.c 511 221
./lib/fts.c 516 420
./src/tail.c 530 262
./src/tr.c 621 375
./src/od.c 667 273
./src/ptx.c 670 594
./lib/regex_internal.c 671 181
./src/pr.c 835 850
./src/sort.c 929 432
./lib/getdate.c 973 568
./src/ls.c 1346 723
./lib/regcomp.c 1378 511
./lib/regexec.c 1580 541
Total 31822 21301
Coreutils-6.7 has 31822 logical lines of code and 21301 comments.
(C) 2006-2007 Martin Monperrus
Don't hesitate to contact me
*/
#include <stdio.h>
#include <stddef.h>
#include <string>
#include <fstream>
#define NORMAL 0
#define COMMENT 1
#define COMMENT_IN 2
#define COMMENT_LINE 3
#define COMMENT_STAR 4
#define COMMENT_STAR_OUT 5
#define STRING 6
#define STRING_SPECIAL 7
#define PREPROCESSING 8
#define CHAR 9
#define CHAR_SPECIAL 10
int main(int argc, char *argv[]) {
int state = NORMAL;
long semicolon =0;
long brace =0;
long comment_logical =0;
long comment_physical =0;
long parenthesis_opened =0;
long preprocessing = 0;
char message;
std::string line;
std::fstream f;
f.open("stripped.cpp", std::ios::out);
do {
message = getchar();
line.push_back(message);
if(message == '\n')
{
line.clear();
}
if ((state!=STRING)&&(state!=CHAR)
&&(state!=STRING_SPECIAL)&&(state!=CHAR_SPECIAL)
&&(state!=PREPROCESSING)
&&(state!=COMMENT_LINE)&&(state!=COMMENT_STAR))
{
if (message==';')
{
f << line << "\n";
semicolon++;
}
//if (message==':') semicolon++;
if (message=='{') brace++;
if (message=='(') parenthesis_opened++;
}
switch (state) {
case NORMAL:
switch (message) {
case '/': state = COMMENT_IN;
break;
case '"': state = STRING;
break;
case '#': preprocessing++;state = PREPROCESSING;
break;
case '\'': state = CHAR;
break;
default: state = NORMAL;
}
break;
case PREPROCESSING:
switch (message) {
case '\n': state = NORMAL;
break;
default: state = PREPROCESSING;
}
break;
case COMMENT_IN:
switch (message) {
case '/': state = COMMENT_LINE;
break;
case '*': state = COMMENT_STAR;
break;
default: state = NORMAL;
}
break;
case COMMENT_LINE:
switch (message) {
case '\n': comment_logical++;comment_physical++; state = NORMAL;
break;
default: state = COMMENT_LINE;
}
break;
case COMMENT_STAR:
switch (message) {
case '*': state = COMMENT_STAR_OUT;
break;
case '\n': comment_physical++;
break;
default: state = COMMENT_STAR;
}
break;
case COMMENT_STAR_OUT:
switch (message) {
case '/': comment_logical++; comment_physical++;state = NORMAL;
break;
case '\n': comment_physical++;
break;
case '*': state = COMMENT_STAR_OUT;
break;
default: state = COMMENT_STAR;
}
break;
case STRING:
switch (message) {
case '\\': state = STRING_SPECIAL;
break;
case '"': state = NORMAL;
break;
default: state = STRING;
}
break;
case STRING_SPECIAL:
switch (message) {
default: state = STRING;
}
break;
case CHAR:
switch (message) {
case '\\': state = CHAR_SPECIAL;
break;
case '\'': state = NORMAL;
break;
default: state = CHAR;
}
break;
case CHAR_SPECIAL:
switch (message) {
default: state = CHAR;
}
break;
default: break;
}
}
while (message != EOF);
f.close();
printf("%ld %ld\n",semicolon,comment_physical);
return 0;
}