xml to function call

L

lordkain

Hello all,

I have a problem which I cant seem to solve, I dont know if it is
solvable.. it should though.

What I want is to call a function, whith x arguments on base of an xml
file. In the xml file are the specification to call the function.

XML FILE
---------------
<function name="test">
<arg nr=1 type="string">hello world</arg>
<arg nr=2 type="int">5</arg>
<arg nr=3 type="long">123456</arg>
</function>

IMPLEMENTATION of the function test:
-------------------------------------------------------
void test(char * arg1, int arg2, long arg3);

Which should result in the function call
-----------------------------------------------------
test("hello world", 5, 123456);



So far i have some source-code to translate the xml to a structure, but
when i call the function i must translate the datatype explicite, this
is what i dont want to do..

void * arg[10] = {NULL}; // maximal 10 arguments
char * arg1 = "hello world";
int arg1 = 5;
long arg1 = 123456;

arg[1] = a1;
arg[2] = &a2;
arg[3] = &a3;

// here i translate the datatype from the arg array to its datatype, i
dont really want this. i want to automaticly castsing here of some
sort..
test( (char *) arg[1],
*((int *)arg[2]),
*((long *)arg[3])
);

Cheerz, David
 
E

Eric Sosman

Hello all,

I have a problem which I cant seem to solve, I dont know if it is
solvable.. it should though.

What I want is to call a function, whith x arguments on base of an xml
file. In the xml file are the specification to call the function.

XML FILE
---------------
<function name="test">
<arg nr=1 type="string">hello world</arg>
<arg nr=2 type="int">5</arg>
<arg nr=3 type="long">123456</arg>
</function>

IMPLEMENTATION of the function test:

C has no mechanism for building function calls at run-time;
the number and types of the arguments supplied in a call is
fixed in stone when the call is compiled.

Some C compilers provide non-standard extensions to C that
support this kind of thing, and that may satisfy your need;
check your compiler's documentation. Be warned, though, that
you will then be writing "C with extras" instead of C, and this
is likely to tie your code to that one particular compiler for
eternity. Only you can decide whether sacrificing portability
is worth while.

A portable alternative (not entirely satisfactory, perhaps)
would be to gather the "arguments" into a struct or array of
some kind, along with information about their number and types.
Then pass a pointer to this "argument block" as the single
actual argument to your function:

struct arg {
enum { STRING, INT, LONG, END } type;
union {
char *sval;
int ival;
long lval;
} value;
};

struct arg arglist[4];
arglist[0].type = STRING;
arglist[0].value.sval = "Hello, world";
arglist[1].type = INT;
arglist[1].value.ival = 5;
arglist[2].type = LONG;
arglist[2].value.lval = 123456L;
arglist[3].type = END;
test (arglist);

(Most likely, all those tedious assignments to arglist[] would
be part of your XML-reading code.)
 
R

Richard Tobin

void * arg[10] = {NULL}; // maximal 10 arguments
char * arg1 = "hello world";
int arg1 = 5;
long arg1 = 123456;

arg[1] = a1;
arg[2] = &a2;
arg[3] = &a3;

// here i translate the datatype from the arg array to its datatype, i
dont really want this. i want to automaticly castsing here of some
sort..
test( (char *) arg[1],
*((int *)arg[2]),
*((long *)arg[3])
);

I suggest having the code that generates the structure also generate a
wrapper for each function. For the function test it would generate

void /* or whatever */ wrapped_test(void *args[])
{
test((char *)arg[1], *((int *)arg[2]), *((long *)arg[3]));
}

and you would then call

wrapped_test(arg);

-- Richard
 
T

Tor Rustad

What I want is to call a function, whith x arguments on
base of an xml file. In the xml file are the specification
to call the function.

XML FILE
---------------
<function name="test">
<arg nr=1 type="string">hello world</arg>
<arg nr=2 type="int">5</arg>
<arg nr=3 type="long">123456</arg>
</function>

You can write a test program:
#define MAX_ARG 31 /* 127 on C99*/
union
{
char *string;
int integer;
long long_int;
....
} arg[MAX_ARG];

.....

/* load_xml */
/* map_func_to_tag */
/* convert arg list */

switch ( func_tag )
{
case FUNC_TEST:
test(arg[1].string, arg[2].integer, arg[3].long_int);
break;
...
}

I don't know what you need that XML for, sometimes I
write test modules which perform stress-test, others do
fault-injection.. etc. Also, some functions should call a
self-test module on initialization... ala POST. [1]
 
L

lordkain

Hi Rustad,

Thank for you reply, actually what i am trying to do is to make a
testtool. In this testtool i want to call interface functions from a
few modules. The testdata is in an xml-file. If i put the arguments
type also in the xml file this brings me one step closer to my goal.
The testtool doesnt need adjustment if i want to call a module. If the
interface function change i just need to adjust the xml file, and
nothing more.

Cheerz, David


Tor Rustad schreef:
What I want is to call a function, whith x arguments on
base of an xml file. In the xml file are the specification
to call the function.

XML FILE
---------------
<function name="test">
<arg nr=1 type="string">hello world</arg>
<arg nr=2 type="int">5</arg>
<arg nr=3 type="long">123456</arg>
</function>

You can write a test program:
#define MAX_ARG 31 /* 127 on C99*/
union
{
char *string;
int integer;
long long_int;
....
} arg[MAX_ARG];

....

/* load_xml */
/* map_func_to_tag */
/* convert arg list */

switch ( func_tag )
{
case FUNC_TEST:
test(arg[1].string, arg[2].integer, arg[3].long_int);
break;
...
}

I don't know what you need that XML for, sometimes I
write test modules which perform stress-test, others do
fault-injection.. etc. Also, some functions should call a
self-test module on initialization... ala POST. [1]
 
L

lordkain

Hi Richard,

Thanks for your reply. This was indeed my second option, thanks for
your input, i really appreciate it.

Cheerz, David


Richard Tobin schreef:
void * arg[10] = {NULL}; // maximal 10 arguments
char * arg1 = "hello world";
int arg1 = 5;
long arg1 = 123456;

arg[1] = a1;
arg[2] = &a2;
arg[3] = &a3;

// here i translate the datatype from the arg array to its datatype, i
dont really want this. i want to automaticly castsing here of some
sort..
test( (char *) arg[1],
*((int *)arg[2]),
*((long *)arg[3])
);

I suggest having the code that generates the structure also generate a
wrapper for each function. For the function test it would generate

void /* or whatever */ wrapped_test(void *args[])
{
test((char *)arg[1], *((int *)arg[2]), *((long *)arg[3]));
}

and you would then call

wrapped_test(arg);

-- Richard
 
L

lordkain

Hi Eric,

I was affraid that C didn't supported what i want. To bad C doesn't
support function overloading of some kind. Thank you very much for your
reaction.

Cheerz, David


Eric Sosman schreef:
 
T

Tor Rustad

(e-mail address removed) skrev:
The testtool doesnt need adjustment if i want to call a
module. If the interface function change i just need
to adjust the xml file, and nothing more.


What are you trying to do here...?! is it to call a function,
which you haven't provided a function prototype for at
compile-time?

What do you mean by "call a module"?
 

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,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top