File size: 6,729 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlobViewer.Common;

namespace SlobViewer.StarDict
{
  public class StarDictionaryInMemory : IWordDictionary
  {
    private string _fileNameOfIfoFile;
    private FileStream _dictFile;
    private FileStream _idxFile;
    private string _version;
    private int _wordCount;
    private int _idxFileSize;
    private string _sametypesequence;

    private Dictionary<string, (uint Offset, uint Size)> _keyDictionary;


    private Dictionary<string, string> _keyValueDictionary;

    public string FileName { get => _fileNameOfIfoFile; set => _fileNameOfIfoFile = value; }

    private string[] _references;



    public StarDictionaryInMemory()
    {

    }


    public void Open(string fileNameOfIfoFile)
    {
      _fileNameOfIfoFile = fileNameOfIfoFile;

      var baseName = Path.Combine(Path.GetDirectoryName(fileNameOfIfoFile), Path.GetFileNameWithoutExtension(fileNameOfIfoFile));


      if (File.Exists(baseName + ".dict"))
        _dictFile = new FileStream(baseName + ".dict", FileMode.Open, FileAccess.Read, FileShare.Read);
      else if (File.Exists(baseName + ".dict.dz"))
        _dictFile = new FileStream(baseName + ".dict.dz", FileMode.Open, FileAccess.Read, FileShare.Read);


      if (File.Exists(baseName + ".idx"))
        _idxFile = new FileStream(baseName + ".idx", FileMode.Open, FileAccess.Read, FileShare.Read);
      else if (File.Exists(baseName + "idx.gz"))
        _idxFile = new FileStream(baseName + ".idx.gz", FileMode.Open, FileAccess.Read, FileShare.Read);

      if (null == _dictFile)
        throw new InvalidDataException($"No dictionary file found");
      if (null == _idxFile)
        throw new InvalidDataException($"No idx file found");
      ReadIfoFile(fileNameOfIfoFile);
      ReadIdxFile();
      ReadDictFile();
    }

    private void ReadIfoFile(string fileNameOfIfoFile)
    {
      string line;
      using (var srr = new StreamReader(fileNameOfIfoFile))
      {
        while (null != (line = srr.ReadLine()))
        {
          var idx = line.IndexOf("=");
          if (idx < 0)
            continue;

          var key = line.Substring(0, idx).Trim().ToLowerInvariant();
          var val = line.Substring(idx + 1, line.Length - idx - 1).Trim();

          switch (key)
          {
            case "version":
              _version = val;
              break;
            case "wordcount":
              _wordCount = int.Parse(val, System.Globalization.CultureInfo.InvariantCulture);
              break;
            case "idxfilesize":
              _idxFileSize = int.Parse(val, System.Globalization.CultureInfo.InvariantCulture);
              break;
            case "sametypesequence":
              _sametypesequence = val;
              break;
          }
        }
      }
    }

    private void ReadIdxFile()
    {
      byte[] buffer;

      if (_idxFile is FileStream fs && fs.Name.ToLowerInvariant().EndsWith(".gz"))
      {

        using (Stream inStream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(_idxFile))
        {
          using (MemoryStream outStream = new MemoryStream())
          {
            inStream.CopyTo(outStream);
            buffer = outStream.ToArray();
          }
        }
      }
      else
      {
        buffer = new byte[_idxFile.Length];
        _idxFile.Read(buffer, 0, buffer.Length);
      }

      _idxFile.Close();
      _keyDictionary = ReadIdxBuffer(buffer);
      _references = _keyDictionary.Keys.ToArray();
      Array.Sort(_references);
    }

    private Dictionary<string, (uint Offset, uint Size)> ReadIdxBuffer(byte[] buffer)
    {
      // each buffer entry consists
      // i) of an zero terminated UTF-8 string
      // ii) of the data offset
      // iii) of the data size

      var dict = new Dictionary<string, (uint Offset, uint Size)>();

      var enc = new UTF8Encoding();

      int wordStart = 0;
      while (buffer[wordStart] < 0x20) wordStart++; // Skip chars at the beginning

      for (; wordStart < buffer.Length;)
      {
        int wordEnd;

        wordEnd = wordStart;
        while (buffer[wordEnd] != 0) wordEnd++;
        string key = enc.GetString(buffer, wordStart, wordEnd - wordStart);
        key = key.TrimStart();
        uint offset = BigEndianBitConverter.ToUInt32(buffer, wordEnd + 1);
        uint length = BigEndianBitConverter.ToUInt32(buffer, wordEnd + 5);

        dict[key] = (offset, length);

        wordStart = wordEnd + 9;
      }

      return dict;
    }

    private void ReadDictFile()
    {
      byte[] buffer;

      if (_dictFile is FileStream fs && fs.Name.ToLowerInvariant().EndsWith(".dz"))
      {
        using (Stream inStream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(_dictFile))
        {
          using (MemoryStream outStream = new MemoryStream())
          {
            inStream.CopyTo(outStream);
            buffer = outStream.ToArray();
          }
        }
      }
      else
      {
        buffer = new byte[_idxFile.Length];
        _idxFile.Read(buffer, 0, buffer.Length);
      }

      var enc = new UTF8Encoding();

      _keyValueDictionary = new Dictionary<string, string>();

      foreach (var entry in _keyDictionary)
      {
        var s = enc.GetString(buffer, (int)entry.Value.Offset, (int)entry.Value.Size);
        _keyValueDictionary[entry.Key] = s;
      }

      _keyDictionary = null; // not of use anymore
    }

    public string[] GetKeys()
    {
      return _references;
    }

    public string[] GetKeys(string key, int count)
    {
      int i;
      for (i = 0; i < _references.Length; ++i)
      {
        if (0 >= string.Compare(key, _references[i]))
          break;
      }

      if (i == _references.Length)
        return null;

      var len = Math.Min(count, _references.Length - i);

      var result = new string[len];

      for (int k = 0; k < len; ++k)
        result[k] = _references[i + k];

      return result;
    }

    public bool TryGetValue(string key, out (string Content, string ContentId) value)
    {
      if (_keyValueDictionary.TryGetValue(key, out var content))
      {
        value = (content, "text/html");
        return true;
      }
      else
      {
        value = (null, null);
        return false;
      }
    }

    public (string Content, string ContentId) this[string key]
    {
      get
      {
        if (TryGetValue(key, out var value))
          return value;
        else
          return (null, null);
      }
    }
  }
}