[Openmcl-devel] Using CFStrings in OpenMCL

David Steuber david at david-steuber.com
Fri Dec 17 14:12:36 PST 2004


I'm wondering if I am going about this the right way.  I'm starting to 
work through the Learning Carbon book, translating the C into Lisp.  I 
want something like the CFSTR() function that is used to turn a C 
string into a CFString for use by the Core Foundation functions and 
this was the closest I could come up with, partly inspired by code for 
WITH-CSTRS in ccl/lib/macros.lisp:

(defun make-cfstring (str)
   "Allocates a CFString object stored in a MACPTR which must be
   CFRelease(d) when no longer needed."
   (ccl::with-cstr (cstr str)
      (#_CFStringCreateWithCString (%null-ptr) cstr 
#$kCFStringEncodingMacRoman)))

(defmacro with-cfstring ((sym str) &rest body)
   "Create, use, and then release a CFString."
   (let ((ret (gensym)))
     `(let* ((,sym (make-cfstring ,str))
             (,ret (progn , at body)))
        (#_CFRelease ,sym)
        ,ret)))

(defmacro with-cfstrings (speclist &body body)
   "Create, use, and then release CFStrings."
   (ccl::with-specs-aux 'with-cfstring speclist body))

CL-USER> (with-cfstrings ((foo "foo") (bar "bar")) (print foo) (print 
bar) 2)

#<A Mac Pointer #x10DC90>
#<A Mac Pointer #x106570>

2
CL-USER>

Is the above reasonable code, or am I making work for myself?  I went 
to the trouble because I don't want memory leaks.




More information about the Openmcl-devel mailing list