You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Very often (especially in unit tests) we need a big data samples to use (now it's in code as byte arrays).
It's good to have access to them from common code (including common tests).
As a first version we can do read-only mode.
Not sure if it belongs resources module or brand new separate one.
Describe the solution you'd like
Some ideas over interface:
sealed class FilePath(val path: String)
expect class FileResource(
path: FilePath
) {
fun open()
fun nextLine(): String?
fun nextByte(): Byte?
fun close()
}
private fun FileResource.lineSequence() = Sequence {
iterator {
while (true) {
nextLine()?.let { yield(it) } ?: break
}
}
}
fun <T> FileResource.useLines(block: (Sequence<String>) -> T): T {
open()
return block(lineSequence()).also { close() }
}
Where FilePath will wrap/maintain path for platform(Android,iOS,Native) and location (commonMain/resources, commonTest/resource, etc)
Do I can easily use it like this:
FileResource(path).useLines { string ->
// use next string from file
}
Similar way can be implemented to access single Byte (by having byte iterator)
JVM
And Android as well:
actual class FileResource actual constructor(path: FilePath) {
private val file = File(path.path)
private lateinit var iterator: Iterator<String>
actual fun open() {
iterator = file.readLines().iterator()
}
actual fun nextLine(): String? {
if (!iterator.hasNext()) return null
return iterator.next()
}
actual fun close() {
}
}
Native
Probably can be used in darwinMain:
actual class FileResource actual constructor(private val path: FilePath) {
private var file: CPointer<FILE>? = null
actual fun open() {
file = fopen(__filename = path.path, __mode = "r")
?: throw NullPointerException("Can't open file at path: <${path.path}>")
}
actual fun nextLine() = memScoped {
val buffer: CPointer<ByteVar> = allocArray(MAX_BUFFER_SIZE)
fgets(buffer, MAX_BUFFER_SIZE, file)?.toKString()?.trimEnd() // remove newline
}
actual fun close() {
fclose(file)
}
}
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
Very often (especially in unit tests) we need a big data samples to use (now it's in code as byte arrays).
It's good to have access to them from common code (including common tests).
As a first version we can do read-only mode.
Not sure if it belongs resources module or brand new separate one.
Describe the solution you'd like
Some ideas over interface:
Where FilePath will wrap/maintain path for platform(Android,iOS,Native) and location (commonMain/resources, commonTest/resource, etc)
Do I can easily use it like this:
Similar way can be implemented to access single Byte (by having byte iterator)
JVM
And Android as well:
Native
Probably can be used in darwinMain:
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: