struct

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

struct

Post 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.
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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....
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

;-)

Thanks...
-- (define? (Cornflakes))

Locked