- (case type
- (:double
- (atof char-ptr))
- ((or :int32 :int)
- (atoi char-ptr))
- (:int64
- (uffi:with-foreign-object (high32-ptr :int)
- (let ((low32 (atol64 char-ptr high32-ptr))
- (high32 (uffi:deref-pointer high32-ptr :int)))
- (if (zerop high32)
- low32
- (make-64-bit-integer high32 low32)))))
- (t
- #+allegro (native-to-string char-ptr) ;; optimized
- #-allegro (uffi:convert-from-foreign-string char-ptr)))))))
-
-
-(uffi:def-function "strlen"
- ((str (* :unsigned-char)))
- :returning :unsigned-int)
-
-#+(and allegro ics)
-(defun 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 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))
+ (if encoding
+ (if length
+ (uffi:convert-from-foreign-string char-ptr
+ :null-terminated-p nil
+ :length length
+ :encoding encoding)
+ (uffi:convert-from-foreign-string char-ptr
+ :null-terminated-p t
+ :encoding encoding))
+ (if length
+ (uffi:convert-from-foreign-string char-ptr
+ :null-terminated-p nil
+ :length length)
+ (uffi:convert-from-foreign-string char-ptr
+ :null-terminated-p t))))))))