r5062: return from san diego
[uffi.git] / src / strings.lisp
index 28ff372810792b0ca77e0c51ef224154bb167189..b4c1917a052486b358b7f5c1c757d57e30a41ff7 100644 (file)
@@ -2,12 +2,12 @@
 ;;;; *************************************************************************
 ;;;; FILE IDENTIFICATION
 ;;;;
-;;;; Name:          strings.cl
+;;;; Name:          strings.lisp
 ;;;; Purpose:       UFFI source to handle strings, cstring and foreigns
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: strings.lisp,v 1.7 2003/03/28 19:58:18 kevin Exp $
+;;;; $Id: strings.lisp,v 1.8 2003/06/06 21:59:18 kevin Exp $
 ;;;;
 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;;
@@ -16,8 +16,7 @@
 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
 ;;;; *************************************************************************
 
-(declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
-(in-package :uffi)
+(in-package #:uffi)
 
 
 (defvar +null-cstring-pointer+
@@ -151,22 +150,27 @@ that LW/CMU automatically converts strings from c-calls."
 ;; Either length or null-terminated-p must be non-nil
 (defmacro convert-from-foreign-string (obj &key
                                           length
+                                          (locale :default)
                                           (null-terminated-p t))
   #+allegro
   `(if (zerop ,obj)
        nil
-     (values (excl:native-to-string
-             ,obj 
-             ,@(if length (list :length length) (values))
-             :truncate (not ,null-terminated-p))))
+     (if (eq ,locale :none)
+        (fast-native-to-string ,obj)
+       (excl:native-to-string
+       ,obj 
+       ,@(when length (list :length length))
+       :truncate (not ,null-terminated-p))))
   #+lispworks
   `(if (fli:null-pointer-p ,obj)
        nil
-     (fli:convert-from-foreign-string 
-      ,obj
-      ,@(if length (list :length length) (values))
-      :null-terminated-p ,null-terminated-p
-      :external-format '(:latin-1 :eol-style :lf)))      
+     (if (eq ,locale :none)
+        (fast-native-to-string ,obj)
+       (fli:convert-from-foreign-string 
+       ,obj
+       ,@(when length (list :length length))
+       :null-terminated-p ,null-terminated-p
+       :external-format '(:latin-1 :eol-style :lf))))
   #+(or cmu scl)
   `(if (null-pointer-p ,obj)
     nil
@@ -188,7 +192,6 @@ that LW/CMU automatically converts strings from c-calls."
   )
 
 
-
 (defmacro allocate-foreign-string (size &key (unsigned t))
   #+(or cmu scl)
   (let ((array-def (gensym)))
@@ -299,3 +302,47 @@ that LW/CMU automatically converts strings from c-calls."
                                              sb-vm:n-word-bits)
                                    (* length sb-vm:n-byte-bits))
       result)))
+
+
+(def-function "strlen"
+    ((str (* :unsigned-char)))
+  :returning :unsigned-int)
+
+#+(or lispworks (and allegro ics))
+(defun fast-native-to-string (s)
+  (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
+          (type char-ptr-def s))
+  (let* ((len (strlen s))
+        (str (make-string len)))
+    (declare (fixnum len)
+            (simple-string str))
+    (do ((i 0))
+       ((= i len))
+      (declare (fixnum i))
+      (setf (schar str i)
+       (code-char (uffi:deref-array s '(:array :unsigned-char) i)))
+      (incf i))
+    str))
+
+#+(and allegro (not ics))
+(defun fast-native-to-string (s)
+  (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
+          (type char-ptr-def s))
+  (let* ((len (strlen s))
+        (len4 (floor len 4))
+        (str (make-string len)))
+    (declare (fixnum len)
+            (type (simple-array (signed-byte 32) (*)) str))
+    (do ((i 0))
+       ((= i len4))
+      (declare (fixnum i))
+      (setf (aref (the (simple-array (signed-byte 32) (*)) str) i)
+       (uffi:deref-array s '(:array :int) i))
+       (incf i))
+    (do ((i (* 4 len4)))
+       ((= i len))
+      (declare (fixnum i))
+      (setf (aref (the (simple-array (signed-byte 8) (*)) str) i)
+       (uffi:deref-array s '(:array :unsigned-char) i))
+      (incf i))
+    str))