Re: auto conversion of lisp to c strings?

JP Massar <[email protected]> Mon, 31 Mar 2003 13:44:57 -0800
Newsgroups gmane.lisp.corman
Message-ID <[email protected]>
At 05:46 PM 3/28/03 -0700, Chris Perkins wrote:
>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.

Why can't you write a similar macro that, if one of the arguments is 
textually a string,
converts the string to a C string at macroexpand (compile) time,
and if it is not textually a string does what you are doing now - test
the arg at run time and convert if necessary?




>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))))))
>
>
>
>
>
>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/



------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/xaxhjB/hdqFAA/VygGAA/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/