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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
******************************************************************************
This file is part of BigWoo.NET.
BigWoo.NET is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
BigWoo.NET is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BigWoo.NET; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
architected and written by
matt raffel
matt.raffel@mindspring.com
copyright (c) 2008 by matt raffel unless noted otherwise
******************************************************************************
*/
#region using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using CodeCounter.Library;
#endregion
namespace CodeCounter
{
/// <summary>
/// This is the class that processes a file. If manages the "conversation" with the
/// ICodeCounterLogic implementation occuring during reading through the file.
/// </summary>
public class CodeCounterEngine
{
#region private data
private FileInfo _fileInfo = null;
private long _statementLines = 0L;
private long _totalLines = 0L;
private long _codeLines = 0L;
private long _commentLines = 0L;
private long _errors = 0L;
private CodeCounterLogicImplementerList _implementerList = null;
#endregion
#region properties
/// <summary>
/// FileInfo instance of the file we want to count code.
/// </summary>
public FileInfo File
{
get { return _fileInfo; }
set { _fileInfo = value; }
}
#endregion
#region event firing helpers
/// <summary>
/// Fires the OnStartEvent if there are listeners. This
/// event is fired once per file.
/// </summary>
/// <param name="startArgs">CodeCounterUpdateEventArgs</param>
private void FireOnStartEvent(CodeCounterUpdateEventArgs startArgs)
{
if (null != OnStart)
{
OnStart(this, startArgs);
}
}
/// <summary>
/// Fires OnUpdate event if there are listeners. This event
/// is fired approx every 250 lines read in the file.
/// </summary>
/// <param name="updateArgs">CodeCounterUpdateEventArgs</param>
private void FireOnUpdateEvent(CodeCounterUpdateEventArgs updateArgs)
{
if (null != OnUpdate)
OnUpdate(this, updateArgs);
}
/// <summary>
/// Fires OnFinish event if there are listeners. This event
/// is fired once per file
/// </summary>
/// <param name="finishedArgs">CodeCounterFinishedEventArgs</param>
private void FireOnFinishedEvent(CodeCounterFinishedEventArgs finishedArgs)
{
if (null != OnFinish)
OnFinish(this, finishedArgs);
}
#endregion
#region private methods
/// <summary>
/// Find the ICodeCounterLogic implementation that can work with the file type we
/// have found.
/// </summary>
/// <exception cref="FileTypeNotSupportedException">Thrown if no logic is found for the given file type</exception>
/// <exception cref="ConfigurationFileException">Thrown if no logic is defined in the config file</exception>
/// <returns>ICodeCounterLogic</returns>
private ICodeCounterLogic GetFileProcessor(FileInfo info)
{
if (null == _implementerList)
{
_implementerList = CodeCounterLogicImplementerList.LoadFromConfigFile();
if (0 == _implementerList.Count)
throw new ConfigurationFileException("No code counter logic found.");
}
ICodeCounterLogic handler = null;
foreach (CodeCounterLogicImplementer implementer in _implementerList)
{
ICodeCounterLogic potentialHandler = implementer.Implementer;
if (true == potentialHandler.CanProcessFile(info.Name))
{
handler = potentialHandler;
break;
}
}
if (null == handler)
throw FileTypeNotSupportedException.CreateException(info);
return handler;
}
/// <summary>
/// Each count is file specific. It is up to the consumer to process
/// those values
/// </summary>
private void InitializeCounters()
{
_statementLines = 0L;
_totalLines = 0L;
_codeLines = 0L;
_commentLines = 0L;
_errors = 0L;
}
#endregion
#region public methods
/// <summary>
/// The heart of the counting of lines
/// </summary>
public void Count()
{
// nothing to process if we have no file name
if (null == _fileInfo)
return;
// ensures member data is reset to default
InitializeCounters();
long linesRead = 0L;
// event arg initialization
CodeCounterFinishedEventArgs finishedArgs = new CodeCounterFinishedEventArgs(_fileInfo, CodeCounterFunctionTypes.Error);
CodeCounterUpdateEventArgs startArgs = new CodeCounterUpdateEventArgs(_fileInfo, CodeCounterFunctionTypes.ProcessingFiles);
CodeCounterUpdateEventArgs updateEventArg = new CodeCounterUpdateEventArgs(_fileInfo, CodeCounterFunctionTypes.ProcessingFiles);
try
{
// let console know we found a file
FireOnStartEvent(startArgs);
// find the appropriate handler for the type
ICodeCounterLogic processor = GetFileProcessor(_fileInfo);
bool _processorDeterminesEmpty = processor.EngineCanDetermineBlankLines();
// allow the ICodeCounterLogic implementation a chance to
// do something with the file
processor.PrefileProcessing(_fileInfo.FullName);
// now we can read through each line and count what it contains
using (TextReader fileReader = _fileInfo.OpenText())
{
string line = "";
while (null != (line = fileReader.ReadLine()))
{
linesRead++;
long mod = (linesRead % 250L);
if (0L == mod)
{
updateEventArg.Lines = linesRead;
FireOnUpdateEvent(updateEventArg);
}
string trimmed = line.Trim();
// when the processor does not know or care how empty lines are determined
// we will do it by testing for null or empty line.
if ((true == _processorDeterminesEmpty) && (true == string.IsNullOrEmpty(trimmed)))
continue;
// now we are ready to let the implemention decide what the line is
CodeCounterLineType lineType = processor.LineType(trimmed);
switch (lineType)
{
case CodeCounterLineType.Statement:
case CodeCounterLineType.Code:
_codeLines++;
break;
case CodeCounterLineType.StatementAndComment:
case CodeCounterLineType.CodeAndComment:
_codeLines++;
_commentLines++;
break;
case CodeCounterLineType.CommentOnly:
_commentLines++;
break;
default:
break;
}
if (CodeCounterLineType.EmptyLine != lineType)
_totalLines++;
}
// yay we are done
fileReader.Close();
// allow the counter implemenation any final moments
processor.PostfileProcessing(_fileInfo.FullName);
finishedArgs.Function = CodeCounterFunctionTypes.Summarizing;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
finishedArgs.Function = CodeCounterFunctionTypes.Error;
finishedArgs.AdditionalData = ex;
}
finally
{
finishedArgs.FileInfo = _fileInfo;
finishedArgs.CodeLines = _codeLines;
finishedArgs.CommentLines = _commentLines;
finishedArgs.StatementLines = _statementLines;
finishedArgs.Lines = _totalLines;
_fileInfo = null;
FireOnFinishedEvent(finishedArgs);
}
}
#endregion
#region event member delcaration
public event CodeCounterUpdateEvent OnStart;
public event CodeCounterUpdateEvent OnUpdate;
public event CodeCounterFinishedEvent OnFinish;
#endregion
}
}
|