Exemple #1
0
        private string FindFile(HelpDatabase referrer, string fileName)
        {
            if (referrer == null)
            {
                throw new ArgumentNullException(nameof(referrer));
            }
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            foreach (string searchPath in GetSearchPaths(referrer))
            {
                string[] fileNames = Directory.GetFiles(
                    searchPath, fileName, SearchOption.AllDirectories);
                foreach (string filePath in fileNames)
                {
                    if (!this.databaseFileNames.ContainsValue(
                            filePath.ToLowerInvariant()))
                    {
                        return(filePath);
                    }
                }
            }
            return(null);
        }
Exemple #2
0
        static string GetDatabasePath(HelpDatabase database)
        {
            string path = database.Name.Replace('.', '_');

            Directory.CreateDirectory(path);
            return(path);
        }
Exemple #3
0
        public void RemoveDatabase(HelpDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            for (int i = history.Count - 1; i >= 0; i--)
            {
                if (history[i].Database == database)
                {
                    history.RemoveAt(i);
                }
            }

            if (database == this.ActiveDatabase)
            {
                this.ActiveDatabase = null;
            }

            databaseFileNames.Remove(database);
            system.Databases.Remove(database);
            if (DatabaseRemoved != null)
            {
                DatabaseRemoved(this, null);
            }
        }
 public IEnumerable <HelpDatabase> LoadDatabases(string fileName)
 {
     using (FileStream stream = File.OpenRead(fileName))
     {
         while (stream.Position < stream.Length)
         {
             HelpDatabase database = DeserializeDatabase(stream);
             yield return(database);
         }
     }
 }
Exemple #5
0
        private IEnumerable <string> GetSearchPaths(HelpDatabase referrer)
        {
            string path      = this.databaseFileNames[referrer];
            int    maxLevels = 2;

            for (int level = 0; level < maxLevels; level++)
            {
                path = Path.GetDirectoryName(path);
                if (string.IsNullOrEmpty(path))
                {
                    break;
                }
                yield return(path);
            }
        }
Exemple #6
0
        public void AddDatabase(HelpDatabase database, string fileName)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            system.Databases.Add(database);
            databaseFileNames[database] = Path.GetFullPath(fileName).ToLowerInvariant();
            if (DatabaseAdded != null)
            {
                DatabaseAdded(this, null);
            }
            if (this.ActiveDatabase == null)
            {
                this.ActiveDatabase = database;
            }
        }
Exemple #7
0
        public static HelpTopic GetDefaultTopicOfDatabase(HelpDatabase database)
        {
            if (database == null)
            {
                return(null);
            }

            HelpTopic topic = database.ResolveContext("h.contents");

            if (topic != null)
            {
                return(topic);
            }

            if (database.Topics.Count > 0)
            {
                return(database.Topics[0]);
            }

            return(null);
        }
        /// <summary>
        /// Deserializes the next help database from a binary reader.
        /// </summary>
        /// <remarks>
        /// This method throws an exception if it encounters an irrecoverable
        /// error in the input, such as an IO error or malformed input in the
        /// meta data. It raises an <c>InvalidTopicData</c> event for each
        /// format error it encounters during topic deserialization.
        /// </remarks>
        public HelpDatabase DeserializeDatabase(
            Stream stream, SerializationOptions options)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (options == null)
            {
                options = new SerializationOptions();
            }

            CheckSignature(stream);

            BinaryHelpFileHeader header  = ReadFileHeader(stream);
            bool         isCaseSensitive = (header.Attributes & HelpFileAttributes.CaseSensitive) != 0;
            HelpDatabase database        = new HelpDatabase(header.DatabaseName, isCaseSensitive);

            options.ControlCharacter = Graphic437.GetChars(new byte[] { header.ControlCharacter })[0];

            using (Stream streamView = new StreamView(stream, header.DatabaseSize, 0x46))
                using (BinaryReader reader = new BinaryReader(streamView))
                {
                    int[] topicOffsets = ReadTopicIndex(reader, header);

                    // Read Context Strings and Context Map sections.
                    if (true)
                    {
                        string[] contextStrings = ReadContextStrings(reader, header);
                        UInt16[] contextMap     = ReadContextMap(reader, header);
                        for (int i = 0; i < header.ContextCount; i++)
                        {
                            database.AddContext(contextStrings[i], contextMap[i]);
                        }
                    }

                    // Read Keywords section.
                    if (header.KeywordsOffset > 0)
                    {
                        options.Keywords     = ReadKeywords(reader, header);
                        options.Compression |= CompressionFlags.Keyword;
                        options.Compression |= CompressionFlags.ExtendedKeyword;
                    }
                    else
                    {
                        options.Keywords = null;
                    }

                    // Read Huffman Tree section.
                    if (header.HuffmanTreeOffset > 0)
                    {
                        options.HuffmanTree = ReadHuffmanTree(reader, header);
                        // file.HuffmanTree.Dump();
                        options.Compression |= CompressionFlags.Huffman;
                    }
                    else
                    {
                        options.HuffmanTree = null;
                    }

                    // Read topic data.
                    if (reader.BaseStream.Position != header.TopicTextOffset)
                    {
                        throw new InvalidDataException("Incorrect topic position.");
                    }
                    for (int i = 0; i < header.TopicCount; i++)
                    {
                        if (reader.BaseStream.Position != topicOffsets[i])
                        {
                            throw new InvalidDataException("Incorrect topic position.");
                        }
                        int inputLength = topicOffsets[i + 1] - topicOffsets[i];

                        byte[]    inputData = reader.ReadBytes(inputLength);
                        HelpTopic topic     = DeserializeTopic(inputData, options);
                        database.Topics.Add(topic);
                    }

                    // TODO: check position
                    if (reader.BaseStream.Position != topicOffsets[header.TopicCount])
                    {
                        throw new InvalidDataException("Incorrect topic end position.");
                    }
                    if (reader.BaseStream.Position != header.DatabaseSize)
                    {
                        throw new InvalidDataException("Incorrect database size.");
                    }
#if DEBUG
                    System.Diagnostics.Debug.WriteLine(string.Format(
                                                           "Decoded database {0} of {1} bytes.",
                                                           database.Name, header.DatabaseSize));
#endif
                }
            return(database);
        }
Exemple #9
0
 public HelpDatabaseViewItem(HelpDatabase database)
 {
     this.database = database;
 }