r2912: rename .cl to .lisp
[uffi.git] / src / strings.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          strings.cl
6 ;;;; Purpose:       UFFI source to handle strings, cstring and foreigns
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: strings.lisp,v 1.1 2002/09/30 10:02:36 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22
23 (defvar +null-cstring-pointer+
24     #+cmu nil
25     #+allegro 0
26     #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
27     #+mcl (ccl:%null-ptr)
28     #-(or cmu allegro lispworks mcl) nil
29 )
30
31 (defmacro convert-from-cstring (obj)
32   "Converts a string from a c-call. Same as convert-from-foreign-string, except
33 that LW/CMU automatically converts strings from c-calls."
34   #+cmu obj
35   #+lispworks obj
36   #+allegro 
37   (let ((stored (gensym)))
38     `(let ((,stored ,obj))
39        (if (zerop ,stored)
40            nil
41          (values (excl:native-to-string ,stored)))))
42   #+mcl 
43   (let ((stored (gensym)))
44     `(let ((,stored ,obj))
45        (if (ccl:%null-ptr-p ,stored)
46            nil
47          (values (ccl:%get-cstring ,stored)))))
48   )
49
50 (defmacro convert-to-cstring (obj)
51   #+cmu obj
52   #+lispworks obj
53   #+allegro
54   `(if (null ,obj)
55     0
56     (values (excl:string-to-native ,obj)))
57   #+mcl
58   `(if (null ,obj)
59     +null-cstring-pointer+
60     (let ((ptr (new-ptr (1+ (length ,obj)))))
61       (ccl:%put-cstring ptr ,obj)
62       ptr))
63   )
64
65 (defmacro free-cstring (obj)
66   #+cmu (declare (ignore obj))
67   #+lispworks (declare (ignore obj))
68   #+allegro
69   `(unless (zerop obj)
70      (ff:free-fobject ,obj))
71   #+mcl
72   `(unless (ccl:%null-ptr-p ,obj)
73      (dispose-ptr ,obj))
74   )
75
76 (defmacro with-cstring ((cstring lisp-string) &body body)
77   #+cmu
78   `(let ((,cstring ,lisp-string)) ,@body) 
79   #+lispworks
80   `(let ((,cstring ,lisp-string)) ,@body) 
81   #+allegro
82   (let ((acl-native (gensym)))
83     `(excl:with-native-string (,acl-native ,lisp-string)
84        (let ((,cstring (if ,lisp-string ,acl-native 0)))
85          ,@body)))
86   #+mcl
87   `(if (stringp ,lisp-string)
88      (ccl:with-cstrs ((,cstring ,lisp-string))
89        ,@body)
90      (let ((,cstring +null-cstring-pointer+))
91        ,@body))
92   )
93
94 (defmacro with-cstrings (bindings &rest body)
95   (if bindings
96       `(with-cstring ,(car bindings)
97         (with-cstrings ,(cdr bindings)
98           ,@body))
99       `(progn ,@body)))
100
101 ;;; Foreign string functions
102
103 (defmacro convert-to-foreign-string (obj)
104   #+lispworks
105   `(if (null ,obj)
106        +null-cstring-pointer+
107     (fli:convert-to-foreign-string ,obj))
108   #+allegro
109   `(if (null ,obj)
110        0
111      (values (excl:string-to-native ,obj)))
112   #+cmu
113   (let ((size (gensym))
114         (storage (gensym))
115         (i (gensym)))
116     `(etypecase ,obj
117       (null 
118        (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
119       (string
120        (let* ((,size (length ,obj))
121               (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
122          (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
123          (locally
124              (declare (optimize (speed 3) (safety 0)))
125            (dotimes (,i ,size)
126              (declare (fixnum ,i))
127              (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
128            (setf (alien:deref ,storage ,size) 0))
129          ,storage))))
130   #+mcl
131   `(if (null ,obj)
132        +null-cstring-pointer+
133      (let ((ptr (new-ptr (1+ (length ,obj)))))
134        (ccl:%put-cstring ptr ,obj)
135        ptr))
136   )
137
138
139 ;; Either length or null-terminated-p must be non-nil
140 (defmacro convert-from-foreign-string (obj &key
141                                            length
142                                            (null-terminated-p t))
143   #+allegro
144   `(if (zerop ,obj)
145        nil
146      (values (excl:native-to-string
147               ,obj 
148               ,@(if length (list :length length) (values))
149               :truncate (not ,null-terminated-p))))
150   #+lispworks
151   `(if (fli:null-pointer-p ,obj)
152        nil
153      (fli:convert-from-foreign-string 
154       ,obj
155       ,@(if length (list :length length) (values))
156       :null-terminated-p ,null-terminated-p
157       :external-format '(:latin-1 :eol-style :lf)))      
158   #+cmu
159   `(if (null-pointer-p ,obj)
160     nil
161     (cmucl-naturalize-cstring (alien:alien-sap ,obj)
162      :length ,length
163      :null-terminated-p ,null-terminated-p))
164   #+mcl
165   (declare (ignore null-terminated-p))
166   #+mcl
167   `(if (ccl:%null-ptr-p ,obj)
168      nil
169      (ccl:%get-cstring ,obj 0 ,@(if length (list length) nil)))
170   )
171
172
173
174 (defmacro allocate-foreign-string (size &key (unsigned t))
175   #+cmu
176   (let ((array-def (gensym)))
177     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
178        (eval `(alien:cast (alien:make-alien ,,array-def) 
179                           ,(if ,unsigned 
180                                '(* (alien:unsigned 8))
181                              '(* (alien:signed 8)))))))
182   #+lispworks
183   `(fli:allocate-foreign-object :type 
184                                 ,(if unsigned 
185                                      ''(:unsigned :char) 
186                                    :char)
187                                 :nelems ,size)
188   #+allegro
189   (declare (ignore unsigned))
190   #+allegro
191   `(ff:allocate-fobject :char :c ,size)
192   #+mcl
193   (declare (ignore unsigned))
194   #+mcl
195   `(new-ptr ,size)
196   )
197
198 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
199   (let ((result (gensym)))
200     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
201             (,result (progn ,@body)))
202       (declare (dynamic-extent ,foreign-string))
203       (free-foreign-object ,foreign-string)
204       ,result)))
205
206
207 ;; Modified from CMUCL's source to handle non-null terminated strings
208 #+cmu
209 (defun cmucl-naturalize-cstring (sap &key 
210                                            length
211                                            (null-terminated-p t))
212   (declare (type system:system-area-pointer sap))
213   (locally
214       (declare (optimize (speed 3) (safety 0)))
215     (let ((null-terminated-length
216            (when null-terminated-p
217              (loop
218                  for offset of-type fixnum upfrom 0
219                  until (zerop (system:sap-ref-8 sap offset))
220                  finally (return offset)))))
221       (if length
222           (if (and null-terminated-length
223                    (> (the fixnum length) (the fixnum null-terminated-length)))
224               (setq length null-terminated-length))
225         (setq length null-terminated-length)))
226     (let ((result (make-string length)))
227       (kernel:copy-from-system-area sap 0
228                                     result (* vm:vector-data-offset
229                                               vm:word-bits)
230                                     (* length vm:byte-bits))
231       result)))