Skip to content

Commit 3baf674

Browse files
committed
feat: slightly enhance the handling of the old opencc dict format (ocd)
1 parent 76828c9 commit 3baf674

File tree

3 files changed

+21
-54
lines changed

3 files changed

+21
-54
lines changed

app/src/main/java/com/osfans/trime/data/opencc/OpenCCDictManager.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ object OpenCCDictManager {
4848
(sharedDictionaries() + userDictionaries())
4949
}
5050

51-
fun openCCDictionaries(): List<OpenCCDictionary> = getAllDictionaries().mapNotNull { it as? OpenCCDictionary }
52-
5351
fun importFromFile(file: File): OpenCCDictionary {
5452
val raw =
5553
Dictionary.new(file)
@@ -60,7 +58,7 @@ object OpenCCDictManager {
6058
raw.toOpenCCDictionary(
6159
File(
6260
userDir,
63-
file.nameWithoutExtension + ".${Dictionary.Type.OPENCC.ext}",
61+
file.nameWithoutExtension + ".${Dictionary.Type.OCD2.ext}",
6462
),
6563
)
6664
Timber.d("Converted $raw to $new")
@@ -127,6 +125,6 @@ object OpenCCDictManager {
127125
@JvmStatic
128126
external fun getOpenCCVersion(): String
129127

130-
const val MODE_BIN_TO_TXT = true // OCD2 to TXT
128+
const val MODE_BIN_TO_TXT = true // OCD(2) to TXT
131129
const val MODE_TXT_TO_BIN = false // TXT to OCD2
132130
}

app/src/main/java/com/osfans/trime/data/opencc/dict/Dictionary.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import java.io.File
44

55
abstract class Dictionary {
66
enum class Type(val ext: String) {
7-
OPENCC("ocd2"),
7+
OCD("ocd"),
8+
OCD2("ocd2"),
89
Text("txt"),
910
;
1011

1112
companion object {
1213
fun fromFileName(name: String): Type? {
1314
return when {
14-
name.endsWith(".ocd2") -> OPENCC
15-
name.endsWith(".ocd") -> OPENCC
15+
name.endsWith(".ocd2") -> OCD2
16+
name.endsWith(".ocd") -> OCD
1617
name.endsWith(".txt") -> Text
1718
else -> null
1819
}
@@ -32,12 +33,12 @@ abstract class Dictionary {
3233
get() = file.nameWithoutExtension
3334

3435
fun toTextDictionary(): TextDictionary {
35-
val dest = file.resolveSibling(name + ".${Type.Text.ext}")
36+
val dest = file.resolveSibling("$name.${Type.Text.ext}")
3637
return toTextDictionary(dest)
3738
}
3839

3940
fun toOpenCCDictionary(): OpenCCDictionary {
40-
val dest = file.resolveSibling(name + ".${Type.OPENCC.ext}")
41+
val dest = file.resolveSibling("$name.${Type.OCD2.ext}")
4142
return toOpenCCDictionary(dest)
4243
}
4344

@@ -48,15 +49,15 @@ abstract class Dictionary {
4849
}
4950

5051
protected fun ensureTxt(dest: File) {
51-
if (dest.extension != Type.Text.ext) {
52+
if (dest.extension !in Type.Text.ext) {
5253
throw IllegalArgumentException("Dest file name must end with .${Type.Text.ext}")
5354
}
5455
dest.delete()
5556
}
5657

5758
protected fun ensureBin(dest: File) {
58-
if (dest.extension != Type.OPENCC.ext) {
59-
throw IllegalArgumentException("Dest file name must end with .${Type.OPENCC.ext}")
59+
if (dest.extension != Type.OCD.ext || dest.extension != Type.OCD2.ext) {
60+
throw IllegalArgumentException("Dest file name must end with .${Type.OCD.ext} or .${Type.OCD2.ext}")
6061
}
6162
dest.delete()
6263
}
@@ -66,7 +67,7 @@ abstract class Dictionary {
6667
companion object {
6768
fun new(it: File): Dictionary? =
6869
when (Type.fromFileName(it.name)) {
69-
Type.OPENCC -> OpenCCDictionary(it)
70+
Type.OCD, Type.OCD2 -> OpenCCDictionary(it)
7071
Type.Text -> TextDictionary(it)
7172
null -> null
7273
}

app/src/main/java/com/osfans/trime/data/opencc/dict/OpenCCDictionary.kt

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,18 @@ class OpenCCDictionary(file: File) : Dictionary() {
77
override var file: File = file
88
private set
99

10-
var isOCD2: Boolean = true
11-
private set
12-
13-
override val type: Type = Type.OPENCC
14-
15-
override val name: String
16-
get() =
17-
if (isOCD2) {
18-
super.name
19-
} else {
20-
file.name.substringBefore("")
21-
}
10+
override val type: Type =
11+
if (file.extension == NEW_FORMAT) {
12+
Type.OCD2
13+
} else {
14+
Type.OCD
15+
}
2216

2317
init {
2418
ensureFileExists()
25-
isOCD2 =
26-
when {
27-
file.extension == type.ext -> {
28-
true
29-
}
30-
file.name.endsWith(".$OLD_FORMAT") -> {
31-
false
32-
}
33-
else -> throw IllegalArgumentException("Not a libime dict ${file.name}")
34-
}
35-
}
36-
37-
fun useOCD2() {
38-
if (isOCD2) {
39-
return
40-
}
41-
val newFile = file.resolveSibling(name + ".${type.ext}")
42-
file.renameTo(newFile)
43-
file = newFile
44-
isOCD2 = true
45-
}
46-
47-
fun useOCD() {
48-
if (!isOCD2) {
49-
return
19+
if (file.extension != type.ext) {
20+
throw IllegalArgumentException("Not a OpenCC dict ${file.name}")
5021
}
51-
val newFile = file.resolveSibling(name + ".$OLD_FORMAT")
52-
file.renameTo(newFile)
53-
file = newFile
54-
isOCD2 = false
5522
}
5623

5724
override fun toTextDictionary(dest: File): TextDictionary {
@@ -71,6 +38,7 @@ class OpenCCDictionary(file: File) : Dictionary() {
7138
}
7239

7340
companion object {
41+
const val NEW_FORMAT = "ocd2"
7442
const val OLD_FORMAT = "ocd"
7543
}
7644
}

0 commit comments

Comments
 (0)