Simple CSV parser (#383)
This commit is contained in:
@@ -2256,10 +2256,8 @@ public inline fun <T> Iterable<T>.asIterable(): Iterable<T> {
|
||||
/**
|
||||
* Creates a [Sequence] instance that wraps the original collection returning its elements when being iterated.
|
||||
*/
|
||||
@FixmeSequences
|
||||
public fun <T> Iterable<T>.asSequence(): Sequence<T> {
|
||||
//return Sequence { this.iterator() }
|
||||
TODO()
|
||||
return Sequence { this.iterator() }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import kotlinx.cinterop.*
|
||||
import stdio.*
|
||||
|
||||
fun parseLine(line: String, separator: Char) : List<String> {
|
||||
val result = mutableListOf<String>()
|
||||
var builder = StringBuilder()
|
||||
var quotes = 0
|
||||
for (ch in line) {
|
||||
when {
|
||||
ch == '\"' -> {
|
||||
quotes++
|
||||
builder.append(ch)
|
||||
}
|
||||
(ch == '\n') || (ch == '\r') -> {}
|
||||
(ch == separator) && (quotes % 2 == 0) -> {
|
||||
result.add(builder.toString())
|
||||
builder = StringBuilder()
|
||||
}
|
||||
else -> builder.append(ch)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.size != 3) {
|
||||
println("usage: csvparser.kexe file.csv column count")
|
||||
return
|
||||
}
|
||||
val fileName = args[0]
|
||||
val column = args[1].toInt()
|
||||
val count = args[2].toInt()
|
||||
|
||||
val file = fopen(fileName, "r")
|
||||
if (file == null) {
|
||||
perror("cannot open input file $fileName")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
memScoped {
|
||||
val bufferLength = 64 * 1024
|
||||
val buffer = allocArray<CInt8Var>(bufferLength)
|
||||
|
||||
for (i in 1..count) {
|
||||
val nextLine = fgets(buffer[0].ptr, bufferLength, file)?.toKString()
|
||||
if (nextLine == null || nextLine.isEmpty()) break
|
||||
|
||||
val records = parseLine(nextLine, ',')
|
||||
println(records[column])
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
fclose(file)
|
||||
}
|
||||
}
|
||||
+262
File diff suppressed because one or more lines are too long
@@ -0,0 +1,14 @@
|
||||
# CVS parser
|
||||
|
||||
This example shows how one could implement simple CSV reader and parser in Kotlin.
|
||||
A sample data [European Mammals Red List for 2009] (https://data.europa.eu/euodp/en/data/dataset?res_format=CSV)
|
||||
from EU is being used.
|
||||
|
||||
To build use `./build` script without arguments (or specify `TARGET` variable if cross-compiling).
|
||||
|
||||
To run use
|
||||
|
||||
./csvparser.kexe ./European_Mammals_Red_List_Nov_2009.csv 4 100
|
||||
|
||||
Which will print fifth column (Family, zero-based index) in first 100 rows of the
|
||||
CSV file.
|
||||
@@ -0,0 +1,4 @@
|
||||
headers = stdio.h stdlib.h string.h
|
||||
compilerOpts = -D_POSIX_SOURCE
|
||||
linkerOpts =
|
||||
excludedFunctions =
|
||||
@@ -1,6 +1,12 @@
|
||||
#Tetris game
|
||||
|
||||
This example shows implementation of simple Tetris game using SDL
|
||||
(Simple DirectMedia Layer) library for rendering. SDL allows easy development
|
||||
of cross-platform game and multimedia applications.
|
||||
|
||||
Start with building compiler by using `dist` and `cross_dist` for cross-targets.
|
||||
To build Tetris application for your host platform use
|
||||
./build
|
||||
./build
|
||||
note that SDL2 must be installed on the host.
|
||||
For cross-compilation to iOS (on Mac host) use
|
||||
TARGET=iphone ./build
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import kotlinx.cinterop.*
|
||||
import konan.internal.*
|
||||
import sdl.*
|
||||
|
||||
typealias Field = Array<ByteArray>
|
||||
|
||||
Reference in New Issue
Block a user