Inheritance: DictionaryMatcher
Example #1
0
        public static DictionaryMatcher LoadData(string filename)
        {
            DictionaryMatcher dicMatcher = null;

            //from ICUBinary.java
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                //file header
                //--------------------------------------------------------------
                BinaryReader reader     = new BinaryReader(fs);
                ushort       headerSize = reader.ReadUInt16(); //0,1
                byte         magic1     = reader.ReadByte();   //2
                byte         magic2     = reader.ReadByte();   //3
                if (magic1 != MAGIC1 && magic2 != MAGIC2)
                {
                    throw new Exception("MAGIC_NUMBER_AUTHENTICATION_FAILED_");
                }
                ushort sizeofUDataInfo = reader.ReadUInt16(); //4,5
                reader.ReadUInt16();                          //6,7
                //
                byte isBigEndian   = reader.ReadByte();       //8
                byte charsetFamily = reader.ReadByte();       //9
                byte sizeofUChar   = reader.ReadByte();       //10
                if (isBigEndian < 0 || 1 < isBigEndian ||
                    charsetFamily != CHAR_SET_ || sizeofUChar != CHAR_SIZE_)
                {
                    throw new IOException("HEADER_AUTHENTICATION_FAILED_");
                }
                if (sizeofUDataInfo < 20 || headerSize < (sizeofUDataInfo + 4))
                {
                    throw new IOException("Internal Error: Header size error");
                }
                reader.ReadByte();//11

                byte[] fourBytesBuffer = new byte[4];
                reader.Read(fourBytesBuffer, 0, 4); //12,13,14,15
                int file_dataFormat =
                    (fourBytesBuffer[0] << 24) |
                    (fourBytesBuffer[1] << 16) |
                    (fourBytesBuffer[2] << 8) |
                    (fourBytesBuffer[3]);
                if (file_dataFormat != DATA_FORMAT_ID)
                {
                    throw new IOException("HEADER_AUTHENTICATION_FAILED_");
                }

                //format version
                reader.Read(fourBytesBuffer, 0, 4);//16,17,18,19


                //must = 20
                //
                //data version
                reader.Read(fourBytesBuffer, 0, 4);
                int data_version =
                    (fourBytesBuffer[0] << 24) |
                    (fourBytesBuffer[1] << 16) |
                    (fourBytesBuffer[2] << 8) |
                    (fourBytesBuffer[3]);
                //--------------------------------------------------------------
                reader.BaseStream.Position = headerSize;
                //body
                int[] indexes = new int[IX_COUNT];
                for (int i = 0; i < IX_COUNT; ++i)
                {
                    indexes[i] = reader.ReadInt32();
                }

                int offset = indexes[IX_STRING_TRIE_OFFSET];

                if (offset >= 4 * IX_COUNT)
                {
                    //must true
                }

                if (offset > (4 * IX_COUNT))
                {
                    int diff = offset - (4 * IX_COUNT);
                    //ICUBinary.skipBytes(bytes, diff);
                    throw new NotSupportedException();
                }

                int trieType  = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
                int totalSize = indexes[IX_TOTAL_SIZE] - offset;

                switch (trieType)
                {
                default: throw new NotSupportedException();

                case TRIE_TYPE_BYTES:
                {
                    int    transform = indexes[IX_TRANSFORM];
                    byte[] data      = new byte[totalSize];
                    reader.Read(data, 0, totalSize);
                    if ((transform & DictionaryData.TRANSFORM_TYPE_MASK) == DictionaryData.TRANSFORM_TYPE_OFFSET)
                    {        //must true
                    }
                    BytesDictionaryMatcher byteDicMatcher = new BytesDictionaryMatcher(data, transform);
                    byteDicMatcher.Tx2('ก');
                    dicMatcher = byteDicMatcher;
                } break;

                case TRIE_TYPE_UCHARS:
                {
                    throw new NotSupportedException();
                } break;
                }
                reader.Close();
            }
            return(dicMatcher);
        }
