https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

saket

07/06/2020, 8:07 PM
Is anyone working with files in their multiplatform project?
b

basher

07/06/2020, 8:10 PM
We're doing it with json mocks in tests with some codegen
s

saket

07/06/2020, 8:12 PM
Are you delegating the calls using expect/actual to the platform? Or are you writing them in kotlin native using expect/actual? I’m going to need this soon for Press and it’d be nice to have some references.
c

Casey Brooks

07/06/2020, 8:13 PM
KorIO might be what you need https://korlibs.soywiz.com/korio/
d

Dominaezzz

07/06/2020, 8:16 PM
I have
expect/actual
for a
fun readText(path: String): String
and a
fun readBytes(path: String): ByteArray
function.
b

basher

07/06/2020, 8:18 PM
yeah expect actual
s

saket

07/06/2020, 8:22 PM
@Dominaezzz @basher are you using posix apis on native?
b

basher

07/06/2020, 8:22 PM
no. for iOS, just iOS ones. on windows, we tried posix and struggled to get it working — ended up using windows APIs there
s

saket

07/06/2020, 8:22 PM
Also, can I see your actual implementations somewhere?
s

saket

07/06/2020, 8:23 PM
@Casey Brooks Ooh this looks interesting. I’ll take a look, thank you!
@basher gotcha. Any specific issues you ran into? @Dominaezzz this is useful, thank you!
fwiw I also found https://github.com/caffeine-mgn/pw.binom.io but it doesn’t support apple
b

basher

07/06/2020, 8:38 PM
Copy code
@ExperimentalUnsignedTypes
internal actual fun loadMock(path: String): String {
    val fd = open(path, O_RDONLY)
    throwIfFailed(fd)
    return try {
        val size = lseek(fd, 0, SEEK_END)
        throwIfFailed(size)
        throwIfFailed(lseek(fd, 0, SEEK_SET))

        val buf = ByteArray(size)
        buf.usePinned { pinned ->
            throwIfFailed(read(fd, pinned.addressOf(0), size.toUInt()))
            buf.decodeToString()
        }

    } finally {
        close(fd)
    }
this was the posix one we were trying to get to work on windows, but it had off-by-one-type errors when the String was passed to kotlinx.serialization
🤔 1
we ended up switching to a windows-specific implementation, which worked
s

saket

07/06/2020, 8:46 PM
I see, thanks for sharing!
👍 1
2 Views