Page 1 of 1

struct

Posted: Sun Mar 21, 2004 1:05 am
by newdep
Hello All,

Has anyone a tip on how i can call a C struct created under newlisp
from within a C function?

Norman.

Posted: Sun Mar 21, 2004 12:00 pm
by newdep
Hello Lutz,

I spitted trought the manual and broke my head on how to create a
C Struct under newlisp (even tried using 'dump with pointer redirection)
but its not working ;-)

I looped into some other Lisp distributions and there are some using it..

Have you thought about extenting newlisp with creation of a C struct which
can be called from within a 'imported C function?

i.e.

(set-struct 'testing
(set 'one 2)
(set 'two "world"))

>(get-string testing'two)
"world"

>testing
12347653 (pointer)

>(cfunction 1 2 3 testing)



Norman

Posted: Sun Mar 21, 2004 12:43 pm
by newdep
Mmm perhpas a nelwisp struct function besides the context function?

Or even a possebility for a context to be used as a struct inside C functions?

Im just thinking out loud....

Posted: Sun Mar 21, 2004 1:49 pm
by Lutz
This is what you can do:

Lets say you have the following 'C' struct

typedef struct
{
int ivalue;
double fvalue;
} MYSTRUCT;

and a 'C' function doing something to that structure snd returning a string pointer:

char * foo(MYSTYRUCT * data)
{
char * result;

data->ivalue = 2 * data->ivalue;
data->fvalue = 10 + data->fvalue;

result = "OK";

return(result);
}

In newLISP you can constuct this structure:

(set 'mystruct (pack "ld lf" 123 4.56))

(import "mylib" "foo")

(get-string (foo mystruct)) => "OK"

(unpack mystruct "ld lf") => (246 14.56)


Lutz

Posted: Sun Mar 21, 2004 6:23 pm
by newdep
;-)

Thanks...