a32edde37bac370c3915c467bcca5751c9107398
[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.7 2002/03/18 22:47:57 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   `(cmucl-naturalize-cstring (alien:alien-sap ,obj)
91                               :length ,length
92                               :null-terminated-p ,null-terminated-p)
93   )
94
95 (defmacro convert-to-foreign-string (obj)
96   #+lispworks
97   `(if (null ,obj)
98        +null-cstring-pointer+
99     (fli:convert-to-foreign-string ,obj))
100   #+allegro
101   `(if (null ,obj)
102        0
103      (values (excl:string-to-native ,obj)))
104   #+cmu
105   (let ((size (gensym))
106         (storage (gensym))
107         (i (gensym)))
108     `(when (stringp ,obj)
109        (let* ((,size (length ,obj))
110               (,storage (alien:make-alien char (1+ ,size))))
111          (setq ,storage (alien:cast ,storage (* char)))
112          (dotimes (,i ,size)
113            (declare (fixnum ,i)
114                     (optimize (speed 3) (safety 0)))
115            (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
116          (setf (alien:deref ,storage ,size) 0)
117          ,storage)))
118   )
119
120
121 (defmacro allocate-foreign-string (size &key (unsigned 
122                                               #+cmu nil
123                                               #+lispworks t))
124   #+cmu
125   (let ((array-def (gensym)))
126     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
127        (eval `(alien:cast (alien:make-alien ,,array-def) 
128                           ,(if ,unsigned 
129                                '(* (alien:unsigned 8))
130                              '(* (alien:signed 8)))))))
131   #+lispworks
132   `(fli:allocate-foreign-object :type 
133                                 ,(if unsigned 
134                                      ''(:unsigned :char) 
135                                    :char)
136                                 :nelems ,size)
137   #+allegro
138   (declare (ignore unsigned))
139   #+allegro
140   `(ff:allocate-fobject :char :c ,size)
141   )
142
143 (defmacro with-cstring ((foreign-string lisp-string) &body body)
144   #+cmu
145   `(let ((,foreign-string ,lisp-string)) ,@body) 
146   #+allegro
147   (let ((acl-native (gensym)))
148     `(excl:with-native-string (,acl-native ,lisp-string)
149        (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
150          ,@body)))
151   #+lispworks
152   (let ((result (gensym)))
153     `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
154             (,result ,@body))
155        (fli:free-foreign-object ,foreign-string)
156        ,result))
157   )
158
159 ;; Modified from CMUCL's source to handle non-null terminated strings
160 #+cmu
161 (defun cmucl-naturalize-cstring (sap &key 
162                                            length
163                                            (null-terminated-p t))
164   (declare (type system:system-area-pointer sap))
165   (locally
166       (declare (optimize (speed 3) (safety 0)))
167     (let ((null-terminated-length
168            (when null-terminated-p
169              (loop
170                  for offset of-type fixnum upfrom 0
171                  until (zerop (system:sap-ref-8 sap offset))
172                  finally (return offset)))))
173       (if length
174           (if (and null-terminated-length
175                    (> (the fixnum length) (the fixnum null-terminated-length)))
176               (setq length null-terminated-length))
177         (setq length null-terminated-length)))
178     (let ((result (make-string length)))
179       (kernel:copy-from-system-area sap 0
180                                     result (* vm:vector-data-offset
181                                               vm:word-bits)
182                                     (* length vm:byte-bits))
183       result)))