]> gitweb.fperrin.net Git - DictionaryPC.git/commitdiff
Add code to convert a Dictionary to the old v6 format.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Mon, 13 Apr 2020 14:35:44 +0000 (16:35 +0200)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Mon, 13 Apr 2020 18:57:13 +0000 (20:57 +0200)
convert_to_v6.sh [new file with mode: 0755]
genv6.sh [new file with mode: 0755]
src/com/hughes/android/dictionary/engine/ConvertToV6.java [new file with mode: 0644]

diff --git a/convert_to_v6.sh b/convert_to_v6.sh
new file mode 100755 (executable)
index 0000000..3b6bd5c
--- /dev/null
@@ -0,0 +1,3 @@
+JAVA=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
+test -x "$JAVA" || JAVA=java
+"$JAVA" -classpath bin/ com.hughes.android.dictionary.engine.ConvertToV6 "$@"
diff --git a/genv6.sh b/genv6.sh
new file mode 100755 (executable)
index 0000000..9bb5147
--- /dev/null
+++ b/genv6.sh
@@ -0,0 +1,20 @@
+set -e
+rm -rf data/outputsv6
+mkdir data/outputsv6
+for i in data/outputs/*.quickdic ; do
+    o=data/outputsv6/$(basename "$i")
+    ./convert_to_v6.sh "$i" "$o"
+    7z a -mx=9 "$o".v006.zip "$o"
+    rm "$o"
+    # skipHtml makes no sense for single-language dictionaries
+    if echo "$o" | grep -q '-' ; then
+        if ./convert_to_v6.sh "$i" "$o" skipHtmlOpt ; then
+            7z a -mx=9 "$o".small.v006.zip "$o"
+            rm "$o"
+        elif [ $? -ne 3 ] ; then
+            # Check for magic 3 indicating "no HTML entries in dictionary"
+            echo "Converting dictionary failed!"
+            exit 1
+        fi
+    fi
+done
diff --git a/src/com/hughes/android/dictionary/engine/ConvertToV6.java b/src/com/hughes/android/dictionary/engine/ConvertToV6.java
new file mode 100644 (file)
index 0000000..444a7c2
--- /dev/null
@@ -0,0 +1,75 @@
+// Copyright 2020 Reimar Döffinger. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.hughes.android.dictionary.engine;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+
+public class ConvertToV6 {
+    public static void main(final String[] args) throws IOException {
+        if (args.length != 2 && args.length != 3) {
+            System.out.println("Usage: ConvertToV6 <input.v007> <output.v006> [skipHtml]");
+            System.out.println("If the option third argument is given as 'skipHtml'");
+            System.out.println("the v6 dictionary will be without all HTML entries to reduce its size");
+            return;
+        }
+        boolean skipHtml = false;
+        boolean skipHtmlOpt = false;
+        if (args.length == 3) {
+            if (!args[2].equals("skipHtml") && !args[2].equals("skipHtmlOpt")) {
+                System.out.println("Unknown extra argument '" + args[2] + "'");
+                return;
+            }
+            skipHtml = true;
+            skipHtmlOpt = args[2].equals("skipHtmlOpt");
+        }
+        final String inname = args[0];
+        final String outname = args[1];
+        FileInputStream in;
+        try {
+            in = new FileInputStream(inname);
+        } catch (FileNotFoundException e) {
+            System.out.println("Could not open input file '" + inname + "'");
+            System.out.println(e);
+            return;
+        }
+        final Dictionary dictionary = new Dictionary(in.getChannel());
+        if (dictionary.dictFileVersion <= 6) {
+            System.out.println("Input dictionary is already v6 or older!");
+            return;
+        }
+        if (skipHtmlOpt && dictionary.htmlEntries.size() == 0) {
+            System.exit(3);
+        }
+        RandomAccessFile out;
+        try {
+            out = new RandomAccessFile(outname, "rw");
+        } catch (FileNotFoundException e) {
+            System.out.println("Could not open output file '" + outname + "'");
+            System.out.println(e);
+            return;
+        }
+        if (out.length() > 0) {
+            System.out.println("Output file '" + outname + "' already exists, aborting!");
+            return;
+        }
+        dictionary.writev6(out, skipHtml);
+        out.close();
+        in.close();
+    }
+}