Example #2
0
        public static DictionaryMatcher LoadData(string filename)
        {
            DictionaryMatcher dicMatcher = null;

            //from ICUBinary.java
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                //file header
                //--------------------------------------------------------------
                BinaryReader reader = new BinaryReader(fs);
                ushort headerSize = reader.ReadUInt16();//0,1 
                byte magic1 = reader.ReadByte();//2
                byte magic2 = reader.ReadByte();//3
                if (magic1 != MAGIC1 && magic2 != MAGIC2)
                {
                    throw new Exception("MAGIC_NUMBER_AUTHENTICATION_FAILED_");
                }
                ushort sizeofUDataInfo = reader.ReadUInt16();//4,5                 
                reader.ReadUInt16(); //6,7
                //
                byte isBigEndian = reader.ReadByte();//8
                byte charsetFamily = reader.ReadByte();//9
                byte sizeofUChar = reader.ReadByte(); //10
                if (isBigEndian < 0 || 1 < isBigEndian ||
                        charsetFamily != CHAR_SET_ || sizeofUChar != CHAR_SIZE_)
                {
                    throw new IOException("HEADER_AUTHENTICATION_FAILED_");
                }
                if (sizeofUDataInfo < 20 || headerSize < (sizeofUDataInfo + 4))
                {
                    throw new IOException("Internal Error: Header size error");
                }
                reader.ReadByte();//11

                byte[] fourBytesBuffer = new byte[4];
                reader.Read(fourBytesBuffer, 0, 4); //12,13,14,15
                int file_dataFormat =
                    (fourBytesBuffer[0] << 24) |
                    (fourBytesBuffer[1] << 16) |
                    (fourBytesBuffer[2] << 8) |
                    (fourBytesBuffer[3]);
                if (file_dataFormat != DATA_FORMAT_ID)
                {
                    throw new IOException("HEADER_AUTHENTICATION_FAILED_");
                }

                //format version
                reader.Read(fourBytesBuffer, 0, 4);//16,17,18,19


                //must = 20 
                //
                //data version
                reader.Read(fourBytesBuffer, 0, 4);
                int data_version =
                    (fourBytesBuffer[0] << 24) |
                    (fourBytesBuffer[1] << 16) |
                    (fourBytesBuffer[2] << 8) |
                    (fourBytesBuffer[3]);
                //-------------------------------------------------------------- 
                reader.BaseStream.Position = headerSize;
                //body
                int[] indexes = new int[IX_COUNT];
                for (int i = 0; i < IX_COUNT; ++i)
                {
                    indexes[i] = reader.ReadInt32();
                }

                int offset = indexes[IX_STRING_TRIE_OFFSET];

                if (offset >= 4 * IX_COUNT)
                {
                    //must true

                }

                if (offset > (4 * IX_COUNT))
                {
                    int diff = offset - (4 * IX_COUNT);
                    //ICUBinary.skipBytes(bytes, diff);
                    throw new NotSupportedException();
                }

                int trieType = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
                int totalSize = indexes[IX_TOTAL_SIZE] - offset;

                switch (trieType)
                {
                    default: throw new NotSupportedException();
                    case TRIE_TYPE_BYTES:
                        {
                            int transform = indexes[IX_TRANSFORM];
                            byte[] data = new byte[totalSize];
                            reader.Read(data, 0, totalSize);
                            if ((transform & DictionaryData.TRANSFORM_TYPE_MASK) == DictionaryData.TRANSFORM_TYPE_OFFSET)
                            {//must true
                            }
                            BytesDictionaryMatcher byteDicMatcher = new BytesDictionaryMatcher(data, transform);
                            byteDicMatcher.Tx2('ก');
                            dicMatcher = byteDicMatcher;
                        } break;
                    case TRIE_TYPE_UCHARS:
                        {
                            throw new NotSupportedException();
                        } break;

                }
                reader.Close();
            }
            return dicMatcher;
        }