コード例 #1
0
 public void AddStats(SourceStats stats)
 {
     CommentedLines    += stats.CommentedLines;
     EmptyLines        += stats.EmptyLines;
     PreprocessorLines += stats.PreprocessorLines;
     CodeLines         += stats.CodeLines;
     TotalLines        += stats.TotalLines;
 }
コード例 #2
0
    private static ProcessedFile ProcessFile(string fileName, LanguageProfile profile)
    {
        var stats = new SourceStats();

        var fileStream = new FileStream(fileName, FileMode.Open);

        var inMultilineComment = false;

        foreach (var line in fileStream.ReadLines(Encoding.UTF8))
        {
            stats.TotalLines++;

            switch (GetLineType(line, profile, inMultilineComment))
            {
            case LineType.Empty:
                stats.EmptyLines++;
                break;

            case LineType.Source:
                break;

            case LineType.Preprocessor:
                stats.PreprocessorLines++;
                break;

            case LineType.MultilineCommentBegin:
                stats.CommentedLines++;
                inMultilineComment = true;
                break;

            case LineType.MultilineCommentEnd:
            case LineType.MultilineCommentInterspersedEnd:
                stats.CommentedLines++;
                inMultilineComment = false;
                break;

            case LineType.MultilineCommentInterspersedBegin:
                stats.CodeLines++;
                inMultilineComment = true;
                break;

            case LineType.MultilineCommentInterspersedEndContinued:
                stats.CodeLines++;
                inMultilineComment = false;
                break;

            case LineType.Comment:
            case LineType.MultilineCommentMiddle:
            case LineType.MultilineCommentInline:
                stats.CommentedLines++;
                break;
            }
        }

        fileStream.Close();

        stats.CodeLines = stats.TotalLines - stats.CommentedLines - stats.EmptyLines;

        var file = new ProcessedFile(new FileInfo(fileName), profile, stats);

        return(file);
    }
コード例 #3
0
    public void Start()
    {
        _cancellationTokenSource = new CancellationTokenSource();
        var cancellationToken = _cancellationTokenSource.Token;
        var progressReporter  = new ProgressReporter();

        Stats         = new SourceStats();
        _wasCancelled = false;
        _profileStats.Clear();
        _sourceFiles.Clear();

        var task = Task.Factory.StartNew(() =>
        {
            progressReporter.ReportProgress(() =>
            {
                if (BuildingFileList != null)
                {
                    BuildingFileList(this, EventArgs.Empty);
                }
            });

            var files = BuildFileList(progressReporter, cancellationToken);

            var count = 0;

            foreach (var file in files)
            {
                ProcessedFile processedFile;

                try
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    processedFile = ProcessFile(file.Key, file.Value);
                }
                catch (OperationCanceledException)
                {
                    _wasCancelled = true;
                    return;
                }

                _sourceFiles.Add(file.Key, processedFile);

                Stats.AddStats(processedFile.Stats);

                if (!_profileStats.ContainsKey(processedFile.Profile))
                {
                    _profileStats[processedFile.Profile] = new SourceStats();
                }
                _profileStats[processedFile.Profile].AddStats(processedFile.Stats);

                TotalFiles++;
                TotalBytes += processedFile.Info.Length;

                count++;

                progressReporter.ReportProgress(() =>
                {
                    if (ProgressChanged != null)
                    {
                        ProgressChanged(this, new CounterProgressEventArgs(file.Key, count * 100 / files.Count));
                    }
                });
            }
        }, cancellationToken);

        progressReporter.RegisterContinuation(task, () =>
        {
            if (Completed != null)
            {
                Completed(this, new CounterCompletedEventArgs(_wasCancelled));
            }
        });
    }