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
52 (values (excl:string-to-native ,obj)))
55 +null-cstring-pointer+
56 (let ((ptr (new-ptr (1+ (length ,obj)))))
57 (ccl::%put-cstring ptr ,obj)
61 (defmacro free-cstring (obj)
62 #+(or cmu sbcl scl lispworks) (declare (ignore obj))
65 (ff:free-fobject ,obj))
67 `(unless (ccl:%null-ptr-p ,obj)
71 (defmacro with-cstring ((cstring lisp-string) &body body)
72 #+(or cmu sbcl scl lispworks)
73 `(let ((,cstring ,lisp-string)) ,@body)
75 (let ((acl-native (gensym)))
76 `(excl:with-native-string (,acl-native ,lisp-string)
77 (let ((,cstring (if ,lisp-string ,acl-native 0)))
80 `(if (stringp ,lisp-string)
81 (ccl:with-cstrs ((,cstring ,lisp-string))
83 (let ((,cstring +null-cstring-pointer+))
87 (defmacro with-cstrings (bindings &rest body)
89 `(with-cstring ,(car bindings)
90 (with-cstrings ,(cdr bindings)
94 ;;; Foreign string functions
96 (defmacro convert-to-foreign-string (obj)
99 +null-cstring-pointer+
100 (fli:convert-to-foreign-string ,obj :external-format '(:latin-1 :eol-style :lf)))
104 (values (excl:string-to-native ,obj)))
106 (let ((size (gensym))
111 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
113 (let* ((,size (length ,obj))
114 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
115 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
117 (declare (optimize (speed 3) (safety 0)))
119 (declare (fixnum ,i))
120 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
121 (setf (alien:deref ,storage ,size) 0))
124 (let ((size (gensym))
129 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
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))))
135 (declare (optimize (speed 3) (safety 0)))
137 (declare (fixnum ,i))
138 (setf (sb-alien:deref ,storage ,i) (char-code (char ,obj ,i))))
139 (setf (sb-alien:deref ,storage ,size) 0))
143 +null-cstring-pointer+
144 (let ((ptr (new-ptr (1+ (length ,obj)))))
145 (ccl::%put-cstring ptr ,obj)
150 ;; Either length or null-terminated-p must be non-nil
151 (defmacro convert-from-foreign-string (obj &key
154 (null-terminated-p t))
158 (if (eq ,locale :none)
159 (fast-native-to-string ,obj ,length)
160 (excl:native-to-string
162 ,@(when length (list :length length))
163 :truncate (not ,null-terminated-p))))
165 `(if (fli:null-pointer-p ,obj)
167 (if (eq ,locale :none)
168 (fast-native-to-string ,obj ,length)
169 (fli:convert-from-foreign-string
171 ,@(when length (list :length length))
172 :null-terminated-p ,null-terminated-p
173 :external-format '(:latin-1 :eol-style :lf))))
175 `(if (null-pointer-p ,obj)
177 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
179 :null-terminated-p ,null-terminated-p))
181 `(if (null-pointer-p ,obj)
183 (sbcl-naturalize-cstring (sb-alien:alien-sap ,obj)
185 :null-terminated-p ,null-terminated-p))
187 (declare (ignore null-terminated-p))
189 `(if (ccl:%null-ptr-p ,obj)
191 #+(and mcl (not openmcl)) (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil))
192 #+openmcl (let ((str (ccl:%get-cstring ,obj)))
193 ,(if length '(subseq str 0 length) 'str)))
197 (defmacro allocate-foreign-string (size &key (unsigned t))
199 (let ((array-def (gensym)))
200 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
201 (eval `(alien:cast (alien:make-alien ,,array-def)
203 '(* (alien:unsigned 8))
204 '(* (alien:signed 8)))))))
207 `(alien:make-alien ,(if unsigned
213 `(sb-alien:make-alien ,(if unsigned
214 '(sb-alien:unsigned 8)
215 '(sb-alien:signed 8))
219 `(fli:allocate-foreign-object :type
225 (declare (ignore unsigned))
227 `(ff:allocate-fobject :char :c ,size)
229 (declare (ignore unsigned))
234 (defun foreign-string-length (foreign-string)
235 #+allegro `(ff:foreign-strlen ,foreign-string)
238 until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
240 finally return size))
243 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
244 (let ((result (gensym)))
245 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
246 (,result (progn ,@body)))
247 (declare (dynamic-extent ,foreign-string))
248 (free-foreign-object ,foreign-string)
252 ;; Modified from CMUCL's source to handle non-null terminated strings
254 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
255 (declare (type system:system-area-pointer sap))
257 (declare (optimize (speed 3) (safety 0)))
258 (let ((null-terminated-length
259 (when null-terminated-p
261 for offset of-type fixnum upfrom 0
262 until (zerop (system:sap-ref-8 sap offset))
263 finally (return offset)))))
265 (if (and null-terminated-length
266 (> (the fixnum length) (the fixnum null-terminated-length)))
267 (setq length null-terminated-length))
268 (setq length null-terminated-length)))
269 (let ((result (make-string length)))
270 (kernel:copy-from-system-area sap 0
271 result (* vm:vector-data-offset
273 (* length vm:byte-bits))
277 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
278 ;; so have to iteratively copy from sap
279 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
280 (declare (type system:system-area-pointer sap))
282 (declare (optimize (speed 3) (safety 0)))
283 (let ((null-terminated-length
284 (when null-terminated-p
286 for offset of-type fixnum upfrom 0
287 until (zerop (system:sap-ref-8 sap offset))
288 finally (return offset)))))
290 (if (and null-terminated-length
291 (> (the fixnum length) (the fixnum null-terminated-length)))
292 (setq length null-terminated-length))
293 (setq length null-terminated-length)))
294 (let ((result (make-string length)))
296 (declare (type fixnum i))
297 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
301 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
302 (declare (type sb-sys:system-area-pointer sap))
304 (declare (optimize (speed 3) (safety 0)))
305 (let ((null-terminated-length
306 (when null-terminated-p
308 for offset of-type fixnum upfrom 0
309 until (zerop (sb-sys:sap-ref-8 sap offset))
310 finally (return offset)))))
312 (if (and null-terminated-length
313 (> (the fixnum length) (the fixnum null-terminated-length)))
314 (setq length null-terminated-length))
315 (setq length null-terminated-length)))
316 (let ((result (make-string length)))
317 (sb-kernel:copy-from-system-area sap 0
318 result (* sb-vm:vector-data-offset
320 (* length sb-vm:n-byte-bits))
323 (eval-when (:compile-toplevel :load-toplevel :execute)
324 (def-function "strlen"
325 ((str (* :unsigned-char)))
326 :returning :unsigned-int))
328 (def-type char-ptr-def (* :unsigned-char))
330 #+(or lispworks (and allegro (not ics)))
331 (defun fast-native-to-string (s len)
332 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
333 (type char-ptr-def s))
334 (let* ((len (or len (strlen s)))
335 (str (make-string len)))
336 (declare (fixnum len)
337 (type (simple-array (signed-byte 8) (*)) str))
340 (uffi:deref-array s '(:array :char) i)))))
343 (defun fast-native-to-string (s len)
344 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
345 (type char-ptr-def s))
346 (let* ((len (or len (strlen s)))
347 (str (make-string len)))
349 (setf (schar str i) (code-char (uffi:deref-array s '(:array :byte) i))))))