From 5fd13e86e29351db994eefb6a21b4ef216935530 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 27 May 2021 10:06:30 -0700 Subject: [PATCH] add functions for compcode and compname These are needed to set difference compressors in the HDF5 filter. --- src/Blosc.jl | 37 ++++++++++++++++++++++++++++++++----- test/runtests.jl | 9 +++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/Blosc.jl b/src/Blosc.jl index b4ca50d..1fdbb8e 100644 --- a/src/Blosc.jl +++ b/src/Blosc.jl @@ -188,11 +188,11 @@ end """ set_compressor(s::AbstractString) -Set the current compression algorithm to `s`. The currently supported -algorithms in the default Blosc module build are `"blosclz"`, `"lz4"`, -and `"lz4hc"`. (Throws an `ArgumentError` if `s` is not the name -of a supported algorithm.) Returns a nonnegative integer code used -internally by Blosc to identify the compressor. +Set the current compression algorithm to `s`. Supported algorithms are specified by +[`Blosc.compressors()`](@ref) (the default is `"blosclz"`). + +Throws an `ArgumentError` if `s` is not the name of a supported algorithm, otherwise +returns a nonnegative integer code used internally by Blosc to identify the compressor. """ function set_compressor(s::AbstractString) compcode = ccall((:blosc_set_compressor,libblosc), Cint, (Cstring,), s) @@ -307,4 +307,31 @@ whereas it returns `true` on success. """ free_resources!() = ccall((:blosc_free_resources,libblosc), Cint, ()) == 0 + +""" + compcode(s::AbstractString) + +Returns a nonnegative integer code used internally by Blosc to identify the compressor. +Throws an `ArgumentError` if `s` is not the name of a supported algorithm. +""" +function compcode(s::AbstractString) + compcode = ccall((:blosc_compname_to_compcode,libblosc), Cint, (Cstring,), s) + compcode == -1 && throw(ArgumentError("unrecognized compressor $s")) + return compcode +end + +""" + compname(compcode::Integer) + +Returns the compressor name corresponding to the internal integer code used by Blosc. +Throws an `ArgumentError` if `compcode` is not a valid code. +""" +function compname(compcode::Integer) + refstr = Ref{Cstring}() + retcode = ccall((:blosc_compcode_to_compname,libblosc), Cint, (Cint,Ptr{Cstring}), compcode, refstr) + retcode == -1 && throw(ArgumentError("unrecognized compcode $compcode")) + return unsafe_string(refstr[]) +end + + end # module diff --git a/test/runtests.jl b/test/runtests.jl index ca0d286..9f4efb3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -68,4 +68,13 @@ end @test_throws ArgumentError Blosc.compress(ones(UInt8, 256), level=-1) @test_throws ArgumentError Blosc.compress(ones(UInt8, 256), level=11) + +# test compcode/compname +for name in Blosc.compressors() + @test Blosc.compname(Blosc.compcode(name)) == name +end + +@test_throws ArgumentError Blosc.compname(99) +@test_throws ArgumentError Blosc.compcode("not_a_compressor") + @test Blosc.free_resources!()