diff options
author | Marvin Borner | 2019-08-14 23:17:02 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-14 23:17:02 +0200 |
commit | 46f00834b7c11c35bda6857aeaefefe5c81bb5cc (patch) | |
tree | 9ccd7715f15069214a951133eead74b991c018ce | |
parent | bd256690dac25fe3ec94d8a24b312f588f99fcd4 (diff) |
Added C implementation of a filereader
-rw-r--r-- | src/runMain/kotlin/Loader.kt | 34 |
1 files changed, 31 insertions, 3 deletions
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<ByteVar>(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 |