Skip to content

Commit

Permalink
Merge pull request #81 from simonbyrne/sb/compcode
Browse files Browse the repository at this point in the history
add functions for compcode and compname
  • Loading branch information
musm authored Oct 28, 2021
2 parents d3c78f9 + 5fd13e8 commit d3724f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/Blosc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!()

0 comments on commit d3724f7

Please sign in to comment.