X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=examples%2Fstrtol.lisp;h=f7c7c5e35a5835ae8cfd9daa039de5adf6e0c2a9;hb=HEAD;hp=2d54b8ba82ecd6c6f14b5db92687a1224813bcb7;hpb=ebedde4e67b858b1f65c5eb4dc7bc45978ed1e40;p=uffi.git diff --git a/examples/strtol.lisp b/examples/strtol.lisp index 2d54b8b..f7c7c5e 100644 --- a/examples/strtol.lisp +++ b/examples/strtol.lisp @@ -7,24 +7,19 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Feb 2002 ;;;; -;;;; $Id$ +;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg ;;;; -;;;; This file, part of UFFI, is Copyright (c) 2002 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) (uffi:def-foreign-type char-ptr (* :unsigned-char)) - + ;; This example does not use :cstring to pass the input string since ;; the routine needs to do pointer arithmetic to see how many characters ;; were parsed -(uffi:def-function ("strtol" c-strtol) +(uffi:def-function ("strtol" c-strtol) ((nptr char-ptr) (endptr (* char-ptr)) (base :int)) @@ -36,34 +31,34 @@ Condition flag is T if all of string parses as a long, NIL if their was no string at all, or an integer indicating position in string of first non-valid character" (let* ((str-native (uffi:convert-to-foreign-string str)) - (endptr (uffi:allocate-foreign-object 'char-ptr)) - (value (c-strtol str-native endptr base)) - (endptr-value (uffi:deref-pointer endptr 'char-ptr))) + (endptr (uffi:allocate-foreign-object 'char-ptr)) + (value (c-strtol str-native endptr base)) + (endptr-value (uffi:deref-pointer endptr 'char-ptr))) (unwind-protect - (if (uffi:null-pointer-p endptr-value) - (values value t) - (let ((next-char-value (uffi:deref-pointer endptr-value :unsigned-char)) - (chars-parsed (- (uffi:pointer-address endptr-value) - (uffi:pointer-address str-native)))) - (cond - ((zerop chars-parsed) - (values nil nil)) - ((uffi:null-char-p next-char-value) - (values value t)) - (t - (values value chars-parsed))))) + (if (uffi:null-pointer-p endptr-value) + (values value t) + (let ((next-char-value (uffi:deref-pointer endptr-value :unsigned-char)) + (chars-parsed (- (uffi:pointer-address endptr-value) + (uffi:pointer-address str-native)))) + (cond + ((zerop chars-parsed) + (values nil nil)) + ((uffi:null-char-p next-char-value) + (values value t)) + (t + (values value chars-parsed))))) (progn - (uffi:free-foreign-object str-native) - (uffi:free-foreign-object endptr))))) + (uffi:free-foreign-object str-native) + (uffi:free-foreign-object endptr))))) + + - - #+examples-uffi (progn (flet ((print-results (str) - (multiple-value-bind (result flag) (strtol str) - (format t "~&(strtol ~S) => ~S,~S" str result flag)))) + (multiple-value-bind (result flag) (strtol str) + (format t "~&(strtol ~S) => ~S,~S" str result flag)))) (print-results "55") (print-results "55.3") (print-results "a"))) @@ -71,13 +66,13 @@ of first non-valid character" #+test-uffi (progn (flet ((test-strtol (str results) - (util.test:test (multiple-value-list (strtol str)) results - :test #'equal - :fail-info "Error testing strtol"))) + (util.test:test (multiple-value-list (strtol str)) results + :test #'equal + :fail-info "Error testing strtol"))) (test-strtol "123" '(123 t)) (test-strtol "0" '(0 t)) (test-strtol "55a" '(55 2)) (test-strtol "a" '(nil nil)))) - +