X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=src%2Fstrings.lisp;h=250df2e67734ba18bcaa27b274e606977ea411c4;hb=977d59fade4c0005596c6a8ec40ce42a59770751;hp=63847cc3885fa0bcd61c6157a1c946ed604d2e58;hpb=7653a03580807774202e699a091a2e8520db5cf8;p=uffi.git diff --git a/src/strings.lisp b/src/strings.lisp index 63847cc..250df2e 100644 --- a/src/strings.lisp +++ b/src/strings.lisp @@ -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.3 2002/10/14 03:07:41 kevin Exp $ +;;;; $Id$ ;;;; ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; @@ -16,25 +16,20 @@ ;;;; (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+ - #+cmu nil - #+sbcl nil + #+(or cmu sbcl scl) nil #+allegro 0 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char)) #+mcl (ccl:%null-ptr) - #-(or cmu allegro lispworks mcl) nil ) (defmacro convert-from-cstring (obj) "Converts a string from a c-call. Same as convert-from-foreign-string, except that LW/CMU automatically converts strings from c-calls." - #+cmu obj - #+sbcl obj - #+lispworks obj + #+(or cmu sbcl lispworks scl) obj #+allegro (let ((stored (gensym))) `(let ((,stored ,obj)) @@ -50,9 +45,7 @@ that LW/CMU automatically converts strings from c-calls." ) (defmacro convert-to-cstring (obj) - #+cmu obj - #+sbcl obj - #+lispworks obj + #+(or cmu sbcl scl lispworks) obj #+allegro `(if (null ,obj) 0 @@ -61,14 +54,14 @@ that LW/CMU automatically converts strings from c-calls." `(if (null ,obj) +null-cstring-pointer+ (let ((ptr (new-ptr (1+ (length ,obj))))) - (ccl:%put-cstring ptr ,obj) + (ccl::%put-cstring ptr ,obj) ptr)) ) (defmacro free-cstring (obj) - #+(or cmu sbcl lispworks) (declare (ignore obj)) + #+(or cmu sbcl scl lispworks) (declare (ignore obj)) #+allegro - `(unless (zerop obj) + `(unless (zerop ,obj) (ff:free-fobject ,obj)) #+mcl `(unless (ccl:%null-ptr-p ,obj) @@ -76,7 +69,7 @@ that LW/CMU automatically converts strings from c-calls." ) (defmacro with-cstring ((cstring lisp-string) &body body) - #+(or cmu sbcl lispworks) + #+(or cmu sbcl scl lispworks) `(let ((,cstring ,lisp-string)) ,@body) #+allegro (let ((acl-native (gensym))) @@ -103,13 +96,13 @@ that LW/CMU automatically converts strings from c-calls." (defmacro convert-to-foreign-string (obj) #+lispworks `(if (null ,obj) - +null-cstring-pointer+ - (fli:convert-to-foreign-string ,obj)) + +null-cstring-pointer+ + (fli:convert-to-foreign-string ,obj :external-format '(:latin-1 :eol-style :lf))) #+allegro `(if (null ,obj) 0 (values (excl:string-to-native ,obj))) - #+cmu + #+(or cmu scl) (let ((size (gensym)) (storage (gensym)) (i (gensym))) @@ -149,7 +142,7 @@ that LW/CMU automatically converts strings from c-calls." `(if (null ,obj) +null-cstring-pointer+ (let ((ptr (new-ptr (1+ (length ,obj))))) - (ccl:%put-cstring ptr ,obj) + (ccl::%put-cstring ptr ,obj) ptr)) ) @@ -157,23 +150,28 @@ 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))) - #+cmu + (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 (cmucl-naturalize-cstring (alien:alien-sap ,obj) @@ -194,9 +192,8 @@ that LW/CMU automatically converts strings from c-calls." ) - (defmacro allocate-foreign-string (size &key (unsigned t)) - #+cmu + #+(or cmu scl) (let ((array-def (gensym))) `(let ((,array-def (list 'alien:array 'c-call:char ,size))) (eval `(alien:cast (alien:make-alien ,,array-def) @@ -259,6 +256,30 @@ that LW/CMU automatically converts strings from c-calls." (* length vm:byte-bits)) result))) +#+scl +;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL, +;; so have to iteratively copy from sap +(defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t)) + (declare (type system:system-area-pointer sap)) + (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))))) + (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))) + (let ((result (make-string length))) + (dotimes (i length) + (declare (type fixnum i)) + (setf (char result i) (code-char (system:sap-ref-8 sap i)))) + result))) + #+sbcl (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t)) (declare (type sb-sys:system-area-pointer sap)) @@ -281,3 +302,54 @@ 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) + +(def-type char-ptr-def (* :unsigned-char)) + +#+(or lispworks (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)) + (str (make-string len))) + (declare (fixnum len) + (type (simple-array (signed-byte 8) (*)) str)) + (dotimes (i len str) + (setf (aref str i) (uffi:deref-array s '(:array :char) i))))) + +#+(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)) + (dotimes (i len str) + (setf (schar str i) (ensure-char-character + (uffi:deref-array s '(:array :char) i)))))) + +#+ignore +(defun fast-native-to-string (s) + (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0) (debug 0)) + (type char-ptr-def s)) + (let* ((len (strlen s)) + (len4 (the fixnum (ash (the fixnum len) -2))) + (end4 (the fixnum (ash (the fixnum len4) 2))) + (remainder (the fixnum (- len end4))) + (str (make-string len)) + (str4 str)) + (declare (type fixnum len len4 end4 remainder) + (type (simple-array (signed-byte 32) (*)) str4) + (type (simple-array (signed-byte 8) (*)) str)) + (dotimes (i len4) + (declare (fixnum i)) + (setf (aref str4 i) (uffi:deref-array s '(:array :int) i))) + (dotimes (i remainder) + (declare (fixnum i)) + (setf (aref str end4) (uffi:deref-array s '(:array :char) end4)) + (incf end4)) + str))