/// <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); } }
/// <summary> /// Finished counting lines in a file, now its time to note it /// </summary> /// <param name="sender"></param> /// <param name="counterFinishedArgs">CodeCounterFinishedEventArgs</param> private void OnCodeCountingFinished(object sender, CodeCounterFinishedEventArgs counterFinishedArgs) { lock (_lockObject) { try { if (CodeCounterFunctionTypes.Summarizing == counterFinishedArgs.Function) { if (false == _quiet) { // move cursor back to starting position, ignoring if we muck up ConsoleBackspace(_erasableTextLengh); string spaces = new string(' ', _erasableTextLengh); Console.Write(spaces); Console.WriteLine(""); } // only files we succeeding in counting are part of the file count // error files are not included in the value _filesCounted++; _codeLines += counterFinishedArgs.CodeLines; _totalLines += counterFinishedArgs.Lines; _commentLines += counterFinishedArgs.CommentLines; _statementLines += counterFinishedArgs.StatementLines; } else { _errors++; if ((counterFinishedArgs.AdditionalData is ConfigurationFileException) || (counterFinishedArgs.AdditionalData is FileTypeNotSupportedException)) { if (false == _quiet) { // move cursor back to starting position, ignoring if we muck up ConsoleBackspace(_erasableTextLengh); Console.Write("...file type not supported"); Console.WriteLine(""); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } finally { _spinnerPosition = 0; _erasableTextLengh = 0; _eventReset.Set(); } } }
/// <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); } }
/// <summary> /// Finished counting lines in a file, now its time to note it /// </summary> /// <param name="sender"></param> /// <param name="counterFinishedArgs">CodeCounterFinishedEventArgs</param> private void OnCodeCountingFinished(object sender, CodeCounterFinishedEventArgs counterFinishedArgs) { lock (_lockObject) { try { if (CodeCounterFunctionTypes.Summarizing == counterFinishedArgs.Function) { if (false == _quiet) { // move cursor back to starting position, ignoring if we muck up ConsoleBackspace(_erasableTextLengh); string spaces = new string(' ', _erasableTextLengh); Console.Write(spaces); Console.WriteLine(""); } // only files we succeeding in counting are part of the file count // error files are not included in the value _filesCounted++; _codeLines += counterFinishedArgs.CodeLines; _totalLines += counterFinishedArgs.Lines; _commentLines += counterFinishedArgs.CommentLines; _statementLines += counterFinishedArgs.StatementLines; } else { _errors++; if ((counterFinishedArgs.AdditionalData is ConfigurationFileException) || (counterFinishedArgs.AdditionalData is FileTypeNotSupportedException)) { if (false == _quiet) { // move cursor back to starting position, ignoring if we muck up ConsoleBackspace(_erasableTextLengh); Console.Write("...file type not supported"); Console.WriteLine(""); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } finally { _spinnerPosition = 0; _erasableTextLengh = 0; _eventReset.Set(); } } }
/// <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); }
/// <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); } }