Hello, I am writing some simple wrappers around an old C library.
I am new to Julia and absolutely new to C, so it might be something obvious.
I have managed to get majority of functions working, but I am unsure about the handling of returned Cchar matrices.
Here is an example:
##C-call:
# int list_of_signals(int *, int *, char *, int *)
# char list[24*N]
# int ierr, id, ndim, status
# status = listas_(&id, &ndim, &num_of_sigs, list, &ierr)
Function is supposed to return first num_of_sigs signals in a list corresponding to a given id.
function list(id::Int, num_of_sigs::Int)
ier=Ref{Cint}(0);
ID=Ref{Cint}(id);
NSIG=Ref{Cint}(num_of_sigs);
list=Array{Cchar}(undef,24,n_dim);
status = ccall(("list_of_signals", path_to_lib), Int32,
(Ref{Cint}, Ref{Cint}, Ptr{Cchar}, Ptr{Cint}),
ID, NSIG, list, ier)
return list, nsr[], status, ier[]
end
Function returns without an error and the output (after some manipulations) looks something like this:
permutedims(Char.(list))=
40Ă—24 Matrix{Char}:
'S' 'I' 'G' 'N' '1' '\0' '\0' '\0' … '\0' '\0' '\0' '\0' '\0' '\0'
'S' 'I' 'G' 'N' '1' '0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
'S' 'I' 'G' 'N' '1' '1' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
...etc
And I want to convert this Matrix{Char} into Vector{String}
I managed to do it in a relatively ugly way:
list_out=Vector{String}(undef,num_of_sigs)
charlist=Char.(list);
for i in 1:num_of_sigs
list_out[i] = String(charlist[:,i]);
end
This successfully converts it to a vector of string, however null termination is not eliminated:
String[
"SIGN1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"SIGN10\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
...
And I have an feeling that there should be a more elegant solution (either in the way the input is initially declared and passed to the ccall, or how it is converted/formatted later on)
PS: If there are any suggestions or notes on how the wrapper is written - they are very welcome! I am still a bit confused with Ref{T} and Ptr{T} and was just using Ref=inputs, Ptr=outputs as a rule of thumb, but I am unsure if this is the way to go.
Cheers and thanks for your help!