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); } }
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); } }
public void Open(BinaryReader reader, string fileName = null) { uint cSize = reader.ReadUInt32(); if (reader.BaseStream.CanSeek) { long fSize = sizeof(uint) + 32 * cSize + sizeof(uint) * charInfoList.Length; if (reader.BaseStream.Length != fSize) { throw new MeCabInvalidFileException("invalid file size", fileName); } } this.cList = new string[cSize]; for (int i = 0; i < this.cList.Length; i++) { this.cList[i] = StrUtils.GetString(reader.ReadBytes(32), Encoding.ASCII); } for (int i = 0; i < this.charInfoList.Length; i++) { this.charInfoList[i] = new CharInfo(reader.ReadUInt32()); } }
/// <summary> /// バイト配列の中から終端が\0で表された文字列を取り出す。 /// </summary> /// <remarks> /// バイト配列の長さはInt32.MaxValueを超えていても良い。 /// </remarks> /// <param name="bytes">バイト配列</param> /// <param name="offset">オフセット位置</param> /// <param name="enc">文字エンコーディング</param> /// <returns>文字列(\0は含まない)</returns> public unsafe static string GetString(byte[] bytes, long offset, Encoding enc) { fixed(byte *pBytes = bytes) return(StrUtils.GetString(pBytes + offset, enc)); }
/// <summary> /// バイト配列の中から終端が\0で表された文字列を取り出す。 /// </summary> /// <remarks> /// バイト配列の長さはInt32.MaxValueを超えていても良い。 /// </remarks> /// <param name="bytes">バイト配列</param> /// <param name="enc">文字エンコーディング</param> /// <returns>文字列(\0は含まない)</returns> public static string GetString(byte[] bytes, Encoding enc) { return(StrUtils.GetString(bytes, 0L, enc)); }
public string GetFeature(uint featurePos) { return(StrUtils.GetString(this.features, (long)featurePos, this.encoding)); }