Remove old CVS $Id$ keyword
[uffi.git] / src / strings.lisp
index ed3f7e820de6cceb90dc02834edb648189a34dbe..3905004cf02909af262f754d5fda90fc0575481f 100644 (file)
@@ -7,9 +7,7 @@
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id$
-;;;;
-;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
+;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
 ;;;; *************************************************************************
 
 (in-package #:uffi)
@@ -30,14 +28,14 @@ that LW/CMU automatically converts strings from c-calls."
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (if (zerop ,stored)
-          nil
-          (values (excl:native-to-string ,stored)))))
+           nil
+           (values (excl:native-to-string ,stored)))))
   #+(or openmcl digitool)
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (if (ccl:%null-ptr-p ,stored)
-          nil
-        (values (ccl:%get-cstring ,stored)))))
+           nil
+         (values (ccl:%get-cstring ,stored)))))
   )
 
 (defmacro convert-to-cstring (obj)
@@ -46,16 +44,16 @@ that LW/CMU automatically converts strings from c-calls."
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (if (null ,stored)
-          0
-          (values (excl:string-to-native ,stored)))))
+           0
+           (values (excl:string-to-native ,stored)))))
   #+(or openmcl digitool)
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (if (null ,stored)
-          +null-cstring-pointer+
-          (let ((ptr (new-ptr (1+ (length ,stored)))))
-            (ccl::%put-cstring ptr ,stored)
-            ptr))))
+           +null-cstring-pointer+
+           (let ((ptr (new-ptr (1+ (length ,stored)))))
+             (ccl::%put-cstring ptr ,stored)
+             ptr))))
   )
 
 (defmacro free-cstring (obj)
@@ -64,12 +62,12 @@ that LW/CMU automatically converts strings from c-calls."
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (unless (zerop ,stored)
-        (ff:free-fobject ,stored))))
+         (ff:free-fobject ,stored))))
   #+(or openmcl digitool)
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (unless (ccl:%null-ptr-p ,stored)
-        (dispose-ptr ,stored))))
+         (dispose-ptr ,stored))))
   )
 
 (defmacro with-cstring ((cstring lisp-string) &body body)
@@ -77,26 +75,26 @@ that LW/CMU automatically converts strings from c-calls."
   `(let ((,cstring ,lisp-string)) ,@body)
   #+allegro
   (let ((acl-native (gensym))
-       (stored-lisp-string (gensym)))
+        (stored-lisp-string (gensym)))
     `(let ((,stored-lisp-string ,lisp-string))
        (excl:with-native-string (,acl-native ,stored-lisp-string)
-        (let ((,cstring (if ,stored-lisp-string ,acl-native 0)))
-          ,@body))))
+         (let ((,cstring (if ,stored-lisp-string ,acl-native 0)))
+           ,@body))))
   #+(or openmcl digitool)
   (let ((stored-lisp-string (gensym)))
     `(let ((,stored-lisp-string ,lisp-string))
        (if (stringp ,stored-lisp-string)
-          (ccl:with-cstrs ((,cstring ,stored-lisp-string))
-            ,@body)
-          (let ((,cstring +null-cstring-pointer+))
-            ,@body))))
+           (ccl:with-cstrs ((,cstring ,stored-lisp-string))
+             ,@body)
+           (let ((,cstring +null-cstring-pointer+))
+             ,@body))))
   )
 
 (defmacro with-cstrings (bindings &rest body)
   (if bindings
       `(with-cstring ,(car bindings)
-       (with-cstrings ,(cdr bindings)
-         ,@body))
+        (with-cstrings ,(cdr bindings)
+          ,@body))
       `(progn ,@body)))
 
 ;;; Foreign string functions
@@ -106,127 +104,127 @@ that LW/CMU automatically converts strings from c-calls."
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (if (null ,stored)
-          +null-cstring-pointer+
-          (fli:convert-to-foreign-string
-           ,stored
-           :external-format '(:latin-1 :eol-style :lf)))))
+           +null-cstring-pointer+
+           (fli:convert-to-foreign-string
+            ,stored
+            :external-format '(:latin-1 :eol-style :lf)))))
   #+allegro
   (let ((stored (gensym)))
     `(let ((,stored ,obj))
        (if (null ,stored)
-          0
-          (values (excl:string-to-native ,stored)))))
+           0
+           (values (excl:string-to-native ,stored)))))
   #+(or cmu scl)
   (let ((size (gensym))
-       (storage (gensym))
-       (stored-obj (gensym))
-       (i (gensym)))
+        (storage (gensym))
+        (stored-obj (gensym))
+        (i (gensym)))
     `(let ((,stored-obj ,obj))
        (etypecase ,stored-obj
-        (null
-         (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
-        (string
-         (let* ((,size (length ,stored-obj))
-                (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
-           (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
-           (locally
-               (declare (optimize (speed 3) (safety 0)))
-             (dotimes (,i ,size)
-               (declare (fixnum ,i))
-               (setf (alien:deref ,storage ,i)
-                     (char-code (char ,stored-obj ,i))))
-          (setf (alien:deref ,storage ,size) 0))
-        ,storage)))))
+         (null
+          (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
+         (string
+          (let* ((,size (length ,stored-obj))
+                 (,storage (alien:make-alien (alien:unsigned 8) (1+ ,size))))
+            (setq ,storage (alien:cast ,storage (* (alien:unsigned 8))))
+            (locally
+                (declare (optimize (speed 3) (safety 0)))
+              (dotimes (,i ,size)
+                (declare (fixnum ,i))
+                (setf (alien:deref ,storage ,i)
+                      (char-code (char ,stored-obj ,i))))
+           (setf (alien:deref ,storage ,size) 0))
+         ,storage)))))
   #+sbcl
   (let ((size (gensym))
-       (storage (gensym))
-       (stored-obj (gensym))
-       (i (gensym)))
+        (storage (gensym))
+        (stored-obj (gensym))
+        (i (gensym)))
     `(let ((,stored-obj ,obj))
        (etypecase ,stored-obj
-        (null
-         (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
-        (string
-         (let* ((,size (length ,stored-obj))
-                (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
-           (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
-           (locally
-               (declare (optimize (speed 3) (safety 0)))
-             (dotimes (,i ,size)
-               (declare (fixnum ,i))
-               (setf (sb-alien:deref ,storage ,i)
-                     (char-code (char ,stored-obj ,i))))
-             (setf (sb-alien:deref ,storage ,size) 0))
-           ,storage)))))
+         (null
+          (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
+         (string
+          (let* ((,size (length ,stored-obj))
+                 (,storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ ,size))))
+            (setq ,storage (sb-alien:cast ,storage (* (sb-alien:unsigned 8))))
+            (locally
+                (declare (optimize (speed 3) (safety 0)))
+              (dotimes (,i ,size)
+                (declare (fixnum ,i))
+                (setf (sb-alien:deref ,storage ,i)
+                      (char-code (char ,stored-obj ,i))))
+              (setf (sb-alien:deref ,storage ,size) 0))
+            ,storage)))))
   #+(or openmcl digitool)
   (let ((stored-obj (gensym)))
     `(let ((,stored-obj ,obj))
        (if (null ,stored-obj)
-          +null-cstring-pointer+
-          (let ((ptr (new-ptr (1+ (length ,stored-obj)))))
-            (ccl::%put-cstring ptr ,stored-obj)
-            ptr))))
+           +null-cstring-pointer+
+           (let ((ptr (new-ptr (1+ (length ,stored-obj)))))
+             (ccl::%put-cstring ptr ,stored-obj)
+             ptr))))
   )
 
 ;; Either length or null-terminated-p must be non-nil
 (defmacro convert-from-foreign-string (obj &key
-                                          length
-                                          (locale :default)
-                                          (null-terminated-p t))
+                                           length
+                                           (locale :default)
+                                           (null-terminated-p t))
   #+allegro
   (let ((stored-obj (gensym)))
     `(let ((,stored-obj ,obj))
        (if (zerop ,stored-obj)
-          nil
-          (if (eq ,locale :none)
-              (fast-native-to-string ,stored-obj ,length)
-              (values
-               (excl:native-to-string
-                ,stored-obj
-                ,@(when length (list :length length))
-                :truncate (not ,null-terminated-p)))))))
+           nil
+           (if (eq ,locale :none)
+               (fast-native-to-string ,stored-obj ,length)
+               (values
+                (excl:native-to-string
+                 ,stored-obj
+                 ,@(when length (list :length length))
+                 :truncate (not ,null-terminated-p)))))))
   #+lispworks
   (let ((stored-obj (gensym)))
     `(let ((,stored-obj ,obj))
        (if (fli:null-pointer-p ,stored-obj)
-          nil
-          (if (eq ,locale :none)
-              (fast-native-to-string ,stored-obj ,length)
-              (fli:convert-from-foreign-string
-               ,stored-obj
-               ,@(when length (list :length length))
-               :null-terminated-p ,null-terminated-p
-               :external-format '(:latin-1 :eol-style :lf))))))
+           nil
+           (if (eq ,locale :none)
+               (fast-native-to-string ,stored-obj ,length)
+               (fli:convert-from-foreign-string
+                ,stored-obj
+                ,@(when length (list :length length))
+                :null-terminated-p ,null-terminated-p
+                :external-format '(:latin-1 :eol-style :lf))))))
   #+(or cmu scl)
   (let ((stored-obj (gensym)))
     `(let ((,stored-obj ,obj))
        (if (null-pointer-p ,stored-obj)
-          nil
-          (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
-                                    :length ,length
-                                    :null-terminated-p ,null-terminated-p))))
+           nil
+           (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
+                                     :length ,length
+                                     :null-terminated-p ,null-terminated-p))))
 
   #+sbcl
   (let ((stored-obj (gensym)))
     `(let ((,stored-obj ,obj))
        (if (null-pointer-p ,stored-obj)
-           nil
-           (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
-                                    :length ,length
-                                    :null-terminated-p ,null-terminated-p))))
+            nil
+            (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
+                                     :length ,length
+                                     :null-terminated-p ,null-terminated-p))))
   #+(or openmcl digitool)
   (declare (ignore null-terminated-p))
   #+(or openmcl digitool)
   (let ((stored-obj (gensym)))
     `(let ((,stored-obj ,obj))
        (if (ccl:%null-ptr-p ,stored-obj)
-          nil
-          #+digitool (ccl:%get-cstring
-                                     ,stored-obj 0
-                                     ,@(if length (list length) nil))
-          #+openmcl ,@(if length
-                          `((ccl:%str-from-ptr ,stored-obj ,length))
-                          `((ccl:%get-cstring ,stored-obj))))))
+           nil
+           #+digitool (ccl:%get-cstring
+                                      ,stored-obj 0
+                                      ,@(if length (list length) nil))
+           #+openmcl ,@(if length
+                           `((ccl:%str-from-ptr ,stored-obj ,length))
+                           `((ccl:%get-cstring ,stored-obj))))))
   )
 
 
