Skip to content

Commit 5f8812a

Browse files
committed
Resolve full path directly when parsing content.opf and toc.ncx file.
1 parent 0a19b84 commit 5f8812a

6 files changed

Lines changed: 43 additions & 44 deletions

lib/libebook/ebook_epub.cpp

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -227,35 +227,22 @@ bool EBook_EPUB::parseBookinfo()
227227
return false;
228228

229229
// Parse the content.opf
230-
HelperXmlHandler_EpubContent content_parser;
230+
// TOC is relative to the container_parser.contentPath
231+
QString parentPath = getParentPath( container_parser.contentPath );
232+
HelperXmlHandler_EpubContent content_parser( parentPath );
231233

232234
if ( !parseXML( container_parser.contentPath, &content_parser ) )
233235
return false;
234236

235237
// At least the TOC must be present
236-
if ( content_parser.tocname.isEmpty() )
238+
if ( content_parser.tocPath.isEmpty() )
237239
return false;
238240

239-
// TOC is relative to the container_parser.contentPath
240-
QString contentRoot;
241-
int sep = container_parser.contentPath.lastIndexOf( '/' );
242-
243-
if ( sep != -1 )
244-
contentRoot = container_parser.contentPath.left( sep + 1 ); // Keep the trailing slash
245-
246-
QString tocPath = combinePath( contentRoot, content_parser.tocname );
247-
248-
// All pages are relative to the container_parser.tocname
249-
QString tocRoot;
250-
sep = tocPath.lastIndexOf( '/' );
251-
252-
if ( sep != -1 )
253-
tocRoot = tocPath.left( sep + 1 ); // Keep the trailing slash
254-
255241
// Parse the TOC
256-
HelperXmlHandler_EpubTOC toc_parser( this, tocRoot );
242+
parentPath = getParentPath( content_parser.tocPath );
243+
HelperXmlHandler_EpubTOC toc_parser( this, parentPath );
257244

258-
if ( !parseXML( tocPath, &toc_parser ) )
245+
if ( !parseXML( content_parser.tocPath, &toc_parser ) )
259246
return false;
260247

261248
// Get the data
@@ -266,18 +253,12 @@ bool EBook_EPUB::parseBookinfo()
266253

267254
// Move the manifest entries into the list
268255
Q_FOREACH ( QString f, content_parser.manifest.values() )
269-
{
270-
QString combined = combinePath( contentRoot, f );
271-
m_ebookManifest.push_back( pathToUrl( combined ) );
272-
}
256+
m_ebookManifest.push_back( pathToUrl( f ) );
273257

274258
for ( const auto& si : qAsConst( content_parser.spine ) )
275259
{
276260
if ( content_parser.manifest.contains( si ) )
277-
{
278-
QString combined = combinePath( contentRoot, content_parser.manifest[ si ] );
279-
m_spinePath.push_back( combined );
280-
}
261+
m_spinePath.push_back( content_parser.manifest[ si ] );
281262
}
282263

283264
// Copy the manifest information and fill up the other maps if we have it
@@ -429,8 +410,18 @@ bool EBook_EPUB::getFileAsBinary( QByteArray& data, const QString& path ) const
429410
return true;
430411
}
431412

432-
QString EBook_EPUB::combinePath( const QString& baseDir, const QString& path )
413+
QString EBook_EPUB::combinePath( const QString& baseDirPath, const QString& path )
433414
{
434-
QString combined = QDir( baseDir ).filePath( path );
415+
QString combined = QDir( baseDirPath ).filePath( path );
435416
return QDir::cleanPath( combined );
436417
}
418+
419+
QString EBook_EPUB::getParentPath( const QString& path )
420+
{
421+
int sep = path.lastIndexOf( '/' );
422+
423+
if ( sep != -1 )
424+
return path.left( sep + 1 ); // Keep the trailing slash
425+
426+
return "";
427+
}

lib/libebook/ebook_epub.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class EBook_EPUB : public EBook
179179
protected:
180180
void loadNavigation( Navigator& nav ) override;
181181

182+
public:
183+
// Combine path and resolve relative path
184+
static QString combinePath( const QString& baseDirPath, const QString& path );
185+
182186
private:
183187
// Parses the XML file using a specified parser
184188
bool parseXML( const QString& uri, QXmlDefaultHandler* reader );
@@ -190,7 +194,8 @@ class EBook_EPUB : public EBook
190194
bool getFileAsString( QString& str, const QString& path ) const;
191195
bool getFileAsBinary( QByteArray& data, const QString& path ) const;
192196

193-
static QString combinePath( const QString& baseDir, const QString& path );
197+
// Get parent path
198+
static QString getParentPath( const QString& path );
194199

195200
// ZIP archive fd and structs
196201
QFile m_epubFile;

lib/libebook/helperxmlhandler_epubcontent.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#include <QString>
2020
#include <QXmlAttributes>
2121

