1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: strings.lisp
6 ;;;; Purpose: UFFI source to handle strings, cstring and foreigns
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
18 (def-pointer-var +null-cstring-pointer+
19 #+(or cmu sbcl scl) nil
21 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
22 #+(or openmcl digitool) (ccl:%null-ptr)
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
30 (let ((stored (gensym)))
31 `(let ((,stored ,obj))
34 (values (excl:native-to-string ,stored)))))
35 #+(or openmcl digitool)
36 (let ((stored (gensym)))
37 `(let ((,stored ,obj))
38 (if (ccl:%null-ptr-p ,stored)
40 (values (ccl:%get-cstring ,stored)))))
43 (defmacro convert-to-cstring (obj)
44 #+(or cmu sbcl scl lispworks) obj
46 (let ((stored (gensym)))
47 `(let ((,stored ,obj))
50 (values (excl:string-to-native ,stored)))))
51 #+(or openmcl digitool)
52 (let ((stored (gensym)))
53 `(let ((,stored ,obj))
55 +null-cstring-pointer+
56 (let ((ptr (new-ptr (1+ (length ,stored)))))
57 (ccl::%put-cstring ptr ,stored)
61 (defmacro free-cstring (obj)
62 #+(or cmu sbcl scl lispworks) (declare (ignore obj))
64 (let ((stored (gensym)))
65 `(let ((,stored ,obj))
66 (unless (zerop ,stored)
67 (ff:free-fobject ,stored))))
68 #+(or openmcl digitool)
69 (let ((stored (gensym)))
70 `(let ((,stored ,obj))
71 (unless (ccl:%null-ptr-p ,stored)
72 (dispose-ptr ,stored))))
75 (defmacro with-cstring ((cstring lisp-string) &body body)
76 #+(or cmu sbcl scl lispworks)
77 `(let ((,cstring ,lisp-string)) ,@body)
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)))
85 #+(or openmcl digitool)
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))
91 (let ((,cstring +null-cstring-pointer+))
95 (defmacro with-cstrings (bindings &rest body)
97 `(with-cstring ,(car bindings)
98 (with-cstrings ,(cdr bindings)
102 ;;; Foreign string functions
104 (defmacro convert-to-foreign-string (obj)
106 (let ((stored (gensym)))
107 `(let ((,stored ,obj))
109 +null-cstring-pointer+
110 (fli:convert-to-foreign-string
112 :external-format '(:latin-1 :eol-style :lf)))))
114 (let ((stored (gensym)))
115 `(let ((,stored ,obj))
118 (values (excl:string-to-native ,stored)))))
120 (let ((size (gensym))
122 (stored-obj (gensym))
124 `(let ((,stored-obj ,obj))
125 (etypecase ,stored-obj
127 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
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))))
133 (declare (optimize (speed 3) (safety 0)))
135 (declare (fixnum ,i))
136 (setf (alien:deref ,storage ,i)
137 (char-code (char ,stored-obj ,i))))
138 (setf (alien:deref ,storage ,size) 0))
141 (let ((size (gensym))
143 (stored-obj (gensym))
145 `(let ((,stored-obj ,obj))
146 (etypecase ,stored-obj
148 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
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))))
154 (declare (optimize (speed 3) (safety 0)))
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))
161 #+(or openmcl digitool)
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)
171 ;; Either length or null-terminated-p must be non-nil
172 (defmacro convert-from-foreign-string (obj &key
175 (null-terminated-p t))
177 (let ((stored-obj (gensym)))
178 `(let ((,stored-obj ,obj))
179 (if (zerop ,stored-obj)
181 (if (eq ,locale :none)
182 (fast-native-to-string ,stored-obj ,length)
184 (excl:native-to-string
186 ,@(when length (list :length length))
187 :truncate (not ,null-terminated-p)))))))
189 (let ((stored-obj (gensym)))
190 `(let ((,stored-obj ,obj))
191 (if (fli:null-pointer-p ,stored-obj)
193 (if (eq ,locale :none)
194 (fast-native-to-string ,stored-obj ,length)
195 (fli:convert-from-foreign-string
197 ,@(when length (list :length length))
198 :null-terminated-p ,null-terminated-p
199 :external-format '(:latin-1 :eol-style :lf))))))
201 (let ((stored-obj (gensym)))
202 `(let ((,stored-obj ,obj))
203 (if (null-pointer-p ,stored-obj)
205 (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
207 :null-terminated-p ,null-terminated-p))))
210 (let ((stored-obj (gensym)))
211 `(let ((,stored-obj ,obj))
212 (if (null-pointer-p ,stored-obj)
214 (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
216 :null-terminated-p ,null-terminated-p))))
217 #+(or openmcl digitool)
218 (declare (ignore null-terminated-p))
219 #+(or openmcl digitool)
220 (let ((stored-obj (gensym)))
221 `(let ((,stored-obj ,obj))
222 (if (ccl:%null-ptr-p ,stored-obj)
224 #+digitool (ccl:%get-cstring
226 ,@(if length (list length) nil))
227 #+openmcl ,@(if length
228 `((ccl:%str-from-ptr ,stored-obj ,length))
229 `((ccl:%get-cstring ,stored-obj))))))
233 (defmacro allocate-foreign-string (size &key (unsigned t))
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)
239 '(* (alien:unsigned 8))
240 '(* (alien:signed 8)))))))
243 `(alien:make-alien ,(if unsigned
249 `(sb-alien:make-alien ,(if unsigned
250 '(sb-alien:unsigned 8)
251 '(sb-alien:signed 8))
255 `(fli:allocate-foreign-object :type
261 (declare (ignore unsigned))
263 `(ff:allocate-fobject :char :c ,size)
264 #+(or openmcl digitool)
265 (declare (ignore unsigned))
266 #+(or openmcl digitool)
270 (defun foreign-string-length (foreign-string)
271 #+allegro `(ff:foreign-strlen ,foreign-string)
274 until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
276 finally return size))
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)
287 (defmacro with-foreign-strings (bindings &body body)
288 `(with-foreign-string ,(car bindings)
290 `((with-foreign-strings ,(cdr bindings) ,@body))
293 ;; Modified from CMUCL's source to handle non-null terminated strings
295 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
296 (declare (type system:system-area-pointer sap))
298 (declare (optimize (speed 3) (safety 0)))
299 (let ((null-terminated-length
300 (when null-terminated-p
302 for offset of-type fixnum upfrom 0
303 until (zerop (system:sap-ref-8 sap offset))
304 finally (return offset)))))
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
314 (* length vm:byte-bits))
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))
323 (declare (optimize (speed 3) (safety 0)))
324 (let ((null-terminated-length
325 (when null-terminated-p
327 for offset of-type fixnum upfrom 0
328 until (zerop (system:sap-ref-8 sap offset))
329 finally (return offset)))))
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)))
337 (declare (type fixnum i))
338 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
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))
346 (declare (optimize (speed 3) (safety 0)))
347 (let ((null-terminated-length
348 (when null-terminated-p
350 for offset of-type fixnum upfrom 0
351 until (zerop (sb-sys:sap-ref-8 sap offset))
352 finally (return offset)))))
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+))
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))
368 (declare (optimize (speed 3) (safety 0)))
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)))
375 (copy-seq (subseq casted 0 length))
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+))
385 (eval-when (:compile-toplevel :load-toplevel :execute)
386 (def-function "strlen"
387 ((str (* :unsigned-char)))
388 :returning :unsigned-int))
390 (def-type char-ptr-def (* :unsigned-char))
392 #+(or (and allegro (not ics)) (and lispworks (not lispworks5) (not lispworks6)))
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 #+lispworks base-char
400 #-lispworks (signed-byte 8) (*)) str))
403 (uffi:deref-array s '(:array :char) i)))))
405 #+(or (and allegro ics) lispworks5 lispworks6)
406 (defun fast-native-to-string (s len)
407 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
408 (type char-ptr-def s))
409 (let* ((len (or len (strlen s)))
410 (str (make-string len)))
412 (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))