1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
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.1 2002/04/04 05:03:38 desoi 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 (defvar +null-cstring-pointer+ (ccl:%null-ptr))
25 (defmacro convert-from-cstring (obj)
26 "Converts a string from a c-call. Same as convert-from-foreign-string, except
27 that CMU automatically converts strings from c-calls."
30 (let ((stored (gensym)))
31 `(let ((,stored ,obj))
32 (if (fli:null-pointer-p ,stored)
34 (fli:convert-from-foreign-string ,stored))))
36 (let ((stored (gensym)))
37 `(let ((,stored ,obj))
40 (values (excl:native-to-string ,stored)))))
42 (let ((stored (gensym)))
43 `(let ((,stored ,obj))
44 (if (ccl:%null-ptr-p ,stored)
46 (values (ccl:%get-cstring ,stored)))))
51 (defmacro convert-to-cstring (obj)
54 +null-cstring-pointer+
55 (fli:convert-to-foreign-string ,obj))
59 (values (excl:string-to-native ,obj)))
61 (declare (ignore obj))
64 +null-cstring-pointer+
65 (let ((ptr (new-ptr (1+ (length ,obj)))))
66 (ccl:%put-cstring ptr ,obj)
70 (defmacro free-cstring (obj)
72 `(unless (fli:null-pointer-p ,obj)
73 (fli:free-foreign-object ,obj))
76 (ff:free-fobject ,obj))
78 (declare (ignore obj))
80 `(unless (ccl:%null-ptr-p ,obj)
85 ;; Either length or null-terminated-p must be non-nil
86 (defmacro convert-from-foreign-string (obj &key
88 (null-terminated-p t))
92 (values (excl:native-to-string
94 ,@(if length (list :length length) (values))
95 :truncate (not ,null-terminated-p))))
97 `(if (fli:null-pointer-p ,obj)
99 (fli:convert-from-foreign-string
101 ,@(if length (list :length length) (values))
102 :null-terminated-p ,null-terminated-p
103 :external-format '(:latin-1 :eol-style :lf)))
105 `(cmucl-naturalize-cstring (alien:alien-sap ,obj)
107 :null-terminated-p ,null-terminated-p)
109 (declare (ignore null-terminated-p))
111 `(if (ccl:%null-ptr-p ,obj)
113 (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
116 (defmacro convert-to-foreign-string (obj)
119 +null-cstring-pointer+
120 (fli:convert-to-foreign-string ,obj))
124 (values (excl:string-to-native ,obj)))
126 (let ((size (gensym))
129 `(when (stringp ,obj)
130 (let* ((,size (length ,obj))
131 (,storage (alien:make-alien char (1+ ,size))))
132 (setq ,storage (alien:cast ,storage (* char)))
135 (optimize (speed 3) (safety 0)))
136 (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
137 (setf (alien:deref ,storage ,size) 0)
141 +null-cstring-pointer+
142 (let ((ptr (new-ptr (1+ (length ,obj)))))
143 (ccl:%put-cstring ptr ,obj)
148 (defmacro allocate-foreign-string (size &key (unsigned t))
150 (let ((array-def (gensym)))
151 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
152 (eval `(alien:cast (alien:make-alien ,,array-def)
154 '(* (alien:unsigned 8))
155 '(* (alien:signed 8)))))))
157 `(fli:allocate-foreign-object :type
163 (declare (ignore unsigned))
165 `(ff:allocate-fobject :char :c ,size)
167 (declare (ignore unsigned))
174 ; I'm sure there must be a better way to write this...
175 (defmacro with-cstring ((foreign-string lisp-string) &body body)
176 `(if (stringp ,lisp-string)
177 (ccl:with-cstrs ((,foreign-string ,lisp-string))
179 (let ((,foreign-string +null-cstring-pointer+))
183 #| Works but, supposedly the built in method is better
184 (defmacro with-cstring ((foreign-string lisp-string) &body body)
185 (let ((result (gensym)))
186 `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
188 (dispose-ptr ,foreign-string)