r1555: *** empty log message ***
[uffi.git] / src / strings.cl
1 ;;;; -*- Mode: 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.5 2002/03/14 21:03:12 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 :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:make-pointer
51       :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
52       :type :char))
53   #+allegro
54   `(if (null ,obj)
55        0
56      (values (excl:string-to-native ,obj)))
57   #+cmu
58   (declare (ignore obj))
59   )
60
61 (defmacro free-cstring (obj)
62   #+lispworks
63   `(unless (fli:null-pointer-p ,obj)
64      (fli:free-foreign-object ,obj))
65   #+allegro
66   `(unless (zerop obj)
67      (ff:free-fobject ,obj))
68   #+cmu
69   (declare (ignore obj))
70   )
71
72 ;; Either length or null-terminated-p must be non-nil
73 (defmacro convert-from-foreign-string (obj &key
74                                            length
75                                            (null-terminated-p t))
76   #+allegro
77   `(if (zerop ,obj)
78        nil
79      (values (excl:native-to-string
80               ,obj 
81               ,@(if length (list :length length) (values))
82               :truncate (not ,null-terminated-p))))
83   #+lispworks
84   `(if (fli:null-pointer-p ,obj)
85        nil
86      (fli:convert-from-foreign-string 
87       ,obj
88       ,@(if length (list :length length) (values))
89       :null-terminated-p ,null-terminated-p
90       :external-format '(:latin-1 :eol-style :lf)))      
91   #+cmu
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:make-pointer
102       :address (fli:pointer-address (fli:convert-to-foreign-string ,obj))
103       :type :char))
104   #+allegro
105   `(if (null ,obj)
106        0
107      (values (excl:string-to-native ,obj)))
108   #+cmu
109   (let ((size (gensym))
110         (storage (gensym))
111         (i (gensym)))
112     `(when (stringp ,obj)
113        (let* ((,size (length ,obj))
114               (,storage (alien:make-alien char (1+ ,size))))
115          (setq ,storage (alien:cast ,storage (* char)))
116          (dotimes (,i ,size)
117            (declare (fixnum ,i)
118                     (optimize (speed 3) (safety 0)))
119            (setf (alien:deref ,storage ,i) (char-code (char ,obj ,i))))
120          (setf (alien:deref ,storage ,size) 0)
121          ,storage)))
122   )
123
124
125 (defmacro allocate-foreign-string (size &key (unsigned nil))
126   #+cmu
127   (let ((array-def (gensym)))
128     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
129        (eval `(alien:cast (alien:make-alien ,,array-def) 
130                           ,(if ,unsigned 
131                                '(* (alien:unsigned 8))
132                              '(* (alien:signed 8)))))))
133   #+lispworks
134   `(fli:allocate-foreign-object :type 
135                                 ,(if unsigned 
136                                      ''(:unsigned :char) 
137                                    :char)
138                                 :nelems ,size)
139   #+allegro
140   (declare (ignore unsigned))
141   #+allegro
142   `(ff:allocate-fobject :char :c ,size)
143   )
144
145 (defmacro with-cstring ((foreign-string lisp-string) &body body)
146   #+cmu
147   `(let ((,foreign-string ,lisp-string)) ,@body) 
148   #+allegro
149   (let ((acl-native (gensym)))
150     `(excl:with-native-string (,acl-native ,lisp-string)
151        (let ((,foreign-string (if ,lisp-string ,acl-native 0)))
152          ,@body)))
153   #+lispworks
154   (let ((result (gensym)))
155     `(let* ((,foreign-string (convert-to-cstring ,lisp-string))
156             (,result ,@body))
157        (fli:free-foreign-object ,foreign-string)
158        ,result))
159   )
160
161 ;; Modified from CMUCL's source to handle non-null terminated strings
162 #+cmu
163 (defun cmucl-naturalize-cstring (sap &key 
164                                            length
165                                            (null-terminated-p t))
166   (declare (type system:system-area-pointer sap))
167   (locally
168       (declare (optimize (speed 3) (safety 0)))
169     (let ((null-terminated-length
170            (when null-terminated-p
171              (loop
172                  for offset of-type fixnum upfrom 0
173                  until (zerop (system:sap-ref-8 sap offset))
174                  finally (return offset)))))
175       (if length
176           (if (and null-terminated-length
177                    (> (the fixnum length) (the fixnum null-terminated-length)))
178               (setq length null-terminated-length))
179         (setq length null-terminated-length)))
180     (let ((result (make-string length)))
181       (kernel:copy-from-system-area sap 0
182                                     result (* vm:vector-data-offset
183                                               vm:word-bits)
184                                     (* length vm:byte-bits))
185       result)))