r10608: update license
[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$
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-constant +column-length+ 10)
19 (uffi:def-constant +row-length+ 10)
20
21 (uffi:def-foreign-type long-ptr (* :long))
22
23 (defun test-array-1d ()
24   "Tests vector"
25   (let ((a (uffi:allocate-foreign-object :long +column-length+)))
26     (dotimes (i +column-length+)
27       (setf (uffi:deref-array a '(:array :long) i) (* i i)))
28     (dotimes (i +column-length+)
29       (format t "~&~D => ~D" i (uffi:deref-array a '(:array :long) i)))
30     (uffi:free-foreign-object a))
31   (values))
32
33 (defun test-array-2d ()
34   "Tests 2d array"
35   (let ((a (uffi:allocate-foreign-object 'long-ptr +row-length+)))
36     (dotimes (r +row-length+)
37       (declare (fixnum r))
38       (setf (uffi:deref-array a '(:array (* :long)) r)
39             (uffi:allocate-foreign-object :long +column-length+))
40       (let ((col (uffi:deref-array a '(:array (* :long)) r)))
41         (dotimes (c +column-length+)
42           (declare (fixnum c))
43           (setf (uffi:deref-array col '(:array :long) c) (+ (* r +column-length+) c)))))
44
45     (dotimes (r +row-length+)
46       (declare (fixnum r))
47       (format t "~&Row ~D: " r)
48       (let ((col (uffi:deref-array a '(:array (* :long)) r)))
49         (dotimes (c +column-length+)
50           (declare (fixnum c))
51           (let ((result (uffi:deref-array col '(:array :long) c)))
52             (format t "~d " result)))))
53
54     (uffi:free-foreign-object a))
55   (values))
56
57 #+examples-uffi
58 (test-array-1d)
59
60 #+examples-uffi
61 (test-array-2d)
62
63