From 0efa78d4a0f1399a3eb224f1fd62b6f54f9fe0b2 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 5 Oct 2017 17:21:10 +0300 Subject: [PATCH] Add `.toCStringArray(AutofreeScope)` to interop utils --- .../src/main/kotlin/kotlinx/cinterop/Utils.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt index ecb5aa053bc..d6b06f3c1f2 100644 --- a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt +++ b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt @@ -361,6 +361,20 @@ val String.cstr: CValues } } +/** + * Convert this list of Kotlin strings to C array of C strings, + * allocating memory for the array and C strings with given [AutofreeScope]. + */ +fun List.toCStringArray(autofreeScope: AutofreeScope): CPointer> = + autofreeScope.allocArrayOf(this.map { it.cstr.getPointer(autofreeScope) }) + +/** + * Convert this array of Kotlin strings to C array of C strings, + * allocating memory for the array and C strings with given [AutofreeScope]. + */ +fun Array.toCStringArray(autofreeScope: AutofreeScope): CPointer> = + autofreeScope.allocArrayOf(this.map { it.cstr.getPointer(autofreeScope) }) + val String.wcstr: CValues get() { val chars = CharArray(this.length, { i -> this.get(i)})