By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

xml to function call

P: n/a
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

Nov 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
lo******@tlen.pl wrote:
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);
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.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 16 '06 #2

P: n/a
In article <11**********************@f16g2000cwb.googlegroups .com>,
<lo******@tlen.plwrote:
>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

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 16 '06 #3

P: n/a
lo******@tlen.pl wrote:
>
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]

--
Tor <torust AT online DOT no>
[1] Power-On-Self-Test

Nov 16 '06 #4

P: n/a
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:
lo******@tlen.pl wrote:

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]

--
Tor <torust AT online DOT no>
[1] Power-On-Self-Test
Nov 17 '06 #5

P: n/a
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:
In article <11**********************@f16g2000cwb.googlegroups .com>,
<lo******@tlen.plwrote:
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

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 17 '06 #6

P: n/a
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:
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.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 17 '06 #7

P: n/a
lo******@tlen.pl 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"?

--
Tor <torust AT online DOT no>

Nov 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.