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 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
22 (defvar +null-cstring-pointer+
23 #+(or cmu sbcl scl) nil
25 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
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
34 (let ((stored (gensym)))
35 `(let ((,stored ,obj))
38 (values (excl:native-to-string ,stored)))))
40 (let ((stored (gensym)))
41 `(let ((,stored ,obj))
42 (if (ccl:%null-ptr-p ,stored)
44 (values (ccl:%get-cstring ,stored)))))
47 (defmacro convert-to-cstring (obj)
48 #+(or cmu sbcl scl lispworks) obj
50 (let ((stored (gensym)))
51 `(let ((,stored ,obj))
54 (values (excl:string-to-native ,stored)))))
56 (let ((stored (gensym)))
57 `(let ((,stored ,obj))
59 +null-cstring-pointer+
60 (let ((ptr (new-ptr (1+ (length ,stored)))))
61 (ccl::%put-cstring ptr ,stored)
65 (defmacro free-cstring (obj)
66 #+(or cmu sbcl scl lispworks) (declare (ignore obj))
68 (let ((stored (gensym)))
69 `(let ((,stored ,obj))
70 (unless (zerop ,stored)
71 (ff:free-fobject ,stored))))
73 (let ((stored (gensym)))
74 `(let ((,stored ,obj))
75 (unless (ccl:%null-ptr-p ,stored)
76 (dispose-ptr ,stored))))
79 (defmacro with-cstring ((cstring lisp-string) &body body)
80 #+(or cmu sbcl scl lispworks)
81 `(let ((,cstring ,lisp-string)) ,@body)
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)))
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))
95 (let ((,cstring +null-cstring-pointer+))
99 (defmacro with-cstrings (bindings &rest body)
101 `(with-cstring ,(car bindings)
102 (with-cstrings ,(cdr bindings)
106 ;;; Foreign string functions
108 (defmacro convert-to-foreign-string (obj)
110 (let ((stored (gensym)))
111 `(let ((,stored ,obj))
113 +null-cstring-pointer+
114 (fli:convert-to-foreign-string
116 :external-format '(:latin-1 :eol-style :lf)))))
118 (let ((stored (gensym)))
119 `(let ((,stored ,obj))
122 (values (excl:string-to-native ,stored)))))
124 (let ((size (gensym))
126 (stored-obj (gensym))
128 `(let ((,stored-obj ,obj))
129 (etypecase ,stored-obj
131 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
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))))
137 (declare (optimize (speed 3) (safety 0)))
139 (declare (fixnum ,i))
140 (setf (alien:deref ,storage ,i)
141 (char-code (char ,stored-obj ,i))))
142 (setf (alien:deref ,storage ,size) 0))
145 (let ((size (gensym))
147 (stored-obj (gensym))
149 `(let ((,stored-obj ,obj))
150 (etypecase ,stored-obj
152 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
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))))
158 (declare (optimize (speed 3) (safety 0)))
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))
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)
175 ;; Either length or null-terminated-p must be non-nil
176 (defmacro convert-from-foreign-string (obj &key
179 (null-terminated-p t))
181 (let ((stored-obj (gensym)))
182 `(let ((,stored-obj ,obj))
183 (if (zerop ,stored-obj)
185 (if (eq ,locale :none)
186 (fast-native-to-string ,stored-obj ,length)
188 (excl:native-to-string
190 ,@(when length (list :length length))
191 :truncate (not ,null-terminated-p)))))))
193 (let ((stored-obj (gensym)))
194 `(let ((,stored-obj ,obj))
195 (if (fli:null-pointer-p ,stored-obj)
197 (if (eq ,locale :none)
198 (fast-native-to-string ,stored-obj ,length)
199 (fli:convert-from-foreign-string
201 ,@(when length (list :length length))
202 :null-terminated-p ,null-terminated-p
203 :external-format '(:latin-1 :eol-style :lf))))))
205 (let ((stored-obj (gensym)))
206 `(let ((,stored-obj ,obj))
207 (if (null-pointer-p ,stored-obj)
209 (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
211 :null-terminated-p ,null-terminated-p))))
214 (let ((stored-obj (gensym)))
215 `(let ((,stored-obj ,obj))
216 (if (null-pointer-p ,stored-obj)
218 (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
220 :null-terminated-p ,null-terminated-p))))
222 (declare (ignore null-terminated-p))
224 (let ((stored-obj (gensym)))
225 `(let ((,stored-obj ,obj))
226 (if (ccl:%null-ptr-p ,stored-obj)
228 #+(and mcl (not openmcl)) (ccl:%get-cstring
230 ,@(if length (list length) nil))
231 #+openmcl ,@(if length
232 `((ccl:%str-from-ptr ,stored-obj ,length))
233 `((ccl:%get-cstring ,stored-obj))))))
237 (defmacro allocate-foreign-string (size &key (unsigned t))
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)
243 '(* (alien:unsigned 8))
244 '(* (alien:signed 8)))))))
247 `(alien:make-alien ,(if unsigned
253 `(sb-alien:make-alien ,(if unsigned
254 '(sb-alien:unsigned 8)
255 '(sb-alien:signed 8))
259 `(fli:allocate-foreign-object :type
265 (declare (ignore unsigned))
267 `(ff:allocate-fobject :char :c ,size)
269 (declare (ignore unsigned))
274 (defun foreign-string-length (foreign-string)
275 #+allegro `(ff:foreign-strlen ,foreign-string)
278 until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
280 finally return size))
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)
292 ;; Modified from CMUCL's source to handle non-null terminated strings
294 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
295 (declare (type system:system-area-pointer sap))
297 (declare (optimize (speed 3) (safety 0)))
298 (let ((null-terminated-length
299 (when null-terminated-p
301 for offset of-type fixnum upfrom 0
302 until (zerop (system:sap-ref-8 sap offset))
303 finally (return offset)))))
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
313 (* length vm:byte-bits))
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))
322 (declare (optimize (speed 3) (safety 0)))
323 (let ((null-terminated-length
324 (when null-terminated-p
326 for offset of-type fixnum upfrom 0
327 until (zerop (system:sap-ref-8 sap offset))
328 finally (return offset)))))
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)))
336 (declare (type fixnum i))
337 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
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))
345 (declare (optimize (speed 3) (safety 0)))
346 (let ((null-terminated-length
347 (when null-terminated-p
349 for offset of-type fixnum upfrom 0
350 until (zerop (sb-sys:sap-ref-8 sap offset))
351 finally (return offset)))))
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+))
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))
367 (declare (optimize (speed 3) (safety 0)))
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)))
374 (copy-seq (subseq casted 0 length))
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+))
384 (eval-when (:compile-toplevel :load-toplevel :execute)
385 (def-function "strlen"
386 ((str (* :unsigned-char)))
387 :returning :unsigned-int))
389 (def-type char-ptr-def (* :unsigned-char))
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))
401 (uffi:deref-array s '(:array :char) i)))))
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))))))