r10608: update license
[uffi.git] / src / strings.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          strings.lisp
6 ;;;; Purpose:       UFFI source to handle strings, cstring and foreigns
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
14
15 (in-package #:uffi)
16
17
18 (defvar +null-cstring-pointer+
19     #+(or cmu sbcl scl) nil
20     #+allegro 0
21     #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
22     #+mcl (ccl:%null-ptr)
23 )
24
25 (defmacro convert-from-cstring (obj)
26   "Converts a string from a c-call. Same as convert-from-foreign-string, except
27 that LW/CMU automatically converts strings from c-calls."
28   #+(or cmu sbcl lispworks scl) obj
29   #+allegro 
30   (let ((stored (gensym)))
31     `(let ((,stored ,obj))
32        (if (zerop ,stored)
33            nil
34            (values (excl:native-to-string ,stored)))))
35   #+mcl 
36   (let ((stored (gensym)))
37     `(let ((,stored ,obj))
38        (if (ccl:%null-ptr-p ,stored)
39            nil
40          (values (ccl:%get-cstring ,stored)))))
41   )
42
43 (defmacro convert-to-cstring (obj)
44   #+(or cmu sbcl scl lispworks) obj
45   #+allegro
46   (let ((stored (gensym)))
47     `(let ((,stored ,obj))
48        (if (null ,stored)
49            0
50            (values (excl:string-to-native ,stored)))))
51   #+mcl
52   (let ((stored (gensym)))
53     `(let ((,stored ,obj))
54        (if (null ,stored)
55            +null-cstring-pointer+
56            (let ((ptr (new-ptr (1+ (length ,stored)))))
57              (ccl::%put-cstring ptr ,stored)
58              ptr))))
59   )
60
61 (defmacro free-cstring (obj)
62   #+(or cmu sbcl scl lispworks) (declare (ignore obj))
63   #+allegro
64   (let ((stored (gensym)))
65     `(let ((,stored ,obj))
66        (unless (zerop ,stored)
67          (ff:free-fobject ,stored))))
68   #+mcl
69   (let ((stored (gensym)))
70     `(let ((,stored ,obj))
71        (unless (ccl:%null-ptr-p ,stored)
72          (dispose-ptr ,stored))))
73   )
74
75 (defmacro with-cstring ((cstring lisp-string) &body body)
76   #+(or cmu sbcl scl lispworks)
77   `(let ((,cstring ,lisp-string)) ,@body) 
78   #+allegro
79   (let ((acl-native (gensym))
80         (stored-lisp-string (gensym)))
81     `(let ((,stored-lisp-string ,lisp-string))
82        (excl:with-native-string (,acl-native ,stored-lisp-string)
83          (let ((,cstring (if ,stored-lisp-string ,acl-native 0)))
84            ,@body))))
85   #+mcl
86   (let ((stored-lisp-string (gensym)))
87     `(let ((,stored-lisp-string ,lisp-string))
88        (if (stringp ,stored-lisp-string)
89            (ccl:with-cstrs ((,cstring ,stored-lisp-string))
90              ,@body)
91            (let ((,cstring +null-cstring-pointer+))
92              ,@body))))
93   )
94
95 (defmacro with-cstrings (bindings &rest body)
96   (if bindings
97       `(with-cstring ,(car bindings)
98         (with-cstrings ,(cdr bindings)
99           ,@body))
100       `(progn ,@body)))
101
102 ;;; Foreign string functions
103
104 (defmacro convert-to-foreign-string (obj)
105   #+lispworks
106   (let ((stored (gensym)))
107     `(let ((,stored ,obj))
108        (if (null ,stored)
109            +null-cstring-pointer+
110            (fli:convert-to-foreign-string 
111             ,stored
112             :external-format '(:latin-1 :eol-style :lf)))))
113   #+allegro
114   (let ((stored (gensym)))
115     `(let ((,stored ,obj))
116        (if (null ,stored)
117            0
118            (values (excl:string-to-native ,stored)))))
119   #+(or cmu scl)
120   (let ((size (gensym))
121         (storage (gensym))
122         (stored-obj (gensym))
123         (i (gensym)))
124     `(let ((,stored-obj ,obj))
125        (etypecase ,stored-obj
126          (null 
127           (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
128          (string
129           (let* ((,size (length ,stored-obj))
130                  (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
131             (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
132             (locally
133                 (declare (optimize (speed 3) (safety 0)))
134               (dotimes (,i ,size)
135                 (declare (fixnum ,i))
136                 (setf (alien:deref ,storage ,i)
137                       (char-code (char ,stored-obj ,i))))
138            (setf (alien:deref ,storage ,size) 0))
139          ,storage)))))
140   #+sbcl
141   (let ((size (gensym))
142         (storage (gensym))
143         (stored-obj (gensym))
144         (i (gensym)))
145     `(let ((,stored-obj ,obj))
146        (etypecase ,stored-obj
147          (null 
148           (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
149          (string
150           (let* ((,size (length ,stored-obj))
151                  (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
152             (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
153             (locally
154                 (declare (optimize (speed 3) (safety 0)))
155               (dotimes (,i ,size)
156                 (declare (fixnum ,i))
157                 (setf (sb-alien:deref ,storage ,i)
158                       (char-code (char ,stored-obj ,i))))
159               (setf (sb-alien:deref ,storage ,size) 0))
160             ,storage)))))
161   #+mcl
162   (let ((stored-obj (gensym)))
163     `(let ((,stored-obj ,obj))
164        (if (null ,stored-obj)
165            +null-cstring-pointer+
166            (let ((ptr (new-ptr (1+ (length ,stored-obj)))))
167              (ccl::%put-cstring ptr ,stored-obj)
168              ptr))))
169   )
170
171 ;; Either length or null-terminated-p must be non-nil
172 (defmacro convert-from-foreign-string (obj &key
173                                            length
174                                            (locale :default)
175                                            (null-terminated-p t))
176   #+allegro
177   (let ((stored-obj (gensym)))
178     `(let ((,stored-obj ,obj))
179        (if (zerop ,stored-obj)
180            nil
181            (if (eq ,locale :none)
182                (fast-native-to-string ,stored-obj ,length)
183                (values
184                 (excl:native-to-string
185                  ,stored-obj 
186                  ,@(when length (list :length length))
187                  :truncate (not ,null-terminated-p)))))))
188   #+lispworks
189   (let ((stored-obj (gensym)))
190     `(let ((,stored-obj ,obj))
191        (if (fli:null-pointer-p ,stored-obj)
192            nil
193            (if (eq ,locale :none)
194                (fast-native-to-string ,stored-obj ,length)
195                (fli:convert-from-foreign-string 
196                 ,stored-obj
197                 ,@(when length (list :length length))
198                 :null-terminated-p ,null-terminated-p
199                 :external-format '(:latin-1 :eol-style :lf))))))
200   #+(or cmu scl)
201   (let ((stored-obj (gensym)))
202     `(let ((,stored-obj ,obj))
203        (if (null-pointer-p ,stored-obj)
204            nil
205            (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
206                                      :length ,length
207                                      :null-terminated-p ,null-terminated-p))))
208
209   #+sbcl
210   (let ((stored-obj (gensym)))
211     `(let ((,stored-obj ,obj))
212        (if (null-pointer-p ,stored-obj)
213             nil
214             (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
215                                      :length ,length
216                                      :null-terminated-p ,null-terminated-p))))
217   #+mcl
218   (declare (ignore null-terminated-p))
219   #+mcl
220   (let ((stored-obj (gensym)))
221     `(let ((,stored-obj ,obj))
222        (if (ccl:%null-ptr-p ,stored-obj)
223            nil
224            #+(and mcl (not openmcl)) (ccl:%get-cstring
225                                       ,stored-obj 0
226                                       ,@(if length (list length) nil))
227            #+openmcl ,@(if length
228                            `((ccl:%str-from-ptr ,stored-obj ,length))
229                            `((ccl:%get-cstring ,stored-obj))))))
230   )
231
232
233 (defmacro allocate-foreign-string (size &key (unsigned t))
234   #+ignore
235   (let ((array-def (gensym)))
236     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
237        (eval `(alien:cast (alien:make-alien ,,array-def) 
238                           ,(if ,unsigned 
239                                '(* (alien:unsigned 8))
240                              '(* (alien:signed 8)))))))
241
242   #+(or cmu scl)
243   `(alien:make-alien ,(if unsigned 
244                              '(alien:unsigned 8)
245                              '(alien:signed 8))
246     ,size)
247
248   #+sbcl
249   `(sb-alien:make-alien ,(if unsigned 
250                              '(sb-alien:unsigned 8)
251                              '(sb-alien:signed 8))
252     ,size)
253
254   #+lispworks
255   `(fli:allocate-foreign-object :type 
256                                 ,(if unsigned 
257                                      ''(:unsigned :char) 
258                                    :char)
259                                 :nelems ,size)
260   #+allegro
261   (declare (ignore unsigned))
262   #+allegro
263   `(ff:allocate-fobject :char :c ,size)
264   #+mcl
265   (declare (ignore unsigned))
266   #+mcl
267   `(new-ptr ,size)
268   )
269
270 (defun foreign-string-length (foreign-string)
271   #+allegro `(ff:foreign-strlen ,foreign-string)
272   #-allegro
273   `(loop with size = 0
274     until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
275     do (incf size)
276     finally return size))
277
278
279 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
280   (let ((result (gensym)))
281     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
282             (,result (progn ,@body)))
283       (declare (dynamic-extent ,foreign-string))
284       (free-foreign-object ,foreign-string)
285       ,result)))
286
287
288 ;; Modified from CMUCL's source to handle non-null terminated strings
289 #+cmu
290 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
291   (declare (type system:system-area-pointer sap))
292   (locally
293       (declare (optimize (speed 3) (safety 0)))
294     (let ((null-terminated-length
295            (when null-terminated-p
296              (loop
297                  for offset of-type fixnum upfrom 0
298                  until (zerop (system:sap-ref-8 sap offset))
299                  finally (return offset)))))
300       (if length
301           (if (and null-terminated-length
302                    (> (the fixnum length) (the fixnum null-terminated-length)))
303               (setq length null-terminated-length))
304         (setq length null-terminated-length)))
305     (let ((result (make-string length)))
306       (kernel:copy-from-system-area sap 0
307                                     result (* vm:vector-data-offset
308                                               vm:word-bits)
309                                     (* length vm:byte-bits))
310       result)))
311
312 #+scl
313 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
314 ;; so have to iteratively copy from sap
315 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
316   (declare (type system:system-area-pointer sap))
317   (locally
318       (declare (optimize (speed 3) (safety 0)))
319     (let ((null-terminated-length
320            (when null-terminated-p
321              (loop
322                  for offset of-type fixnum upfrom 0
323                  until (zerop (system:sap-ref-8 sap offset))
324                  finally (return offset)))))
325       (if length
326           (if (and null-terminated-length
327                    (> (the fixnum length) (the fixnum null-terminated-length)))
328               (setq length null-terminated-length))
329         (setq length null-terminated-length)))
330     (let ((result (make-string length)))
331       (dotimes (i length)
332         (declare (type fixnum i))
333         (setf (char result i) (code-char (system:sap-ref-8 sap i))))
334       result)))
335
336 #+(and sbcl (not sb-unicode))
337 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
338   (declare (type sb-sys:system-area-pointer sap)
339            (type (or null fixnum) length))
340   (locally
341    (declare (optimize (speed 3) (safety 0)))
342    (let ((null-terminated-length
343           (when null-terminated-p
344             (loop
345              for offset of-type fixnum upfrom 0
346              until (zerop (sb-sys:sap-ref-8 sap offset))
347              finally (return offset)))))
348      (if length
349          (if (and null-terminated-length
350                   (> (the fixnum length) (the fixnum null-terminated-length)))
351              (setq length null-terminated-length))
352        (setq length null-terminated-length)))
353    (let ((result (make-string length)))
354        (funcall *system-copy-fn* sap 0 result +system-copy-offset+
355                 (* length +system-copy-multiplier+))
356        result)))
357
358 #+(and sbcl sb-unicode)
359 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
360   (declare (type sb-sys:system-area-pointer sap)
361            (type (or null fixnum) length))
362   (locally
363    (declare (optimize (speed 3) (safety 0)))
364    (cond
365     (null-terminated-p
366      (let ((casted (sb-alien:cast (sb-alien:sap-alien sap (* char))
367                                   #+sb-unicode sb-alien:utf8-string
368                                   #-sb-unicode sb-alien:c-string)))
369        (if length
370            (copy-seq (subseq casted 0 length))
371          (copy-seq casted))))
372     (t
373      (let ((result (make-string length)))
374        ;; this will not work in sb-unicode
375        (funcall *system-copy-fn* sap 0 result +system-copy-offset+
376                 (* length +system-copy-multiplier+))
377        result)))))
378
379
380 (eval-when (:compile-toplevel :load-toplevel :execute)
381    (def-function "strlen"
382      ((str (* :unsigned-char)))
383      :returning :unsigned-int))
384
385 (def-type char-ptr-def (* :unsigned-char))
386
387 #+(or lispworks (and allegro (not ics)))
388 (defun fast-native-to-string (s len)
389   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
390            (type char-ptr-def s))
391   (let* ((len (or len (strlen s)))
392          (str (make-string len)))
393     (declare (fixnum len)
394              (type (simple-array (signed-byte 8) (*)) str))
395     (dotimes (i len str)
396       (setf (aref str i) 
397         (uffi:deref-array s '(:array :char) i)))))
398
399 #+(and allegro ics)
400 (defun fast-native-to-string (s len)
401   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
402            (type char-ptr-def s))
403   (let* ((len (or len (strlen s)))
404          (str (make-string len)))
405       (dotimes (i len str)
406         (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))