1# cython: language_level=3
2# This file is part of h5py, a Python interface to the HDF5 library.
3#
4# http://www.h5py.org
5#
6# Copyright 2008-2019 Andrew Collette and contributors
7#
8# License:  Standard 3-clause BSD; see "license.txt" for full license terms
9#           and contributor agreement.
10
11"""
12    Provides access to the low-level HDF5 "H5PL" plugins interface.
13
14    These functions are only available with HDF5 1.10.1 or later.
15    They are probably not thread safe.
16"""
17
18include "config.pxi"
19from .utils cimport emalloc, efree
20
21# === C API ===================================================================
22
23IF HDF5_VERSION >= (1, 10, 1):
24    cpdef append(const char* search_path):
25        """(STRING search_path)
26
27        Add a directory to the end of the plugin search path.
28        """
29        H5PLappend(search_path)
30
31    cpdef prepend(const char* search_path):
32        """(STRING search_path)
33
34        Add a directory to the start of the plugin search path.
35        """
36        H5PLprepend(search_path)
37
38    cpdef replace(const char* search_path, unsigned int index):
39        """(STRING search_path, UINT index)
40
41        Replace the directory at the given index in the plugin search path.
42        """
43        H5PLreplace(search_path, index)
44
45    cpdef insert(const char* search_path, unsigned int index):
46        """(STRING search_path, UINT index)
47
48        Insert a directory at the given index in the plugin search path.
49        """
50        H5PLinsert(search_path, index)
51
52    cpdef remove(unsigned int index):
53        """(UINT index)
54
55        Remove the specified entry from the plugin search path.
56        """
57        H5PLremove(index)
58
59    cpdef get(unsigned int index):
60        """(UINT index) => STRING
61
62        Get the directory path at the given index (starting from 0) in the
63        plugin search path. Returns a Python bytes object.
64        """
65        cdef size_t n
66        cdef char* buf = NULL
67
68        n = H5PLget(index, NULL, 0)
69        buf = <char*>emalloc(sizeof(char)*(n + 1))
70        try:
71            H5PLget(index, buf, n + 1)
72            return PyBytes_FromStringAndSize(buf, n)
73        finally:
74            efree(buf)
75
76    cpdef size():
77        """() => UINT
78
79        Get the number of directories currently in the plugin search path.
80        """
81        cdef unsigned int n = 0
82        H5PLsize(&n)
83        return n
84