9daaa4dc776d74667f303e087d0af29a53d0e4b7
[uffi.git] / examples / atoifl.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          atoifl.cl
6 ;;;; Purpose:       UFFI Example file to atoi/atof/atol
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package :cl-user)
17
18 (uffi:def-function ("atoi" c-atoi)
19     ((str :cstring))
20   :returning :int)
21
22 (uffi:def-function ("atol" c-atol)
23     ((str :cstring))
24   :returning :long)
25
26 (uffi:def-function ("atof" c-atof)
27     ((str :cstring))
28   :returning :double)
29
30 (defun atoi (str)
31   "Returns a int from a string."
32   (uffi:with-cstring (str-cstring str)
33     (c-atoi str-cstring)))
34
35 (defun atof (str)
36   "Returns a double float from a string."
37   (uffi:with-cstring (str-cstring str)
38     (c-atof str-cstring)))
39
40 #+examples-uffi
41 (progn
42   (flet ((print-results (str)
43            (format t "~&(atoi ~S) => ~S" str (atoi str))))
44     (print-results "55")))
45
46
47 #+test-uffi
48 (progn
49   (util.test:test (atoi "123") 123 :test #'eql
50                   :fail-info "Error with atoi")
51   (util.test:test (atoi "") 0 :test #'eql
52                   :fail-info "Error with atoi")
53   (util.test:test (atof "2.23") 2.23d0 :test #'eql
54                   :fail-info "Error with atof")
55   )
56