예제 #1
0
        public unsafe void Open(BinaryReader reader)
        {
            uint magic = reader.ReadUInt32();

            //CanSeekの時のみストリーム長のチェック
            if (reader.BaseStream.CanSeek && reader.BaseStream.Length != (magic ^ DictionaryMagicID))
            {
                throw new MeCabInvalidFileException("dictionary file is broken", this.FileName);
            }

            this.Version = reader.ReadUInt32();
            if (this.Version != DicVersion)
            {
                throw new MeCabInvalidFileException("incompatible version", this.FileName);
            }

            this.Type    = (DictionaryType)reader.ReadUInt32();
            this.LexSize = reader.ReadUInt32();
            this.LSize   = reader.ReadUInt32();
            this.RSize   = reader.ReadUInt32();
            uint dSize = reader.ReadUInt32();
            uint tSize = reader.ReadUInt32();
            uint fSize = reader.ReadUInt32();

            reader.ReadUInt32(); //dummy

            string charSet = StrUtils.GetString(reader.ReadBytes(32), Encoding.ASCII);

            this.encoding = StrUtils.GetEncoding(charSet);

            this.da.Open(reader, dSize);

            this.tokens = new Token[tSize / sizeof(Token)];
            for (int i = 0; i < this.tokens.Length; i++)
            {
                this.tokens[i] = Token.Create(reader);
            }

            this.features = reader.ReadBytes((int)fSize);

            if (reader.BaseStream.ReadByte() != -1)
            {
                throw new MeCabInvalidFileException("dictionary file is broken", this.FileName);
            }
        }
예제 #2
0
        public void Open(MemoryMappedFile mmf, string filePath = null)
        {
            this.FileName = filePath;

            using (MemoryMappedViewStream stream = mmf.CreateViewStream(
                       0L, 0L, MemoryMappedFileAccess.Read))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    uint magic = reader.ReadUInt32();
                    if (stream.CanSeek && stream.Length < (magic ^ DictionaryMagicID)) //正確なサイズ取得ができないので不等号で代用
                    {
                        throw new MeCabInvalidFileException("dictionary file is broken", filePath);
                    }

                    this.Version = reader.ReadUInt32();
                    if (this.Version != DicVersion)
                    {
                        throw new MeCabInvalidFileException("incompatible version", filePath);
                    }

                    this.Type    = (DictionaryType)reader.ReadUInt32();
                    this.LexSize = reader.ReadUInt32();
                    this.LSize   = reader.ReadUInt32();
                    this.RSize   = reader.ReadUInt32();
                    uint dSize = reader.ReadUInt32();
                    uint tSize = reader.ReadUInt32();
                    uint fSize = reader.ReadUInt32();
                    reader.ReadUInt32(); //dummy

                    string charSet = StrUtils.GetString(reader.ReadBytes(32), Encoding.ASCII);
                    this.encoding = StrUtils.GetEncoding(charSet);

                    long offset = stream.Position;
                    this.da.Open(mmf, offset, dSize);
                    offset       += dSize;
                    this.tokens   = mmf.CreateViewAccessor(offset, tSize, MemoryMappedFileAccess.Read);
                    offset       += tSize;
                    this.features = mmf.CreateViewAccessor(offset, fSize, MemoryMappedFileAccess.Read);
                }
        }