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.1 2002/09/30 10:02:36 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+
26 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
28 #-(or cmu allegro lispworks mcl) nil
31 (defmacro convert-from-cstring (obj)
32 "Converts a string from a c-call. Same as convert-from-foreign-string, except
33 that LW/CMU automatically converts strings from c-calls."
37 (let ((stored (gensym)))
38 `(let ((,stored ,obj))
41 (values (excl:native-to-string ,stored)))))
43 (let ((stored (gensym)))
44 `(let ((,stored ,obj))
45 (if (ccl:%null-ptr-p ,stored)
47 (values (ccl:%get-cstring ,stored)))))
50 (defmacro convert-to-cstring (obj)
56 (values (excl:string-to-native ,obj)))
59 +null-cstring-pointer+
60 (let ((ptr (new-ptr (1+ (length ,obj)))))
61 (ccl:%put-cstring ptr ,obj)
65 (defmacro free-cstring (obj)
66 #+cmu (declare (ignore obj))
67 #+lispworks (declare (ignore obj))
70 (ff:free-fobject ,obj))
72 `(unless (ccl:%null-ptr-p ,obj)
76 (defmacro with-cstring ((cstring lisp-string) &body body)
78 `(let ((,cstring ,lisp-string)) ,@body)
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))
132 +null-cstring-pointer+
133 (let ((ptr (new-ptr (1+ (length ,obj)))))
134 (ccl:%put-cstring ptr ,obj)
139 ;; Either length or null-terminated-p must be non-nil
140 (defmacro convert-from-foreign-string (obj &key
142 (null-terminated-p t))
146 (values (excl:native-to-string
148 ,@(if length (list :length length) (values))
149 :truncate (not ,null-terminated-p))))
151 `(if (fli:null-pointer-p ,obj)
153 (fli:convert-from-foreign-string
155 ,@(if length (list :length length) (values))
156 :null-terminated-p ,null-terminated-p
157 :external-format '(:latin-1 :eol-style :lf)))
159 `(if (null-pointer-p ,obj)
161 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
163 :null-terminated-p ,null-terminated-p))
165 (declare (ignore null-terminated-p))
167 `(if (ccl:%null-ptr-p ,obj)
169 (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
174 (defmacro allocate-foreign-string (size &key (unsigned t))
176 (let ((array-def (gensym)))
177 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
178 (eval `(alien:cast (alien:make-alien ,,array-def)
180 '(* (alien:unsigned 8))
181 '(* (alien:signed 8)))))))
183 `(fli:allocate-foreign-object :type
189 (declare (ignore unsigned))
191 `(ff:allocate-fobject :char :c ,size)
193 (declare (ignore unsigned))
198 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
199 (let ((result (gensym)))
200 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
201 (,result (progn ,@body)))
202 (declare (dynamic-extent ,foreign-string))
203 (free-foreign-object ,foreign-string)
207 ;; Modified from CMUCL's source to handle non-null terminated strings
209 (defun cmucl-naturalize-cstring (sap &key
211 (null-terminated-p t))
212 (declare (type system:system-area-pointer sap))
214 (declare (optimize (speed 3) (safety 0)))
215 (let ((null-terminated-length
216 (when null-terminated-p
218 for offset of-type fixnum upfrom 0
219 until (zerop (system:sap-ref-8 sap offset))
220 finally (return offset)))))
222 (if (and null-terminated-length
223 (> (the fixnum length) (the fixnum null-terminated-length)))
224 (setq length null-terminated-length))
225 (setq length null-terminated-length)))
226 (let ((result (make-string length)))
227 (kernel:copy-from-system-area sap 0
228 result (* vm:vector-data-offset
230 (* length vm:byte-bits))