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.3 2002/10/14 03:07:41 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+
27 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
29 #-(or cmu allegro lispworks mcl) nil
32 (defmacro convert-from-cstring (obj)
33 "Converts a string from a c-call. Same as convert-from-foreign-string, except
34 that LW/CMU automatically converts strings from c-calls."
39 (let ((stored (gensym)))
40 `(let ((,stored ,obj))
43 (values (excl:native-to-string ,stored)))))
45 (let ((stored (gensym)))
46 `(let ((,stored ,obj))
47 (if (ccl:%null-ptr-p ,stored)
49 (values (ccl:%get-cstring ,stored)))))
52 (defmacro convert-to-cstring (obj)
59 (values (excl:string-to-native ,obj)))
62 +null-cstring-pointer+
63 (let ((ptr (new-ptr (1+ (length ,obj)))))
64 (ccl:%put-cstring ptr ,obj)
68 (defmacro free-cstring (obj)
69 #+(or cmu sbcl lispworks) (declare (ignore obj))
72 (ff:free-fobject ,obj))
74 `(unless (ccl:%null-ptr-p ,obj)
78 (defmacro with-cstring ((cstring lisp-string) &body body)
79 #+(or cmu sbcl lispworks)
80 `(let ((,cstring ,lisp-string)) ,@body)
82 (let ((acl-native (gensym)))
83 `(excl:with-native-string (,acl-native ,lisp-string)
84 (let ((,cstring (if ,lisp-string ,acl-native 0)))
87 `(if (stringp ,lisp-string)
88 (ccl:with-cstrs ((,cstring ,lisp-string))
90 (let ((,cstring +null-cstring-pointer+))
94 (defmacro with-cstrings (bindings &rest body)
96 `(with-cstring ,(car bindings)
97 (with-cstrings ,(cdr bindings)
101 ;;; Foreign string functions
103 (defmacro convert-to-foreign-string (obj)
106 +null-cstring-pointer+
107 (fli:convert-to-foreign-string ,obj))
111 (values (excl:string-to-native ,obj)))
113 (let ((size (gensym))
118 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
120 (let* ((,size (length ,obj))
121 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
122 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
124 (declare (optimize (speed 3) (safety 0)))
126 (declare (fixnum ,i))
127 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
128 (setf (alien:deref ,storage ,size) 0))
131 (let ((size (gensym))
136 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
138 (let* ((,size (length ,obj))
139 (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
140 (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
142 (declare (optimize (speed 3) (safety 0)))
144 (declare (fixnum ,i))
145 (setf (sb-alien:deref ,storage ,i) (char-code (char ,obj ,i))))
146 (setf (sb-alien:deref ,storage ,size) 0))
150 +null-cstring-pointer+
151 (let ((ptr (new-ptr (1+ (length ,obj)))))
152 (ccl:%put-cstring ptr ,obj)
157 ;; Either length or null-terminated-p must be non-nil
158 (defmacro convert-from-foreign-string (obj &key
160 (null-terminated-p t))
164 (values (excl:native-to-string
166 ,@(if length (list :length length) (values))
167 :truncate (not ,null-terminated-p))))
169 `(if (fli:null-pointer-p ,obj)
171 (fli:convert-from-foreign-string
173 ,@(if length (list :length length) (values))
174 :null-terminated-p ,null-terminated-p
175 :external-format '(:latin-1 :eol-style :lf)))
177 `(if (null-pointer-p ,obj)
179 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
181 :null-terminated-p ,null-terminated-p))
183 `(if (null-pointer-p ,obj)
185 (sbcl-naturalize-cstring (sb-alien:alien-sap ,obj)
187 :null-terminated-p ,null-terminated-p))
189 (declare (ignore null-terminated-p))
191 `(if (ccl:%null-ptr-p ,obj)
193 (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
198 (defmacro allocate-foreign-string (size &key (unsigned t))
200 (let ((array-def (gensym)))
201 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
202 (eval `(alien:cast (alien:make-alien ,,array-def)
204 '(* (alien:unsigned 8))
205 '(* (alien:signed 8)))))))
207 (let ((array-def (gensym)))
208 `(let ((,array-def (list 'sb-alien:array 'char ,size)))
209 (eval `(sb-alien:cast (sb-alien:make-alien ,,array-def)
211 '(* (sb-alien:unsigned 8))
212 '(* (sb-alien:signed 8)))))))
214 `(fli:allocate-foreign-object :type
220 (declare (ignore unsigned))
222 `(ff:allocate-fobject :char :c ,size)
224 (declare (ignore unsigned))
229 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
230 (let ((result (gensym)))
231 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
232 (,result (progn ,@body)))
233 (declare (dynamic-extent ,foreign-string))
234 (free-foreign-object ,foreign-string)
238 ;; Modified from CMUCL's source to handle non-null terminated strings
240 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
241 (declare (type system:system-area-pointer sap))
243 (declare (optimize (speed 3) (safety 0)))
244 (let ((null-terminated-length
245 (when null-terminated-p
247 for offset of-type fixnum upfrom 0
248 until (zerop (system:sap-ref-8 sap offset))
249 finally (return offset)))))
251 (if (and null-terminated-length
252 (> (the fixnum length) (the fixnum null-terminated-length)))
253 (setq length null-terminated-length))
254 (setq length null-terminated-length)))
255 (let ((result (make-string length)))
256 (kernel:copy-from-system-area sap 0
257 result (* vm:vector-data-offset
259 (* length vm:byte-bits))
263 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
264 (declare (type sb-sys:system-area-pointer sap))
266 (declare (optimize (speed 3) (safety 0)))
267 (let ((null-terminated-length
268 (when null-terminated-p
270 for offset of-type fixnum upfrom 0
271 until (zerop (sb-sys:sap-ref-8 sap offset))
272 finally (return offset)))))
274 (if (and null-terminated-length
275 (> (the fixnum length) (the fixnum null-terminated-length)))
276 (setq length null-terminated-length))
277 (setq length null-terminated-length)))
278 (let ((result (make-string length)))
279 (sb-kernel:copy-from-system-area sap 0
280 result (* sb-vm:vector-data-offset
282 (* length sb-vm:n-byte-bits))