@@ -235,28 +233,28 @@ that LW/CMU automatically converts strings from c-calls."
   (let ((array-def (gensym)))
     `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
        (eval `(alien:cast (alien:make-alien ,,array-def)
-                         ,(if ,unsigned
-                              '(* (alien:unsigned 8))
-                            '(* (alien:signed 8)))))))
+                          ,(if ,unsigned
+                               '(* (alien:unsigned 8))
+                             '(* (alien:signed 8)))))))
 
   #+(or cmu scl)
   `(alien:make-alien ,(if unsigned
-                            '(alien:unsigned 8)
-                            '(alien:signed 8))
+                             '(alien:unsigned 8)
+                             '(alien:signed 8))
     ,size)
 
   #+sbcl
   `(sb-alien:make-alien ,(if unsigned
-                            '(sb-alien:unsigned 8)
-                            '(sb-alien:signed 8))
+                             '(sb-alien:unsigned 8)
+                             '(sb-alien:signed 8))
     ,size)
 
   #+lispworks
   `(fli:allocate-foreign-object :type
-                               ,(if unsigned
-                                    ''(:unsigned :char)
-                                  :char)
-                               :nelems ,size)
+                                ,(if unsigned
+                                     ''(:unsigned :char)
+                                   :char)
+                                :nelems ,size)
   #+allegro
   (declare (ignore unsigned))
   #+allegro
@@ -279,7 +277,7 @@ that LW/CMU automatically converts strings from c-calls."
 (defmacro with-foreign-string ((foreign-string lisp-string) &body body)
   (let ((result (gensym)))
     `(let* ((,foreign-string (convert-to-foreign-string ,lisp-string))
-           (,result (progn ,@body)))
+            (,result (progn ,@body)))
       (declare (dynamic-extent ,foreign-string))
       (free-foreign-object ,foreign-string)
       ,result)))
@@ -297,21 +295,21 @@ that LW/CMU automatically converts strings from c-calls."
   (locally
       (declare (optimize (speed 3) (safety 0)))
     (let ((null-terminated-length
-          (when null-terminated-p
-            (loop
-                for offset of-type fixnum upfrom 0
-                until (zerop (system:sap-ref-8 sap offset))
-                finally (return offset)))))
+           (when null-terminated-p
+             (loop
+                 for offset of-type fixnum upfrom 0
+                 until (zerop (system:sap-ref-8 sap offset))
+                 finally (return offset)))))
       (if length
-         (if (and null-terminated-length
-                  (> (the fixnum length) (the fixnum null-terminated-length)))
-             (setq length null-terminated-length))
-       (setq length null-terminated-length)))
+          (if (and null-terminated-length
+                   (> (the fixnum length) (the fixnum null-terminated-length)))
+              (setq length null-terminated-length))
+        (setq length null-terminated-length)))
     (let ((result (make-string length)))
       (kernel:copy-from-system-area sap 0
-                                   result (* vm:vector-data-offset
-                                             vm:word-bits)
-                                   (* length vm:byte-bits))
+                                    result (* vm:vector-data-offset
+                                              vm:word-bits)
+                                    (* length vm:byte-bits))
       result)))
 
 #+scl
