r1600: Fixed foreign-string on cmucl to handle null strings
[uffi.git] / src / strings.cl
1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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.cl,v 1.10 2002/03/21 05:57:03 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 (def-constant +null-cstring-pointer+
24     #+cmu nil
25     #+allegro 0
26     #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char)))
27
28 (defmacro convert-from-cstring (obj)
29   "Converts a string from a c-call. Same as convert-from-foreign-string, except
30 that CMU automatically converts strings from c-calls."
31   #+cmu obj
32   #+lispworks 
33   (let ((stored (gensym)))
34     `(let ((,stored ,obj))
35        (if (fli:null-pointer-p ,stored)
36            nil
37          (fli:convert-from-foreign-string ,stored))))
38   #+allegro 
39   (let ((stored (gensym)))
40     `(let ((,stored ,obj))
41        (if (zerop ,stored)
42            nil
43          (values (excl:native-to-string ,stored)))))
44   )
45
46 (defmacro convert-to-cstring (obj)
47   #+lispworks
48   `(if (null ,obj)
49     +null-cstring-pointer+
50     (fli:convert-to-foreign-string ,obj))
51   #+allegro
52   `(if (null ,obj)
53     0
54     (values (excl:string-to-native ,obj)))
55   #+cmu
56   (declare (ignore obj))
57   )
58
59 (defmacro free-cstring (obj)
60   #+lispworks
61   `(unless (fli:null-pointer-p ,obj)
62      (fli:free-foreign-object ,obj))
63   #+allegro
64   `(unless (zerop obj)
65      (ff:free-fobject ,obj))
66   #+cmu
67   (declare (ignore obj))
68   )
69
70 ;; Either length or null-terminated-p must be non-nil
71 (defmacro convert-from-foreign-string (obj &key
72                                            length
73                                            (null-terminated-p t))
74   #+allegro
75   `(if (zerop ,obj)
76        nil
77      (values (excl:native-to-string
78               ,obj 
79               ,@(if length (list :length length) (values))
80               :truncate (not ,null-terminated-p))))
81   #+lispworks
82   `(if (fli:null-pointer-p ,obj)
83        nil
84      (fli:convert-from-foreign-string 
85       ,obj
86       ,@(if length (list :length length) (values))
87       :null-terminated-p ,null-terminated-p
88       :external-format '(:latin-1 :eol-style :lf)))      
89   #+cmu
90   `(if (null-pointer-p ,obj)
91     nil
92     (cmucl-naturalize-cstring (alien:alien-sap ,obj)
93      :length ,length
94      :null-terminated-p ,null-terminated-p))
95   )
96
97 (defmacro convert-to-foreign-string (obj)
98   #+lispworks
99   `(if (null ,obj)
100        +null-cstring-pointer+
101     (fli:convert-to-foreign-string ,obj))
102   #+allegro
103   `(if (null ,obj)
104        0
105      (values (excl:string-to-native ,obj)))
106   #+cmu
107   (let ((size (gensym))
108         (storage (gensym))
109         (i (gensym)))
110     `(etypecase ,obj
111       (null 
112        (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
113       (string
114        (let* ((,size (length ,obj))
115               (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
116          (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
117          (locally
118              (declare (optimize (speed 3) (safety 0)))
119            (dotimes (,i ,size)
120              (declare (fixnum ,i))
121              (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
122            (setf (alien:deref ,storage ,size) 0))
123          ,storage))))
124       )
125
126
127 (defmacro allocate-foreign-string (size &key (unsigned t))
128   #+cmu
129   (let ((array-def (gensym)))
130     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
131        (eval `(alien:cast (alien:make-alien ,,array-def) 
132                           ,(if ,unsigned 
133                                '(* (alien:unsigned 8))
134                              '(* (alien:signed 8)))))))
135   #+lispworks
136   `(fli:allocate-foreign-object :type 
137                                 ,(if unsigned 
138                                      ''(:unsigned :char) 
139                                    :char)
140                                 :nelems ,size)
141   #+allegro
142   (declare (ignore unsigned))
143   #+allegro
144   `(ff:allocate-fobject :char :c ,size)
145   )
146
147 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
148   (let ((result (gensym)))
149     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
150             (,result (progn ,@body)))
151       (declare (dynamic-extent ,foreign-string))
152       (free-foreign-object ,foreign-string)
153       ,result)))
154
155 (defmacro with-cstring ((cstring lisp-string) &body body)
156   #+cmu
157   `(let ((,cstring ,lisp-string)) ,@body) 
158   #+allegro
159   (let ((acl-native (gensym)))
160     `(excl:with-native-string (,acl-native ,lisp-string)
161        (let ((,cstring (if ,lisp-string ,acl-native 0)))
162          ,@body)))
163   #+lispworks
164   (let ((result (gensym)))
165     `(let* ((,cstring (convert-to-cstring ,lisp-string))
166             (,result (progn ,@body)))
167        (fli:free-foreign-object ,cstring)
168        ,result))
169   )
170
171
172
173 ;; Modified from CMUCL's source to handle non-null terminated strings
174 #+cmu
175 (defun cmucl-naturalize-cstring (sap &key 
176                                            length
177                                            (null-terminated-p t))
178   (declare (type system:system-area-pointer sap))
179   (locally
180       (declare (optimize (speed 3) (safety 0)))
181     (let ((null-terminated-length
182            (when null-terminated-p
183              (loop
184                  for offset of-type fixnum upfrom 0
185                  until (zerop (system:sap-ref-8 sap offset))
186                  finally (return offset)))))
187       (if length
188           (if (and null-terminated-length
189                    (> (the fixnum length) (the fixnum null-terminated-length)))
190               (setq length null-terminated-length))
191         (setq length null-terminated-length)))
192     (let ((result (make-string length)))
193       (kernel:copy-from-system-area sap 0
194                                     result (* vm:vector-data-offset
195                                               vm:word-bits)
196                                     (* length vm:byte-bits))
197       result)))