2d54b8ba82ecd6c6f14b5db92687a1224813bcb7
[uffi.git] / examples / strtol.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          strtol.cl
6 ;;;; Purpose:       UFFI Example file to strtol, uses pointer arithmetic
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package :cl-user)
20
21 (uffi:def-foreign-type char-ptr (* :unsigned-char))
22   
23 ;; This example does not use :cstring to pass the input string since
24 ;; the routine needs to do pointer arithmetic to see how many characters
25 ;; were parsed
26
27 (uffi:def-function ("strtol" c-strtol) 
28     ((nptr char-ptr)
29      (endptr (* char-ptr))
30      (base :int))
31   :returning :long)
32
33 (defun strtol (str &optional (base 10))
34   "Returns a long int from a string. Returns number and condition flag.
35 Condition flag is T if all of string parses as a long, NIL if
36 their was no string at all, or an integer indicating position in string
37 of first non-valid character"
38   (let* ((str-native (uffi:convert-to-foreign-string str))
39          (endptr (uffi:allocate-foreign-object 'char-ptr))
40          (value (c-strtol str-native endptr base))
41          (endptr-value (uffi:deref-pointer endptr 'char-ptr)))
42
43     (unwind-protect
44          (if (uffi:null-pointer-p endptr-value)
45              (values value t)
46              (let ((next-char-value (uffi:deref-pointer endptr-value :unsigned-char))
47                    (chars-parsed (- (uffi:pointer-address endptr-value)
48                                     (uffi:pointer-address str-native))))
49                (cond
50                  ((zerop chars-parsed)
51                   (values nil nil))
52                  ((uffi:null-char-p next-char-value)
53                   (values value t))
54                  (t
55                   (values value chars-parsed)))))
56       (progn
57         (uffi:free-foreign-object str-native)
58         (uffi:free-foreign-object endptr)))))
59
60  
61   
62 #+examples-uffi
63 (progn
64   (flet ((print-results (str)
65            (multiple-value-bind (result flag) (strtol str)
66              (format t "~&(strtol ~S) => ~S,~S" str result flag))))
67     (print-results "55")
68     (print-results "55.3")
69     (print-results "a")))
70
71 #+test-uffi
72 (progn
73   (flet ((test-strtol (str results)
74            (util.test:test (multiple-value-list (strtol str)) results
75                            :test #'equal
76                            :fail-info "Error testing strtol")))
77     (test-strtol "123" '(123 t))
78     (test-strtol "0" '(0 t))
79     (test-strtol "55a" '(55 2))
80     (test-strtol "a" '(nil nil))))
81
82                            
83