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.12 2002/03/22 20:51:08 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)))
71 ;;; Foreign string functions
73 (defmacro convert-to-foreign-string (obj)
76 +null-cstring-pointer+
77 (fli:convert-to-foreign-string ,obj))
81 (values (excl:string-to-native ,obj)))
88 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
90 (let* ((,size (length ,obj))
91 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
92 (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
94 (declare (optimize (speed 3) (safety 0)))
97 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
98 (setf (alien:deref ,storage ,size) 0))
103 ;; Either length or null-terminated-p must be non-nil
104 (defmacro convert-from-foreign-string (obj &key
106 (null-terminated-p t))
110 (values (excl:native-to-string
112 ,@(if length (list :length length) (values))
113 :truncate (not ,null-terminated-p))))
115 `(if (fli:null-pointer-p ,obj)
117 (fli:convert-from-foreign-string
119 ,@(if length (list :length length) (values))
120 :null-terminated-p ,null-terminated-p
121 :external-format '(:latin-1 :eol-style :lf)))
123 `(if (null-pointer-p ,obj)
125 (cmucl-naturalize-cstring (alien:alien-sap ,obj)
127 :null-terminated-p ,null-terminated-p))
132 (defmacro allocate-foreign-string (size &key (unsigned t))
134 (let ((array-def (gensym)))
135 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
136 (eval `(alien:cast (alien:make-alien ,,array-def)
138 '(* (alien:unsigned 8))
139 '(* (alien:signed 8)))))))
141 `(fli:allocate-foreign-object :type
147 (declare (ignore unsigned))
149 `(ff:allocate-fobject :char :c ,size)
152 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
154 (let ((result (gensym)))
155 `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
156 (,result (progn ,@body)))
157 (declare (dynamic-extent ,foreign-string))
158 (free-foreign-object ,foreign-string)
165 ;; Modified from CMUCL's source to handle non-null terminated strings
167 (defun cmucl-naturalize-cstring (sap &key
169 (null-terminated-p t))
170 (declare (type system:system-area-pointer sap))
172 (declare (optimize (speed 3) (safety 0)))
173 (let ((null-terminated-length
174 (when null-terminated-p
176 for offset of-type fixnum upfrom 0
177 until (zerop (system:sap-ref-8 sap offset))
178 finally (return offset)))))
180 (if (and null-terminated-length
181 (> (the fixnum length) (the fixnum null-terminated-length)))
182 (setq length null-terminated-length))
183 (setq length null-terminated-length)))
184 (let ((result (make-string length)))
185 (kernel:copy-from-system-area sap 0
186 result (* vm:vector-data-offset
188 (* length vm:byte-bits))