-
Notifications
You must be signed in to change notification settings - Fork 1
/
write.kt
46 lines (40 loc) · 1.03 KB
/
write.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
40
41
42
43
44
45
46
package datkt.fs
import kotlinx.cinterop.staticCFunction
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.usePinned
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.alloc
import kotlinx.cinterop.ptr
import datkt.fs.WriteFileCallback as Callback
import datkt.uv.uv_fs_write
import datkt.uv.uv_buf_t
import datkt.uv.uv_fs_t
fun write(
fd: Int,
buffer: ByteArray,
offset: Int = 0,
length: Int = buffer.size,
position: Int = 0,
callback: Callback
) {
val cb = staticCFunction(::onwrite)
val req = uv.init<Callback>(callback, buffer)
val ref = uv.toCValuesRef<uv_fs_t>(req)
val len = length.toLong().toULong()
val pos = position.toLong()
memScoped {
val buf = alloc<uv_buf_t>()
buffer.usePinned { pinned ->
buf.base = pinned.addressOf(offset)
buf.len = len
uv_fs_write(loop.default, ref, fd, buf.ptr, 1, pos, cb)
}
}
}
private fun onwrite(req: CPointer<uv_fs_t>?) {
uv.request<Callback>(req) { err, uv ->
uv.cleanup()
uv.done(err)
}
}