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