r3060: *** empty log message ***
[uffi.git] / src / strings.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
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.lisp,v 1.4 2002/10/16 11:56:43 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 (defvar +null-cstring-pointer+
24     #+(or cmu sbcl scl) nil
25     #+allegro 0
26     #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
27     #+mcl (ccl:%null-ptr)
28 )
29
30 (defmacro convert-from-cstring (obj)
31   "Converts a string from a c-call. Same as convert-from-foreign-string, except
32 that LW/CMU automatically converts strings from c-calls."
33   #+(or cmu sbcl lispworks scl) obj
34   #+allegro 
35   (let ((stored (gensym)))
36     `(let ((,stored ,obj))
37        (if (zerop ,stored)
38            nil
39          (values (excl:native-to-string ,stored)))))
40   #+mcl 
41   (let ((stored (gensym)))
42     `(let ((,stored ,obj))
43        (if (ccl:%null-ptr-p ,stored)
44            nil
45          (values (ccl:%get-cstring ,stored)))))
46   )
47
48 (defmacro convert-to-cstring (obj)
49   #+(or cmu sbcl scl lispworks) obj
50   #+allegro
51   `(if (null ,obj)
52     0
53     (values (excl:string-to-native ,obj)))
54   #+mcl
55   `(if (null ,obj)
56     +null-cstring-pointer+
57     (let ((ptr (new-ptr (1+ (length ,obj)))))
58       (ccl:%put-cstring ptr ,obj)
59       ptr))
60   )
61
62 (defmacro free-cstring (obj)
63   #+(or cmu sbcl scl lispworks) (declare (ignore obj))
64   #+allegro
65   `(unless (zerop obj)
66      (ff:free-fobject ,obj))
67   #+mcl
68   `(unless (ccl:%null-ptr-p ,obj)
69      (dispose-ptr ,obj))
70   )
71
72 (defmacro with-cstring ((cstring lisp-string) &body body)
73   #+(or cmu sbcl scl lispworks)
74   `(let ((,cstring ,lisp-string)) ,@body) 
75   #+allegro
76   (let ((acl-native (gensym)))
77     `(excl:with-native-string (,acl-native ,lisp-string)
78        (let ((,cstring (if ,lisp-string ,acl-native 0)))
79          ,@body)))
80   #+mcl
81   `(if (stringp ,lisp-string)
82      (ccl:with-cstrs ((,cstring ,lisp-string))
83        ,@body)
84      (let ((,cstring +null-cstring-pointer+))
85        ,@body))
86   )
87
88 (defmacro with-cstrings (bindings &rest body)
89   (if bindings
90       `(with-cstring ,(car bindings)
91         (with-cstrings ,(cdr bindings)
92           ,@body))
93       `(progn ,@body)))
94
95 ;;; Foreign string functions
96
97 (defmacro convert-to-foreign-string (obj)
98   #+lispworks
99   `(if (null ,obj)
100        +null-cstring-pointer+
101     (fli:convert-to-foreign-string ,obj))
102   #+allegro
103   `(if (null ,obj)
104        0
105      (values (excl:string-to-native ,obj)))
106   #+(or cmu scl)
107   (let ((size (gensym))
108         (storage (gensym))
109         (i (gensym)))
110     `(etypecase ,obj
111       (null 
112        (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
113       (string
114        (let* ((,size (length ,obj))
115               (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
116          (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
117          (locally
118              (declare (optimize (speed 3) (safety 0)))
119            (dotimes (,i ,size)
120              (declare (fixnum ,i))
121              (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
122            (setf (alien:deref ,storage ,size) 0))
123          ,storage))))
124   #+sbcl
125   (let ((size (gensym))
126         (storage (gensym))
127         (i (gensym)))
128     `(etypecase ,obj
129       (null 
130        (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
131       (string
132        (let* ((,size (length ,obj))
133               (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
134          (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
135          (locally
136              (declare (optimize (speed 3) (safety 0)))
137            (dotimes (,i ,size)
138              (declare (fixnum ,i))
139              (setf (sb-alien:deref ,storage ,i) (char-code (char ,obj ,i))))
140            (setf (sb-alien:deref ,storage ,size) 0))
141          ,storage))))
142   #+mcl
143   `(if (null ,obj)
144        +null-cstring-pointer+
145      (let ((ptr (new-ptr (1+ (length ,obj)))))
146        (ccl:%put-cstring ptr ,obj)
147        ptr))
148   )
149
150
151 ;; Either length or null-terminated-p must be non-nil
152 (defmacro convert-from-foreign-string (obj &key
153                                            length
154                                            (null-terminated-p t))
155   #+allegro
156   `(if (zerop ,obj)
157        nil
158      (values (excl:native-to-string
159               ,obj 
160               ,@(if length (list :length length) (values))
161               :truncate (not ,null-terminated-p))))
162   #+lispworks
163   `(if (fli:null-pointer-p ,obj)
164        nil
165      (fli:convert-from-foreign-string 
166       ,obj
167       ,@(if length (list :length length) (values))
168       :null-terminated-p ,null-terminated-p
169       :external-format '(:latin-1 :eol-style :lf)))      
170   #+(or cmu scl)
171   `(if (null-pointer-p ,obj)
172     nil
173     (cmucl-naturalize-cstring (alien:alien-sap ,obj)
174      :length ,length
175      :null-terminated-p ,null-terminated-p))
176   #+sbcl
177   `(if (null-pointer-p ,obj)
178     nil
179     (sbcl-naturalize-cstring (sb-alien:alien-sap ,obj)
180      :length ,length
181      :null-terminated-p ,null-terminated-p))
182   #+mcl
183   (declare (ignore null-terminated-p))
184   #+mcl
185   `(if (ccl:%null-ptr-p ,obj)
186      nil
187      (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
188   )
189
190
191
192 (defmacro allocate-foreign-string (size &key (unsigned t))
193   #+(or cmu scl)
194   (let ((array-def (gensym)))
195     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
196        (eval `(alien:cast (alien:make-alien ,,array-def) 
197                           ,(if ,unsigned 
198                                '(* (alien:unsigned 8))
199                              '(* (alien:signed 8)))))))
200   #+sbcl
201   (let ((array-def (gensym)))
202     `(let ((,array-def (list 'sb-alien:array 'char ,size)))
203        (eval `(sb-alien:cast (sb-alien:make-alien ,,array-def) 
204                           ,(if ,unsigned 
205                                '(* (sb-alien:unsigned 8))
206                              '(* (sb-alien:signed 8)))))))
207   #+lispworks
208   `(fli:allocate-foreign-object :type 
209                                 ,(if unsigned 
210                                      ''(:unsigned :char) 
211                                    :char)
212                                 :nelems ,size)
213   #+allegro
214   (declare (ignore unsigned))
215   #+allegro
216   `(ff:allocate-fobject :char :c ,size)
217   #+mcl
218   (declare (ignore unsigned))
219   #+mcl
220   `(new-ptr ,size)
221   )
222
223 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
224   (let ((result (gensym)))
225     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
226             (,result (progn ,@body)))
227       (declare (dynamic-extent ,foreign-string))
228       (free-foreign-object ,foreign-string)
229       ,result)))
230
231
232 ;; Modified from CMUCL's source to handle non-null terminated strings
233 #+cmu
234 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
235   (declare (type system:system-area-pointer sap))
236   (locally
237       (declare (optimize (speed 3) (safety 0)))
238     (let ((null-terminated-length
239            (when null-terminated-p
240              (loop
241                  for offset of-type fixnum upfrom 0
242                  until (zerop (system:sap-ref-8 sap offset))
243                  finally (return offset)))))
244       (if length
245           (if (and null-terminated-length
246                    (> (the fixnum length) (the fixnum null-terminated-length)))
247               (setq length null-terminated-length))
248         (setq length null-terminated-length)))
249     (let ((result (make-string length)))
250       (kernel:copy-from-system-area sap 0
251                                     result (* vm:vector-data-offset
252                                               vm:word-bits)
253                                     (* length vm:byte-bits))
254       result)))
255
256 #+scl
257 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
258 ;; so have to iteratively copy from sap
259 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
260   (declare (type system:system-area-pointer sap))
261   (locally
262       (declare (optimize (speed 3) (safety 0)))
263     (let ((null-terminated-length
264            (when null-terminated-p
265              (loop
266                  for offset of-type fixnum upfrom 0
267                  until (zerop (system:sap-ref-8 sap offset))
268                  finally (return offset)))))
269       (if length
270           (if (and null-terminated-length
271                    (> (the fixnum length) (the fixnum null-terminated-length)))
272               (setq length null-terminated-length))
273         (setq length null-terminated-length)))
274     (let ((result (make-string length)))
275       (dotimes (i length)
276         (declare (type fixnum i))
277         (setf (char result i) (code-char (system:sap-ref-8 sap i))))
278       result)))
279
280 #+sbcl
281 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
282   (declare (type sb-sys:system-area-pointer sap))
283   (locally
284       (declare (optimize (speed 3) (safety 0)))
285     (let ((null-terminated-length
286            (when null-terminated-p
287              (loop
288                  for offset of-type fixnum upfrom 0
289                  until (zerop (sb-sys:sap-ref-8 sap offset))
290                  finally (return offset)))))
291       (if length
292           (if (and null-terminated-length
293                    (> (the fixnum length) (the fixnum null-terminated-length)))
294               (setq length null-terminated-length))
295         (setq length null-terminated-length)))
296     (let ((result (make-string length)))
297       (sb-kernel:copy-from-system-area sap 0
298                                     result (* sb-vm:vector-data-offset
299                                               sb-vm:n-word-bits)
300                                     (* length sb-vm:n-byte-bits))
301       result)))