2

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.

  • 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

Sandy
  • 21
  • 2

4 Answers4

1

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

Community
  • 1
  • 1
Nathan Monteleone
  • 5,430
  • 29
  • 43
0

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.

c_k
  • 1,746
  • 1
  • 20
  • 35
0

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.
GuardianX
  • 515
  • 11
  • 29
  • Yes, I just screwed the term "tools". I meant that it can't be done by using standard language constructions, only with help of libraries (std for example). – GuardianX Sep 25 '12 at 19:03
0

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.

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58