34 lines
1002 B
Kotlin
34 lines
1002 B
Kotlin
package fi.schro.util
|
|
|
|
import kotlinx.cinterop.ByteVar
|
|
import kotlinx.cinterop.allocArray
|
|
import kotlinx.cinterop.memScoped
|
|
import kotlinx.cinterop.toKString
|
|
import platform.posix.fclose
|
|
import platform.posix.fgets
|
|
import platform.posix.fopen
|
|
|
|
object FileUtil {
|
|
fun readAllText(filePath: String): String {
|
|
val returnBuffer = StringBuilder()
|
|
val file = fopen(filePath, "r") ?:
|
|
throw IllegalArgumentException("Cannot open input file $filePath")
|
|
|
|
try {
|
|
memScoped {
|
|
val readBufferLength = 64 * 1024
|
|
val buffer = allocArray<ByteVar>(readBufferLength)
|
|
var line = fgets(buffer, readBufferLength, file)?.toKString()
|
|
while (line != null) {
|
|
returnBuffer.append(line)
|
|
line = fgets(buffer, readBufferLength, file)?.toKString()
|
|
}
|
|
}
|
|
} finally {
|
|
fclose(file)
|
|
}
|
|
|
|
return returnBuffer.toString()
|
|
}
|
|
|
|
} |