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.cl,v 1.18 2002/04/28 06:03:13 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 (def-constant +null-cstring-pointer+
26 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
27 #-(or cmu allegro lispworks) nil
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."
36 (let ((stored (gensym)))
37 `(let ((,stored ,obj))
40 (values (excl:native-to-string ,stored)))))
43 (defmacro convert-to-cstring (obj)
49 (values (excl:string-to-native ,obj)))
52 (defmacro free-cstring (obj)
53 #+cmu (declare (ignore obj))
54 #+lispworks (declare (ignore obj))
57 (ff:free-fobject ,obj))
60 (defmacro with-cstring ((cstring lisp-string) &body body)
62 `(let ((,cstring ,lisp-string)) ,@body)
64 `(let ((,cstring ,lisp-string)) ,@body)
66 (let ((acl-native (gensym)))
67 `(excl:with-native-string (,acl-native ,lisp-string)
68 (let ((,cstring (if ,lisp-string ,acl-native 0)))
72 (defmacro with-cstrings (bindings &rest body)
74 `(with-cstring ,(car bindings)
75 (with-cstrings ,(cdr bindings)
79 ;;; Foreign string functions
81 (defmacro convert-to-foreign-string (obj)
84 +null-cstring-pointer+
85 (fli:convert-to-foreign-string ,obj))
89 (values (excl:string-to-native ,obj)))
96 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
98 (let* ((,size (length ,obj))
99 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
100 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
102 (declare (optimize (speed 3) (safety 0)))
104 (declare (fixnum ,i))
105 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
106 (setf (alien:deref ,storage ,size) 0))
111 ;; Either length or null-terminated-p must be non-nil
112 (defmacro convert-from-foreign-string (obj &key
114 (null-terminated-p t))
118 (values (excl:native-to-string
120 ,@(if length (list :length length) (values))
121 :truncate (not ,null-terminated-p))))
123 `(if (fli:null-pointer-p ,obj)
125 (fli:convert-from-foreign-string
127 ,@(if length (list :length length) (values))
128 :null-terminated-p ,null-terminated-p
129 :external-format '(:latin-1 :eol-style :lf)))
131 `(if (null-pointer-p ,obj)
133 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
135 :null-terminated-p ,null-terminated-p))
140 (defmacro allocate-foreign-string (size &key (unsigned t))
142 (let ((array-def (gensym)))
143 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
144 (eval `(alien:cast (alien:make-alien ,,array-def)
146 '(* (alien:unsigned 8))
147 '(* (alien:signed 8)))))))
149 `(fli:allocate-foreign-object :type
155 (declare (ignore unsigned))
157 `(ff:allocate-fobject :char :c ,size)
160 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
161 (let ((result (gensym)))
162 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
163 (,result (progn ,@body)))
164 (declare (dynamic-extent ,foreign-string))
165 (free-foreign-object ,foreign-string)
169 ;; Modified from CMUCL's source to handle non-null terminated strings
171 (defun cmucl-naturalize-cstring (sap &key
173 (null-terminated-p t))
174 (declare (type system:system-area-pointer sap))
176 (declare (optimize (speed 3) (safety 0)))
177 (let ((null-terminated-length
178 (when null-terminated-p
180 for offset of-type fixnum upfrom 0
181 until (zerop (system:sap-ref-8 sap offset))
182 finally (return offset)))))
184 (if (and null-terminated-length
185 (> (the fixnum length) (the fixnum null-terminated-length)))
186 (setq length null-terminated-length))
187 (setq length null-terminated-length)))
188 (let ((result (make-string length)))
189 (kernel:copy-from-system-area sap 0
190 result (* vm:vector-data-offset
192 (* length vm:byte-bits))