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.3 2002/03/10 05:09:00 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 (defmacro convert-from-c-string (obj)
35 "Converts a string from a c-call. Same as convert-from-foreign-string, except
36 that CMU automatically converts strings from c-calls."
39 (let ((stored (gensym)))
40 `(let ((,stored ,obj))
41 (if (fli:null-pointer-p ,stored)
43 (fli:convert-from-foreign-string ,stored))))
45 (let ((stored (gensym)))
46 `(let ((,stored ,obj))
49 (values (excl:native-to-string ,stored)))))
52 (defmacro convert-to-c-string (obj)
55 +null-c-string-pointer+
57 :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
62 (values (excl:string-to-native ,obj)))
64 (declare (ignore obj))
67 (defmacro free-c-string (obj)
69 `(unless (fli:null-pointer-p ,obj)
70 (fli:free-foreign-object ,obj))
73 (ff:free-fobject ,obj))
75 (declare (ignore obj))
78 ;; Either length or null-terminated-p must be non-nil
79 (defmacro convert-from-foreign-string (obj &key
81 (null-terminated-p t))
85 (values (excl:native-to-string
87 ,@(if length (list :length length) (values))
88 :truncate (not ,null-terminated-p))))
90 `(if (fli:null-pointer-p ,obj)
92 (fli:convert-from-foreign-string
94 ,@(if length (list :length length) (values))
95 :null-terminated-p ,null-terminated-p
96 :external-format '(:latin-1 :eol-style :lf)))
98 `(cmucl-naturalize-c-string (alien:alien-sap ,obj)
100 :null-terminated-p ,null-terminated-p)
103 (defmacro convert-to-foreign-string (obj)
106 +null-c-string-pointer+
108 :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
113 (values (excl:string-to-native ,obj)))
115 (let ((size (gensym))
118 `(when (stringp ,obj)
119 (let* ((,size (length ,obj))
120 (,storage (alien:make-alien char (1+ ,size))))
121 (setq ,storage (alien:cast ,storage (* char)))
124 (optimize (speed 3) (safety 0)))
125 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
126 (setf (alien:deref ,storage ,size) 0)
131 (defmacro allocate-foreign-string (size &key (unsigned nil))
133 (let ((array-def (gensym)))
134 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
135 (eval `(alien:cast (alien:make-alien ,,array-def)
137 '(* (alien:unsigned 8))
138 '(* (alien:signed 8)))))))
140 `(fli:allocate-foreign-object :type
146 (declare (ignore unsigned))
148 `(ff:allocate-fobject :char :c ,size)
151 (defmacro with-c-string ((foreign-string lisp-string) &body body)
153 `(let ((,foreign-string ,lisp-string)) ,@body)
155 (let ((acl-native (gensym)))
156 `(excl:with-native-string (,acl-native ,lisp-string)
157 (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
160 (let ((result (gensym)))
161 `(let* ((,foreign-string (convert-to-c-string ,lisp-string))
163 (fli:free-foreign-object ,foreign-string)
167 ;; Modified from CMUCL's source to handle non-null terminated strings
169 (defun cmucl-naturalize-c-string (sap &key
171 (null-terminated-p t))
172 (declare (type system:system-area-pointer sap))
174 (declare (optimize (speed 3) (safety 0)))
175 (let ((null-terminated-length
176 (when null-terminated-p
178 for offset of-type fixnum upfrom 0
179 until (zerop (system:sap-ref-8 sap offset))
180 finally (return offset)))))
182 (if (and null-terminated-length
183 (> (the fixnum length) (the fixnum null-terminated-length)))
184 (setq length null-terminated-length))
185 (setq length null-terminated-length)))
186 (let ((result (make-string length)))
187 (kernel:copy-from-system-area sap 0
188 result (* vm:vector-data-offset
190 (* length vm:byte-bits))