1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: UFFI source to handle strings, cstring and foreigns
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: strings.lisp,v 1.4 2002/10/16 11:56:43 kevin Exp $
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 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
23 (defvar +null-cstring-pointer+
24 #+(or cmu sbcl scl) nil
26 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
30 (defmacro convert-from-cstring (obj)
31 "Converts a string from a c-call. Same as convert-from-foreign-string, except
32 that LW/CMU automatically converts strings from c-calls."
33 #+(or cmu sbcl lispworks scl) obj
35 (let ((stored (gensym)))
36 `(let ((,stored ,obj))
39 (values (excl:native-to-string ,stored)))))
41 (let ((stored (gensym)))
42 `(let ((,stored ,obj))
43 (if (ccl:%null-ptr-p ,stored)
45 (values (ccl:%get-cstring ,stored)))))
48 (defmacro convert-to-cstring (obj)
49 #+(or cmu sbcl scl lispworks) obj
53 (values (excl:string-to-native ,obj)))
56 +null-cstring-pointer+
57 (let ((ptr (new-ptr (1+ (length ,obj)))))
58 (ccl:%put-cstring ptr ,obj)
62 (defmacro free-cstring (obj)
63 #+(or cmu sbcl scl lispworks) (declare (ignore obj))
66 (ff:free-fobject ,obj))
68 `(unless (ccl:%null-ptr-p ,obj)
72 (defmacro with-cstring ((cstring lisp-string) &body body)
73 #+(or cmu sbcl scl lispworks)
74 `(let ((,cstring ,lisp-string)) ,@body)
76 (let ((acl-native (gensym)))
77 `(excl:with-native-string (,acl-native ,lisp-string)
78 (let ((,cstring (if ,lisp-string ,acl-native 0)))
81 `(if (stringp ,lisp-string)
82 (ccl:with-cstrs ((,cstring ,lisp-string))
84 (let ((,cstring +null-cstring-pointer+))
88 (defmacro with-cstrings (bindings &rest body)
90 `(with-cstring ,(car bindings)
91 (with-cstrings ,(cdr bindings)
95 ;;; Foreign string functions
97 (defmacro convert-to-foreign-string (obj)
100 +null-cstring-pointer+
101 (fli:convert-to-foreign-string ,obj))
105 (values (excl:string-to-native ,obj)))
107 (let ((size (gensym))
112 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
114 (let* ((,size (length ,obj))
115 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
116 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
118 (declare (optimize (speed 3) (safety 0)))
120 (declare (fixnum ,i))
121 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
122 (setf (alien:deref ,storage ,size) 0))
125 (let ((size (gensym))
130 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
132 (let* ((,size (length ,obj))
133 (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
134 (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
136 (declare (optimize (speed 3) (safety 0)))
138 (declare (fixnum ,i))
139 (setf (sb-alien:deref ,storage ,i) (char-code (char ,obj ,i))))
140 (setf (sb-alien:deref ,storage ,size) 0))
144 +null-cstring-pointer+
145 (let ((ptr (new-ptr (1+ (length ,obj)))))
146 (ccl:%put-cstring ptr ,obj)
151 ;; Either length or null-terminated-p must be non-nil
152 (defmacro convert-from-foreign-string (obj &key
154 (null-terminated-p t))
158 (values (excl:native-to-string
160 ,@(if length (list :length length) (values))
161 :truncate (not ,null-terminated-p))))
163 `(if (fli:null-pointer-p ,obj)
165 (fli:convert-from-foreign-string
167 ,@(if length (list :length length) (values))
168 :null-terminated-p ,null-terminated-p
169 :external-format '(:latin-1 :eol-style :lf)))
171 `(if (null-pointer-p ,obj)
173 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
175 :null-terminated-p ,null-terminated-p))
177 `(if (null-pointer-p ,obj)
179 (sbcl-naturalize-cstring (sb-alien:alien-sap ,obj)
181 :null-terminated-p ,null-terminated-p))
183 (declare (ignore null-terminated-p))
185 `(if (ccl:%null-ptr-p ,obj)
187 (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
192 (defmacro allocate-foreign-string (size &key (unsigned t))
194 (let ((array-def (gensym)))
195 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
196 (eval `(alien:cast (alien:make-alien ,,array-def)
198 '(* (alien:unsigned 8))
199 '(* (alien:signed 8)))))))
201 (let ((array-def (gensym)))
202 `(let ((,array-def (list 'sb-alien:array 'char ,size)))
203 (eval `(sb-alien:cast (sb-alien:make-alien ,,array-def)
205 '(* (sb-alien:unsigned 8))
206 '(* (sb-alien:signed 8)))))))
208 `(fli:allocate-foreign-object :type
214 (declare (ignore unsigned))
216 `(ff:allocate-fobject :char :c ,size)
218 (declare (ignore unsigned))
223 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
224 (let ((result (gensym)))
225 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
226 (,result (progn ,@body)))
227 (declare (dynamic-extent ,foreign-string))
228 (free-foreign-object ,foreign-string)
232 ;; Modified from CMUCL's source to handle non-null terminated strings
234 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
235 (declare (type system:system-area-pointer sap))
237 (declare (optimize (speed 3) (safety 0)))
238 (let ((null-terminated-length
239 (when null-terminated-p
241 for offset of-type fixnum upfrom 0
242 until (zerop (system:sap-ref-8 sap offset))
243 finally (return offset)))))
245 (if (and null-terminated-length
246 (> (the fixnum length) (the fixnum null-terminated-length)))
247 (setq length null-terminated-length))
248 (setq length null-terminated-length)))
249 (let ((result (make-string length)))
250 (kernel:copy-from-system-area sap 0
251 result (* vm:vector-data-offset
253 (* length vm:byte-bits))
257 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
258 ;; so have to iteratively copy from sap
259 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
260 (declare (type system:system-area-pointer sap))
262 (declare (optimize (speed 3) (safety 0)))
263 (let ((null-terminated-length
264 (when null-terminated-p
266 for offset of-type fixnum upfrom 0
267 until (zerop (system:sap-ref-8 sap offset))
268 finally (return offset)))))
270 (if (and null-terminated-length
271 (> (the fixnum length) (the fixnum null-terminated-length)))
272 (setq length null-terminated-length))
273 (setq length null-terminated-length)))
274 (let ((result (make-string length)))
276 (declare (type fixnum i))
277 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
281 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
282 (declare (type sb-sys:system-area-pointer sap))
284 (declare (optimize (speed 3) (safety 0)))
285 (let ((null-terminated-length
286 (when null-terminated-p
288 for offset of-type fixnum upfrom 0
289 until (zerop (sb-sys:sap-ref-8 sap offset))
290 finally (return offset)))))
292 (if (and null-terminated-length
293 (> (the fixnum length) (the fixnum null-terminated-length)))
294 (setq length null-terminated-length))
295 (setq length null-terminated-length)))
296 (let ((result (make-string length)))
297 (sb-kernel:copy-from-system-area sap 0
298 result (* sb-vm:vector-data-offset
300 (* length sb-vm:n-byte-bits))