search name of the function at runtime

F

foice

Hello,
I have a large .h file where many functions are defined. These functions are what the user calls at runtime to execute specific operations with my code. It's kind of tedious to remember the names of these functions and ofteneven I have to go back to the source code to remember the exact name that I gave to these functions.

So, I was wondering that I can write a function that lists all the functions defined in a given .h file, maybe even filtering according to some regular expression.
The "search" must be doable at runtime without reading the source, if possible.

Thanks for helping,
Roberto
 
S

Stefan Ram

foice said:
So, I was wondering that I can write a function that lists
all the functions defined in a given .h file, maybe even
filtering according to some regular expression.

In C++, you can read and parse the .h file and then
print all function from the syntax tree. Since it
takes time to write a parser from scratch, you might
look for a third-party library.

Also, look up »ctags« on a web encyclopedia.

You can also try to identify function declarations
using C++'s regular expression (but possibly not all
syntax rules can be expressed using regular expressions).
 
N

Nobody

So, I was wondering that I can write a function that lists all the
functions defined in a given .h file, maybe even filtering according to
some regular expression.

It sounds as if you're asking for "introspection", which C++ doesn't have.

You can certainly write such a function, but the language itself won't
provide any help, beyond what it provides for any programming task.

Writing a function which lists all functions defined in e.g. a Fortran
source file would be roughly as much work as listing all functions defined
(or declared) in a C++ source file. It's just a text-processing task, and
the fact that the text is C++ code doesn't make any difference.
 
V

Vlad from Moscow

If you use MS Visual Studio then you can use a special type of comments that consist of three slashes. They can also contain some tags that can be used to document yout functions.

четверг, 3 октÑÐ±Ñ€Ñ 2013 г., 18:09:20 UTC+4 пользователь foice напиÑал:
 
F

franceschini.roberto

Or maybe this is some kind of shared library which is used by another

program which can look up dynamically symbols at run-time and call them,

like LabVIEW or (I guess) Visual Basic? In that case you can probably use

some platform-specific utilities for listing the exported symbols in the

I guess you are right. These are functions of the "extern" type
extern "C" long double myfunc() {
}

I do not know if this makes a difference or not.

As somebody mentioned it is definitively possible by using OS tools to figure out what librararies and functions are used by an executable, so my question did not sound too much out of this world.

I can definitively try to get something done associating the executable with and external file that contains all the info that the user can be possibily interested in, but I was hoping that some tool was just available by using some built-in tools

The goal here would be to have some sort of auto-completion like the one that we have with Tab in Linux/Unix ... which I have no idea where it gets to know all the names of the commands that I can complete my string with.

I hope this makes thing more clear.

In any case, thanks a lot to everybody for the inputs.
 
J

Jorgen Grahn

I guess you are right. These are functions of the "extern" type
extern "C" long double myfunc() {
}

I do not know if this makes a difference or not.

As somebody mentioned it is definitively possible by using OS tools
to figure out what librararies and functions are used by an
executable, so my question did not sound too much out of this world.

I can definitively try to get something done associating the
executable with and external file that contains all the info that the
user can be possibily interested in, but I was hoping that some tool
was just available by using some built-in tools

The goal here would be to have some sort of auto-completion like the
one that we have with Tab in Linux/Unix ... which I have no idea where
it gets to know all the names of the commands that I can complete my
string with.

It's simple. A Unix shell searches its list of built-in commands, its
list of aliases and its list of ordinary executables, which is
generated by searching the list of directories listed in $PATH.
I hope this makes thing more clear.

Maybe ... first, do all of your functions take the same arguments and
return the same type? If not, you have a lot of work to do because
how would you call them, in practice?

Assuming they *are* all the same:

I would simply manually create a name -> function mapping. I suppose
people have shown it already:

struct { const char* name, int (*f)(long) }
functions[] = ...

(And of course you can then have code for sorting this, creating
search structures, etc.)

There *is* some work involved in this case, but isn't there work
involved in creating a user command anyway? E.g. documenting,
testing?

I resort to generating such a list with external tools in one case:
unit tests. There, it's very important to be able to add a test
quickly, and there is no other work that needs doing.

