* Added ensure-char-* and def-union to documentation
* Added double-float vector example to c-test-fns
* Reworked cstring on Lispworks to have LW handle string conversion
- * First pass at with-foreign-object -- untested/unoptimized
+ * First pass at with-foreign-object -- unoptimized
+ * Added gethostname2 to examples/gethostname.cl which uses with-foreign-object
+ * Added char-array-to-pointer function to encapsulate converting a char array
+ to a pointer to char
20 Mar 2002
* Updated strings.cl so that foreign-strings are always unsigned.
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Feb 2002
;;;;
-;;;; $Id: gethostname.cl,v 1.6 2002/03/19 16:42:59 kevin Exp $
+;;;; $Id: gethostname.cl,v 1.7 2002/03/21 15:57:01 kevin Exp $
;;;;
;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
(uffi:convert-from-foreign-string name)
(error "gethostname() failed."))
(uffi:free-foreign-object name))))
-
+
+(defun gethostname2 ()
+ "Returns the hostname"
+ (uffi:with-foreign-object (name (:array :unsigned-char 256))
+ (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
+ (uffi:convert-from-foreign-string name)
+ (error "gethostname() failed."))))
+
#+test-uffi
(format t "~&Hostname: ~A" (gethostname))
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Feb 2002
;;;;
-;;;; $Id: objects.cl,v 1.11 2002/03/21 14:49:14 kevin Exp $
+;;;; $Id: objects.cl,v 1.12 2002/03/21 15:57:01 kevin Exp $
;;;;
;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
#+lispworks `(fli:make-pointer :address 0 :type ,type)
)
+(defmacro char-array-to-pointer (obj)
+ #+cmu `(alien:cast ,obj (* (alien:unsigned 8)))
+ #+lispworks `(fli:make-pointer :type '(:unsigned :char)
+ :address (fli:pointer-address ,obj))
+ #+allegro obj
+ )
+
(defmacro deref-pointer (ptr type)
"Returns a object pointed"
#+(or cmu lispworks) (declare (ignore type))
;; Simple first pass. Will later create optimized routines for
;; various platforms.
(defmacro with-foreign-object ((var type &rest etc) &rest body)
- (let ((result (gensym)))
- `(let* ((,var (allocate-foreign-object ,type ,@etc))
- (,result (progn ,@body)))
- (free-foreign-object ,var)
- ,result)))
+ `(let ((,var (allocate-foreign-object ,type ,@etc)))
+ (unwind-protect
+ (progn ,@body)
+ (free-foreign-object ,var))))
+
(defmacro with-foreign-objects (bindings &rest body)
(if bindings
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Feb 2002
;;;;
-;;;; $Id: gethostname.cl,v 1.6 2002/03/19 16:42:59 kevin Exp $
+;;;; $Id: gethostname.cl,v 1.7 2002/03/21 15:57:01 kevin Exp $
;;;;
;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
(uffi:convert-from-foreign-string name)
(error "gethostname() failed."))
(uffi:free-foreign-object name))))
-
+
+(defun gethostname2 ()
+ "Returns the hostname"
+ (uffi:with-foreign-object (name (:array :unsigned-char 256))
+ (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
+ (uffi:convert-from-foreign-string name)
+ (error "gethostname() failed."))))
+
#+test-uffi
(format t "~&Hostname: ~A" (gethostname))