r1612: *** empty log message ***
authorKevin M. Rosenberg <kevin@rosenberg.net>
Thu, 21 Mar 2002 15:57:01 +0000 (15:57 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Thu, 21 Mar 2002 15:57:01 +0000 (15:57 +0000)
ChangeLog
examples/gethostname.cl
src/objects.cl
tests/gethostname.cl

index 56c0d7d71556d26af26448968cb85ef79f80e8da..fe54d30af0e8c97ebd0e131d26ef6afb7e2ca0f9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,7 +12,10 @@ See TODO file -- actively maintained. Includes changes that you
        * 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.
index e90e7dadfae9d2ea52e5b66867c735b8592f5119..0062e4037035bc6f01ec3538b28498092cc1c7a4 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; 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))
 
index b0b34f07ca98fb8191238cdf7f9fd8e5285c24eb..7f10ab5ba30124e4a08f5e43edf5b7b4c3abbff8 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; 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
 ;;;;
@@ -64,6 +64,13 @@ an array of TYPE with size SIZE."
   #+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))
@@ -103,11 +110,11 @@ an array of TYPE with size SIZE."
 ;; 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
index e90e7dadfae9d2ea52e5b66867c735b8592f5119..0062e4037035bc6f01ec3538b28498092cc1c7a4 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; 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))