1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: immediates.cl
6 ;;;; Purpose: UFFI source to handle immediate types
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; Copyright (c) 2002 Kevin M. Rosenberg
12 ;;;; $Id: strings.cl,v 1.4 2002/03/10 11:13:07 kevin Exp $
14 ;;;; This file is part of the UFFI.
16 ;;;; UFFI is free software; you can redistribute it and/or modify
17 ;;;; it under the terms of the GNU General Public License (version 2) as
18 ;;;; published by the Free Software Foundation.
20 ;;;; UFFI is distributed in the hope that it will be useful,
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;;;; GNU General Public License for more details.
25 ;;;; You should have received a copy of the GNU General Public License
26 ;;;; along with UFFI; if not, write to the Free Software
27 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 ;;;; *************************************************************************
30 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
34 (def-constant +null-cstring-pointer+
37 #+lispworks (fli:make-pointer :address 0 :type :char))
39 (defmacro convert-from-cstring (obj)
40 "Converts a string from a c-call. Same as convert-from-foreign-string, except
41 that CMU automatically converts strings from c-calls."
44 (let ((stored (gensym)))
45 `(let ((,stored ,obj))
46 (if (fli:null-pointer-p ,stored)
48 (fli:convert-from-foreign-string ,stored))))
50 (let ((stored (gensym)))
51 `(let ((,stored ,obj))
54 (values (excl:native-to-string ,stored)))))
57 (defmacro convert-to-cstring (obj)
60 +null-cstring-pointer+
62 :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
67 (values (excl:string-to-native ,obj)))
69 (declare (ignore obj))
72 (defmacro free-cstring (obj)
74 `(unless (fli:null-pointer-p ,obj)
75 (fli:free-foreign-object ,obj))
78 (ff:free-fobject ,obj))
80 (declare (ignore obj))
83 ;; Either length or null-terminated-p must be non-nil
84 (defmacro convert-from-foreign-string (obj &key
86 (null-terminated-p t))
90 (values (excl:native-to-string
92 ,@(if length (list :length length) (values))
93 :truncate (not ,null-terminated-p))))
95 `(if (fli:null-pointer-p ,obj)
97 (fli:convert-from-foreign-string
99 ,@(if length (list :length length) (values))
100 :null-terminated-p ,null-terminated-p
101 :external-format '(:latin-1 :eol-style :lf)))
103 `(cmucl-naturalize-cstring (alien:alien-sap ,obj)
105 :null-terminated-p ,null-terminated-p)
108 (defmacro convert-to-foreign-string (obj)
111 +null-cstring-pointer+
113 :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
118 (values (excl:string-to-native ,obj)))
120 (let ((size (gensym))
123 `(when (stringp ,obj)
124 (let* ((,size (length ,obj))
125 (,storage (alien:make-alien char (1+ ,size))))
126 (setq ,storage (alien:cast ,storage (* char)))
129 (optimize (speed 3) (safety 0)))
130 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
131 (setf (alien:deref ,storage ,size) 0)
136 (defmacro allocate-foreign-string (size &key (unsigned nil))
138 (let ((array-def (gensym)))
139 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
140 (eval `(alien:cast (alien:make-alien ,,array-def)
142 '(* (alien:unsigned 8))
143 '(* (alien:signed 8)))))))
145 `(fli:allocate-foreign-object :type
151 (declare (ignore unsigned))
153 `(ff:allocate-fobject :char :c ,size)
156 (defmacro with-cstring ((foreign-string lisp-string) &body body)
158 `(let ((,foreign-string ,lisp-string)) ,@body)
160 (let ((acl-native (gensym)))
161 `(excl:with-native-string (,acl-native ,lisp-string)
162 (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
165 (let ((result (gensym)))
166 `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
168 (fli:free-foreign-object ,foreign-string)
172 ;; Modified from CMUCL's source to handle non-null terminated strings
174 (defun cmucl-naturalize-cstring (sap &key
176 (null-terminated-p t))
177 (declare (type system:system-area-pointer sap))
179 (declare (optimize (speed 3) (safety 0)))
180 (let ((null-terminated-length
181 (when null-terminated-p
183 for offset of-type fixnum upfrom 0
184 until (zerop (system:sap-ref-8 sap offset))
185 finally (return offset)))))
187 (if (and null-terminated-length
188 (> (the fixnum length) (the fixnum null-terminated-length)))
189 (setq length null-terminated-length))
190 (setq length null-terminated-length)))
191 (let ((result (make-string length)))
192 (kernel:copy-from-system-area sap 0
193 result (* vm:vector-data-offset
195 (* length vm:byte-bits))