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.1 2002/03/09 19:55:33 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)
56 (fli:convert-to-foreign-string ,obj))
60 (values (excl:string-to-native ,obj)))
62 (declare (ignore obj))
65 (defmacro free-c-string (obj)
67 `(unless (fli:null-pointer-p ,obj)
68 (fli:free-foreign-object ,obj))
71 (ff:free-fobject ,obj))
73 (declare (ignore obj))
76 ;; Either length or null-terminated-p must be non-nil
77 (defmacro convert-from-foreign-string (obj &key
79 (null-terminated-p t))
83 (values (excl:native-to-string
85 ,@(if length (list :length length) (values))
86 :truncate (not ,null-terminated-p))))
88 `(if (fli:null-pointer-p ,obj)
90 (fli:convert-from-foreign-string
92 ,@(if length (list :length length) (values))
93 :null-terminated-p ,null-terminated-p
94 :external-format '(:latin-1 :eol-style :lf)))
96 `(cmucl-naturalize-c-string (alien:alien-sap ,obj)
98 :null-terminated-p ,null-terminated-p)
101 (defmacro convert-to-foreign-string (obj)
105 (fli:convert-to-foreign-string ,obj))
109 (values (excl:string-to-native ,obj)))
111 (let ((size (gensym))
114 `(when (stringp ,obj)
115 (let* ((,size (length ,obj))
116 (,storage (alien:make-alien char (1+ ,size))))
117 (setq ,storage (alien:cast ,storage (* char)))
120 (optimize (speed 3) (safety 0)))
121 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
122 (setf (alien:deref ,storage ,size) 0)
127 (defmacro allocate-foreign-string (size)
129 (let ((array-def (gensym)))
130 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
131 (eval `(alien:cast (alien:make-alien ,,array-def) (* (alien:unsigned 8))))))
133 `(fli:allocate-foreign-object :type '(:unsigned :char) :nelems ,size)
135 `(ff:allocate-fobject :char :c ,size)
138 (defmacro with-c-string ((foreign-string lisp-string) &body body)
140 `(let ((,foreign-string ,lisp-string)) ,@body)
142 (let ((acl-native (gensym)))
143 `(excl:with-native-string (,acl-native ,lisp-string)
144 (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
147 (let ((result (gensym)))
148 `(let* ((,foreign-string (convert-to-c-string ,lisp-string))
150 (fli:free-foreign-object ,foreign-string)
154 ;; Modified from CMUCL's source to handle non-null terminated strings
156 (defun cmucl-naturalize-c-string (sap &key
158 (null-terminated-p t))
159 (declare (type system:system-area-pointer sap))
161 (declare (optimize (speed 3) (safety 0)))
162 (let ((null-terminated-length
163 (when null-terminated-p
165 for offset of-type fixnum upfrom 0
166 until (zerop (system:sap-ref-8 sap offset))
167 finally (return offset)))))
169 (if (and null-terminated-length
170 (> (the fixnum length) (the fixnum null-terminated-length)))
171 (setq length null-terminated-length))
172 (setq length null-terminated-length)))
173 (let ((result (make-string length)))
174 (kernel:copy-from-system-area sap 0
175 result (* vm:vector-data-offset
177 (* length vm:byte-bits))