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