@@ -322,63 +320,63 @@ that LW/CMU automatically converts strings from c-calls."
   (locally
       (declare (optimize (speed 3) (safety 0)))
     (let ((null-terminated-length
-          (when null-terminated-p
-            (loop
-                for offset of-type fixnum upfrom 0
-                until (zerop (system:sap-ref-8 sap offset))
-                finally (return offset)))))
+           (when null-terminated-p
+             (loop
+                 for offset of-type fixnum upfrom 0
+                 until (zerop (system:sap-ref-8 sap offset))
+                 finally (return offset)))))
       (if length
-         (if (and null-terminated-length
-                  (> (the fixnum length) (the fixnum null-terminated-length)))
-             (setq length null-terminated-length))
-       (setq length null-terminated-length)))
+          (if (and null-terminated-length
+                   (> (the fixnum length) (the fixnum null-terminated-length)))
+              (setq length null-terminated-length))
+        (setq length null-terminated-length)))
     (let ((result (make-string length)))
       (dotimes (i length)
-       (declare (type fixnum i))
-       (setf (char result i) (code-char (system:sap-ref-8 sap i))))
+        (declare (type fixnum i))
+        (setf (char result i) (code-char (system:sap-ref-8 sap i))))
       result)))
 
 #+(and sbcl (not sb-unicode))
 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
   (declare (type sb-sys:system-area-pointer sap)
-          (type (or null fixnum) length))
+           (type (or null fixnum) length))
   (locally
    (declare (optimize (speed 3) (safety 0)))
    (let ((null-terminated-length
-         (when null-terminated-p
-           (loop
-            for offset of-type fixnum upfrom 0
-            until (zerop (sb-sys:sap-ref-8 sap offset))
-            finally (return offset)))))
+          (when null-terminated-p
+            (loop
+             for offset of-type fixnum upfrom 0
+             until (zerop (sb-sys:sap-ref-8 sap offset))
+             finally (return offset)))))
      (if length
-        (if (and null-terminated-length
-                 (> (the fixnum length) (the fixnum null-terminated-length)))
-            (setq length null-terminated-length))
+         (if (and null-terminated-length
+                  (> (the fixnum length) (the fixnum null-terminated-length)))
+             (setq length null-terminated-length))
        (setq length null-terminated-length)))
    (let ((result (make-string length)))
        (funcall *system-copy-fn* sap 0 result +system-copy-offset+
-               (* length +system-copy-multiplier+))
+                (* length +system-copy-multiplier+))
        result)))
 
 #+(and sbcl sb-unicode)
 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
   (declare (type sb-sys:system-area-pointer sap)
-          (type (or null fixnum) length))
+           (type (or null fixnum) length))
   (locally
    (declare (optimize (speed 3) (safety 0)))
    (cond
     (null-terminated-p
      (let ((casted (sb-alien:cast (sb-alien:sap-alien sap (* char))
-                                 #+sb-unicode sb-alien:utf8-string
-                                 #-sb-unicode sb-alien:c-string)))
+                                  #+sb-unicode sb-alien:utf8-string
+                                  #-sb-unicode sb-alien:c-string)))
        (if length
-          (copy-seq (subseq casted 0 length))
-        (copy-seq casted))))
+           (copy-seq (subseq casted 0 length))
+         (copy-seq casted))))
     (t
      (let ((result (make-string length)))
        ;; this will not work in sb-unicode
        (funcall *system-copy-fn* sap 0 result +system-copy-offset+
-               (* length +system-copy-multiplier+))
+                (* length +system-copy-multiplier+))
        result)))))
 
 
@@ -389,23 +387,23 @@ that LW/CMU automatically converts strings from c-calls."
 
 (def-type char-ptr-def (* :unsigned-char))
 
-#+(or lispworks (and allegro (not ics)))
+#+(or (and allegro (not ics)) (and lispworks (not lispworks5) (not lispworks6)))
 (defun fast-native-to-string (s len)
   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
-          (type char-ptr-def s))
+           (type char-ptr-def s))
   (let* ((len (or len (strlen s)))
          (str (make-string len)))
     (declare (fixnum len)
-            (type (simple-array #+lispworks base-char
+             (type (simple-array #+lispworks base-char
                                  #-lispworks (signed-byte 8) (*)) str))
     (dotimes (i len str)
       (setf (aref str i)
-       (uffi:deref-array s '(:array :char) i)))))
+        (uffi:deref-array s '(:array :char) i)))))
 
-#+(and allegro ics)
+#+(or (and allegro ics) lispworks5 lispworks6)
 (defun fast-native-to-string (s len)
   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
-          (type char-ptr-def s))
+           (type char-ptr-def s))
   (let* ((len (or len (strlen s)))
          (str (make-string len)))
       (dotimes (i len str)