22+
#include "ebook_epub.h"
2223
#include "helperxmlhandler_epubcontent.h"
2324

2425

25-
HelperXmlHandler_EpubContent::HelperXmlHandler_EpubContent()
26+
HelperXmlHandler_EpubContent::HelperXmlHandler_EpubContent( const QString& basePath )
27+
: m_state( STATE_NONE ),
28+
m_basePath( basePath )
2629
{
27-
m_state = STATE_NONE;
2830
}
2931

3032
bool HelperXmlHandler_EpubContent::startElement( const QString&, const QString& localName, const QString&, const QXmlAttributes& atts )
@@ -48,10 +50,10 @@ bool HelperXmlHandler_EpubContent::startElement( const QString&, const QString&
4850
if ( idx_id == -1 || idx_href == -1 || idx_mtype == -1 )
4951
return false;
5052

51-
manifest[ atts.value( idx_id ) ] = atts.value( idx_href );
53+
manifest[ atts.value( idx_id ) ] = EBook_EPUB::combinePath( m_basePath, atts.value( idx_href ) );
5254

5355
if ( atts.value( idx_mtype ) == "application/x-dtbncx+xml" )
54-
tocname = atts.value( idx_href );
56+
tocPath = EBook_EPUB::combinePath( m_basePath, atts.value( idx_href ) );
5557

5658
//qDebug() << "MANIFEST: " << atts.value( idx_id ) << "->" << atts.value( idx_href );
5759
}

lib/libebook/helperxmlhandler_epubcontent.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class QXmlAttributes;
3030
class HelperXmlHandler_EpubContent : public QXmlDefaultHandler
3131
{
3232
public:
33-
HelperXmlHandler_EpubContent();
33+
HelperXmlHandler_EpubContent( const QString& basePath );
3434

3535
// Keep the tag-associated metadata
3636
QMap< QString, QString > metadata;
@@ -41,8 +41,8 @@ class HelperXmlHandler_EpubContent : public QXmlDefaultHandler
4141
// Spine storage
4242
QList< QString > spine;
4343

44-
// TOC (NCX) filename
45-
QString tocname;
44+
// TOC (NCX) file path
45+
QString tocPath;
4646

4747
private:
4848
enum State
@@ -60,6 +60,7 @@ class HelperXmlHandler_EpubContent : public QXmlDefaultHandler
6060
// Tracking
6161
State m_state;
6262
QString m_tagname;
63+
QString m_basePath;
6364
};
6465

6566
#endif // HELPERXMLHANDLER_EPUBCONTENT_H

lib/libebook/helperxmlhandler_epubtoc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
#include "helperxmlhandler_epubtoc.h"
2626

2727

28-
HelperXmlHandler_EpubTOC::HelperXmlHandler_EpubTOC( EBook_EPUB* epub, const QString& documentRoot )
28+
HelperXmlHandler_EpubTOC::HelperXmlHandler_EpubTOC( EBook_EPUB* epub, const QString& basePath )
2929
: m_inNavMap( false ),
3030
m_inText( false ),
3131
m_indent( 0 ),
3232
m_epub( epub ),
33-
m_documentRoot( documentRoot )
33+
m_basePath( basePath )
3434
{
3535
}
3636

@@ -105,8 +105,8 @@ void HelperXmlHandler_EpubTOC::checkNewTocEntry()
105105
{
106106
EBookTocEntry entry;
107107
entry.name = m_lastTitle;
108-
QString combined = QDir( m_documentRoot ).filePath( m_lastId );
109-
entry.url = m_epub->pathToUrl( QDir::cleanPath( combined ) );
108+
QString combined = EBook_EPUB::combinePath( m_basePath, m_lastId );
109+
entry.url = m_epub->pathToUrl( combined );
110110
entry.iconid = EBookTocEntry::IMAGE_AUTO;
111111
entry.indent = m_indent - 1;
112112

lib/libebook/helperxmlhandler_epubtoc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class EBook_EPUB;
3333
class HelperXmlHandler_EpubTOC : public QXmlDefaultHandler
3434
{
3535
public:
36-
HelperXmlHandler_EpubTOC( EBook_EPUB* epub, const QString& documentRoot );
36+
HelperXmlHandler_EpubTOC( EBook_EPUB* epub, const QString& basePath );
3737

3838
QList< EBookTocEntry > entries;
3939

@@ -50,7 +50,7 @@ class HelperXmlHandler_EpubTOC : public QXmlDefaultHandler
5050
QString m_lastId;
5151
QString m_lastTitle;
5252
EBook_EPUB* m_epub;
53-
QString m_documentRoot;
53+
QString m_basePath;
5454
};
5555

5656
#endif // HELPERXMLHANDLER_EPUBTOC_H

0 commit comments

Comments
 (0)