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.16 2002/04/06 19:45:14 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 LW/CMU automatically converts strings from c-calls."
34 (let ((stored (gensym)))
35 `(let ((,stored ,obj))
38 (values (excl:native-to-string ,stored)))))
41 (defmacro convert-to-cstring (obj)
47 (values (excl:string-to-native ,obj)))
50 (defmacro free-cstring (obj)
51 #+cmu (declare (ignore obj))
52 #+lispworks (declare (ignore obj))
55 (ff:free-fobject ,obj))
58 (defmacro with-cstring ((cstring lisp-string) &body body)
60 `(let ((,cstring ,lisp-string)) ,@body)
62 `(let ((,cstring ,lisp-string)) ,@body)
64 (let ((acl-native (gensym)))
65 `(excl:with-native-string (,acl-native ,lisp-string)
66 (let ((,cstring (if ,lisp-string ,acl-native 0)))
70 (defmacro with-cstrings (bindings &rest body)
72 `(with-cstring ,(car bindings)
73 (with-cstrings ,(cdr bindings)
77 ;;; Foreign string functions
79 (defmacro convert-to-foreign-string (obj)
82 +null-cstring-pointer+
83 (fli:convert-to-foreign-string ,obj))
87 (values (excl:string-to-native ,obj)))
94 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
96 (let* ((,size (length ,obj))
97 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
98 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
100 (declare (optimize (speed 3) (safety 0)))
102 (declare (fixnum ,i))
103 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
104 (setf (alien:deref ,storage ,size) 0))
109 ;; Either length or null-terminated-p must be non-nil
110 (defmacro convert-from-foreign-string (obj &key
112 (null-terminated-p t))
116 (values (excl:native-to-string
118 ,@(if length (list :length length) (values))
119 :truncate (not ,null-terminated-p))))
121 `(if (fli:null-pointer-p ,obj)
123 (fli:convert-from-foreign-string
125 ,@(if length (list :length length) (values))
126 :null-terminated-p ,null-terminated-p
127 :external-format '(:latin-1 :eol-style :lf)))
129 `(if (null-pointer-p ,obj)
131 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
133 :null-terminated-p ,null-terminated-p))
138 (defmacro allocate-foreign-string (size &key (unsigned t))
140 (let ((array-def (gensym)))
141 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
142 (eval `(alien:cast (alien:make-alien ,,array-def)
144 '(* (alien:unsigned 8))
145 '(* (alien:signed 8)))))))
147 `(fli:allocate-foreign-object :type
153 (declare (ignore unsigned))
155 `(ff:allocate-fobject :char :c ,size)
158 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
159 (let ((result (gensym)))
160 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
161 (,result (progn ,@body)))
162 (declare (dynamic-extent ,foreign-string))
163 (free-foreign-object ,foreign-string)
167 ;; Modified from CMUCL's source to handle non-null terminated strings
169 (defun cmucl-naturalize-cstring (sap &key
171 (null-terminated-p t))
172 (declare (type system:system-area-pointer sap))
174 (declare (optimize (speed 3) (safety 0)))
175 (let ((null-terminated-length
176 (when null-terminated-p
178 for offset of-type fixnum upfrom 0
179 until (zerop (system:sap-ref-8 sap offset))
180 finally (return offset)))))
182 (if (and null-terminated-length
183 (> (the fixnum length) (the fixnum null-terminated-length)))
184 (setq length null-terminated-length))
185 (setq length null-terminated-length)))
186 (let ((result (make-string length)))
187 (kernel:copy-from-system-area sap 0
188 result (* vm:vector-data-offset
190 (* length vm:byte-bits))