0

How can i count number of functions in C program File using C# program? I have create a simple C# class to count the LOC in C file.

private bool IsInMultipleComment = false; 

private int getNumberOFFuncions(FileInfo fs)
{

    StreamReader rdr;
    int count = 0;
    string tempStr;

    // initialize
    rdr = fs.OpenText();
    tempStr = rdr.ReadLine();

    while (true)
    {
        if (tempStr == null)
            break;
        if (IsFunction(tempStr))
            count++;
        tempStr = rdr.ReadLine();
    }
    return count;

}

Supportive method:

private bool IsFunction(string line)
    {
        if (line.Contains("//"))
            return false;
        if (line.Contains("/*"))
            IsInMultipleComment = true;
        if (line.Contains("*/"))
            IsInMultipleComment = false;

        if (!IsInMultipleComment)
        {
            if (line.Contains("void") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
            {
                if (!line.Contains(";"))
                {
                    return true;
                }
            }
        }
        return false;
    }

This is how I count variables:

private int getNumberOfVariables(FileInfo fs)
    {
        StreamReader rdr;
        int count = 0;
        string tempStr;

        // initialize
        rdr = fs.OpenText();
        tempStr = rdr.ReadLine();

        while (true)
        {
            if (tempStr == null)
                break;
            count += getVariblesOfLine(tempStr);
            tempStr = rdr.ReadLine();
        }
        return count;
    }

Supportive method:

private int getVariblesOfLine(string line)
    {
        line = line.Trim();  // trim the lines

        if (line.Contains("#"))     // remove preprocessive declarations
            return 0;
        if (line.Contains("//"))
            return 0;
        if (line.Contains("/*"))
            IsInMultipleComment = true;
        if (line.Contains("*/"))
            IsInMultipleComment = false;
        if (!IsInMultipleComment)
        {
            if (line.Contains("unsigned") || line.Contains("signed") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
            {
                if (!line.Contains("("))    // remove if this is function
                {
                    Console.WriteLine(line);
                    if (line.Contains(","))     // count at multiple declarations
                    {
                        int y = line.Count(f => f == ',');
                        return y + 1;
                    }
                    return 1;
                }
            }
        }
        return 0;
    }
5
  • please show what code you have now...
    – Richard87
    Aug 3, 2015 at 17:52
  • i uploaded a class file that i created below
    – user5137308
    Aug 3, 2015 at 17:58
  • you should edit your question with the relevant code...
    – Richard87
    Aug 3, 2015 at 18:00
  • I added full class as a answer below.i am getting the result.but do not sure it is correct.
    – user5137308
    Aug 3, 2015 at 18:25
  • Surly you can manually count things yourself to verify its correctness, and then debug your code to see why something is missing? Such as, ehat if my function returned a class of myclass, or a struct of mystruct?
    – BugFinder
    Aug 3, 2015 at 21:17

1 Answer 1

0

Learn about Regex(s). There is a string pattern what a function declaration looks like. You won't catch every possible contortion of a function, you can get most of them, if you use declare functions in the general accepted .net way. Expresso is learning good learning tool for helping you to get the pattern of the Regex.

Here is a pattern to identify a function. It looks crazy but it's not. Expresso will decode it for you. It's not fully developed in that it won't catch private functions where you don't put the word private in front of it and it doesn't do protected internal. There is probably many more that it won't catch.

Regex regex = new Regex("\s*(private|public|internal|protected)\s*\w+\s+([a-zA-Z_0-9.]+)\s*\(.*\)",RegexOptions.Compiled)

if (regex.IsMatch(lineOfCode)
{
     //then it's a function
}

On another note, don't keep opening and re-reading the file. Open it once, make a pass, that's it.

I've got some code (in javascript) to do line counts and such on Csharp files you might be able to pull out some of the regex patterns. Note how the regexes are kept in an object (dictionary in .net) In javascript, /pattern/ is the same as .net "pattern"

module.exports = ( function() {

    var classifiers = [] ;

    classifiers.push(
        {
            ctype: "using",
            regex: /^(\s*using\s*)([a-zA-Z_0-9.]+)/,
            extractMethod: function(lineInfo) {
                lineInfo.extractValue = lineInfo.line.split(this.regex)[2] ;
            }
        },
        {
            ctype: "namespace",
            regex: /^(\s*namespace\s*)([a-zA-Z_0-9.]+)/,
            extractMethod: function(lineInfo) {
                lineInfo.extractValue = lineInfo.line.split(this.regex)[2] ;
            }
        },
        {
            ctype: "comment",
            regex: /^\s*\/\/[/ A-Za-z,*]*/,
            extractMethod: function(lineInfo) {
                lineInfo.extractValue = null ;
            }
        },
        {
            ctype: "n/a",
            regex: /^\s*$|^\s*[;{}]+?\s*$/,
            extractMethod: function(lineInfo) {
                lineInfo.extractValue = null ;
            }
        }
    );

    function classifyLine(line, lineNo) {
        var lineInfo = {} ;
        lineInfo.line = line ;
        lineInfo.lineNo = lineNo;

        for (var index = 0; index < classifiers.length; index++) {
            var classifier = classifiers[index];

            if (classifier.regex.test(line)) {
                lineInfo.ctype = classifier.ctype;
                lineInfo.line = line ;
                classifier.extractMethod(lineInfo) ;
                break ;
            }
        }

        if (lineInfo.ctype == undefined){
            lineInfo.ctype = "code" ;           
        }

        return lineInfo ;
     }

     return {
        classifyLine : classifyLine    
    };

} )();
2
  • One does not do stack parsing with a regex. This code is broken. It's also about interpreting C code, not C#.
    – CodeCaster
    Aug 4, 2015 at 8:56
  • use regex is very eazy than my previous one .thank you very much for reply and help me to learn new one .
    – user5137308
    Aug 15, 2015 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.