r1636: Updated/cleaned def-array to def-array-pointer
[uffi.git] / src / strings.cl
1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          strings.cl
6 ;;;; Purpose:       UFFI source to handle strings, cstring and foreigns
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: strings.cl,v 1.13 2002/03/23 12:58:12 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22
23 (def-constant +null-cstring-pointer+
24     #+cmu nil
25     #+allegro 0
26     #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char)))
27
28 (defmacro convert-from-cstring (obj)
29   "Converts a string from a c-call. Same as convert-from-foreign-string, except
30 that LW/CMU automatically converts strings from c-calls."
31   #+cmu obj
32   #+lispworks obj
33   #+allegro 
34   (let ((stored (gensym)))
35     `(let ((,stored ,obj))
36        (if (zerop ,stored)
37            nil
38          (values (excl:native-to-string ,stored)))))
39   )
40
41 (defmacro convert-to-cstring (obj)
42   #+cmu obj
43   #+lispworks obj
44   #+allegro
45   `(if (null ,obj)
46     0
47     (values (excl:string-to-native ,obj)))
48   )
49
50 (defmacro free-cstring (obj)
51   #+cmu (declare (ignore obj))
52   #+lispworks (declare (ignore obj))
53   #+allegro
54   `(unless (zerop obj)
55      (ff:free-fobject ,obj))
56   )
57
58 (defmacro with-cstring ((cstring lisp-string) &body body)
59   #+cmu
60   `(let ((,cstring ,lisp-string)) ,@body) 
61   #+lispworks
62   `(let ((,cstring ,lisp-string)) ,@body) 
63   #+allegro
64   (let ((acl-native (gensym)))
65     `(excl:with-native-string (,acl-native ,lisp-string)
66        (let ((,cstring (if ,lisp-string ,acl-native 0)))
67          ,@body)))
68   )
69
70
71 ;;; Foreign string functions
72
73 (defmacro convert-to-foreign-string (obj)
74   #+lispworks
75   `(if (null ,obj)
76        +null-cstring-pointer+
77     (fli:convert-to-foreign-string ,obj))
78   #+allegro
79   `(if (null ,obj)
80        0
81      (values (excl:string-to-native ,obj)))
82   #+cmu
83   (let ((size (gensym))
84         (storage (gensym))
85         (i (gensym)))
86     `(etypecase ,obj
87       (null 
88        (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
89       (string
90        (let* ((,size (length ,obj))
91               (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
92          (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
93          (locally
94              (declare (optimize (speed 3) (safety 0)))
95            (dotimes (,i ,size)
96              (declare (fixnum ,i))
97              (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
98            (setf (alien:deref ,storage ,size) 0))
99          ,storage))))
100       )
101
102
103 ;; Either length or null-terminated-p must be non-nil
104 (defmacro convert-from-foreign-string (obj &key
105                                            length
106                                            (null-terminated-p t))
107   #+allegro
108   `(if (zerop ,obj)
109        nil
110      (values (excl:native-to-string
111               ,obj 
112               ,@(if length (list :length length) (values))
113               :truncate (not ,null-terminated-p))))
114   #+lispworks
115   `(if (fli:null-pointer-p ,obj)
116        nil
117      (fli:convert-from-foreign-string 
118       ,obj
119       ,@(if length (list :length length) (values))
120       :null-terminated-p ,null-terminated-p
121       :external-format '(:latin-1 :eol-style :lf)))      
122   #+cmu
123   `(if (null-pointer-p ,obj)
124     nil
125     (cmucl-naturalize-cstring (alien:alien-sap ,obj)
126      :length ,length
127      :null-terminated-p ,null-terminated-p))
128   )
129
130
131
132 (defmacro allocate-foreign-string (size &key (unsigned t))
133   #+cmu
134   (let ((array-def (gensym)))
135     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
136        (eval `(alien:cast (alien:make-alien ,,array-def) 
137                           ,(if ,unsigned 
138                                '(* (alien:unsigned 8))
139                              '(* (alien:signed 8)))))))
140   #+lispworks
141   `(fli:allocate-foreign-object :type 
142                                 ,(if unsigned 
143                                      ''(:unsigned :char) 
144                                    :char)
145                                 :nelems ,size)
146   #+allegro
147   (declare (ignore unsigned))
148   #+allegro
149   `(ff:allocate-fobject :char :c ,size)
150   )
151
152 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
153   #-(or lispworks cmu) 
154   (let ((result (gensym)))
155     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
156             (,result (progn ,@body)))
157       (declare (dynamic-extent ,foreign-string))
158       (free-foreign-object ,foreign-string)
159       ,result)))
160
161
162 ;; Modified from CMUCL's source to handle non-null terminated strings
163 #+cmu
164 (defun cmucl-naturalize-cstring (sap &key 
165                                            length
166                                            (null-terminated-p t))
167   (declare (type system:system-area-pointer sap))
168   (locally
169       (declare (optimize (speed 3) (safety 0)))
170     (let ((null-terminated-length
171            (when null-terminated-p
172              (loop
173                  for offset of-type fixnum upfrom 0
174                  until (zerop (system:sap-ref-8 sap offset))
175                  finally (return offset)))))
176       (if length
177           (if (and null-terminated-length
178                    (> (the fixnum length) (the fixnum null-terminated-length)))
179               (setq length null-terminated-length))
180         (setq length null-terminated-length)))
181     (let ((result (make-string length)))
182       (kernel:copy-from-system-area sap 0
183                                     result (* vm:vector-data-offset
184                                               vm:word-bits)
185                                     (* length vm:byte-bits))
186       result)))