-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkdtemp.kt
39 lines (32 loc) · 937 Bytes
/
mkdtemp.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package datkt.fs
import kotlinx.cinterop.staticCFunction
import kotlinx.cinterop.toKString
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ByteVar
import datkt.fs.BufferCallback as Callback
import datkt.uv.uv_fs_mkdtemp
import datkt.uv.uv_fs_t
fun mkdtemp(path: String, callback: Callback) {
val req = uv.init<Callback>(callback)
uv_fs_mkdtemp(
datkt.fs.loop.default,
uv.toCValuesRef<uv_fs_t>(req),
path,
staticCFunction(::onmkdtemp))
}
private fun onmkdtemp(req: CPointer<uv_fs_t>?) {
uv.request<Callback>(req) { err, uv ->
val path = uv?.fs?.path
if (null != err) {
uv.done(err, null)
} else if (null != path) {
val bytes: CPointer<ByteVar> = path as CPointer<ByteVar>
val string = bytes.toKString()
val byteArray = string.toUtf8()
uv.done(null, byteArray)
} else {
uv.done(Error("An unknown error occured."), null)
}
uv.cleanup()
}
}