uffi/examples compress.lisp,1.2,1.3 union.lisp,1.2,1.3

"Kevin M. Rosenberg" <[email protected]> Mon, 9 Dec 2002 09:30:23 -0700
Newsgroups gmane.lisp.uffi.cvs
Message-ID <[email protected]>
Update of /pubcvs/uffi/examples
In directory boa.b9.com:/tmp/cvs-serv15198/uffi/examples

Modified Files:
	compress.lisp union.lisp 
Log Message:



Index: compress.lisp
===================================================================
RCS file: /pubcvs/uffi/examples/compress.lisp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** compress.lisp	3 Dec 2002 06:58:39 -0000	1.2
--- compress.lisp	9 Dec 2002 16:30:20 -0000	1.3
***************
*** 64,67 ****
--- 64,94 ----
  	    (uffi:free-foreign-object dest)))))))
  
+ (uffi:def-function ("uncompress" c-uncompress)
+     ((dest (* :unsigned-char))
+      (destlen (* :long))
+      (source :cstring)
+      (source-len :long))
+   :returning :int
+   :module "zlib")
+ 
+ (defun uncompress (source)
+   (let* ((sourcelen (length source))
+ 	 (destsize 200000)  ;adjust as needed
+ 	 (dest (uffi:allocate-foreign-string destsize :unsigned t))
+ 	 (destlen (uffi:allocate-foreign-object :long)))
+     (setf (uffi:deref-pointer destlen :long) destsize)
+     (uffi:with-cstring (source-native source)
+       (let ((result (c-uncompress dest destlen source-native sourcelen))
+ 	    (newdestlen (uffi:deref-pointer destlen :long)))
+ 	(unwind-protect
+ 	     (if (zerop result)
+ 		 (uffi:convert-from-foreign-string 
+ 		  dest
+ 		  :length newdestlen
+ 		  :null-terminated-p nil)
+ 		 (error "zlib error, code ~D" result))
+ 	  (progn
+ 	    (uffi:free-foreign-object destlen)
+ 	    (uffi:free-foreign-object dest)))))))
  
  #+examples-uffi
***************
*** 76,79 ****
--- 103,117 ----
      (print-results "test")
      (print-results "test2")))
+ 
+ #+test-uffi
+ (progn
+   (flet ((test-compress (str)
+ 	   (multiple-value-bind (compressed len) (compress str)
+ 	     (multiple-value-bind (uncompressed len2) (uncompress compressed)
+ 	       (util.test:test str uncompressed :test #'string=
+ 			       :fail-info "Error uncompressing a compressed string")))))
+     (test-compress "")
+     (test-compress "test")
+     (test-compress "test2")))
  
  ;; Results of the above on my system:

Index: union.lisp
===================================================================
RCS file: /pubcvs/uffi/examples/union.lisp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** union.lisp	3 Dec 2002 06:58:39 -0000	1.2
--- union.lisp	9 Dec 2002 16:30:20 -0000	1.3
***************
*** 30,34 ****
      (setf (uffi:get-slot-value u 'tunion1 'uint)
        ;; little endian
!       #-(or sparc sparc-v9 powerpc ppc little-endian)
        (+ (* 1 (char-code #\A))
  	 (* 256 (char-code #\B))
--- 30,34 ----
      (setf (uffi:get-slot-value u 'tunion1 'uint)
        ;; little endian
!       #-(or sparc sparc-v9 powerpc ppc big-endian)
        (+ (* 1 (char-code #\A))
  	 (* 256 (char-code #\B))