/// <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);
        }
예제 #2
0
        private ICodeCounterLogic GetImplementerInstance()
        {
            if (null == _logic)
            {
                _logic = AssemblyUtility.InvokeDefaultCtor(ImplementerType) as ICodeCounterLogic;
            }

            return(_logic);
        }
        /// <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);
            }
        }