r1604: *** empty log message ***
[uffi.git] / examples / c-test-fns.cl
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          c-test-fns.cl
6 ;;;; Purpose:       UFFI Example file for zlib compression
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: c-test-fns.cl,v 1.2 2002/03/21 09:54:34 kevin Exp $
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 (unless (uffi:load-foreign-library 
22          (make-pathname :name "c-test-fns" 
23                         :type #+(or linux unix)"so" #+(or win32 mswindows) "dll"
24                         :defaults *load-truename*)
25          :supporting-libraries '("c")
26          :force-load t)
27   (warn "Unable to load c-test-fns library"))
28
29 (uffi:def-function ("cs_to_upper" cs-to-upper)
30   ((input (* :unsigned-char)))
31   :returning :void
32   )
33
34 (defun string-to-upper (str)
35   (uffi:with-foreign-string (str-foreign str)
36     (cs-to-upper str-foreign)
37     (uffi:convert-from-foreign-string str-foreign)))
38
39 (uffi:def-function ("cs_count_upper" cs-count-upper)
40   ((input :cstring))
41   :returning :int
42   )
43
44 (defun string-count-upper (str)
45   (uffi:with-cstring (str-cstring str)
46     (cs-count-upper str-cstring)))
47
48 (uffi:def-function ("half_double_vector" half-double-vector)
49     ((size :int)
50      (vec (* :double)))
51   :returning :void)
52
53 (uffi:def-constant +double-vec-length+ 10)
54 (defun test-half-double-vector ()
55   (let ((vec (uffi:allocate-foreign-object :double +double-vec-length+))
56         results)
57     (dotimes (i +double-vec-length+)
58       (setf (uffi:deref-array vec '(:array :double) i) 
59             (coerce i 'double-float)))
60     (half-double-vector +double-vec-length+ vec)
61     (dotimes (i +double-vec-length+)
62       (push (uffi:deref-array vec '(:array :double) i) results))
63     (uffi:free-foreign-object vec)
64     (nreverse results)))
65
66 (defun t2 ()
67   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
68     (dotimes (i +double-vec-length+)
69       (setf (aref vec i) (coerce i 'double-float)))
70     (half-double-vector +double-vec-length+ vec)
71     vec))
72
73 #+cmu
74 (defun t3 ()
75   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
76     (dotimes (i +double-vec-length+)
77       (setf (aref vec i) (coerce i 'double-float)))
78     (system:without-gcing
79      (half-double-vector +double-vec-length+ (system:vector-sap vec)))
80     vec))
81     
82 #+test-uffi
83 (format t "~&(string-to-upper \"this is a test\") => ~A" 
84         (string-to-upper "this is a test"))
85
86 #+test-uffi
87 (format t "~&(string-to-upper nil) => ~A" 
88         (string-to-upper nil))
89
90 #+test-uffi
91 (format t "~&(string-count-upper \"This is a Test\") => ~A" 
92         (string-count-upper "This is a Test"))
93
94 #+test-uffi
95 (format t "~&(string-count-upper nil) => ~A" 
96         (string-count-upper nil))
97
98 #+test-uffi
99 (format t "~&Half vector: ~S" (test-half-double-vector))
100