1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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.5 2002/03/14 21:03:12 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 :char))
28 (defmacro convert-from-cstring (obj)
29 "Converts a string from a c-call. Same as convert-from-foreign-string, except
30 that CMU automatically converts strings from c-calls."
33 (let ((stored (gensym)))
34 `(let ((,stored ,obj))
35 (if (fli:null-pointer-p ,stored)
37 (fli:convert-from-foreign-string ,stored))))
39 (let ((stored (gensym)))
40 `(let ((,stored ,obj))
43 (values (excl:native-to-string ,stored)))))
46 (defmacro convert-to-cstring (obj)
49 +null-cstring-pointer+
51 :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
56 (values (excl:string-to-native ,obj)))
58 (declare (ignore obj))
61 (defmacro free-cstring (obj)
63 `(unless (fli:null-pointer-p ,obj)
64 (fli:free-foreign-object ,obj))
67 (ff:free-fobject ,obj))
69 (declare (ignore obj))
72 ;; Either length or null-terminated-p must be non-nil
73 (defmacro convert-from-foreign-string (obj &key
75 (null-terminated-p t))
79 (values (excl:native-to-string
81 ,@(if length (list :length length) (values))
82 :truncate (not ,null-terminated-p))))
84 `(if (fli:null-pointer-p ,obj)
86 (fli:convert-from-foreign-string
88 ,@(if length (list :length length) (values))
89 :null-terminated-p ,null-terminated-p
90 :external-format '(:latin-1 :eol-style :lf)))
92 `(cmucl-naturalize-cstring (alien:alien-sap ,obj)
94 :null-terminated-p ,null-terminated-p)
97 (defmacro convert-to-foreign-string (obj)
100 +null-cstring-pointer+
102 :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
107 (values (excl:string-to-native ,obj)))
109 (let ((size (gensym))
112 `(when (stringp ,obj)
113 (let* ((,size (length ,obj))
114 (,storage (alien:make-alien char (1+ ,size))))
115 (setq ,storage (alien:cast ,storage (* char)))
118 (optimize (speed 3) (safety 0)))
119 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
120 (setf (alien:deref ,storage ,size) 0)
125 (defmacro allocate-foreign-string (size &key (unsigned nil))
127 (let ((array-def (gensym)))
128 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
129 (eval `(alien:cast (alien:make-alien ,,array-def)
131 '(* (alien:unsigned 8))
132 '(* (alien:signed 8)))))))
134 `(fli:allocate-foreign-object :type
140 (declare (ignore unsigned))
142 `(ff:allocate-fobject :char :c ,size)
145 (defmacro with-cstring ((foreign-string lisp-string) &body body)
147 `(let ((,foreign-string ,lisp-string)) ,@body)
149 (let ((acl-native (gensym)))
150 `(excl:with-native-string (,acl-native ,lisp-string)
151 (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
154 (let ((result (gensym)))
155 `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
157 (fli:free-foreign-object ,foreign-string)
161 ;; Modified from CMUCL's source to handle non-null terminated strings
163 (defun cmucl-naturalize-cstring (sap &key
165 (null-terminated-p t))
166 (declare (type system:system-area-pointer sap))
168 (declare (optimize (speed 3) (safety 0)))
169 (let ((null-terminated-length
170 (when null-terminated-p
172 for offset of-type fixnum upfrom 0
173 until (zerop (system:sap-ref-8 sap offset))
174 finally (return offset)))))
176 (if (and null-terminated-length
177 (> (the fixnum length) (the fixnum null-terminated-length)))
178 (setq length null-terminated-length))
179 (setq length null-terminated-length)))
180 (let ((result (make-string length)))
181 (kernel:copy-from-system-area sap 0
182 result (* vm:vector-data-offset
184 (* length vm:byte-bits))