Trying to call a C function (`cfg_init` from libco...
# kotlin-native
n
Trying to call a C function (
cfg_init
from libconfuse) that is expecting a
CValuesRef<cfg_opt_t>?
as the first argument. Using the
cValuesOf
function returns
CValues<CPointerVar<cfg_opt_t>>
which doesn't match. Below is the sample code:
Copy code
val targetStr = configString(name = "target".cstr, def = "NONE".cstr, flags = CFGF_NONE)
val end = configEnd()
val config = cfg_init(cValuesOf(targetStr, end), CFGF_NONE)
Below is the mapping for the C function:
Copy code
public fun cfg_init(opts: kotlinx.cinterop.CValuesRef<libconfuse.cfg_opt_t>?, flags: libconfuse.cfg_flag_t /* = <http://kotlin.Int|kotlin.Int> */): kotlinx.cinterop.CPointer<libconfuse.cfg_t>? { /* compiled code */ }
a
Can you try to use something like this?
Copy code
val elements = arrayOf(targetStr, end)
val opts = createValues<cfg_opt_t>(elements.size) { index -> this.value = elements[index] }
val config = cfg_init(opts, CFGF_NONE)
n
In the second line this.value doesn't exist.
I have replaced the second line with the following:
Copy code
val opts = createValues<cfg_opt_t>(elements.size) { pos ->
        val tmp = elements[pos]
        if (tmp != null) {
            this.name = tmp.pointed.name
            this.flags = tmp.pointed.flags
        }
    }
Unfortunately this.def is read only which is a bit of a pain. Means a default value cannot be set for the first configuration option.
Is there a straightforward way in Kotlin Native to deep copy a C struct?
After running the program with the applied changes the following errors appear:
Copy code
internal error in cfg_init_defaults(@�")
/home/napperley/battery_telemetry.conf:1: no such option 'target'
Looks as though the config options haven't been properly initialised.
Would be a good idea to cover the
createValues
function in the Kotlin Native documentation, for C interop in the Passing pointers to bindings section ( https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md#passing-pointers-to-bindings ).