From 730d84f0b16f3bd22c70eeb0ecab332f3971af73 Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Fri, 12 Apr 2024 10:52:03 +0200 Subject: [PATCH] Add H5PLget and use it to set the plugin path on load --- R/H5PL.R | 14 ++++++++++++++ R/zzz.R | 6 ++++++ src/H5PL.c | 21 +++++++++++++++++++-- src/H5PL.h | 1 + src/wrap.c | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/R/H5PL.R b/R/H5PL.R index 8ca300ae..7291bc96 100644 --- a/R/H5PL.R +++ b/R/H5PL.R @@ -8,4 +8,18 @@ H5PLprepend <- function(path) { H5PLsize <- function() { size <- .Call("_H5PLsize", PACKAGE = 'rhdf5') return(size) +} + +H5PLget <- function(index) { + path <- .Call("_H5PLget", as.integer(index-1), PACKAGE = "rhdf5") + return(path) +} + +h5getPluginPaths <- function() { + + n_paths <- H5PLsize() + paths <- vapply(seq_len(n_paths), + FUN = H5PLget, + FUN.VALUE = character(1)) + return(paths) } \ No newline at end of file diff --git a/R/zzz.R b/R/zzz.R index dafe3891..290b3b7b 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -2,4 +2,10 @@ .onLoad <- function(libname, pkgname) { h5constants <<- H5loadConstants() h5errorHandling() + + if(requireNamespace('rhdf5filters')) { + plugin_path <- rhdf5filters::hdf5_plugin_path() + H5PLprepend(plugin_path) + } + } diff --git a/src/H5PL.c b/src/H5PL.c index f950653d..d3311bd9 100644 --- a/src/H5PL.c +++ b/src/H5PL.c @@ -14,13 +14,30 @@ SEXP _H5PLprepend( SEXP _search_path ) { res = H5PLprepend(search_path); if(res < 0) { - error("Unable to prepend value to plugin search path"); + error("Unable to prepend value to plugin search path\n"); } SEXP Rval = ScalarLogical(1); return Rval; } +/* ssize_t H5PLget ( unsigned int index, char *path_buf, size_t buf_size ) */ +SEXP _H5PLget( SEXP index ) { + + int i = asInteger(index); + char buf[512]; + SEXP Rval; + + if(H5PLget(i, buf, 512) < 0) { + error("Unable to read plugin path position\n"); + } + + PROTECT(Rval = mkString(buf)); + UNPROTECT(1); + return Rval; +} + + /* herr_t H5PLsize ( unsigned int * num_paths ) */ SEXP _H5PLsize() { @@ -28,7 +45,7 @@ SEXP _H5PLsize() { unsigned int nvals = 0; if(H5PLsize( &nvals ) < 0 ) { - error("Unable to prepend value to plugin search path"); + error("Unable to determine size of the plugin path\n"); } if(nvals <= INT32_MAX) { diff --git a/src/H5PL.h b/src/H5PL.h index efaba355..ac73941c 100644 --- a/src/H5PL.h +++ b/src/H5PL.h @@ -10,5 +10,6 @@ SEXP _H5PLprepend( SEXP _search_path ); SEXP _H5PLsize(); +SEXP _H5PLget( SEXP index ); #endif diff --git a/src/wrap.c b/src/wrap.c index a7fe7b17..3a020205 100644 --- a/src/wrap.c +++ b/src/wrap.c @@ -338,6 +338,7 @@ static R_CallMethodDef libraryRCalls[] = { {"_h5getEnumValues", (DL_FUNC) &_h5getEnumValues, 1}, {"_H5PLprepend", (DL_FUNC) &_H5PLprepend, 1}, {"_H5PLsize", (DL_FUNC) &_H5PLsize, 0}, + {"_H5PLget", (DL_FUNC) &_H5PLget, 1}, #ifdef _H5P_filters {"_H5Pset_lzf", (DL_FUNC) &_H5Pset_lzf, 2}, {"_H5Pset_bzip2", (DL_FUNC) &_H5Pset_bzip2, 2},