-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMCount.m
More file actions
164 lines (149 loc) · 5.23 KB
/
MCount.m
File metadata and controls
164 lines (149 loc) · 5.23 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
classdef MCount < handle
%MCount counts the number of M-codes you have written
%
% MCount.lines(path) count the lines you have written under
% directory filepath.
%
% MCount.reallines(path) count the lines you have written under
% directory filepath, but omits the emtpy lines and comment lines.
%
% MCount.size(path) adds up the size of your m-files under directory
% filepath
%
% All of them support multi-paths (in cells), for example,
% MCount.lines({'patha', 'pathb/pathc', 'filed'});
%
% My(zhang@zhiqiang.org) result: in recent 1 year, I wrote 19524 lines of
% M-codes totally, 12311 lines except for empty lines and comments lines,
% 603K in file size.
%
% author: zhang@zhiqiang.org, 2011,
% url: http://zhiqiang.org/blog/it/how-many-codes-have-you-written.html
% version: 2011-03-24
methods (Static = true)
function l = lines(filepath)
if nargin == 0
filepath = pwd;
end
if iscell(filepath)
l = 0;
for i = 1:numel(filepath)
l = l + MCount.lines(filepath{i});
end
return
end
l = 0;
if nargin == 0, filepath = cd; end
if ~exist(filepath, 'file')
error([filepath ' is not a file or directory']);
end
if exist(filepath, 'dir')
files = dir(filepath);
for i = 1:numel(files)
file = files(i);
if ~strcmp(file.name, '.') && ~strcmp(file.name, '..')
l = l + MCount.lines([filepath, '/', file.name]);
end
end
else
if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
l = MCount.getfilelines(filepath);
end
end
end
function l = reallines(filepath)
if nargin == 0
filepath = pwd;
end
if iscell(filepath)
l = 0;
for i = 1:numel(filepath)
l = l + MCount.reallines(filepath{i});
end
return
end
l = 0;
if nargin == 0, filepath = cd; end
if ~exist(filepath, 'file')
error([filepath ' is not a file or directory']);
end
if exist(filepath, 'dir')
files = dir(filepath);
for i = 1:numel(files)
file = files(i);
if ~strcmp(file.name, '.') && ~strcmp(file.name, '..')
l = l + MCount.reallines([filepath, '/', file.name]);
end
end
else
if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
l = MCount.getfilereallines(filepath);
end
end
end
function l = size(filepath)
if nargin == 0
filepath = pwd;
end
if iscell(filepath)
l = 0;
for i = 1:numel(filepath)
l = l + MCount.size(filepath{i});
end
return
end
l = 0;
if nargin == 0, filepath = cd; end
if ~exist(filepath, 'file')
error([filepath ' is not a file or directory']);
end
if exist(filepath, 'dir')
files = dir(filepath);
for i = 1:numel(files)
file = files(i);
if ~strcmp(file.name, '.') && ~strcmp(file.name, '..') && file.isdir == 1
l = l + MCount.size([filepath, '/', file.name]);
elseif length(file.name) > 2 && file.name(end) == 'm' && file.name(end-1) == '.'
l = l + file.bytes;
end
end
else
if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
t = dir(filepath);
l = l + t(1).bytes;
end
end
end
function l = getfilelines(filename)
fid = fopen(filename, 'rt');
if fid<0
error(['Can''t open file for reading (' filename ')'])
end
l = 0;
while(~feof(fid))
fgetl(fid);
l = l+1;
end
fclose(fid);
end
function l = getfilereallines(filename)
fid = fopen(filename, 'r');
if fid<0
error(['Can''t open file for reading (' filename ')'])
end
l = 0;
while ~feof(fid)
t = fgetl(fid);
for i = 1:numel(t)
if t(i) ~= ' '
break;
end
end
if numel(t) && i < numel(t) && t(i) ~= '%'
l = l + 1;
end
end
fclose(fid);
end
end
end