File indexing completed on 2024-05-12 04:33:57

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
0002 //
0003 // C++ Implementation: dvisourcesplitter
0004 //
0005 // Author: Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>, (C) 2004
0006 //
0007 // Copyright: See COPYING file that comes with this distribution
0008 //
0009 
0010 #include <config.h>
0011 
0012 #include "debug_dvi.h"
0013 #include "dvisourcesplitter.h"
0014 
0015 #include <QDir>
0016 
0017 //#define DEBUG_SOURCESPLITTER
0018 
0019 DVI_SourceFileSplitter::DVI_SourceFileSplitter(const QString &srclink, const QString &dviFile)
0020 {
0021     QString filepart = srclink, linepart;
0022     // if sourcefilename starts with a number
0023     // then there could be a mix up, i.e. src:123file.tex
0024     // line 123 and file.tex or line 12 and 3file.tex?
0025     bool possibleNumberMixUp = false;
0026 
0027 #ifdef DEBUG_SOURCESPLITTER
0028     qCDebug(OkularDviDebug) << "DVI_SourceSplitter: srclink " << srclink;
0029 #endif
0030 
0031     // remove src: if necessary
0032     if (filepart.left(4) == QLatin1String("src:")) {
0033         filepart = srclink.mid(4);
0034     }
0035 
0036     // split first
0037     quint32 max = filepart.length(), i = 0;
0038     for (i = 0; i < max; ++i) {
0039         if (!filepart[i].isDigit()) {
0040             break;
0041         }
0042     }
0043     linepart = filepart.left(i);
0044     filepart = filepart.mid(i);
0045 
0046     // check for number mix up
0047     if (filepart[0] != QLatin1Char(' ') && (linepart.length() != 1)) {
0048         possibleNumberMixUp = true;
0049     }
0050 
0051     // remove a spaces
0052     filepart = filepart.trimmed();
0053     linepart = linepart.trimmed();
0054 
0055 #ifdef DEBUG_SOURCESPLITTER
0056     qCDebug(OkularDviDebug) << "DVI_SourceSplitter: filepart " << filepart << " linepart " << linepart;
0057 #endif
0058 
0059     // test if the file exists
0060     m_fileInfo.setFile(QFileInfo(dviFile).absoluteDir(), filepart);
0061     bool fiExists = m_fileInfo.exists();
0062 
0063     // if it doesn't exist, but adding ".tex"
0064     if (!fiExists && QFileInfo::exists(m_fileInfo.absoluteFilePath() + QStringLiteral(".tex"))) {
0065         m_fileInfo.setFile(m_fileInfo.absoluteFilePath() + QStringLiteral(".tex"));
0066     }
0067 
0068     // if that doesn't help either, perhaps the file started with a
0069     // number: move the numbers from the sourceline to the filename
0070     // one by one (also try to add .tex repeatedly)
0071     if (possibleNumberMixUp && !fiExists) {
0072         QFileInfo tempInfo(m_fileInfo);
0073         QString tempFileName = tempInfo.fileName();
0074         quint32 index, maxindex = linepart.length();
0075         bool found = false;
0076         for (index = 1; index < maxindex; ++index) {
0077             tempInfo.setFile(linepart.right(index) + tempFileName);
0078 #ifdef DEBUG_SOURCESPLITTER
0079             qCDebug(OkularDviDebug) << "DVI_SourceSplitter: trying " << tempInfo.fileName();
0080 #endif
0081             if (tempInfo.exists()) {
0082                 found = true;
0083                 break;
0084             }
0085             tempInfo.setFile(linepart.right(index) + tempFileName + QStringLiteral(".tex"));
0086 #ifdef DEBUG_SOURCESPLITTER
0087             qCDebug(OkularDviDebug) << "DVI_SourceSplitter: trying " << tempInfo.fileName();
0088 #endif
0089             if (tempInfo.exists()) {
0090                 found = true;
0091                 break;
0092             }
0093         }
0094 
0095         if (found) {
0096             m_fileInfo = tempInfo;
0097             linepart = linepart.left(maxindex - index);
0098         }
0099     }
0100 
0101     bool ok;
0102     m_line = linepart.toInt(&ok);
0103     if (!ok) {
0104         m_line = 0;
0105     }
0106 
0107 #ifdef DEBUG_SOURCESPLITTER
0108     qCDebug(OkularDviDebug) << "DVI_SourceSplitter: result: file " << m_fileInfo.absoluteFilePath() << " line " << m_line;
0109 #endif
0110 }