1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
2 //
3 // C++ Implementation: dvisourcesplitter
4 //
5 // Author: Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>, (C) 2004
6 //
7 // Copyright: See COPYING file that comes with this distribution
8 //
9
10 #include <config.h>
11
12 #include "debug_dvi.h"
13 #include "dvisourcesplitter.h"
14
15 #include <QDir>
16
17 //#define DEBUG_SOURCESPLITTER
18
DVI_SourceFileSplitter(const QString & srclink,const QString & dviFile)19 DVI_SourceFileSplitter::DVI_SourceFileSplitter(const QString &srclink, const QString &dviFile)
20 {
21 QString filepart = srclink, linepart;
22 // if sourcefilename starts with a number
23 // then there could be a mix up, i.e. src:123file.tex
24 // line 123 and file.tex or line 12 and 3file.tex?
25 bool possibleNumberMixUp = false;
26
27 #ifdef DEBUG_SOURCESPLITTER
28 qCDebug(OkularDviDebug) << "DVI_SourceSplitter: srclink " << srclink;
29 #endif
30
31 // remove src: if necessary
32 if (filepart.left(4) == QLatin1String("src:"))
33 filepart = srclink.mid(4);
34
35 // split first
36 quint32 max = filepart.length(), i = 0;
37 for (i = 0; i < max; ++i)
38 if (!filepart[i].isDigit())
39 break;
40 linepart = filepart.left(i);
41 filepart = filepart.mid(i);
42
43 // check for number mix up
44 if (filepart[0] != QLatin1Char(' ') && (linepart.length() != 1))
45 possibleNumberMixUp = true;
46
47 // remove a spaces
48 filepart = filepart.trimmed();
49 linepart = linepart.trimmed();
50
51 #ifdef DEBUG_SOURCESPLITTER
52 qCDebug(OkularDviDebug) << "DVI_SourceSplitter: filepart " << filepart << " linepart " << linepart;
53 #endif
54
55 // test if the file exists
56 m_fileInfo.setFile(QFileInfo(dviFile).absoluteDir(), filepart);
57 bool fiExists = m_fileInfo.exists();
58
59 // if it doesn't exist, but adding ".tex"
60 if (!fiExists && QFileInfo::exists(m_fileInfo.absoluteFilePath() + QStringLiteral(".tex")))
61 m_fileInfo.setFile(m_fileInfo.absoluteFilePath() + QStringLiteral(".tex"));
62
63 // if that doesn't help either, perhaps the file started with a
64 // number: move the numbers from the sourceline to the filename
65 // one by one (also try to add .tex repeatedly)
66 if (possibleNumberMixUp && !fiExists) {
67 QFileInfo tempInfo(m_fileInfo);
68 QString tempFileName = tempInfo.fileName();
69 quint32 index, maxindex = linepart.length();
70 bool found = false;
71 for (index = 1; index < maxindex; ++index) {
72 tempInfo.setFile(linepart.right(index) + tempFileName);
73 #ifdef DEBUG_SOURCESPLITTER
74 qCDebug(OkularDviDebug) << "DVI_SourceSplitter: trying " << tempInfo.fileName();
75 #endif
76 if (tempInfo.exists()) {
77 found = true;
78 break;
79 }
80 tempInfo.setFile(linepart.right(index) + tempFileName + QStringLiteral(".tex"));
81 #ifdef DEBUG_SOURCESPLITTER
82 qCDebug(OkularDviDebug) << "DVI_SourceSplitter: trying " << tempInfo.fileName();
83 #endif
84 if (tempInfo.exists()) {
85 found = true;
86 break;
87 }
88 }
89
90 if (found) {
91 m_fileInfo = tempInfo;
92 linepart = linepart.left(maxindex - index);
93 }
94 }
95
96 bool ok;
97 m_line = linepart.toInt(&ok);
98 if (!ok)
99 m_line = 0;
100
101 #ifdef DEBUG_SOURCESPLITTER
102 qCDebug(OkularDviDebug) << "DVI_SourceSplitter: result: file " << m_fileInfo.absoluteFilePath() << " line " << m_line;
103 #endif
104 }
105