Пример #1
0
        /// <summary>
        /// Parses all text record
        /// </summary>
        private IEnumerable <MobipocketRecord> ParseTextMobipocketRecords()
        {
            int bytesUsed = 0;

            var parser = new TrailingEntryParser(Header.TrailingEntries);
            int previousOverlapSize = 0;

            //skip first record as header, and process all text records
            for (int recordIndex = 0; recordIndex < Header.TextRecordCount; recordIndex++)
            {
                //get current record
                var record = _pdb.Records[recordIndex + 1];

                //read record
                var mobiRecord = MobipocketRecord.FromBinary(record.Data, true, parser);

                //uncompress text
                mobiRecord.Text = GetRecordText(mobiRecord, previousOverlapSize, ref bytesUsed);

                //yield record
                yield return(mobiRecord);

                //save trailing entries
                previousOverlapSize = mobiRecord.TrailingEntries.IsMultibyteCharacterOverlapping ?
                                      mobiRecord.TrailingEntries.MultibyteCharacterOverlap.Length : 0;
            }

            if (bytesUsed != Header.TotalTextLength)
            {
                throw new Exception("text length doesn't match used characters");
            }
        }
Пример #2
0
        //---------------------------------------------
        //
        // Private methods
        //
        //---------------------------------------------

        #region Private methods

        private IEnumerable <MobipocketRecord> ParseMobipocketRecords()
        {
            foreach (var record in ParseTextMobipocketRecords())
            {
                AddResource(MobipocketResourceTypes.Text, record.Text);
                yield return(record);
            }

            for (int index = (int)Header.FirstNonTextRecord; index < _pdb.Records.Count; index++)
            {
                var data   = _pdb.Records[index].Data;
                var record = MobipocketRecord.FromBinary(data);

                yield return(record);

                if (record.IsFLISRecord)
                {
                    AddResource(MobipocketResourceTypes.FLIS, data);
                }
                else if (record.IsFCISRecord)
                {
                    AddResource(MobipocketResourceTypes.FCIS, data);
                }
                else if (record.IsEOFRecord)
                {
                    AddResource(MobipocketResourceTypes.EOF, data);
                }
                else
                {
                    try
                    {
                        using (var stream = new MemoryStream(record.Data))
                            using (var image = Image.FromStream(stream))
                            {
                                var format = image.RawFormat;

                                if (format.Equals(ImageFormat.Jpeg) ||
                                    format.Equals(ImageFormat.Gif) ||
                                    format.Equals(ImageFormat.Bmp))
                                {
                                    AddResource(MobipocketResourceTypes.Image, data);
                                }

                                else
                                {
                                    throw new NotSupportedException("image not supported");
                                }
                            }
                    }
                    catch
                    {
                        AddResource(MobipocketResourceTypes.Binary, data);
                    }
                }
            }
        }