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