SubwayTooter-Android-App/_Emoji/src/main/kotlin/jp/juggler/subwaytooter/emoji/JavaCodeWriter.kt

65 lines
1.1 KiB
Kotlin
Raw Normal View History

2021-02-15 08:32:16 +01:00
package jp.juggler.subwaytooter.emoji
2021-02-19 02:18:58 +01:00
import java.io.*
2021-02-15 08:32:16 +01:00
2021-02-19 02:18:58 +01:00
class JavaCodeWriter(file: File) : AutoCloseable {
companion object {
const val lineFeed = "\u000a"
}
private val writer = OutputStreamWriter(BufferedOutputStream(FileOutputStream(file)), Charsets.UTF_8)
2021-02-15 08:32:16 +01:00
private var linesInFunction = 0
var functionsCount = 0
2021-02-15 08:32:16 +01:00
2021-02-19 02:18:58 +01:00
override fun close() {
writer.flush()
writer.close()
}
fun print(x: String) {
writer.write(x, 0, x.length)
}
fun println(x: String) {
print(x)
print(lineFeed)
writer.flush()
}
2021-02-15 08:32:16 +01:00
fun addCode(code: String) {
// open new function
if (linesInFunction == 0) {
++functionsCount
println("\n\tprivate static void init$functionsCount(EmojiMap e){")
2021-02-15 08:32:16 +01:00
}
// write code
2021-02-19 02:18:58 +01:00
print("\t\t")
println(code)
2021-02-15 08:32:16 +01:00
// close function
if (++linesInFunction > 100) {
2021-02-19 02:18:58 +01:00
println("\t}")
2021-02-15 08:32:16 +01:00
linesInFunction = 0
}
}
fun closeFunction() {
if (linesInFunction > 0) {
2021-02-19 02:18:58 +01:00
println("\t}")
2021-02-15 08:32:16 +01:00
linesInFunction = 0
}
}
fun writeDefinition(s: String) {
2021-02-19 02:18:58 +01:00
println("\t$s")
println("")
2021-02-15 08:32:16 +01:00
}
2021-02-15 08:32:16 +01:00
}