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
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;; *************************************************************************
16 (def-pointer-var +null-cstring-pointer+
17 #+(or cmu sbcl scl) nil
19 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
20 #+(or openmcl digitool) (ccl:%null-ptr)
23 (defmacro convert-from-cstring (obj)
24 "Converts a string from a c-call. Same as convert-from-foreign-string, except
25 that LW/CMU automatically converts strings from c-calls."
26 #+(or cmu sbcl lispworks scl) obj
28 (let ((stored (gensym)))
29 `(let ((,stored ,obj))
32 (values (excl:native-to-string ,stored)))))
33 #+(or openmcl digitool)
34 (let ((stored (gensym)))
35 `(let ((,stored ,obj))
36 (if (ccl:%null-ptr-p ,stored)
38 (values (ccl:%get-cstring ,stored)))))
41 (defmacro convert-to-cstring (obj)
42 #+(or cmu sbcl scl lispworks) obj
44 (let ((stored (gensym)))
45 `(let ((,stored ,obj))
48 (values (excl:string-to-native ,stored)))))
49 #+(or openmcl digitool)
50 (let ((stored (gensym)))
51 `(let ((,stored ,obj))
53 +null-cstring-pointer+
54 (let ((ptr (new-ptr (1+ (length ,stored)))))
55 (ccl::%put-cstring ptr ,stored)
59 (defmacro free-cstring (obj)
60 #+(or cmu sbcl scl lispworks) (declare (ignore obj))
62 (let ((stored (gensym)))
63 `(let ((,stored ,obj))
64 (unless (zerop ,stored)
65 (ff:free-fobject ,stored))))
66 #+(or openmcl digitool)
67 (let ((stored (gensym)))
68 `(let ((,stored ,obj))
69 (unless (ccl:%null-ptr-p ,stored)
70 (dispose-ptr ,stored))))
73 (defmacro with-cstring ((cstring lisp-string) &body body)
74 #+(or cmu sbcl scl lispworks)
75 `(let ((,cstring ,lisp-string)) ,@body)
77 (let ((acl-native (gensym))
78 (stored-lisp-string (gensym)))
79 `(let ((,stored-lisp-string ,lisp-string))
80 (excl:with-native-string (,acl-native ,stored-lisp-string)
81 (let ((,cstring (if ,stored-lisp-string ,acl-native 0)))
83 #+(or openmcl digitool)
84 (let ((stored-lisp-string (gensym)))
85 `(let ((,stored-lisp-string ,lisp-string))
86 (if (stringp ,stored-lisp-string)
87 (ccl:with-cstrs ((,cstring ,stored-lisp-string))
89 (let ((,cstring +null-cstring-pointer+))
93 (defmacro with-cstrings (bindings &rest body)
95 `(with-cstring ,(car bindings)
96 (with-cstrings ,(cdr bindings)
100 ;;; Foreign string functions
102 (defmacro convert-to-foreign-string (obj)
104 (let ((stored (gensym)))
105 `(let ((,stored ,obj))
107 +null-cstring-pointer+
108 (fli:convert-to-foreign-string
110 :external-format '(:latin-1 :eol-style :lf)))))
112 (let ((stored (gensym)))
113 `(let ((,stored ,obj))
116 (values (excl:string-to-native ,stored)))))
118 (let ((size (gensym))
120 (stored-obj (gensym))
122 `(let ((,stored-obj ,obj))
123 (etypecase ,stored-obj
125 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
127 (let* ((,size (length ,stored-obj))
128 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
129 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
131 (declare (optimize (speed 3) (safety 0)))
133 (declare (fixnum ,i))
134 (setf (alien:deref ,storage ,i)
135 (char-code (char ,stored-obj ,i))))
136 (setf (alien:deref ,storage ,size) 0))
139 (let ((size (gensym))
141 (stored-obj (gensym))
143 `(let ((,stored-obj ,obj))
144 (etypecase ,stored-obj
146 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
148 (let* ((,size (length ,stored-obj))
149 (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
150 (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
152 (declare (optimize (speed 3) (safety 0)))
154 (declare (fixnum ,i))
155 (setf (sb-alien:deref ,storage ,i)
156 (char-code (char ,stored-obj ,i))))
157 (setf (sb-alien:deref ,storage ,size) 0))
159 #+(or openmcl digitool)
160 (let ((stored-obj (gensym)))
161 `(let ((,stored-obj ,obj))
162 (if (null ,stored-obj)
163 +null-cstring-pointer+
164 (let ((ptr (new-ptr (1+ (length ,stored-obj)))))
165 (ccl::%put-cstring ptr ,stored-obj)
169 ;; Either length or null-terminated-p must be non-nil
170 (defmacro convert-from-foreign-string (obj &key
173 (null-terminated-p t))
175 (let ((stored-obj (gensym)))
176 `(let ((,stored-obj ,obj))
177 (if (zerop ,stored-obj)
179 (if (eq ,locale :none)
180 (fast-native-to-string ,stored-obj ,length)
182 (excl:native-to-string
184 ,@(when length (list :length length))
185 :truncate (not ,null-terminated-p)))))))
187 (let ((stored-obj (gensym)))
188 `(let ((,stored-obj ,obj))
189 (if (fli:null-pointer-p ,stored-obj)
191 (if (eq ,locale :none)
192 (fast-native-to-string ,stored-obj ,length)
193 (fli:convert-from-foreign-string
195 ,@(when length (list :length length))
196 :null-terminated-p ,null-terminated-p
197 :external-format '(:latin-1 :eol-style :lf))))))
199 (let ((stored-obj (gensym)))
200 `(let ((,stored-obj ,obj))
201 (if (null-pointer-p ,stored-obj)
203 (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
205 :null-terminated-p ,null-terminated-p))))
208 (let ((stored-obj (gensym)))
209 `(let ((,stored-obj ,obj))
210 (if (null-pointer-p ,stored-obj)
212 (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
214 :null-terminated-p ,null-terminated-p))))
215 #+(or openmcl digitool)
216 (declare (ignore null-terminated-p))
217 #+(or openmcl digitool)
218 (let ((stored-obj (gensym)))
219 `(let ((,stored-obj ,obj))
220 (if (ccl:%null-ptr-p ,stored-obj)
222 #+digitool (ccl:%get-cstring
224 ,@(if length (list length) nil))
225 #+openmcl ,@(if length
226 `((ccl:%str-from-ptr ,stored-obj ,length))
227 `((ccl:%get-cstring ,stored-obj))))))
231 (defmacro allocate-foreign-string (size &key (unsigned t))
233 (let ((array-def (gensym)))
234 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
235 (eval `(alien:cast (alien:make-alien ,,array-def)
237 '(* (alien:unsigned 8))
238 '(* (alien:signed 8)))))))
241 `(alien:make-alien ,(if unsigned
247 `(sb-alien:make-alien ,(if unsigned
248 '(sb-alien:unsigned 8)
249 '(sb-alien:signed 8))
253 `(fli:allocate-foreign-object :type
259 (declare (ignore unsigned))
261 `(ff:allocate-fobject :char :c ,size)
262 #+(or openmcl digitool)
263 (declare (ignore unsigned))
264 #+(or openmcl digitool)
268 (defun foreign-string-length (foreign-string)
269 #+allegro `(ff:foreign-strlen ,foreign-string)
272 until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
274 finally return size))
277 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
278 (let ((result (gensym)))
279 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
280 (,result (progn ,@body)))
281 (declare (dynamic-extent ,foreign-string))
282 (free-foreign-object ,foreign-string)
285 (defmacro with-foreign-strings (bindings &body body)
286 `(with-foreign-string ,(car bindings)
288 `((with-foreign-strings ,(cdr bindings) ,@body))
291 ;; Modified from CMUCL's source to handle non-null terminated strings
293 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
294 (declare (type system:system-area-pointer sap))
296 (declare (optimize (speed 3) (safety 0)))
297 (let ((null-terminated-length
298 (when null-terminated-p
300 for offset of-type fixnum upfrom 0
301 until (zerop (system:sap-ref-8 sap offset))
302 finally (return offset)))))
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 (kernel:copy-from-system-area sap 0
310 result (* vm:vector-data-offset
312 (* length vm:byte-bits))
316 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
317 ;; so have to iteratively copy from sap
318 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
319 (declare (type system:system-area-pointer sap))
321 (declare (optimize (speed 3) (safety 0)))
322 (let ((null-terminated-length
323 (when null-terminated-p
325 for offset of-type fixnum upfrom 0
326 until (zerop (system:sap-ref-8 sap offset))
327 finally (return offset)))))
329 (if (and null-terminated-length
330 (> (the fixnum length) (the fixnum null-terminated-length)))
331 (setq length null-terminated-length))
332 (setq length null-terminated-length)))
333 (let ((result (make-string length)))
335 (declare (type fixnum i))
336 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
339 #+(and sbcl (not sb-unicode))
340 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
341 (declare (type sb-sys:system-area-pointer sap)
342 (type (or null fixnum) length))
344 (declare (optimize (speed 3) (safety 0)))
345 (let ((null-terminated-length
346 (when null-terminated-p
348 for offset of-type fixnum upfrom 0
349 until (zerop (sb-sys:sap-ref-8 sap offset))
350 finally (return offset)))))
352 (if (and null-terminated-length
353 (> (the fixnum length) (the fixnum null-terminated-length)))
354 (setq length null-terminated-length))
355 (setq length null-terminated-length)))
356 (let ((result (make-string length)))
357 (funcall *system-copy-fn* sap 0 result +system-copy-offset+
358 (* length +system-copy-multiplier+))
361 #+(and sbcl sb-unicode)
362 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
363 (declare (type sb-sys:system-area-pointer sap)
364 (type (or null fixnum) length))
366 (declare (optimize (speed 3) (safety 0)))
369 (let ((casted (sb-alien:cast (sb-alien:sap-alien sap (* char))
370 #+sb-unicode sb-alien:utf8-string
371 #-sb-unicode sb-alien:c-string)))
373 (copy-seq (subseq casted 0 length))
376 (let ((result (make-string length)))
377 ;; this will not work in sb-unicode
378 (funcall *system-copy-fn* sap 0 result +system-copy-offset+
379 (* length +system-copy-multiplier+))
383 (eval-when (:compile-toplevel :load-toplevel :execute)
384 (def-function "strlen"
385 ((str (* :unsigned-char)))
386 :returning :unsigned-int))
388 (def-type char-ptr-def (* :unsigned-char))
390 #+(or (and allegro (not ics)) (and lispworks (not lispworks5) (not lispworks6)))
391 (defun fast-native-to-string (s len)
392 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
393 (type char-ptr-def s))
394 (let* ((len (or len (strlen s)))
395 (str (make-string len)))
396 (declare (fixnum len)
397 (type (simple-array #+lispworks base-char
398 #-lispworks (signed-byte 8) (*)) str))
401 (uffi:deref-array s '(:array :char) i)))))
403 #+(or (and allegro ics) lispworks5 lispworks6)
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)))
410 (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))