andreasmattsson
08/14/2017, 2:15 PMreaddir
from dirent.h
.
I have the following code:
while (true) {
val ep = readdir (dp)
if (ep == null) break
val str = ep.pointed.d_name.toKString()
println(str)
}
This works for some dirs but not all. On some, I get the following partway through the enumeration:
terminate called after throwing an instance of 'utf8::invalid_utf8'
what(): Invalid UTF-8
The execution terminates immediately. How do I catch/recover from this error? I've tried wrapping with try/catch Throwable to no avail.while (true) {
val ep = readdir (dp)
if (ep == null) break
val str = ep.pointed.d_name.run {
generateSequence(0) { it + 1 }
.map { this[it] }
.takeWhile { it.toInt() != 0 }
.joinToString("") { it.toChar().toString() }
}
println(str)
}
Surely there must be a better way? 😛tunedal
08/14/2017, 3:20 PMandreasmattsson
08/14/2017, 4:41 PMtunedal
08/14/2017, 7:17 PMtoKString()
ends up calling the C++ function utf8to16
in runtime/src/main/cpp/utf8/ which exists in a checked and an unchecked version, the former of which throws a C++ exception. So... I dunno. 🙂