From 3dd32205c2d49f126223f5e9ad083093d0636de7 Mon Sep 17 00:00:00 2001 From: "Kevin M. Rosenberg" Date: Thu, 21 Mar 2002 15:57:01 +0000 Subject: [PATCH] r1612: *** empty log message *** --- ChangeLog | 5 ++++- examples/gethostname.cl | 11 +++++++++-- src/objects.cl | 19 +++++++++++++------ tests/gethostname.cl | 11 +++++++++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 56c0d7d..fe54d30 100644 --- 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. diff --git a/examples/gethostname.cl b/examples/gethostname.cl index e90e7da..0062e40 100644 --- a/examples/gethostname.cl +++ b/examples/gethostname.cl @@ -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 ;;;; @@ -35,7 +35,14 @@ (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)) diff --git a/src/objects.cl b/src/objects.cl index b0b34f0..7f10ab5 100644 --- a/src/objects.cl +++ b/src/objects.cl @@ -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 diff --git a/tests/gethostname.cl b/tests/gethostname.cl index e90e7da..0062e40 100644 --- a/tests/gethostname.cl +++ b/tests/gethostname.cl @@ -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 ;;;; @@ -35,7 +35,14 @@ (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)) -- 2.34.1