@@ -8,18 +8,19 @@ import java.io.IOException
88
99object SKKKanaRule {
1010 internal const val INTERNAL_FILE_NAME = " kana-rule.conf"
11+ internal const val DEFAULT_RULE_FILE = " skk-kana-rule.conf"
1112 private const val MAX_FILE_SIZE = 1 * 1024 * 1024 // 1MB
1213
13- fun exists (context : Context ): Boolean =
14- getInternalFile (context).exists( )
15-
16- private fun getInternalFile ( context : Context ): File =
17- File (context.filesDir, INTERNAL_FILE_NAME )
14+ fun getInternalFile (context : Context ): File {
15+ val file = File (context.filesDir, INTERNAL_FILE_NAME )
16+ if ( ! file.exists()) clear(context)
17+ return file
18+ }
1819
1920 /* *
2021 * kana-rule.conf テキストをパースして Map<入力列, ひらがな> を返す。
21- * フォーマット: 入力,ひらがな[,カタカナ]
22- * # で始まる行と空行は無視する。
22+ * フォーマット: 入力,ひらがな
23+ * ( # で始まる行と空行は無視する。)
2324 */
2425 fun parse (text : String ): Map <String , String > {
2526 val result = mutableMapOf<String , String >()
@@ -29,6 +30,8 @@ object SKKKanaRule {
2930 val fields = trimmed.split(" ," )
3031 if (fields.size < 2 ) continue
3132 val input = fields[0 ].trim()
33+ .replace(" ♯" , " #" , ignoreCase = true )
34+ .replace(" ," , " ," , ignoreCase = true )
3235 val hiragana = fields[1 ].trim()
3336 if (input.isNotEmpty() && hiragana.isNotEmpty()) {
3437 result[input] = hiragana
@@ -43,7 +46,6 @@ object SKKKanaRule {
4346 */
4447 fun loadFromInternalStorage (context : Context ): Map <String , String >? {
4548 val file = getInternalFile(context)
46- if (! file.exists()) return null
4749 if (file.length() > MAX_FILE_SIZE ) {
4850 Log .e(" SKK" , " SKKKanaRule#loadFromInternalStorage() Error: File is too large" )
4951 return null
@@ -68,7 +70,10 @@ object SKKKanaRule {
6870 if (sizeIndex != - 1 && cursor.moveToFirst()) {
6971 val size = cursor.getLong(sizeIndex)
7072 if (size > MAX_FILE_SIZE ) {
71- Log .e(" SKK" , " SKKKanaRule#saveFromUri() Error: File is too large ($size bytes)" )
73+ Log .e(
74+ " SKK" ,
75+ " SKKKanaRule#saveFromUri() Error: File is too large ($size bytes)"
76+ )
7277 return false
7378 }
7479 }
@@ -93,9 +98,12 @@ object SKKKanaRule {
9398 }
9499
95100 /* *
96- * 内部ストレージの kana-rule.conf を削除する 。
101+ * 内部ストレージの kana-rule.conf を初期化する 。
97102 */
98103 fun clear (context : Context ) {
99- getInternalFile(context).delete()
104+ val file = File (context.filesDir, INTERNAL_FILE_NAME )
105+ val defaultRule = context.resources.assets.open(DEFAULT_RULE_FILE )
106+ .bufferedReader().use { it.readText() }
107+ file.writeText(defaultRule)
100108 }
101109}
0 commit comments