Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Public Member Functions | Private Member Functions | Private Attributes
PdbIm Class Reference

An import filter for Palm Documents (PDB files). More...

#include <pdbim.h>

Collaboration diagram for PdbIm:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 PdbIm (const QString &fname, const QString &enc, gtWriter *w)
 Parse and decode the PDB file.
 ~PdbIm ()
void write ()
 Write data into Scribus text frame.

Private Member Functions

void loadFile (QString fname)
 Parse the PDB file.
void selectSwap ()
 Learn which endian to use.
Word swap_Word (Word r)
 Recompute the binary value for given endian.
DWord swap_DWord (DWord r)
 Recompute the binary value for given endian.
void uncompress (buffer *m_buf)
 PDB strange decompress algorithm implementation.

Private Attributes

bufferm_buf
 Binary buffer for extraction tasks.
QString data
 Store the extracted text here.
QString encoding
 Name of the codec/encoding to recode.
gtWriter * writer
 Imp plugin handler.
bool m_littlendian
 A "bit order" flag. True on little endian systems.
bool bCompressed
 A "document uses that strange compress algorithm" flag.

Detailed Description

An import filter for Palm Documents (PDB files).

PDB documents are simple non-formatted texts in binary forms used mainly for e-books distribution. It's taken from Abiword's PDB import/export plugin http://www.abisource.com I've simplified the importer guts to fit special Scribus needs.

Warning:
This plugin uses a very special low-level IO and memory operations. It uses big/little endian handling for bit related operations. Welcome in the "macro and gtk hell" ;)
Note:
User should specify file encoding correctly.
Author:
Copyright (C) 2001 AbiSource, Inc.
Petr Vanek petr@.nosp@m.scri.nosp@m.bus.i.nosp@m.nfo

Definition at line 135 of file pdbim.h.


Constructor & Destructor Documentation

PdbIm::PdbIm ( const QString &  fname,
const QString &  enc,
gtWriter *  w 
)

Parse and decode the PDB file.

Parameters:
fnamea file name of the document
encuser selected text encoding. See encoding attr.
wa reference to the gtWriter instance

Definition at line 56 of file pdbim.cpp.

Here is the call graph for this function:

PdbIm::~PdbIm ( ) [inline]

Definition at line 143 of file pdbim.h.

{};

Member Function Documentation

void PdbIm::loadFile ( QString  fname) [private]

Parse the PDB file.

Parameters:
fnamea filename to open

Definition at line 82 of file pdbim.cpp.

