Ejemplo n.º 1
0
        public string GetKeyValue(MdictHelper.Tuple <long, long, string> findKey)
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = RecordListStartPosition;

                long h = 0, start = 0, ofset = 0;
                var  findBlock = new MdictHelper.Tuple <long, long>(-1, -1);
                for (int kj = 0; kj < RecordBlockInfoList.Count; kj++)
                {
                    h     += RecordBlockInfoList[kj].Value2;
                    ofset += RecordBlockInfoList[kj].Value1;

                    if (h > findKey.Value1)
                    {
                        findBlock = RecordBlockInfoList[kj];
                        start     = RecordBlockInfoList[kj].Value2 - (h - findKey.Value1);

                        ofset -= RecordBlockInfoList[kj].Value1;

                        break;
                    }
                }

                mdictStream.BaseStream.Position += ofset;

                var findKeyBuffer = new byte[findBlock.Value1];
                mdictStream.BaseStream.Read(findKeyBuffer, 0, findKeyBuffer.Length);

                string findKeyCompressedText = BitConverter.ToString(findKeyBuffer).Replace("-", "");

                string findKeyDecompressedText = MdictHelper.UnZipZlibHexString(findKeyCompressedText.Substring(16));

                Encoding encoder = Headers["Encoding"] == "UTF-16" ? Encoding.Unicode : Encoding.UTF8;

                string findKeyText = encoder.GetString(MdictHelper.HexStringToByteArray(findKeyDecompressedText.Replace("-", "").Substring((int)start * 2, ((int)(findKey.Value2)) * 2))).Replace("\0", "");

                var pattern = new Regex(@"[`](?<id>\d*)[`]",
                                        RegexOptions.Compiled | RegexOptions.IgnoreCase);
                MatchCollection _q = pattern.Matches(findKeyText);
                foreach (Match qm in _q)
                {
                    int id = int.Parse(qm.Groups["id"].Value);
                    findKeyText = findKeyText.Replace("`" + id + "`", StyleSheets.Single(p => p.Key == id).Value);
                }

                mdictStream.Close();

                if (!findKeyText.StartsWith("@@@LINK="))
                {
                    return("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\" <html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" ><style type=\"text/css\">body{background-color:;color:;font-family:\"\";}</style></head><body>" + findKeyText + "</body></html>");
                }
                return(findKeyText);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public byte[] GetKeyValue(MdictHelper.Tuple <long, long, string> findKey)
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = RecordListStartPosition;

                long h = 0, start = 0, ofset = 0;
                var  findBlock = new MdictHelper.Tuple <long, long>(-1, -1);
                for (int kj = 0; kj < RecordBlockInfoList.Count; kj++)
                {
                    h     += RecordBlockInfoList[kj].Value2;
                    ofset += RecordBlockInfoList[kj].Value1;

                    if (h > findKey.Value1)
                    {
                        findBlock = RecordBlockInfoList[kj];
                        start     = RecordBlockInfoList[kj].Value2 - (h - findKey.Value1);

                        ofset -= RecordBlockInfoList[kj].Value1;

                        break;
                    }
                }

                mdictStream.BaseStream.Position += ofset;

                var findKeyBuffer = new byte[findBlock.Value1];
                mdictStream.BaseStream.Read(findKeyBuffer, 0, findKeyBuffer.Length);

                string findKeyCompressedText = BitConverter.ToString(findKeyBuffer).Replace("-", "");

                string findKeyDecompressedText = MdictHelper.UnZipZlibHexString(findKeyCompressedText.Substring(16));

                mdictStream.Close();

                return(MdictHelper.HexStringToByteArray(findKeyDecompressedText.Replace("-", "").Substring((int)start * 2, (int)findKey.Value2 * 2)));
            }
            catch
            {
                return(null);
            }
        }