4 using System.Diagnostics;
5 using System.Collections.Specialized;
7 using ICSharpCode.SharpZipLib;
8 using ICSharpCode.SharpZipLib.Zip;
9 using ICSharpCode.SharpZipLib.Zip.Compression;
10 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
13 // using HtmlHelp.Storage;
15 namespace HtmlHelp.ChmDecoding
18 /// Enumeration for specifying the dumping compression
20 public enum DumpCompression
23 /// None - no data compression will be used.
24 /// Fastest but most memory intensive
28 /// Minimum - a minimum data compression will be used.
29 /// Fast but not much data reduction
33 /// Medium - a medium data compression will be used.
34 /// Slower but medium data reduction
38 /// Maximum - a maximum data compression will be used.
39 /// Slowest but maximum data reduction
45 /// Flags which specify which data should be dumped
48 public enum DumpingFlags
51 /// DumpTextTOC - if this flag is set, text-based TOCs (sitemap format) will be dumped
55 /// DumpBinaryTOC - if this flag is set, binary TOCs will be dumped
59 /// DumpTextIndex - if this flag is set, the text-based index (sitemap format) will be dumped
63 /// DumpBinaryIndex - if this flag is set, the binary index will be dumped
67 /// DumpStrings - if this flag is set, the internal #STRINGS file will be dumped
71 /// DumpUrlStr - if this flag is set, the internal #URLSTR file will be dumped
75 /// DumpUrlTbl - if this flag is set, the internal #URLTBL file will be dumped
79 /// DumpTopics - if this flag is set, the internal #TOPICS file will be dumped
83 /// DumpFullText - if this flag is set, the internal $FIftiMain file will be dumped
89 /// The class <c>DumpingInfo</c> implements information properties for the CHMFile class
90 /// if and how data dumping should be used.
92 public sealed class DumpingInfo
94 public bool m_bAllowSaveDump=true;
96 private readonly static BitVector32.Section DumpFlags = BitVector32.CreateSection(512);
98 private const string _dumpHeader = "HtmlHelpSystem dump file 1.0";
100 private string _outputDir = ""; // emtpy string means, same directory as chm file
101 private DumpCompression _compressionLevel = DumpCompression.Maximum;
102 private CHMFile _chmFile = null;
104 private DeflaterOutputStream _outputStream = null;
105 private InflaterInputStream _inputStream = null;
107 private BinaryWriter _writer = null;
108 private BinaryReader _reader = null;
110 private BitVector32 _flags;
113 /// Constructor of the class
115 /// <param name="flags">Combine flag values to specify which data should be dumped.</param>
116 /// <param name="outputDir">output directory. emtpy string means,
117 /// same directory as chm file (only if destination = ExternalFile)</param>
118 /// <param name="compressionLevel">compression which should be used</param>
119 public DumpingInfo(DumpingFlags flags, string outputDir, DumpCompression compressionLevel)
121 _flags = new BitVector32(0);
122 int i = _flags[DumpFlags];
123 _flags[DumpFlags] = i | (int)flags;
125 _outputDir = outputDir;
126 _compressionLevel = compressionLevel;
130 /// Gets the flag if text-based TOCs will be written to the dumping file
132 public bool DumpTextTOC
134 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpTextTOC) != 0); }
138 /// Gets the flag if binary TOCs will be written to the dumping file
140 public bool DumpBinaryTOC
142 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpBinaryTOC) != 0); }
146 /// Gets the flag if the text-based index will be written to the dumping file
148 public bool DumpTextIndex
150 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpTextIndex) != 0); }
154 /// Gets the flag if the binary index will be written to the dumping file
156 public bool DumpBinaryIndex
158 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpBinaryIndex) != 0); }
162 /// Gets the flag if the #STRINGS file will be written to the dumping file
164 public bool DumpStrings
166 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpStrings) != 0); }
170 /// Gets the flag if the #URLSTR file will be written to the dumping file
172 public bool DumpUrlStr
174 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpUrlStr) != 0); }
178 /// Gets the flag if the #URLTBL file will be written to the dumping file
180 public bool DumpUrlTbl
182 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpUrlTbl) != 0); }
186 /// Gets the flag if the #TOPICS file will be written to the dumping file
188 public bool DumpTopics
190 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpTopics) != 0); }
194 /// Gets the flag if the $FIftiMain file will be written to the dumping file
196 public bool DumpFullText
198 get { return ((_flags[DumpFlags] & (int)DumpingFlags.DumpFullText) != 0); }
202 /// Gets the dump output directory.
204 /// <value>emtpy string means, same directory as chm file</value>
205 /// <remarks>If Destination is set to DumpingOutput.InternalFile this property will be ignored</remarks>
206 public string OutputDir
208 get { return _outputDir; }
212 /// The compression level used.
214 public DumpCompression CompressionLevel
216 get { return _compressionLevel; }
220 /// Gets/Sets the CHMFile instance associated with this object
222 internal CHMFile ChmFile
224 get { return _chmFile; }
225 set { _chmFile = value; }
229 /// Translates the compression level to the deflater constants
235 switch(CompressionLevel)
237 case DumpCompression.None: return Deflater.NO_COMPRESSION;
238 case DumpCompression.Minimum: return Deflater.BEST_SPEED;
239 case DumpCompression.Medium: return Deflater.DEFAULT_COMPRESSION;
240 case DumpCompression.Maximum: return Deflater.BEST_COMPRESSION;
243 return Deflater.BEST_COMPRESSION;
248 /// Checks if a dump exists
250 internal bool DumpExists
254 if(_flags[DumpFlags] == 0)
257 // we have a reader or writer to the dump so it must exist
258 if( (_reader != null) || (_writer != null) )
261 string sDmpFile = _chmFile.ChmFilePath;
262 sDmpFile=sDmpFile.ToLower().Replace(".chm",".CHB");
264 return File.Exists(sDmpFile);
269 /// Gets a binary writer instance which allows you to write to the dump
271 internal BinaryWriter Writer
275 if (m_bAllowSaveDump==false)
278 if(_flags[DumpFlags] == 0)
279 throw new InvalidOperationException("Nothing to dump. No flags have been set !");
282 throw new InvalidOperationException("Can't write and read at the same time !");
285 throw new InvalidOperationException("Only usable with an associated CHMFile instance !");
289 string sDmpFile = _chmFile.ChmFilePath;
290 sDmpFile=sDmpFile.ToLower().Replace(".chm",".CHB");
291 StreamWriter stream = new StreamWriter(sDmpFile, false, _chmFile.TextEncoding);
293 // write header info uncompressed
294 BinaryWriter _hwriter = new BinaryWriter(stream.BaseStream);
295 _hwriter.Write(_dumpHeader);
296 _hwriter.Write((int)CompressionLevel);
298 if(_compressionLevel == DumpCompression.None)
300 _writer = new BinaryWriter(stream.BaseStream);
304 _outputStream = new DeflaterOutputStream(stream.BaseStream, new Deflater(CompLvl));
306 _writer = new BinaryWriter(_outputStream);
316 /// Gets a binary reader which allows you to read from the dump
318 internal BinaryReader Reader
323 throw new InvalidOperationException("Can't write and read at the same time !");
326 throw new InvalidOperationException("Only usable with an associated CHMFile instance !");
330 string sDmpFile = _chmFile.ChmFilePath;
331 sDmpFile=sDmpFile.ToLower().Replace(".chm",".CHB");
332 StreamReader stream = new StreamReader(sDmpFile, _chmFile.TextEncoding);
334 BinaryReader _hReader = new BinaryReader(stream.BaseStream);
335 string sH = _hReader.ReadString();
337 if(sH != _dumpHeader)
340 Debug.WriteLine("Unexpected dump-file header !");
341 throw new FormatException("DumpingInfo.Reader - Unexpected dump-file header !");
344 _compressionLevel = (DumpCompression)_hReader.ReadInt32();
345 // if(_compressionLevel != (DumpCompression)_hReader.ReadInt32())
351 if(_compressionLevel == DumpCompression.None)
353 _reader = new BinaryReader(stream.BaseStream);
357 _inputStream = new InflaterInputStream(stream.BaseStream, new Inflater());
359 _reader = new BinaryReader(_inputStream);
368 /// Saves data and closes the dump
370 /// <returns>true if succeed</returns>
371 internal bool SaveData()
373 if (m_bAllowSaveDump==false)
380 _outputStream = null;