{
       FILE *m_pdfp = fopen(fname, "rb");
       pdb_header m_header;
       DWord file_size, offset;
       doc_record0 m_rec0;

       if (!m_pdfp)
       {
              QMessageBox::warning(ScCore->primaryMainWindow(), QObject::tr("PDB Import", "PDB Importer"),
                                                  "<qt>" + QObject::tr("Could not open file %1", "PDB Importer").arg(fname) + "</qt>",
                                                  0);
              return;
       }
       fread( &m_header, PDB_HEADER_SIZE, 1, m_pdfp );
       if (strncmp(m_header.type, DOC_TYPE, sizeof(m_header.type) ) ||
              strncmp( m_header.creator, DOC_CREATOR, sizeof(m_header.creator)))
       {
              QMessageBox::warning(ScCore->primaryMainWindow(), QObject::tr("PDB Import", "PDB Importer"),
                                                  "<qt>" + QObject::tr("This file is not recognized as a PDB document. Please, report this as a bug if you are sure it is one.", "PDB Importer") + "</qt>",
                                                  0);
              return;
       }

       // progressbar
       int num_records = swap_Word( m_header.numRecords ) - 1;
       ScCore->primaryMainWindow()->mainWindowProgressBar->setTotalSteps(num_records);
       fseek(m_pdfp, PDB_HEADER_SIZE, SEEK_SET);
       GET_DWord(m_pdfp, offset);
       fseek(m_pdfp, offset, SEEK_SET);
       fread(&m_rec0, sizeof(m_rec0), 1, m_pdfp);

       if (swap_Word( m_rec0.version ) == 2 )
              bCompressed = true;

       fseek( m_pdfp, 0, SEEK_END );
       file_size = ftell( m_pdfp );
       for (int rec_num = 1; rec_num <= num_records; ++rec_num )
       {
              DWord next_offset;

              ScCore->primaryMainWindow()->mainWindowProgressBar->setProgress(rec_num);
              fseek( m_pdfp, PDB_HEADER_SIZE + PDB_RECORD_HEADER_SIZE * rec_num, SEEK_SET);
              GET_DWord( m_pdfp, offset );
              if( rec_num < num_records )
              {
                     fseek( m_pdfp, PDB_HEADER_SIZE + PDB_RECORD_HEADER_SIZE * (rec_num + 1), SEEK_SET);
                     GET_DWord( m_pdfp, next_offset );
              }
              else
                     next_offset = file_size;
              fseek( m_pdfp, offset, SEEK_SET );
              // be overly cautious here
              _zero_fill (m_buf->buf, BUFFER_SIZE);
              m_buf->position = fread( m_buf->buf, 1, next_offset - offset, m_pdfp );

              if ( bCompressed )
                     uncompress( m_buf );

              m_buf->position = 0;
              while ( (m_buf->position) < (m_buf->len) )
              {
                     if (m_buf->buf[m_buf->position] == '\0')
                     {
                            ++m_buf->position;
                            continue;
                     }
                     data += m_buf->buf[m_buf->position];
                     ++m_buf->position;
              } 
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PdbIm::selectSwap ( ) [private]

Learn which endian to use.

It fills the m_littlendian flag

Definition at line 155 of file pdbim.cpp.

{
       union { char c[2];  Word n; }  w;
       strncpy(  w.c, "\1\2",     2 );

       if ( w.n == 0x0201 )
              m_littlendian = true;
       else
              m_littlendian = false;

}  

Here is the caller graph for this function:

DWord PdbIm::swap_DWord ( DWord  r) [private]

Recompute the binary value for given endian.

Parameters:
ra binary DWord
Return values:
DWorda correct endian Word

Definition at line 175 of file pdbim.cpp.

{
       if (m_littlendian)
              return ( (r >> 24) & 0x00FF ) | (r << 24) | ( (r >> 8) & 0xFF00 ) | ( (r << 8) & 0xFF0000 );
       else
              return r;
}
Word PdbIm::swap_Word ( Word  r) [private]

Recompute the binary value for given endian.

Parameters:
ra binary Word
Return values:
Worda correct endian Word

Definition at line 167 of file pdbim.cpp.

{
       if (m_littlendian)
              return (r >> 8) | (r << 8);
       else
              return r;
}

Here is the caller graph for this function:

void PdbIm::uncompress ( buffer m_buf) [private]

PDB strange decompress algorithm implementation.

Parameters:
m_bufa reference to the current buffer

Definition at line 183 of file pdbim.cpp.

{
       buffer *m_new_buf = new buffer;
       UT_uint16 i, j;
       Byte c;

       _zero_fill (m_new_buf->buf, BUFFER_SIZE);

       for (i = j = 0; i < m_buf->position && j < BUFFER_SIZE; )
       {
              c = m_buf->buf[ i++ ];

              if ( c >= 1 && c <= 8 )
                     while ( c-- && j < BUFFER_SIZE-1)
                            m_new_buf->buf[ j++ ] = m_buf->buf[ i++ ];

              else if ( c <= 0x7F )
                     m_new_buf->buf[ j++ ] = c;

              else if ( c >= 0xC0 && j < BUFFER_SIZE-2)
              {
                     m_new_buf->buf[ j++ ] = ' ';
                     m_new_buf->buf[ j++ ] = c ^ 0x80;
              }
              else
              {
                     int di, n;
                     unsigned int temp_c = c;
                  // c--> temp_c //tomy 2001.11.13 
                     temp_c = (temp_c << 8) ;
                     temp_c = temp_c + m_buf->buf[ i++ ];
                     di = (temp_c & 0x3FFF) >> COUNT_BITS;
                     for (n = (temp_c & ((1 << COUNT_BITS) - 1)) + 3; n-- && j < BUFFER_SIZE; ++j )
                            m_new_buf->buf[ j ] = m_new_buf->buf[ j - di ];
                     temp_c = 0;
              }
       }
       //UT_ASSERT(j <= BUFFER_SIZE);

       memcpy( static_cast<void *>(m_buf->buf), static_cast<void *>(m_new_buf->buf), static_cast<size_t>(j) );

       m_buf->position = j;
       delete( m_new_buf );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PdbIm::write ( )

Write data into Scribus text frame.

User should specify encoding of the imported text - it's recoded here.

Definition at line 68 of file pdbim.cpp.

{
       QTextCodec *codec;
       if (encoding.isEmpty())
              codec = QTextCodec::codecForLocale();
       else
              codec = QTextCodec::codecForName(encoding);
       data = codec->toUnicode(data);
       gtParagraphStyle *pstyle = new gtParagraphStyle(*(writer->getDefaultStyle()));
       pstyle->setName(writer->getFrameName() + "-" + QObject::tr("PDB_data", "PDB Importer"));
       writer->append(data, pstyle);
       delete pstyle;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

A "document uses that strange compress algorithm" flag.

Definition at line 159 of file pdbim.h.

QString PdbIm::data [private]

Store the extracted text here.

Definition at line 151 of file pdbim.h.

QString PdbIm::encoding [private]

Name of the codec/encoding to recode.

Definition at line 153 of file pdbim.h.

buffer* PdbIm::m_buf [private]

Binary buffer for extraction tasks.

Definition at line 149 of file pdbim.h.

A "bit order" flag. True on little endian systems.

Definition at line 157 of file pdbim.h.

gtWriter* PdbIm::writer [private]

Imp plugin handler.

Definition at line 155 of file pdbim.h.


The documentation for this class was generated from the following files: