1 l;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: clsql-uffi.cl
6 ;;;; Purpose: Common functions for interfaces using UFFI
7 ;;;; Programmers: Kevin M. Rosenberg
8 ;;;; Date Started: Mar 2002
10 ;;;; $Id: clsql-uffi.lisp,v 1.25 2003/05/22 15:44:30 kevin Exp $
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; CLSQL 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 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :clsql-uffi)
23 (defun canonicalize-type-list (types auto-list)
24 "Ensure a field type list meets expectations"
25 (let ((length-types (length types))
27 (loop for i from 0 below (length auto-list)
29 (if (>= i length-types)
30 (push t new-types) ;; types is shorted than num-fields
34 (case (nth i auto-list)
42 (case (nth i auto-list)
48 (if (eq :int32 (nth i auto-list))
52 (if (eq :int64 (nth i auto-list))
58 (nreverse new-types)))
60 (uffi:def-function "atoi"
61 ((str (* :unsigned-char)))
64 (uffi:def-function "atol"
65 ((str (* :unsigned-char)))
68 (uffi:def-function "atof"
69 ((str (* :unsigned-char)))
72 (uffi:def-function "atol64"
73 ((str (* :unsigned-char))
75 :returning :unsigned-int)
77 (uffi:def-constant +2^32+ 4294967296)
78 (uffi:def-constant +2^32-1+ (1- +2^32+))
80 (defmacro make-64-bit-integer (high32 low32)
81 `(+ ,low32 (* ,high32 +2^32+)))
83 (defmacro split-64-bit-integer (int64)
84 `(values (ash ,int64 -32) (logand ,int64 +2^32-1+)))
86 (defvar +ascii-N-value+ (char-code #\N))
87 (defvar +ascii-U-value+ (char-code #\U))
89 (uffi:def-type char-ptr-def (* :unsigned-char))
91 (defun char-ptr-points-to-null (char-ptr)
92 "Returns T if foreign character pointer refers to 'NULL' string. Only called for numeric entries"
93 ;; Uses short cut and returns T if first character is #\N. It should
94 ;; never be non-numeric
95 (declare (type char-ptr-def char-ptr))
96 (let ((char (uffi:ensure-char-character
97 (uffi:deref-pointer char-ptr :char))))
100 (defun convert-raw-field (char-ptr types index)
101 (let ((type (if (listp types)
105 ((and (or (eq type :double) (eq type :int32) (eq type :int)
107 (char-ptr-points-to-null char-ptr))
116 (uffi:with-foreign-object (high32-ptr :int)
117 (let ((low32 (atol64 char-ptr high32-ptr))
118 (high32 (uffi:deref-pointer high32-ptr :int)))
121 (make-64-bit-integer high32 low32)))))
123 #+allegro (native-to-string char-ptr) ;; optimized
124 #-allegro (uffi:convert-from-foreign-string char-ptr)))))))
127 (uffi:def-function "strlen"
128 ((str (* :unsigned-char)))
129 :returning :unsigned-int)
132 (defun native-to-string (s)
133 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
134 (type char-ptr-def s))
135 (let* ((len (strlen s))
136 (str (make-string len)))
137 (declare (fixnum len)
143 (code-char (uffi:deref-array s '(:array :unsigned-char) i)))
147 #+(and allegro (not ics))
148 (defun native-to-string (s)
149 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
150 (type char-ptr-def s))
151 (let* ((len (strlen s))
153 (str (make-string len)))
154 (declare (fixnum len)
155 (type (simple-array (signed-byte 32) (*)) str))
159 (setf (aref (the (simple-array (signed-byte 32) (*)) str) i)
160 (uffi:deref-array s '(:array :int) i))
165 (setf (aref (the (simple-array (signed-byte 8) (*)) str) i)
166 (uffi:deref-array s '(:array :unsigned-char) i))