Click here to Skip to main content
6,629,885 members and growing! (21,307 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Utilities     Beginner License: The Code Project Open License (CPOL)

Parse C++ header and CPP files for class names and their methods

By rj45

Parsing C++ header and CPP files for class names and methods.
C# 2.0, .NET, Dev
Version:4 (See All)
Posted:19 Feb 2009
Views:6,171
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.50 Rating: 1.67 out of 5
1 vote, 50.0%
1

2
1 vote, 50.0%
3

4

5

CodeCoverage1

Introduction

This tool parses out your classes and their methods from header and CPP files. It turns out parsing out classes and methods in your C++ projects is actually very easy (if you don't need it to be life or death accurate). I'm not sure this is particularly useful; I had a specific use for this, and I just found it kind of interesting how easy it was. I'm sorry the formatting is wrong here, but CodeProject ate my whole article twice now, so I'm sorry!

Within our loop of headers and the lines in those headers, we simply check for the following; all other lines will be added and parsed later:

if (lines[lineCount].Contains(@"//"))
{
    continue;
}

if (lines[lineCount].Contains(@"friend"))
{
    continue;
}

if (lines[lineCount].Replace(" ", "").Contains(@"template<class"))
{
    continue;
}

if (lines[lineCount].Contains(@"_T("))
{
    continue;
}

To parse the CPP, your loop will exclude only these keywords (I know I'm missing tons of cases here, but surprisingly it seems to work not too shabby :)):

if (lines[lineCount].Contains(@";"))
{
    continue;
}

if (lines[lineCount].Contains(@"//"))
{
    continue;
}

if (lines[lineCount].Contains(@"_T("))
{
    continue;
}

if (lines[lineCount].Contains(@"if"))
{
    continue;
}

if (lines[lineCount].Contains(@"="))
{
    continue;
}

if (lines[lineCount].Contains(@"."))
{
    continue;
}

if (lines[lineCount].Contains(@"."))
{
    continue;
}

if (lines[lineCount].Contains(@"return"))
{
    continue;
}

Obviously, there is a lot missing to make this useful for most people, but it is kind of interesting and perhaps could be used as a learning tool. Also, you could structure this code much cleaner by searching for exclusion keywords and having them in a list, that might be a good exercise for someone.

It also should check for duplicates to handle overloaded methods and many more such issues.

Cheers..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

rj45


Member

Location: Canada Canada

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralMy vote of 1 Pinmemberwtwhite18:37 23 Feb '09  
GeneralRe: My vote of 1 Pinmemberrj4516:09 25 Feb '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Feb 2009
Editor: Smitha Vijayan
Copyright 2009 by rj45
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project