Initial.
authorviric@llimona
Mon, 23 Jul 2007 21:06:05 +0200
changeset 0 d6867274bc45
child 1 fc732b24c9ba
Initial.
dicts-src/install
src/META-INF/MANIFEST.MF
src/jdict/AskWord.java
src/jdict/DictIndex.java
src/jdict/Ekrano.java
src/jdict/Main.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dicts-src/install	Mon Jul 23 21:06:05 2007 +0200
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+for a in `ls *.index`; do
+    NAME=`basename $a .index`
+
+    split -b 32000 -d $NAME.index x-$NAME.index
+    mv x-$NAME.index* ../src/dicts
+
+    split -b 32000 -d $NAME.dict x-$NAME.dict
+    mv x-$NAME.dict* ../src/dicts
+
+done
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/META-INF/MANIFEST.MF	Mon Jul 23 21:06:05 2007 +0200
@@ -0,0 +1,6 @@
+MIDlet-1: jdict, , jdict.Main
+MIDlet-Name: JDict
+MIDlet-Vendor: viric
+MIDlet-Version: 0.1
+MicroEdition-Configuration: CLDC-1.0
+MicroEdition-Profile: MIDP-1.0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdict/AskWord.java	Mon Jul 23 21:06:05 2007 +0200
@@ -0,0 +1,57 @@
+package jdict;
+
+import javax.microedition.lcdui.*;
+import java.util.Vector;
+
+public class AskWord
+	implements CommandListener
+{
+	private Form myform;
+	private Command cmd_malantauxen;
+	private Command cmd_sercxi;
+    private TextField word;
+	
+	public AskWord()
+	{
+		show();
+	}
+
+	public void show()
+	{
+		myform = new Form("Info");
+
+        word = new TextField("Word:", "", 20, TextField.ANY);
+        myform.append(word);
+
+		/* Commands */
+		cmd_malantauxen = new Command("Enrera", Command.BACK, 0);
+		myform.addCommand(cmd_malantauxen);
+
+		cmd_sercxi = new Command("Serĉi", Command.BACK, 0);
+		myform.addCommand(cmd_sercxi);
+
+		myform.setCommandListener(this);
+		Main.display.setCurrent(myform);
+	}
+
+	public void commandAction(Command c, Displayable d)
+	{
+        if (c == cmd_sercxi)
+        {
+            /*
+            DictIndex index = new DictIndex("tokipona-cat");
+            */
+            DictIndex index = new DictIndex("alos-eo-ca");
+
+            Vector results;
+            results = index.SearchDefinition(word.getString());
+
+            for(int i=0; i < results.size(); ++i)
+            {
+                StringItem item = new StringItem("Result: ",
+                        (String) results.elementAt(i));
+                myform.append(item);
+            }
+        }
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdict/DictIndex.java	Mon Jul 23 21:06:05 2007 +0200
@@ -0,0 +1,354 @@
+package jdict;
+
+import java.io.*;
+import java.util.Vector;
+
+public class DictIndex
+{
+    InputStream in;
+    InputStream defs;
+
+    String index_basename;
+    String defs_basename;
+    int index_id;
+
+    public DictIndex(String dictname)
+    {
+        index_basename = "/dicts/x-" + dictname + ".index";
+        defs_basename = "/dicts/x-" + dictname + ".dict";
+        openNewIndex();
+    }
+
+    public void openNextIndex()
+    {
+        index_id += 1;
+        String name;
+        name = index_basename + int2twochar(index_id);
+        System.out.println("Opening dictionary: " + name);
+        in = getClass().getResourceAsStream(name);
+        if (in == null)
+            System.out.println("Wrong dictionary. Null stream.");
+    }
+
+    public void openNewIndex()
+    {
+        index_id = 0;
+        String name = index_basename + int2twochar(index_id);
+        System.out.println("Opening dictionary: " + name);
+        in = getClass().getResourceAsStream(name);
+        if (in == null)
+            System.out.println("Wrong dictionary. Null stream.");
+    }
+
+    public String int2twochar(int val)
+    {
+        String result;
+        if (val <= 9)
+            result =  "0" + new Integer(val).toString();
+        else
+            result = new Integer(val).toString();
+        return result;
+    }
+
+    public String getName()
+    {
+        byte tmp[] = new byte[50];
+        int i;
+
+        i = 0;
+        do
+        {
+            try {
+                int c = in.read();
+                if (c == -1)
+                {
+                    openNextIndex();
+                    if (in == null)
+                    {
+                        System.out.println("EOF all index files");
+                        return null;
+                    }
+                    c = in.read();
+                    if (c == -1)
+                    {
+                        System.out.println("EOF");
+                        return null;
+                    }
+                }
+                tmp[i] = (byte) c;
+            } catch (IOException e)
+            {
+                System.out.println("IO Exception: " +  e.getMessage());
+                return null;
+            }
+            i += 1;
+        } while (tmp[i-1] != '\t' /* tab */ && i <= 50);
+
+        if (i == 51)
+            return null;
+
+        return new String(tmp, 0, i-1);
+    }
+
+    public int getLength()
+    {
+        /* We reuse getOffset, as it breaks on \n too */
+        return getOffset();
+    }
+
+    public int getOffset()
+    {
+        byte tmp[] = new byte[50];
+        int i;
+
+        i = 0;
+        do
+        {
+            try {
+                int c = in.read();
+                if (c == -1)
+                {
+                    System.out.println("EOF");
+                    return -1;
+                }
+                tmp[i] = (byte) c;
+            } catch (IOException e)
+            {
+                System.out.println("IO Exception: " +  e.getMessage());
+                return -1;
+            }
+            i += 1;
+        } while (tmp[i-1] != '\t' /* tab */ && tmp[i-1] != '\n' && i <= 50);
+
+        if (i == 51)
+            return -1;
+
+        return IA5toNumber(tmp, i-1);
+    }
+
+    public void readUntilNewLine()
+    {
+        byte tmp;
+
+        do
+        {
+            try {
+                int c = in.read();
+                if (c == -1)
+                {
+                    System.out.println("EOF");
+                    return;
+                }
+                tmp = (byte) c;
+            } catch (IOException e)
+            {
+                System.out.println("IO Exception: " +  e.getMessage());
+                return;
+            }
+        } while (tmp != '\n');
+
+    }
+
+    public int IA5value(byte letter)
+    {
+        switch(letter)
+        {
+            case 'A': return 0;
+            case 'B': return 1;
+            case 'C': return 2;
+            case 'D': return 3;
+            case 'E': return 4;
+            case 'F': return 5;
+            case 'G': return 6;
+            case 'H': return 7;
+            case 'I': return 8;
+            case 'J': return 9;
+            case 'K': return 10;
+            case 'L': return 11;
+            case 'M': return 12;
+            case 'N': return 13;
+            case 'O': return 14;
+            case 'P': return 15;
+            case 'Q': return 16;
+            case 'R': return 17;
+            case 'S': return 18;
+            case 'T': return 19;
+            case 'U': return 20;
+            case 'V': return 21;
+            case 'W': return 22;
+            case 'X': return 23;
+            case 'Y': return 24;
+            case 'Z': return 25;
+            case 'a': return 26;
+            case 'b': return 27;
+            case 'c': return 28;
+            case 'd': return 29;
+            case 'e': return 30;
+            case 'f': return 31;
+            case 'g': return 32;
+            case 'h': return 33;
+            case 'i': return 34;
+            case 'j': return 35;
+            case 'k': return 36;
+            case 'l': return 37;
+            case 'm': return 38;
+            case 'n': return 39;
+            case 'o': return 40;
+            case 'p': return 41;
+            case 'q': return 42;
+            case 'r': return 43;
+            case 's': return 44;
+            case 't': return 45;
+            case 'u': return 46;
+            case 'v': return 47;
+            case 'w': return 48;
+            case 'x': return 49;
+            case 'y': return 50;
+            case 'z': return 51;
+            case '0': return 52;
+            case '1': return 53;
+            case '2': return 54;
+            case '3': return 55;
+            case '4': return 56;
+            case '5': return 57;
+            case '6': return 58;
+            case '7': return 59;
+            case '8': return 60;
+            case '9': return 61;
+            case '+': return 62;
+            case '/': return 63;
+            default:
+                      return 0;
+        }
+    }
+
+    public int IA5toNumber(byte array[], int length)
+    {
+        int i = 0;
+        int value = 0;
+
+        while (i < length)
+        {
+            System.out.println("Value1: " + new Integer(value).toString());
+            System.out.println("Array[i]: " + (char) array[i]);
+            System.out.println("IA5Value: " +
+                    new Integer(IA5value(array[i])).toString());
+
+            value = IA5value(array[i]) + value * 64;
+
+            System.out.println("Value2: " + new Integer(value).toString());
+            i += 1;
+        }
+        return value;
+    }
+
+    public String EntryToString()
+    {
+        String name = getName();
+        int offset = getOffset();
+        int length = getLength();
+
+        return new String(name + " " + new Integer(offset).toString() +
+                " " + new Integer(length).toString());
+    }
+
+    public boolean WordMatches(String w1, String w2)
+    {
+        /*System.out.println("Comparing " + w1 + " to " + w2);*/
+        if (w1.equals(w2))
+            return true;
+        return false;
+    }
+
+    public String getDefinition(int offset, int length)
+    {
+        byte text[] = new byte[length];
+        int file_id;
+        int array_offset;
+
+        /* Calc the id.
+         * The files are 32000 bytes long. Should divide with floor(). */
+        file_id = offset / 32000;
+
+        /* Open the file */
+        defs = getClass().getResourceAsStream(defs_basename +
+                int2twochar(file_id));
+
+        /* Relative offset */
+        offset = offset - file_id * 32000;
+
+        array_offset = 0;
+        try {
+            while (length > 0)
+            {
+                int end = offset + length;
+                if (end > 32000)
+                    end = 32000;
+                int toread = end - offset;
+                defs.skip(offset);
+                System.out.println("offset: " +
+                        new Integer(offset).toString() + 
+                        " length: " + 
+                        new Integer(length).toString() + 
+                        " end: " + 
+                        new Integer(end).toString() + 
+                        " toread: " + 
+                        new Integer(toread).toString() +
+                        " array_offset: " + 
+                        new Integer(array_offset).toString());
+                defs.read(text, array_offset, toread);
+                /* For all next reads */
+                offset = 0;
+                array_offset += toread;
+                length -= toread;
+            }
+        } catch (IOException e)
+        {
+            System.out.println("IO Exception on defs: " +  e.getMessage());
+            return null;
+        }
+
+        System.out.println("Definition: " + new String(text));
+
+        String result;
+        try {
+            result = new String(text, 0, text.length, "UTF-8");
+        } catch (UnsupportedEncodingException e)
+        {
+            System.out.println("Unsupported encoding.");
+            return null;
+        }
+
+        return result;
+    }
+
+    public Vector SearchDefinition(String word)
+    {
+        Vector results = new Vector();
+
+        String test;
+        do
+        {
+
+            test = getName();
+            if (test == null)
+                break;
+            if (WordMatches(word,test))
+            {
+                int offset = getOffset();
+                int length = getLength();
+                System.out.println("Definition for " + word + " at " +
+                        new Integer(offset).toString() +  " length " +
+                        new Integer(length).toString());
+                String definition = getDefinition(offset, length);
+
+                results.addElement(definition);
+            } else
+            {
+                readUntilNewLine();
+            }
+        } while (test != null);
+
+        return results;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdict/Ekrano.java	Mon Jul 23 21:06:05 2007 +0200
@@ -0,0 +1,6 @@
+package jdict;
+
+public interface Ekrano
+{
+	public void show();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdict/Main.java	Mon Jul 23 21:06:05 2007 +0200
@@ -0,0 +1,55 @@
+package jdict;
+
+import java.io.*;
+import javax.microedition.lcdui.*;
+import javax.microedition.midlet.MIDlet;
+
+public class Main
+	extends MIDlet
+{
+	/* Maximum 128 */
+	public static Display display;
+    private AskWord askword;
+
+	public Main() {
+		display = Display.getDisplay(this);
+	}
+
+
+	protected void startApp()
+	{
+        /*
+        myform = new Form("Info");
+        
+        InputStream in = getClass().
+            getResourceAsStream("/META-INF/MANIFEST.MF");
+
+        byte b[] = new byte[10];
+
+        try {
+        in.read(b);
+        } catch (IOException e)
+        {
+        }
+        StringItem sitem = new StringItem(null, new String(b));
+        myform.append(sitem);
+        */
+
+        askword = new AskWord();
+	}
+
+	protected void destroyApp(boolean c)
+	{
+		askword = null;
+	}
+
+	protected void pauseApp()
+	{
+	}
+
+	public void quit()
+	{
+		destroyApp(true);
+		notifyDestroyed();
+	}
+}