1 ;;;; -*- Mode: ANSI-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.8 2002/03/19 16:42:59 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)))
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+
50 (fli:convert-to-foreign-string ,obj))
54 (values (excl:string-to-native ,obj)))
56 (declare (ignore obj))
59 (defmacro free-cstring (obj)
61 `(unless (fli:null-pointer-p ,obj)
62 (fli:free-foreign-object ,obj))
65 (ff:free-fobject ,obj))
67 (declare (ignore obj))
70 ;; Either length or null-terminated-p must be non-nil
71 (defmacro convert-from-foreign-string (obj &key
73 (null-terminated-p t))
77 (values (excl:native-to-string
79 ,@(if length (list :length length) (values))
80 :truncate (not ,null-terminated-p))))
82 `(if (fli:null-pointer-p ,obj)
84 (fli:convert-from-foreign-string
86 ,@(if length (list :length length) (values))
87 :null-terminated-p ,null-terminated-p
88 :external-format '(:latin-1 :eol-style :lf)))
90 `(cmucl-naturalize-cstring (alien:alien-sap ,obj)
92 :null-terminated-p ,null-terminated-p)
95 (defmacro convert-to-foreign-string (obj)
98 +null-cstring-pointer+
99 (fli:convert-to-foreign-string ,obj))
103 (values (excl:string-to-native ,obj)))
105 (let ((size (gensym))
108 `(when (stringp ,obj)
109 (let* ((,size (length ,obj))
110 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
111 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
113 (declare (optimize (speed 3) (safety 0)))
115 (declare (fixnum ,i))
116 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
117 (setf (alien:deref ,storage ,size) 0))
122 (defmacro allocate-foreign-string (size &key (unsigned t))
124 (let ((array-def (gensym)))
125 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
126 (eval `(alien:cast (alien:make-alien ,,array-def)
128 '(* (alien:unsigned 8))
129 '(* (alien:signed 8)))))))
131 `(fli:allocate-foreign-object :type
137 (declare (ignore unsigned))
139 `(ff:allocate-fobject :char :c ,size)
142 (defmacro with-cstring ((foreign-string lisp-string) &body body)
144 `(let ((,foreign-string ,lisp-string)) ,@body)
146 (let ((acl-native (gensym)))
147 `(excl:with-native-string (,acl-native ,lisp-string)
148 (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
151 (let ((result (gensym)))
152 `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
154 (fli:free-foreign-object ,foreign-string)
158 ;; Modified from CMUCL's source to handle non-null terminated strings
160 (defun cmucl-naturalize-cstring (sap &key
162 (null-terminated-p t))
163 (declare (type system:system-area-pointer sap))
165 (declare (optimize (speed 3) (safety 0)))
166 (let ((null-terminated-length
167 (when null-terminated-p
169 for offset of-type fixnum upfrom 0
170 until (zerop (system:sap-ref-8 sap offset))
171 finally (return offset)))))
173 (if (and null-terminated-length
174 (> (the fixnum length) (the fixnum null-terminated-length)))
175 (setq length null-terminated-length))
176 (setq length null-terminated-length)))
177 (let ((result (make-string length)))
178 (kernel:copy-from-system-area sap 0
179 result (* vm:vector-data-offset
181 (* length vm:byte-bits))