Sandy
Sandy

Reputation: 21

Retrieving list of pointer variables used in a C++ program/file

Is there any way i can write a method which returns all the pointer variable names used in the C++ file.

For ex: c++ file (abc.cpp)

.......
//some code here
.....
emp* emp1 = do_something();
int a = 10;
student* std1 = getdata();

... ..

When I parse this file (abc.cpp ) i should get two variables in output.

emp1 std1

Is there any way some built in methods/procedure which tells the type of variable and list only pointer type of variables.

Thanks

Upvotes: 2

Views: 540

Answers (4)

Rontogiannis Aristofanis
Rontogiannis Aristofanis

Reputation: 9063

Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cctype>

using namespace std;

vector< string > types;
set< string > names;

int main() {
    types.push_back( "int" );
    types.push_back( "char" );
    types.push_back( "float" );
    types.push_back( "double" );
    types.push_back( "bool" );
    types.push_back( "unsigned" );
    types.push_back( "long" );
    types.push_back( "short" );
    types.push_back( "wchar_t" );

    // ect

    fstream in( "input.cpp", fstream::in );
    string row;
    string tmp;

    while( in >> tmp ) {
        if( tmp == "struct" || tmp == "class" ) {
            in >> tmp;
            string::iterator it = find( tmp.begin(), tmp.end(), '{' );
            tmp.erase( it, tmp.end() );
            types.push_back( tmp );
        }
        row += tmp;
    }

    for( int i=0; i<types.size(); ++i ) {
        int it=-1;

        while( ( it=row.find( types[ i ], it+1 ) ) ) {
            if( it == -1 ) break;
            int spos;
            for( spos=it; row[ spos ] != '*'; ++spos );
            spos++;

            string ptr;

            while( ( isalnum( ( int )row[ spos ] ) || row[ spos ] == '_' ) && spos < row.size()  ) {
                ptr += row[ spos ];
                spos++;
            }

            names.insert( ptr );
        }
    }

    for( set< string >::iterator i=names.begin(); i!=names.end(); ++i ) {
        cout << *i << " ";
    }


    return 0;
} 

What I basically do, is that I put the whole input program into a row, without spaces, then check for user defined structs or classes, which I insert into the types vector, and finally I search for each type if there exists something in the form <type>* in the row. Then, I print it.

Upvotes: 0

GuardianX
GuardianX

Reputation: 512

Sure you can't do that with standard C++ tools. My algorythm to do the job would be something like:

  1. Read the whole .cpp into std::string:

    std::ifstream ifs("filename.cpp");
    std::string str((std::istreambuf_iterator<char>(ifs)), 
                     std::istreambuf_iterator<char>());
    
  2. Find all strings in that string which placed between '*' and '=' symbols and place them in array std::vector - sure it's very rough algorythm but would suite for simple task;

  3. For every string in this array delete all white spaces.
  4. Print all array elements.

Upvotes: 0

c_k
c_k

Reputation: 1766

There is no such thing. You have to open the file and parse it's contents to find out what you want to find out. You could use Boost Regular Expressions to do this.

Upvotes: 0

Nathan Monteleone
Nathan Monteleone

Reputation: 5470

There's no built in method or procedure to do this in C++ itself. However, you could find an open source c++ parser and use it to do this.

There's a stack overflow discussion on this: Good tools for creating a C/C++ parser/analyzer

Upvotes: 1

Related Questions