r10108: Automated commit for Debian build of clsql upstream-version-3.0.10
[clsql.git] / uffi / clsql-uffi.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          clsql-uffi.cl
6 ;;;; Purpose:       Common functions for interfaces using UFFI
7 ;;;; Programmers:   Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:clsql-uffi)
20
21
22 (defun canonicalize-type-list (types auto-list)
23   "Ensure a field type list meets expectations"
24   (declare (optimize (speed 3) (safety 0)))
25   (do ((i 0 (1+ i))
26        (new-types '())
27        (length-types (length types))
28        (length-auto-list (length auto-list)))
29       ((= i length-auto-list)
30        (nreverse new-types))
31     (declare (fixnum length-types length-auto-list i))
32     (if (>= i length-types)
33         (push t new-types) ;; types is shorter than num-fields
34         (push
35          (case (nth i types)
36            (:int
37             (case (nth i auto-list)
38               (:int32
39                :int32)
40               (:int64
41                :int64)
42               (t
43                t)))
44            (:double
45             (case (nth i auto-list)
46               (:double
47                :double)
48               (t
49                t)))
50            (:int32
51             (if (eq :int32 (nth i auto-list))
52                 :int32
53                 t))
54            (:int64
55             (if (eq :int64 (nth i auto-list))
56                 :int64
57               t))
58            (:blob
59             :blob)
60            (t
61             t))
62          new-types))))
63
64 (uffi:def-function "atoi"
65     ((str (* :unsigned-char)))
66   :returning :int)
67
68 (uffi:def-function "atol"
69     ((str (* :unsigned-char)))
70   :returning :long)
71
72 (uffi:def-function "atof"
73     ((str (* :unsigned-char)))
74   :returning :double)
75
76 (uffi:def-function "atol64"
77     ((str (* :unsigned-char))
78      (high32 (* :unsigned-int)))
79   :module "clsql-uffi"
80   :returning :unsigned-int)
81
82 (uffi:def-constant +2^32+ 4294967296)
83 (uffi:def-constant +2^64+ 18446744073709551616)
84 (uffi:def-constant +2^32-1+ (1- +2^32+))
85
86 (defmacro make-64-bit-integer (high32 low32)
87   `(if (zerop (ldb (byte 1 31) ,high32))
88        (+ ,low32 (ash ,high32 32))
89      (- (+ ,low32 (ash ,high32 32)) +2^64+)))
90
91 ;; From high to low ints
92 (defmacro make-128-bit-integer (a b c d)
93   `(+ ,d (ash ,c 32) (ash ,b 64) (ash ,a 96)))
94
95 (defmacro split-64-bit-integer (int64)
96   `(values (ash ,int64 -32) (logand ,int64 +2^32-1+)))
97
98 (uffi:def-type char-ptr-def (* :unsigned-char))
99
100 (defun convert-raw-field (char-ptr types index &optional length)
101   (declare (optimize (speed 3) (safety 0) (space 0))
102            (type char-ptr-def char-ptr))
103   (let ((type (if (consp types)
104                   (nth index types)
105                   types)))
106     (cond
107       ((uffi:null-pointer-p char-ptr)
108        nil)
109       (t
110        (case type
111          (:double
112           (atof char-ptr))
113          ((:int32 :int)
114           (atoi char-ptr))
115          (:int64
116           (uffi:with-foreign-object (high32-ptr :unsigned-int)
117             (let ((low32 (atol64 char-ptr high32-ptr))
118                   (high32 (uffi:deref-pointer high32-ptr :unsigned-int)))
119               (if (zerop high32)
120                   low32
121                 (make-64-bit-integer high32 low32)))))
122          (:blob
123           (if length
124               (uffi:convert-from-foreign-usb8 char-ptr length)
125             (error "Can't return blob since length is not specified.")))
126          (t
127           (if length
128               (uffi:convert-from-foreign-string char-ptr :locale :none
129                                                 :null-terminated-p nil
130                                                 :length length)
131             (uffi:convert-from-foreign-string char-ptr :locale :none))))))))
132