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