stardict字典文件格式识别类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace cn.Laiyunqing
{
public class nStarDict
{
private string idxFile;
private string dictFile;

public nStarDict( string idx, string dict)
{
this .idxFile = idx;
this .dictFile = dict;
}

public List < string > Words( string key)
{
List
< string > words = new List < string > ();
FileStreamfsIdx
= new FileStream( this .idxFile,FileMode.Open,FileAccess.Read);
BinaryReaderbrIdx
= new BinaryReader(fsIdx,Encoding.UTF8);
byte []ch = new byte [ 4096 ];
byte end = ( byte ) ' ' ;
int index = 0 ;
for (index = 0 ;index < 4096 ;index ++ )
ch[index]
= ( byte ) ' ' ;
index
= 0 ;
while ( true )
{
try
{
ch[index]
= brIdx.ReadByte();
index
++ ;
}
catch (EndOfStreamException)
{
break ;
}
if (ch[index - 1 ] == end)
{
byte []word = new byte [index - 1 ];
for ( int i = 0 ;i < index - 1 ;i ++ )
{
word[i]
= ch[i];
ch[i]
= 0 ;
}
string queryString = Encoding.UTF8.GetString(word);
if (queryString.StartsWith(key, true ,System.Globalization.CultureInfo.CurrentCulture))
{
words.Add(queryString);
}
}
}
brIdx.Close();
fsIdx.Close();
return words;
}

public string Result( string originalText)
{
string translatedText = string .Empty;
FileStreamfsIdx
= new FileStream( this .idxFile,FileMode.Open,FileAccess.Read);
BinaryReaderbrIdx
= new BinaryReader(fsIdx,Encoding.UTF8);
FileStreamfsDict
= new FileStream( this .dictFile,FileMode.Open,FileAccess.Read);
BinaryReaderbrDict
= new BinaryReader(fsDict,Encoding.UTF8);
byte []ch = new byte [ 4096 ];
byte end = ( byte ) ' ' ;
int pos,size;
int index = 0 ;
for (index = 0 ;index < 4096 ;index ++ )
ch[index]
= ( byte ) ' ' ;
index
= 0 ;
while ( true )
{
try
{
ch[index]
= brIdx.ReadByte();
index
++ ;
}
catch (EndOfStreamException)
{
break ;
}
if (ch[index - 1 ] == end)
{
byte []word = new byte [index - 1 ];
for ( int i = 0 ;i < index - 1 ;i ++ )
{
word[i]
= ch[i];
ch[i]
= 0 ;
}
string queryString = Encoding.UTF8.GetString(word);

index
= 0 ;
byte []tmp = brIdx.ReadBytes( 4 );
pos
= tmp[ 3 ] + (tmp[ 2 ] << 8 ) + (tmp[ 1 ] << 16 ) + (tmp[ 0 ] << 24 );
tmp
= brIdx.ReadBytes( 4 );
size
= tmp[ 3 ] + (tmp[ 2 ] << 8 ) + (tmp[ 1 ] << 16 ) + (tmp[ 0 ] << 24 );
byte []content = brDict.ReadBytes(size);
string outText = Encoding.UTF8.GetString(content).Replace( " " , " </br> " ) + " </br> " ;
// 是查询的单词
if (queryString == originalText)
{
translatedText
= outText;
break ;
}
}
}
brDict.Close();
fsDict.Close();
brIdx.Close();
fsIdx.Close();
return translatedText;
}

}
}

你可能感兴趣的:(LINQ)