X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=examples%2Fcompress.lisp;h=3f3e838fc182eced393a61d0ced7ea0c2c712342;hb=3ade95bab16abe09642554e9cbf56f117f01e507;hp=75d79c4d02ed95ee3c73341f2671db9254bd1743;hpb=a95b9a217335917d96b8c0cced4f49c3e4846115;p=uffi.git diff --git a/examples/compress.lisp b/examples/compress.lisp index 75d79c4..3f3e838 100644 --- a/examples/compress.lisp +++ b/examples/compress.lisp @@ -7,26 +7,28 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Feb 2002 ;;;; -;;;; $Id: compress.lisp,v 1.1 2002/09/30 10:02:36 kevin Exp $ +;;;; $Id$ ;;;; -;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg +;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg ;;;; -;;;; UFFI users are granted the rights to distribute and use this software -;;;; as governed by the terms of the Lisp Lesser GNU Public License -;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL. ;;;; ************************************************************************* (in-package :cl-user) -(unless (uffi:load-foreign-library - (uffi:find-foreign-library - "libz" - '("/usr/local/lib/" "/usr/lib/" "/zlib/") - :types '("so" "a" "dylib")) - :module "zlib" - :supporting-libraries '("c")) - (warn "Unable to load zlib")) - +(eval-when (:load-toplevel :execute) + (unless (uffi:load-foreign-library + #-(or macosx darwin) + (uffi:find-foreign-library + "libz" + '("/usr/local/lib/" "/usr/lib/" "/zlib/") + :types '("so" "a")) + #+(or macosx darwin) + (uffi:find-foreign-library "z" + `(,(pathname-directory *load-pathname*))) + :module "zlib" + :supporting-libraries '("c")) + (warn "Unable to load zlib"))) + (uffi:def-function ("compress" c-compress) ((dest (* :unsigned-char)) (destlen (* :long)) @@ -58,19 +60,56 @@ (uffi:free-foreign-object destlen) (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 (progn (flet ((print-results (str) (multiple-value-bind (compressed len) (compress str) + (let ((*print-length* nil)) (format t "~&(compress ~S) => " str) - (dotimes (i len) - (format t "~X" (char-code (char compressed i)))) - (format t ",~D" len)))) + (format t "~S~%" (map 'list #'char-code compressed)))))) (print-results "") (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: ;; (compress "") => 789c300001,8 ;; (compress "test") => 789c2b492d2e1045d1c1,12