Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can i eval a string 1

Status
Not open for further replies.

stillflame

Programmer
Jan 12, 2001
416
i come from perl and ruby - high level scripting languages. there is an ability in both of them to construct a string and then evaluate that string as if it were code. i find myself now writing an extension for ruby in c and i have the following problem:
there are a bunch of functions in the API i'm wrapping with names like the following:
'parse_options_get_verbosity'
'parse_options_get_max_cost'
'parse_options_get_linkage_limit'
you get the point. so i'm creating a method which takes as its argument the last part of any of those ('verbosity', 'max_cost' or 'linkage_limit' in this case), and will then concatenate that onto the string 'parse_options_get_', but i'm writing it in c, since the function calls will all be in c, therefore i can't use eval...

my c skills are lacking, i admit to that readily. there may be a simple answer to this, but even so i don't know it, and couldn't find it searching through the forum here. the only thing i could come up with would be to make a separate ruby method for each of the numerous c functions, thus allowing me to do the eval in ruby (which i will end up doing if nothing else is brought forth), but i'm hoping for something better than that. i also really hope this is possible, but the inherent differences a compiled language has from those of my experience seems to be a fairly large barrier to this. in that case, this is really just me not fully accepting/understanding those differences yet.

just in case, let it be known that from ruby, it is impossible to make any direct c calls. i have to wrap every c fuction into something callable from ruby. i thought to try to eval it somehow first and then pass that to c, but it doesn't work.

thanks for any advice,
stillflame "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
use strcat() to concat ine string to other the def. is in string.h if u r talking about that.
 
thank you, but no, i wasn't having any problems constructing the actual string to be 'eval'ed - it's the eval part i'm unsure of. my best guess at this point is that there's a fundamental difference between the languages, and that this is where having a degree computer science actually comes in handy... "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
No, there is no "eval" in C, or anything similar.

C is typically compiled to an efficient stand-alone executable, and in order to
have an "eval" the compiler would have to be packaged with the executable.

It is possible to write a sort of interpreter in C, and if you're running on
a file system with the compiler loaded you might be able to write the string
to be evaluated to a file, compile and execute the file, and get the answer
that way.

It's not so much that there's a difference between the two languages (other
than that some have eval and some don't) as that there are differences in
implementation and intended use.
 
Typically this sort of thing is done with pointers to functions stored in some kind of lookup table.

For an example, say that all of your functions have this prototype:

int (*func)(void);

(Pointer to a function returning int and accepting no arguments)

You could create numeric values that correspond to the functions with an enum:

enum func_indexes {
GET_VERBOSITY,
GET_MAX_COST,
GET_LINKAGE_LIMIT
};

Then create an array of function pointers to this function type (after definining the "parse" functions somewhere else):

int (*funcs[])(void)={
parse_options_get_verbosity,
parse_options_get_max_cost,
parse_options_get_linkage_limit
};

Then do some sort of test in the part of your code that determines which function to call:

void foo(const char *func_to_call)
{
if (strcmp("get_verbosity",func_to_call)==0) {
funcs[GET_VERBOSITY]();
} else if (strcmp("get_max_cost",func_to_call)==0) {
funcs[GET_MAX_COST]();
} else {
funcs[GET_LINKAGE_LIMIT]();
}
}

Russ
bobbitts@hotmail.com
 
AWESOME!!!!!!!! this is exactly the kind of cool, intelligent thing i needed. Thank you thank you thank you russ. i can't wait to implement it.

stillflame "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top