1 ;;;; -*- 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.29 2003/06/06 21:59:09 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 (in-package #:clsql-uffi)
22 (defun canonicalize-type-list (types auto-list)
23 "Ensure a field type list meets expectations"
24 (let ((length-types (length types))
26 (loop for i from 0 below (length auto-list)
28 (if (>= i length-types)
29 (push t new-types) ;; types is shorted than num-fields
33 (case (nth i auto-list)
41 (case (nth i auto-list)
47 (if (eq :int32 (nth i auto-list))
51 (if (eq :int64 (nth i auto-list))
57 (nreverse new-types)))
59 (uffi:def-function "atoi"
60 ((str (* :unsigned-char)))
63 (uffi:def-function "atol"
64 ((str (* :unsigned-char)))
67 (uffi:def-function "atof"
68 ((str (* :unsigned-char)))
71 (uffi:def-function "atol64"
72 ((str (* :unsigned-char))
74 :returning :unsigned-int)
76 (uffi:def-constant +2^32+ 4294967296)
77 (uffi:def-constant +2^32-1+ (1- +2^32+))
79 (defmacro make-64-bit-integer (high32 low32)
80 `(+ ,low32 (* ,high32 +2^32+)))
82 (defmacro split-64-bit-integer (int64)
83 `(values (ash ,int64 -32) (logand ,int64 +2^32-1+)))
85 (uffi:def-type char-ptr-def (* :unsigned-char))
87 (defun char-ptr-points-to-null (char-ptr)
88 "Returns T if foreign character pointer refers to 'NULL' string. Only called for numeric entries"
89 ;; Uses short cut and returns T if first character is #\N. It should
90 ;; never be non-numeric
91 (declare (type char-ptr-def char-ptr))
92 (char-equal #\N (uffi:ensure-char-character
93 (uffi:deref-pointer char-ptr :char))))
95 (defun convert-raw-field (char-ptr types index)
96 (let ((type (if (listp types)
100 ((and (or (eq type :double) (eq type :int32) (eq type :int)
102 (char-ptr-points-to-null char-ptr))
111 (uffi:with-foreign-object (high32-ptr :int)
112 (let ((low32 (atol64 char-ptr high32-ptr))
113 (high32 (uffi:deref-pointer high32-ptr :int)))
116 (make-64-bit-integer high32 low32)))))
118 (uffi:convert-from-foreign-string char-ptr :locale :none)))))))