xml to function call

0 weergaven
Naar het eerste ongelezen bericht

lord...@tlen.pl

ongelezen,
16 nov. 2006 08:21:0916-11-2006
aan
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

Eric Sosman

ongelezen,
16 nov. 2006 08:42:0016-11-2006
aan
lord...@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
eso...@acm-dot-org.invalid

Richard Tobin

ongelezen,
16 nov. 2006 09:15:1616-11-2006
aan
In article <1163683269.5...@f16g2000cwb.googlegroups.com>,
<lord...@tlen.pl> wrote:

>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.

Tor Rustad

ongelezen,
16 nov. 2006 18:19:5116-11-2006
aan
lord...@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

lord...@tlen.pl

ongelezen,
17 nov. 2006 03:09:5017-11-2006
aan
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:

lord...@tlen.pl

ongelezen,
17 nov. 2006 03:11:4617-11-2006
aan
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:

lord...@tlen.pl

ongelezen,
17 nov. 2006 03:15:4217-11-2006
aan
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:

Tor Rustad

ongelezen,
17 nov. 2006 14:02:2017-11-2006
aan
lord...@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"?

Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten