Hi! I am trying to access from Julia a C function whose header is declared as follows:
MTKt_status MtkFileAttrList( const char *filename,
int *attrcnt,
char **attrlist[] );
where the output argument attrlist
should be a list of 31 (string) file attributes, such as
HDFEOSVersion
StructMetadata.0
Path_number
AGP_version_id
DID_version_id
Number_blocks
...
Cam_mode
Num_local_modes
Local_mode_site_name
Orbit_QA
Camera
coremetadata
I’ve tentatively written the Julia wrapper as
function MtkFileAttrList(filename, attrcnt, attrlist)
status = ccall((:MtkFileAttrList, mtklib),
Cint,
(Cstring, Ptr{Cint}, Vector{Ptr{Ptr{UInt8}}}),
filename, attrcnt, attrlist)
return status
end
When I call this wrapper function,
julia> filename = "./data/MISR/MISR_AM1_GRP_TERRAIN_GM_P168_O068050_BA_F03_0024.hdf"
"./data/MISR/MISR_AM1_GRP_TERRAIN_GM_P168_O068050_BA_F03_0024.hdf"
julia> attrcnt = Ref{Int32}(0)
Base.RefValue{Int32}(0)
julia> attrlist = Vector{Ref{Ptr{UInt8}}}()
Ref{Ptr{UInt8}}[]
julia> status = MtkFileAttrList(filename, attrcnt, attrlist)
0
julia> typeof(attrlist)
Vector{Ref{Ptr{UInt8}}} (alias for Array{Ref{Ptr{UInt8}}, 1})
julia> typeof(attrlist[])
ERROR: BoundsError: attempt to access 0-element Vector{Ref{Ptr{UInt8}}} at index []
Stacktrace:
[1] throw_boundserror(A::Vector{Ref{Ptr{UInt8}}}, I::Tuple{})
@ Base ./abstractarray.jl:703
[2] checkbounds
@ ./abstractarray.jl:668 [inlined]
[3] _getindex
@ ./abstractarray.jl:1273 [inlined]
[4] getindex(::Vector{Ref{Ptr{UInt8}}})
@ Base ./abstractarray.jl:1241
[5] top-level scope
@ REPL[13]:1
so the function does not detect any error (status is 0) and returns the correct number of attributes (31), but the output argument attrlist
appears to be empty: I suspect its definition or usage is incorrect. I thus have 3 questions:
-
Is the initial declaration of the
attrlist = Vector{Ref{Ptr{UInt8}}}()
correctly setup? -
Is the
ccall
argumentVector{Ptr{Ptr{UInt8}}}
correctly specified? -
How do I access the individual items in the output list
attrlist
, once it will actually contain the expected attribute list?
Thanks for suggestions on these questions.