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