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-2005 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
18 (defvar +null-cstring-pointer+
19 #+(or cmu sbcl scl) nil
21 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
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)))))
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)))))
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))))
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)))
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))
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))))
218 (declare (ignore null-terminated-p))
220 (let ((stored-obj (gensym)))
221 `(let ((,stored-obj ,obj))
222 (if (ccl:%null-ptr-p ,stored-obj)
224 #+(and mcl (not openmcl)) (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)
265 (declare (ignore unsigned))
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)
288 ;; Modified from CMUCL's source to handle non-null terminated strings
290 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
291 (declare (type system:system-area-pointer sap))
293 (declare (optimize (speed 3) (safety 0)))
294 (let ((null-terminated-length
295 (when null-terminated-p
297 for offset of-type fixnum upfrom 0
298 until (zerop (system:sap-ref-8 sap offset))
299 finally (return offset)))))
301 (if (and null-terminated-length
302 (> (the fixnum length) (the fixnum null-terminated-length)))
303 (setq length null-terminated-length))
304 (setq length null-terminated-length)))
305 (let ((result (make-string length)))
306 (kernel:copy-from-system-area sap 0
307 result (* vm:vector-data-offset
309 (* length vm:byte-bits))
313 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
314 ;; so have to iteratively copy from sap
315 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
316 (declare (type system:system-area-pointer sap))
318 (declare (optimize (speed 3) (safety 0)))
319 (let ((null-terminated-length
320 (when null-terminated-p
322 for offset of-type fixnum upfrom 0
323 until (zerop (system:sap-ref-8 sap offset))
324 finally (return offset)))))
326 (if (and null-terminated-length
327 (> (the fixnum length) (the fixnum null-terminated-length)))
328 (setq length null-terminated-length))
329 (setq length null-terminated-length)))
330 (let ((result (make-string length)))
332 (declare (type fixnum i))
333 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
336 #+(and sbcl (not sb-unicode))
337 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
338 (declare (type sb-sys:system-area-pointer sap)
339 (type (or null fixnum) length))
341 (declare (optimize (speed 3) (safety 0)))
342 (let ((null-terminated-length
343 (when null-terminated-p
345 for offset of-type fixnum upfrom 0
346 until (zerop (sb-sys:sap-ref-8 sap offset))
347 finally (return offset)))))
349 (if (and null-terminated-length
350 (> (the fixnum length) (the fixnum null-terminated-length)))
351 (setq length null-terminated-length))
352 (setq length null-terminated-length)))
353 (let ((result (make-string length)))
354 (funcall *system-copy-fn* sap 0 result +system-copy-offset+
355 (* length +system-copy-multiplier+))
358 #+(and sbcl sb-unicode)
359 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
360 (declare (type sb-sys:system-area-pointer sap)
361 (type (or null fixnum) length))
363 (declare (optimize (speed 3) (safety 0)))
366 (let ((casted (sb-alien:cast (sb-alien:sap-alien sap (* char))
367 #+sb-unicode sb-alien:utf8-string
368 #-sb-unicode sb-alien:c-string)))
370 (copy-seq (subseq casted 0 length))
373 (let ((result (make-string length)))
374 ;; this will not work in sb-unicode
375 (funcall *system-copy-fn* sap 0 result +system-copy-offset+
376 (* length +system-copy-multiplier+))
380 (eval-when (:compile-toplevel :load-toplevel :execute)
381 (def-function "strlen"
382 ((str (* :unsigned-char)))
383 :returning :unsigned-int))
385 (def-type char-ptr-def (* :unsigned-char))
387 #+(or lispworks (and allegro (not ics)))
388 (defun fast-native-to-string (s len)
389 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
390 (type char-ptr-def s))
391 (let* ((len (or len (strlen s)))
392 (str (make-string len)))
393 (declare (fixnum len)
394 (type (simple-array (signed-byte 8) (*)) str))
397 (uffi:deref-array s '(:array :char) i)))))
400 (defun fast-native-to-string (s len)
401 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
402 (type char-ptr-def s))
403 (let* ((len (or len (strlen s)))
404 (str (make-string len)))
406 (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))