From 46f00834b7c11c35bda6857aeaefefe5c81bb5cc Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 14 Aug 2019 23:17:02 +0200 Subject: Added C implementation of a filereader --- src/runMain/kotlin/Loader.kt | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'src/runMain') diff --git a/src/runMain/kotlin/Loader.kt b/src/runMain/kotlin/Loader.kt index 19d695b..7cec0a5 100644 --- a/src/runMain/kotlin/Loader.kt +++ b/src/runMain/kotlin/Loader.kt @@ -1,9 +1,37 @@ +import kotlinx.cinterop.ByteVar +import kotlinx.cinterop.allocArray +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.toKString +import platform.posix.* + class Loader(path: String) { - // private val inputStream: InputStream = File(path).inputStream() - // private val inputString = inputStream.bufferedReader().use { it.readText() } + private val inputString = read(path) // TODO: Add preprocessor managing imports and comments fun load(): String { - return "Hallo" + return inputString + } + + private fun read(path: String): String { + setlocale(LC_CTYPE, "en_US.UTF-8") + + val file = fopen(path, "r") + if (file == null) { + perror("cannot open input file $path") + } + + memScoped { + val bufferLength = 64 * 1024 + val buffer = allocArray(bufferLength) + + var content = "" + var nextLine = fgets(buffer, bufferLength, file)?.toKString() + while (nextLine != null) { + content += nextLine + nextLine = fgets(buffer, bufferLength, file)?.toKString() + } + fclose(file) + return content + } } } \ No newline at end of file -- cgit v1.2.3