auto conversion of lisp to c strings?

Chris Perkins <[email protected]> Fri, 28 Mar 2003 17:46:06 -0700
Newsgroups gmane.lisp.corman
Message-ID <[email protected]>
I'm working with some bindings to a POP3 SMTP library and I've been having to do an awful lot of conversion between lisp and C strings.

So, I used the advice module and wrote a little macro that lets me wrap any function call such that all strings arguments are automatically converted from Lisp to C, and any return value can be optionally converted from C to Lisp.

Unfortunately, while it's mighty handy, this all happens at runtime.  It'd be better to do this at compile time, but short of re-writing the !# reader I don't know how.

Has anyone else run across this problem?  Have a better solution?


Chris


Here is the code I wrote.  Anyone can use it freely.   

(require "advice") ; also uses c-types

(defmacro eval-if (test fun frm)
    `(let ((result ,frm))
        (if ,test (eval (,fun result))
            result)))

(defmacro cstring-advise (ret-cstr? fsym (&rest argList))
    "-- wrap any function such that lisp strings arguments are automatically converted to cstrings
         and any returned cstrings are converted to lisp strings --
        - ret-cstr?   boolean - does the function return a cstring?
        - fsym        the name of the function
        - argList     the arguments of the function
     - ex:  (cstring-advise nil win:MessageBox (hwnd lpcaption lptext utype))
     - call: (win:MessageBox (ct:int-to-foreign-ptr  0) Message  Caption (logior win:MB_OK win:MB_ICONINFORMATION))"
    
    ;;apply adv-function to filtered args collected via loop
    `(advise ,fsym argList
           (eval-if ,ret-cstr? ct:c-string-to-lisp-string
            (apply advised-function  
              (loop for arg in argList
                collect (if (stringp arg)
                              (ct:lisp-string-to-c-string arg)
                            arg))))))




------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/xaxhjB/hdqFAA/xGHJAA/SyjtlB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
[email protected]

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/