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 ;;;; Programmers: Kevin M. Rosenberg and John DeSoi
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: strings.cl,v 1.3 2002/08/23 19:21:54 kevin Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; UFFI users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
24 (defvar +null-cstring-pointer+ (ccl:%null-ptr))
26 (defmacro convert-from-cstring (obj)
27 "Converts a string from a c-call. Same as convert-from-foreign-string, except
28 that CMU automatically converts strings from c-calls."
31 (let ((stored (gensym)))
32 `(let ((,stored ,obj))
33 (if (fli:null-pointer-p ,stored)
35 (fli:convert-from-foreign-string ,stored))))
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)))))
52 (defmacro convert-to-cstring (obj)
55 +null-cstring-pointer+
56 (fli:convert-to-foreign-string ,obj))
60 (values (excl:string-to-native ,obj)))
62 (declare (ignore obj))
65 +null-cstring-pointer+
66 (let ((ptr (new-ptr (1+ (length ,obj)))))
67 (ccl:%put-cstring ptr ,obj)
71 (defmacro free-cstring (obj)
73 `(unless (fli:null-pointer-p ,obj)
74 (fli:free-foreign-object ,obj))
77 (ff:free-fobject ,obj))
79 (declare (ignore obj))
81 `(unless (ccl:%null-ptr-p ,obj)
86 ;; Either length or null-terminated-p must be non-nil
87 (defmacro convert-from-foreign-string (obj &key
89 (null-terminated-p t))
93 (values (excl:native-to-string
95 ,@(if length (list :length length) (values))
96 :truncate (not ,null-terminated-p))))
98 `(if (fli:null-pointer-p ,obj)
100 (fli:convert-from-foreign-string
102 ,@(if length (list :length length) (values))
103 :null-terminated-p ,null-terminated-p
104 :external-format '(:latin-1 :eol-style :lf)))
106 `(cmucl-naturalize-cstring (alien:alien-sap ,obj)
108 :null-terminated-p ,null-terminated-p)
110 (declare (ignore null-terminated-p))
112 `(if (ccl:%null-ptr-p ,obj)
114 (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
117 (defmacro convert-to-foreign-string (obj)
120 +null-cstring-pointer+
121 (fli:convert-to-foreign-string ,obj))
125 (values (excl:string-to-native ,obj)))
127 (let ((size (gensym))
130 `(when (stringp ,obj)
131 (let* ((,size (length ,obj))
132 (,storage (alien:make-alien char (1+ ,size))))
133 (setq ,storage (alien:cast ,storage (* char)))
136 (optimize (speed 3) (safety 0)))
137 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
138 (setf (alien:deref ,storage ,size) 0)
142 +null-cstring-pointer+
143 (let ((ptr (new-ptr (1+ (length ,obj)))))
144 (ccl:%put-cstring ptr ,obj)
149 (defmacro allocate-foreign-string (size &key (unsigned t))
151 (let ((array-def (gensym)))
152 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
153 (eval `(alien:cast (alien:make-alien ,,array-def)
155 '(* (alien:unsigned 8))
156 '(* (alien:signed 8)))))))
158 `(fli:allocate-foreign-object :type
164 (declare (ignore unsigned))
166 `(ff:allocate-fobject :char :c ,size)
168 (declare (ignore unsigned))
175 ; I'm sure there must be a better way to write this...
176 (defmacro with-cstring ((foreign-string lisp-string) &body body)
177 `(if (stringp ,lisp-string)
178 (ccl:with-cstrs ((,foreign-string ,lisp-string))
180 (let ((,foreign-string +null-cstring-pointer+))
184 #| Works but, supposedly the built in method is better
185 (defmacro with-cstring ((foreign-string lisp-string) &body body)
186 (let ((result (gensym)))
187 `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
189 (dispose-ptr ,foreign-string)