r9006: fix on allegro
[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        (excl:native-to-string
161         ,obj 
162         ,@(when length (list :length length))
163         :truncate (not ,null-terminated-p))))
164   #+lispworks
165   `(if (fli:null-pointer-p ,obj)
166        nil
167      (if (eq ,locale :none)
168          (fast-native-to-string ,obj ,length)
169        (fli:convert-from-foreign-string 
170         ,obj
171         ,@(when length (list :length length))
172         :null-terminated-p ,null-terminated-p
173         :external-format '(:latin-1 :eol-style :lf))))
174   #+(or cmu scl)
175   `(if (null-pointer-p ,obj)
176     nil
177     (cmucl-naturalize-cstring (alien:alien-sap ,obj)
178      :length ,length
179      :null-terminated-p ,null-terminated-p))
180   #+sbcl
181   `(if (null-pointer-p ,obj)
182     nil
183     (sbcl-naturalize-cstring (sb-alien:alien-sap ,obj)
184      :length ,length
185      :null-terminated-p ,null-terminated-p))
186   #+mcl
187   (declare (ignore null-terminated-p))
188   #+mcl
189   `(if (ccl:%null-ptr-p ,obj)
190      nil
191      (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
192   )
193
194
195 (defmacro allocate-foreign-string (size &key (unsigned t))
196   #+(or cmu scl)
197   (let ((array-def (gensym)))
198     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
199        (eval `(alien:cast (alien:make-alien ,,array-def) 
200                           ,(if ,unsigned 
201                                '(* (alien:unsigned 8))
202                              '(* (alien:signed 8)))))))
203   #+sbcl
204   (let ((array-def (gensym)))
205     `(let ((,array-def (list 'sb-alien:array 'char ,size)))
206        (eval `(sb-alien:cast (sb-alien:make-alien ,,array-def) 
207                           ,(if ,unsigned 
208                                '(* (sb-alien:unsigned 8))
209                              '(* (sb-alien:signed 8)))))))
210   #+lispworks
211   `(fli:allocate-foreign-object :type 
212                                 ,(if unsigned 
213                                      ''(:unsigned :char) 
214                                    :char)
215                                 :nelems ,size)
216   #+allegro
217   (declare (ignore unsigned))
218   #+allegro
219   `(ff:allocate-fobject :char :c ,size)
220   #+mcl
221   (declare (ignore unsigned))
222   #+mcl
223   `(new-ptr ,size)
224   )
225
226 (defun foreign-string-length (foreign-string)
227   #+allegro `(ff:foreign-strlen ,foreign-string)
228   #-allegro
229   `(loop with size = 0
230     until (char= (deref-array ,ptr '(:array :unsigned-char) size) #\Null)
231     do (incf size)
232     finally return size))
233
234
235 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
236   (let ((result (gensym)))
237     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
238             (,result (progn ,@body)))
239       (declare (dynamic-extent ,foreign-string))
240       (free-foreign-object ,foreign-string)
241       ,result)))
242
243
244 ;; Modified from CMUCL's source to handle non-null terminated strings
245 #+cmu
246 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
247   (declare (type system:system-area-pointer sap))
248   (locally
249       (declare (optimize (speed 3) (safety 0)))
250     (let ((null-terminated-length
251            (when null-terminated-p
252              (loop
253                  for offset of-type fixnum upfrom 0
254                  until (zerop (system:sap-ref-8 sap offset))
255                  finally (return offset)))))
256       (if length
257           (if (and null-terminated-length
258                    (> (the fixnum length) (the fixnum null-terminated-length)))
259               (setq length null-terminated-length))
260         (setq length null-terminated-length)))
261     (let ((result (make-string length)))
262       (kernel:copy-from-system-area sap 0
263                                     result (* vm:vector-data-offset
264                                               vm:word-bits)
265                                     (* length vm:byte-bits))
266       result)))
267
268 #+scl
269 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
270 ;; so have to iteratively copy from sap
271 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
272   (declare (type system:system-area-pointer sap))
273   (locally
274       (declare (optimize (speed 3) (safety 0)))
275     (let ((null-terminated-length
276            (when null-terminated-p
277              (loop
278                  for offset of-type fixnum upfrom 0
279                  until (zerop (system:sap-ref-8 sap offset))
280                  finally (return offset)))))
281       (if length
282           (if (and null-terminated-length
283                    (> (the fixnum length) (the fixnum null-terminated-length)))
284               (setq length null-terminated-length))
285         (setq length null-terminated-length)))
286     (let ((result (make-string length)))
287       (dotimes (i length)
288         (declare (type fixnum i))
289         (setf (char result i) (code-char (system:sap-ref-8 sap i))))
290       result)))
291
292 #+sbcl
293 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
294   (declare (type sb-sys:system-area-pointer sap))
295   (locally
296       (declare (optimize (speed 3) (safety 0)))
297     (let ((null-terminated-length
298            (when null-terminated-p
299              (loop
300                  for offset of-type fixnum upfrom 0
301                  until (zerop (sb-sys:sap-ref-8 sap offset))
302                  finally (return offset)))))
303       (if length
304           (if (and null-terminated-length
305                    (> (the fixnum length) (the fixnum null-terminated-length)))
306               (setq length null-terminated-length))
307         (setq length null-terminated-length)))
308     (let ((result (make-string length)))
309       (sb-kernel:copy-from-system-area sap 0
310                                     result (* sb-vm:vector-data-offset
311                                               sb-vm:n-word-bits)
312                                     (* length sb-vm:n-byte-bits))
313       result)))
314
315
316 (def-function "strlen"
317     ((str (* :unsigned-char)))
318   :returning :unsigned-int)
319
320 (def-type char-ptr-def (* :unsigned-char))
321
322 #+(or lispworks (and allegro (not ics)))
323 (defun fast-native-to-string (s len)
324   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
325            (type char-ptr-def s))
326   (let* ((len (or len (strlen s)))
327          (str (make-string len)))
328     (declare (fixnum len)
329              (type (simple-array (signed-byte 8) (*)) str))
330     (dotimes (i len str)
331       (setf (aref str i) 
332         (uffi:deref-array s '(:array :char) i)))))
333
334 #+(and allegro ics)
335 (defun fast-native-to-string (s len)
336   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
337            (type char-ptr-def s))
338   (let* ((len (or len (strlen s)))
339          (str (make-string len)))
340       (dotimes (i len str)
341         (setf (schar str i) (code-char (uffi:deref-array s '(:array :byte) i))))))