Remove old CVS $Id$ keyword
[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 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package :cl-user)
15
16 (uffi:def-constant +column-length+ 10)
17 (uffi:def-constant +row-length+ 10)
18
19 (uffi:def-foreign-type long-ptr (* :long))
20
21 (defun test-array-1d ()
22   "Tests vector"
23   (let ((a (uffi:allocate-foreign-object :long +column-length+)))
24     (dotimes (i +column-length+)
25       (setf (uffi:deref-array a '(:array :long) i) (* i i)))
26     (dotimes (i +column-length+)
27       (format t "~&~D => ~D" i (uffi:deref-array a '(:array :long) i)))
28     (uffi:free-foreign-object a))
29   (values))
30
31 (defun test-array-2d ()
32   "Tests 2d array"
33   (let ((a (uffi:allocate-foreign-object 'long-ptr +row-length+)))
34     (dotimes (r +row-length+)
35       (declare (fixnum r))
36       (setf (uffi:deref-array a '(:array (* :long)) r)
37             (uffi:allocate-foreign-object :long +column-length+))
38       (let ((col (uffi:deref-array a '(:array (* :long)) r)))
39         (dotimes (c +column-length+)
40           (declare (fixnum c))
41           (setf (uffi:deref-array col '(:array :long) c) (+ (* r +column-length+) c)))))
42
43     (dotimes (r +row-length+)
44       (declare (fixnum r))
45       (format t "~&Row ~D: " r)
46       (let ((col (uffi:deref-array a '(:array (* :long)) r)))
47         (dotimes (c +column-length+)
48           (declare (fixnum c))
49           (let ((result (uffi:deref-array col '(:array :long) c)))
50             (format t "~d " result)))))
51
52     (uffi:free-foreign-object a))
53   (values))
54
55 #+examples-uffi
56 (test-array-1d)
57
58 #+examples-uffi
59 (test-array-2d)
60
61