;; hdf5.ffi - 		 		-*- Scheme -*-

;; Copyright (C) 2018,2022 Matthew R. Wette
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 3 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with this library; if not, see <http://www.gnu.org/licenses/>

;;; Notes:

;; HDF5 is "diverse" in declarations and a challenge to the FH.
;; On my Ubuntu pkg-config says shared lib is in 
;;   /usr/lib/x86_64-linux-gnu/hdf5/serial
;; but there is
;;   /usr/lib/x86_64-linux-gnu/libhdf5_serial.so
;; so things are broken unless we add that.

;;; Code:

(define-ffi-module (ffi hdf5)
  #:pkg-config "hdf5"
  #:include '("hdf5.h" "hdf5_hl.h")
  #:inc-filter (lambda (spec path) (string-contains path "hdf5/")))

(define-public (H5 sym) (ffi-hdf5-symbol-val sym))

(define-public H5S_UNLIMITED
  (case (bytestructure-descriptor-size hsize_t-desc)
    ((8) #xffffffffffffffff) ((4) #xffffffff)))
(define-public H5T_VARIABLE
  (case (bytestructure-descriptor-size hsize_t-desc)
    ((8) #xffffffffffffffff) ((4) #xffffffff)))

(define-syntax H5P_DEFAULT (identifier-syntax (H5 'H5P_DEFAULT)))
(export H5P_DEFAULT)
(define-syntax H5P_DATASET_CREATE (identifier-syntax (H5P_CLS_DATASET_CREATE_ID_g)))
(export H5P_DATASET_CREATE)
(define-syntax H5P_FILE_CREATE (identifier-syntax (H5P_CLS_FILE_CREATE_ID_g)))
(export H5P_FILE_CREATE)

(define-syntax H5R_DATASET_REGION (identifier-syntax (H5 'H5R_DATASET_REGION)))
(export H5R_DATASET_REGION)
(define-syntax H5R_OBJECT (identifier-syntax (H5 'H5R_OBJECT)))
(export H5R_OBJECT)

(define-syntax H5S_ALL (identifier-syntax (H5 'H5S_ALL)))
(export H5S_ALL)
(define-syntax H5S_SCALAR (identifier-syntax (H5 'H5S_SCALAR)))
(export H5S_SCALAR)

(define-syntax H5T_COMPOUND (identifier-syntax (H5 'H5T_COMPOUND)))
(export H5T_COMPOUND)
(define-syntax H5T_CSET_ASCII (identifier-syntax (H5 'H5T_CSET_ASCII)))
(export H5T_CSET_ASCII)
(define-syntax H5T_CSET_UTF8 (identifier-syntax (H5 'H5T_CSET_UTF8)))
(export H5T_CSET_UTF8)
(define-syntax H5T_REFERENCE (identifier-syntax (H5 'H5T_REFERENCE)))
(export H5T_REFERENCE)
(define-syntax H5T_STRING (identifier-syntax (H5 'H5T_STRING)))
(export H5T_STRING)
(define-syntax H5T_C_S1 (identifier-syntax (H5T_C_S1_g)))
(export H5T_C_S1)
(define-syntax H5T_STD_REF_OBJ (identifier-syntax (H5T_STD_REF_OBJ_g)))
(export H5T_STD_REF_OBJ)
(define-syntax H5T_NATIVE_DOUBLE (identifier-syntax (H5T_NATIVE_DOUBLE_g)))
(export H5T_NATIVE_DOUBLE)
(define-syntax H5T_NATIVE_INT (identifier-syntax (H5T_NATIVE_INT_g)))
(export H5T_NATIVE_INT)
(define-syntax H5T_NATIVE_UINT (identifier-syntax (H5T_NATIVE_UINT_g)))
(export H5T_NATIVE_UINT)
(define-syntax H5T_NATIVE_INT8 (identifier-syntax (H5T_NATIVE_INT8_g)))
(export H5T_NATIVE_INT8)
(define-syntax H5T_NATIVE_UINT8 (identifier-syntax (H5T_NATIVE_UINT8_g)))
(export H5T_NATIVE_UINT8)
(define-syntax H5T_NATIVE_INT16 (identifier-syntax (H5T_NATIVE_INT16_g)))
(export H5T_NATIVE_INT16)
(define-syntax H5T_NATIVE_UINT16 (identifier-syntax (H5T_NATIVE_UINT16_g)))
(export H5T_NATIVE_UINT16)
(define-syntax H5T_NATIVE_INT32 (identifier-syntax (H5T_NATIVE_INT32_g)))
(export H5T_NATIVE_INT32)
(define-syntax H5T_NATIVE_UINT32 (identifier-syntax (H5T_NATIVE_UINT32_g)))
(export H5T_NATIVE_UINT32)
(define-syntax H5T_NATIVE_INT64 (identifier-syntax (H5T_NATIVE_INT64_g)))
(export H5T_NATIVE_INT64)
(define-syntax H5T_NATIVE_UINT64 (identifier-syntax (H5T_NATIVE_UINT64_g)))
(export H5T_NATIVE_UINT64)

(define-public H5Acreate H5Acreate2)
(define-public H5Dcreate H5Dcreate2)
(define-public H5Gcreate H5Gcreate2)
(define-public H5Rdereference H5Rdereference2)
(define-public H5Topen H5Topen2)
(define-public H5Tcommit H5Tcommit2)

(define-public H5D_append_cb_t* H5D_append_cb_t)
(define-public H5E_auto1_t* H5E_auto1_t)
(define-public H5E_auto2_t* H5E_auto2_t)
(define-public H5F_flush_cb_t* H5F_flush_cb_t)
(define-public H5MM_allocate_t* H5MM_allocate_t)
(define-public H5MM_free_t* H5MM_free_t)
(define-public H5T_conv_except_func_t* H5T_conv_except_func_t)
(define-public H5L_elink_traverse_t* H5L_elink_traverse_t)
(define-public H5O_mcdt_search_cb_t* H5O_mcdt_search_cb_t)

;; --- last line ---
