r2912: rename .cl to .lisp
[uffi.git] / examples / arrays.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          arrays.cl
6 ;;;; Purpose:       UFFI Example file to test arrays
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: arrays.lisp,v 1.1 2002/09/30 10:02:36 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 (uffi:def-constant +column-length+ 10)
22 (uffi:def-constant +row-length+ 10)
23
24 (defun test-array-1d ()
25   "Tests vector"
26   (let ((a (uffi:allocate-foreign-object :long +column-length+)))
27     (dotimes (i +column-length+)
28       (setf (uffi:deref-array a '(:array :long) i) (* i i)))
29     (dotimes (i +column-length+)
30       (format t "~&~D => ~D" i (uffi:deref-array a '(:array :long) i)))
31     (uffi:free-foreign-object a))
32   (values))
33
34 (defun test-array-2d ()
35   "Tests 2d array"
36   (let ((a (uffi:allocate-foreign-object '(* :long) +row-length+)))
37     (dotimes (r +row-length+)
38       (declare (fixnum r))
39       (setf (uffi:deref-array a '(:array (* :long)) r)
40             (uffi:allocate-foreign-object :long +column-length+))
41       (let ((col (uffi:deref-array a '(:array (* :long)) r)))
42         (dotimes (c +column-length+)
43           (declare (fixnum c))
44           (setf (uffi:deref-array col '(:array :long) c) (+ (* r +column-length+) c)))))
45
46     (dotimes (r +row-length+)
47       (declare (fixnum r))
48       (format t "~&Row ~D: " r)
49       (let ((col (uffi:deref-array a '(:array (* :long)) r)))
50         (dotimes (c +column-length+)
51           (declare (fixnum c))
52           (let ((result (uffi:deref-array col '(:array :long) c)))
53             (format t "~d " result)))))
54
55     (uffi:free-foreign-object a))
56   (values))
57
58 #+examples-uffi
59 (test-array-1d)
60
61 #+examples-uffi
62 (test-array-2d)
63
64