/Jorgen
 
S

Stefan Ram

Jorgen Grahn said:
struct { const char* name, int (*f)(long) }
functions[] = ...

#include <iostream>
#include <ostream>
#include <string>
#include <unordered_map>

int main()
{ ::std::unordered_map< ::std::string, void( * )() >function
{ { "alpha", [](){ ::std::cout << "Hello world\n"; }}};
function[ "alpha" ](); }
 
J

Jorgen Grahn

Jorgen Grahn said:
struct { const char* name, int (*f)(long) }
functions[] = ...

#include <iostream>
#include <ostream>
#include <string>
#include <unordered_map>

int main()
{ ::std::unordered_map< ::std::string, void( * )() >function
{ { "alpha", [](){ ::std::cout << "Hello world\n"; }}};
function[ "alpha" ](); }

If you and everyone around you know C++11 and have such a compiler,
then sure. The rest of us have to resort to C arrays, and use them
to initialize a second tier of more convenient data structures.

/Jorgen
 
S

Stefan Ram

Jorgen Grahn said:
Jorgen Grahn said:
struct { const char* name, int (*f)(long) }
functions[] = ...
#include <iostream>
#include <ostream>
#include <string>
#include <unordered_map>
int main()
{ ::std::unordered_map< ::std::string, void( * )() >function
{ { "alpha", [](){ ::std::cout << "Hello world\n"; }}};
function[ "alpha" ](); }
If you and everyone around you know C++11 and have such a compiler,
then sure. The rest of us have to resort to C arrays, and use them
to initialize a second tier of more convenient data structures.

This newsgroup here is »comp.lang.c++«, not »comp.lang.c«.
ISO/IEC 14882:2011 canceled and replaced all previous C++
standards, so that what some people still call »C++11« is
actually C++, nowadays.

OTOH, I have this C program published in my German-language
C FAQ:

/*
< filename = [stringcall.c]
encoding = [ANSI_X3.4-1968]
notation = [ISO/IEC 9899:1999 (E)]
author = [Stefan Ram] > */

#include <stdio.h> /* puts */
#include <string.h> /* strcmp */

#define F(r,n,b) r n b
#define FUNCTIONS \
F( void, beta, (){ puts( "Bravo" ); } )\
F( void, zeta, (){ puts( "Zulu" ); } )\
/**/

FUNCTIONS

#undef F
#define F(r,n,b) n,
void( * const function[] )() ={ FUNCTIONS };

#undef F
#define F(r,n,b) #n,
char const * const name[] ={ FUNCTIONS };

#undef F
int const top = sizeof name / sizeof 0[ name ];

void call( char const * const n )
{ for( int i = 0; i < top; ++i )
if( !strcmp( n, i[ name ]))i[ function ](); }

int main( void ){ call( "beta" ); call( "zeta" ); }
 
J

Jorgen Grahn

Jorgen Grahn said:
On Sun, 2013-10-06, Stefan Ram wrote:
struct { const char* name, int (*f)(long) }
functions[] = ...
#include <iostream>
#include <ostream>
#include <string>
#include <unordered_map>
int main()
{ ::std::unordered_map< ::std::string, void( * )() >function
{ { "alpha", [](){ ::std::cout << "Hello world\n"; }}};
function[ "alpha" ](); }
If you and everyone around you know C++11 and have such a compiler,
then sure. The rest of us have to resort to C arrays, and use them
to initialize a second tier of more convenient data structures.

This newsgroup here is »comp.lang.c++«, not »comp.lang.c«.
ISO/IEC 14882:2011 canceled and replaced all previous C++
standards, so that what some people still call »C++11« is
actually C++, nowadays.

Not to put too fine a point on it, but that's a pretty ridiculous
position to take.

I wouldn't phrase it like that but ... yeah ;-)

There *is* IMO a point where it becomes ridiculous to use old
constructs (like using <iostream.h> in examples here, or pre-ANSI code
in comp.lang.c) but I think C++98 is far from that stage. If people
here have a problem and don't mention C++11, I still assume C++98.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top