r10834: 2005-11-14 Kevin Rosenberg (kevin@rosenberg.net)
[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 (defmacro with-foreign-strings (bindings &body body)
288   `(with-foreign-string ,(car bindings)
289     ,@(if (cdr bindings)
290           `((with-foreign-strings ,(cdr bindings) ,@body))
291           body)))
292
293 ;; Modified from CMUCL's source to handle non-null terminated strings
294 #+cmu
295 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
296   (declare (type system:system-area-pointer sap))
297   (locally
298       (declare (optimize (speed 3) (safety 0)))
299     (let ((null-terminated-length
300            (when null-terminated-p
301              (loop
302                  for offset of-type fixnum upfrom 0
303                  until (zerop (system:sap-ref-8 sap offset))
304                  finally (return offset)))))
305       (if length
306           (if (and null-terminated-length
307                    (> (the fixnum length) (the fixnum null-terminated-length)))
308               (setq length null-terminated-length))
309         (setq length null-terminated-length)))
310     (let ((result (make-string length)))
311       (kernel:copy-from-system-area sap 0
312                                     result (* vm:vector-data-offset
313                                               vm:word-bits)
314                                     (* length vm:byte-bits))
315       result)))
316
317 #+scl
318 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
319 ;; so have to iteratively copy from sap
320 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
321   (declare (type system:system-area-pointer sap))
322   (locally
323       (declare (optimize (speed 3) (safety 0)))
324     (let ((null-terminated-length
325            (when null-terminated-p
326              (loop
327                  for offset of-type fixnum upfrom 0
328                  until (zerop (system:sap-ref-8 sap offset))
329                  finally (return offset)))))
330       (if length
331           (if (and null-terminated-length
332                    (> (the fixnum length) (the fixnum null-terminated-length)))
333               (setq length null-terminated-length))
334         (setq length null-terminated-length)))
335     (let ((result (make-string length)))
336       (dotimes (i length)
337         (declare (type fixnum i))
338         (setf (char result i) (code-char (system:sap-ref-8 sap i))))
339       result)))
340
341 #+(and sbcl (not sb-unicode))
342 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
343   (declare (type sb-sys:system-area-pointer sap)
344            (type (or null fixnum) length))
345   (locally
346    (declare (optimize (speed 3) (safety 0)))
347    (let ((null-terminated-length
348           (when null-terminated-p
349             (loop
350              for offset of-type fixnum upfrom 0
351              until (zerop (sb-sys:sap-ref-8 sap offset))
352              finally (return offset)))))
353      (if length
354          (if (and null-terminated-length
355                   (> (the fixnum length) (the fixnum null-terminated-length)))
356              (setq length null-terminated-length))
357        (setq length null-terminated-length)))
358    (let ((result (make-string length)))
359        (funcall *system-copy-fn* sap 0 result +system-copy-offset+
360                 (* length +system-copy-multiplier+))
361        result)))
362
363 #+(and sbcl sb-unicode)
364 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
365   (declare (type sb-sys:system-area-pointer sap)
366            (type (or null fixnum) length))
367   (locally
368    (declare (optimize (speed 3) (safety 0)))
369    (cond
370     (null-terminated-p
371      (let ((casted (sb-alien:cast (sb-alien:sap-alien sap (* char))
372                                   #+sb-unicode sb-alien:utf8-string
373                                   #-sb-unicode sb-alien:c-string)))
374        (if length
375            (copy-seq (subseq casted 0 length))
376          (copy-seq casted))))
377     (t
378      (let ((result (make-string length)))
379        ;; this will not work in sb-unicode
380        (funcall *system-copy-fn* sap 0 result +system-copy-offset+
381                 (* length +system-copy-multiplier+))
382        result)))))
383
384
385 (eval-when (:compile-toplevel :load-toplevel :execute)
386    (def-function "strlen"
387      ((str (* :unsigned-char)))
388      :returning :unsigned-int))
389
390 (def-type char-ptr-def (* :unsigned-char))
391
392 #+(or lispworks (and allegro (not ics)))
393 (defun fast-native-to-string (s len)
394   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
395            (type char-ptr-def s))
396   (let* ((len (or len (strlen s)))
397          (str (make-string len)))
398     (declare (fixnum len)
399              (type (simple-array (signed-byte 8) (*)) str))
400     (dotimes (i len str)
401       (setf (aref str i) 
402         (uffi:deref-array s '(:array :char) i)))))
403
404 #+(and allegro ics)
405 (defun fast-native-to-string (s len)
406   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
407            (type char-ptr-def s))
408   (let* ((len (or len (strlen s)))
409          (str (make-string len)))
410       (dotimes (i len str)
411         (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))