-revert janderwalds change until because it breaks the gcc 4.x build
[reactos.git] / irc / TechBot / CHMLibrary / IndexItem.cs
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Collections;
5
6 using HtmlHelp.ChmDecoding;
7
8 namespace HtmlHelp
9 {
10 /// <summary>
11 /// The class <c>IndexItem</c> implements an help-index item
12 /// </summary>
13 public sealed class IndexItem : IComparable
14 {
15 /// <summary>
16 /// Internal member storing the keyword
17 /// </summary>
18 private string _keyWord = "";
19 /// <summary>
20 /// Internal member storing all associated information type strings
21 /// </summary>
22 private ArrayList _infoTypeStrings = new ArrayList();
23 /// <summary>
24 /// Internal member storing the flag if this is a see-also keyword
25 /// </summary>
26 private bool _isSeeAlso = false;
27 /// <summary>
28 /// Internal member storing the indent of the keyword
29 /// </summary>
30 private int _indent = 0;
31 /// <summary>
32 /// Internal member storing the last index of the keyword in the seperated list
33 /// </summary>
34 private int _charIndex = 0;
35 /// <summary>
36 /// Internal member storing the entry index
37 /// </summary>
38 private int _entryIndex = 0;
39 /// <summary>
40 /// Internal member storing an array of see-also values
41 /// </summary>
42 private string[] _seeAlso = new string[0];
43 /// <summary>
44 /// Internal member storing an array of topic offsets
45 /// </summary>
46 private int[] _nTopics = new int[0];
47 /// <summary>
48 /// Internal member storing the topics
49 /// </summary>
50 private ArrayList _Topics = null;
51 /// <summary>
52 /// Associated CHMFile instance
53 /// </summary>
54 private CHMFile _chmFile = null;
55 /// <summary>
56 /// Internal flag specifying the chm file path
57 /// </summary>
58 private string _chmFileName = "";
59
60 /// <summary>
61 /// Constructor of the class
62 /// </summary>
63 /// <param name="chmFile">associated CHMFile instance</param>
64 /// <param name="keyWord">keyword</param>
65 /// <param name="isSeeAlso">true if it is a see-also keyword</param>
66 /// <param name="indent">indent of the entry</param>
67 /// <param name="charIndex">char index of the last keyword in the separated list</param>
68 /// <param name="entryIndex">index of the entry</param>
69 /// <param name="seeAlsoValues">string array with see-also values</param>
70 /// <param name="topicOffsets">integer array with topic offsets</param>
71 internal IndexItem(CHMFile chmFile, string keyWord, bool isSeeAlso, int indent, int charIndex, int entryIndex, string[] seeAlsoValues, int[] topicOffsets)
72 {
73 _chmFile = chmFile;
74 _chmFileName = _chmFile.ChmFilePath;
75 _keyWord = keyWord;
76 _isSeeAlso = isSeeAlso;
77 _indent = indent;
78 _charIndex = charIndex;
79 _entryIndex = entryIndex;
80 _seeAlso = seeAlsoValues;
81 _nTopics = topicOffsets;
82 }
83
84 /// <summary>
85 /// Standard constructor
86 /// </summary>
87 public IndexItem()
88 {
89 }
90
91 #region Data dumping
92 /// <summary>
93 /// Dump the class data to a binary writer
94 /// </summary>
95 /// <param name="writer">writer to write the data</param>
96 /// <param name="writeFileName">true if the chm filename should be written</param>
97 internal void Dump(ref BinaryWriter writer, bool writeFileName)
98 {
99 int i=0;
100
101 writer.Write(_keyWord);
102 writer.Write(_isSeeAlso);
103 writer.Write(_indent);
104
105 if(writeFileName)
106 writer.Write(_chmFileName);
107
108 writer.Write(_infoTypeStrings.Count);
109
110 for(i=0; i<_infoTypeStrings.Count; i++)
111 writer.Write( (_infoTypeStrings[i]).ToString() );
112
113 writer.Write(_seeAlso.Length);
114
115 for(i=0; i<_seeAlso.Length; i++)
116 {
117 if(_seeAlso[i] == null)
118 writer.Write("");
119 else
120 writer.Write( _seeAlso[i] );
121 }
122
123 writer.Write(Topics.Count);
124
125 for(i=0; i<Topics.Count; i++)
126 {
127 IndexTopic topic = ((IndexTopic)(Topics[i]));
128 topic.Dump(ref writer);
129 }
130 }
131
132 /// <summary>
133 /// Dump the class data to a binary writer
134 /// </summary>
135 /// <param name="writer">writer to write the data</param>
136 internal void Dump(ref BinaryWriter writer)
137 {
138 Dump(ref writer, false);
139 }
140
141 /// <summary>
142 /// Reads the object data from a dump store
143 /// </summary>
144 /// <param name="reader">reader to read the data</param>
145 /// <param name="filesList">filelist from helpsystem</param>
146 internal bool ReadDump(ref BinaryReader reader, ArrayList filesList)
147 {
148 int i=0;
149 _keyWord = reader.ReadString();
150 _isSeeAlso = reader.ReadBoolean();
151 _indent = reader.ReadInt32();
152 _chmFileName = reader.ReadString();
153
154 foreach(CHMFile curFile in filesList)
155 {
156 if(curFile.ChmFilePath == _chmFileName)
157 {
158 _chmFile = curFile;
159 break;
160 }
161 }
162
163 if(_chmFile==null)
164 return false;
165
166 int nCnt = reader.ReadInt32();
167
168 for(i=0; i<nCnt; i++)
169 {
170 string sIT = reader.ReadString();
171 _infoTypeStrings.Add(sIT);
172 }
173
174 nCnt = reader.ReadInt32();
175
176 _seeAlso = new string[nCnt];
177
178 for(i=0; i<nCnt; i++)
179 {
180 _seeAlso[i] = reader.ReadString();
181 }
182
183 nCnt = reader.ReadInt32();
184
185 for(i=0; i<nCnt; i++)
186 {
187 IndexTopic topic = new IndexTopic("","","","");
188 topic.SetChmInfo( _chmFile.CompileFile, _chmFile.ChmFilePath);
189 topic.AssociatedFile = _chmFile;
190 topic.ReadDump(ref reader);
191
192 Topics.Add(topic);
193 }
194
195 return true;
196 }
197
198 /// <summary>
199 /// Reads the object data from a dump store
200 /// </summary>
201 /// <param name="reader">reader to read the data</param>
202 internal void ReadDump(ref BinaryReader reader)
203 {
204 int i=0;
205 _keyWord = reader.ReadString();
206 _isSeeAlso = reader.ReadBoolean();
207 _indent = reader.ReadInt32();
208
209 int nCnt = reader.ReadInt32();
210
211 for(i=0; i<nCnt; i++)
212 {
213 string sIT = reader.ReadString();
214 _infoTypeStrings.Add(sIT);
215 }
216
217 nCnt = reader.ReadInt32();
218
219 _seeAlso = new string[nCnt];
220
221 for(i=0; i<nCnt; i++)
222 {
223 _seeAlso[i] = reader.ReadString();
224 }
225
226 nCnt = reader.ReadInt32();
227
228 for(i=0; i<nCnt; i++)
229 {
230 IndexTopic topic = new IndexTopic("","","","");
231 topic.AssociatedFile = _chmFile;
232 topic.SetChmInfo( _chmFile.CompileFile, _chmFile.ChmFilePath);
233 topic.ReadDump(ref reader);
234 Topics.Add(topic);
235 }
236 }
237 #endregion
238
239 /// <summary>
240 /// Implements the compareto method which allows sorting.
241 /// </summary>
242 /// <param name="obj">object to compare to</param>
243 /// <returns>See <see cref="System.IComparable">IComparable.CompareTo()</see></returns>
244 public int CompareTo(object obj)
245 {
246 if( obj.GetType() == this.GetType() )
247 {
248 IndexItem cmp = (IndexItem)obj;
249
250 return this.KeyWordPath.CompareTo( cmp.KeyWordPath );
251 }
252
253 return 0;
254 }
255
256 /// <summary>
257 /// Gets/Sets the associated CHMFile instance
258 /// </summary>
259 internal CHMFile ChmFile
260 {
261 get { return _chmFile; }
262 set { _chmFile = value; }
263 }
264
265 /// <summary>
266 /// Gets the ArrayList which holds all information types/categories this item is associated
267 /// </summary>
268 internal ArrayList InfoTypeStrings
269 {
270 get { return _infoTypeStrings; }
271 }
272
273 /// <summary>
274 /// Adds a see-also string to the index item and marks it as see also item
275 /// </summary>
276 /// <param name="seeAlsoString">see also string to add</param>
277 internal void AddSeeAlso(string seeAlsoString)
278 {
279 string[] seeAlso = new string[ _seeAlso.Length +1 ];
280 for(int i=0; i<_seeAlso.Length; i++)
281 seeAlso[i] = _seeAlso[i];
282
283 seeAlso[_seeAlso.Length] = seeAlsoString;
284 _seeAlso = seeAlso;
285 _isSeeAlso = true;
286 }
287
288 /// <summary>
289 /// Gets/Sets the full keyword-path of this item ( ", " separated list)
290 /// </summary>
291 public string KeyWordPath
292 {
293 get { return _keyWord; }
294 set { _keyWord = value; }
295 }
296
297 /// <summary>
298 /// Gets the keyword of this item
299 /// </summary>
300 public string KeyWord
301 {
302 get
303 {
304 return _keyWord.Substring(_charIndex, _keyWord.Length-_charIndex);
305 }
306 }
307
308 /// <summary>
309 /// Gets the keyword of this item with prefixing indent spaces
310 /// </summary>
311 public string IndentKeyWord
312 {
313 get
314 {
315 string sKW = this.KeyWord;
316 StringBuilder sb = new StringBuilder("",this.Indent*3 + sKW.Length);
317 for(int i=0; i<this.Indent; i++)
318 sb.Append(" ");
319 sb.Append(sKW);
320 return sb.ToString();
321 }
322 }
323
324 /// <summary>
325 /// Gets/Sets the see-also flag of this item
326 /// </summary>
327 public bool IsSeeAlso
328 {
329 get { return _isSeeAlso; }
330 set { _isSeeAlso = value; }
331 }
332
333 /// <summary>
334 /// Gets/Sets the listbox indent for this item
335 /// </summary>
336 public int Indent
337 {
338 get { return _indent; }
339 set { _indent = value; }
340 }
341
342 /// <summary>
343 /// Gets/Sets the character index of an indent keyword
344 /// </summary>
345 public int CharIndex
346 {
347 get { return _charIndex; }
348 set { _charIndex = value; }
349 }
350
351 /// <summary>
352 /// Gets the see-also values of this item
353 /// </summary>
354 public string[] SeeAlso
355 {
356 get { return _seeAlso; }
357 }
358
359 /// <summary>
360 /// Gets an array with the associated topics
361 /// </summary>
362 public ArrayList Topics
363 {
364 get
365 {
366 if( _Topics == null )
367 {
368 if(IsSeeAlso)
369 {
370 _Topics = new ArrayList();
371 }
372 else
373 {
374 if( (_chmFile != null) && (_chmFile.TopicsFile != null) )
375 {
376 _Topics = new ArrayList();
377
378 for(int i=0; i<_nTopics.Length; i++)
379 {
380 IndexTopic newTopic = IndexTopic.FromTopicEntry((TopicEntry)_chmFile.TopicsFile.TopicTable[ _nTopics[i] ]);
381 newTopic.AssociatedFile = _chmFile;
382 _Topics.Add( newTopic );
383 }
384 }
385 else
386 {
387 _Topics = new ArrayList();
388 }
389 }
390 }
391
392 return _Topics;
393 }
394 }
395 }
396 }