summaryrefslogtreecommitdiffstats
path: root/kdvi/dvisourcesplitter.cpp
blob: e0d17aa1aa1edeb82f5b7748c3f35d6a8971b6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// C++ Implementation: dvisourcesplitter
//
// Author: Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//

#include <config.h>

#include <tqdir.h>
#include <kdebug.h>

#include "dvisourcesplitter.h"

//#define DEBUG_SOURCESPLITTER


DVI_SourceFileSplitter::DVI_SourceFileSplitter(const TQString &srclink, const TQString &dviFile)
{
  TQString filepart = srclink, linepart;
  bool possibleNumberMixUp = false; //if sourcefilename starts with a number 
                                    //then there could be a mix up, i.e. src:123file.tex 
                                    //line 123 and file.tex or line 12 and 3file.tex?
  
#ifdef DEBUG_SOURCESPLITTER
  kdDebug(4300) << "DVI_SourceSplitter: srclink " << srclink << endl;
#endif 

  //remove src: if necessary
  if ( filepart.left(4) == "src:" ) filepart = srclink.mid(4);
    
  //split first
  Q_UINT32 max = filepart.length(), i = 0;
  for(i=0; i<max; ++i) if ( !filepart[i].isDigit()) break;
  linepart = filepart.left(i);
  filepart = filepart.mid(i);
    
  //check for number mix up
  if ( filepart[0] != ' ' && (linepart.length() != 1) ) possibleNumberMixUp = true;

  //remove a spaces  
  filepart = filepart.stripWhiteSpace();
  linepart = linepart.stripWhiteSpace();
  
#ifdef DEBUG_SOURCESPLITTER
  kdDebug() << "DVI_SourceSplitter: filepart " << filepart << " linepart " << linepart << endl;
#endif

  //test if the file exists
  m_fileInfo.setFile(TQFileInfo(dviFile).dir(true), filepart);
  bool fiExists = m_fileInfo.exists();
  
  //if it doesn't exist, but adding ".tex" 
  if ( !fiExists && TQFileInfo(m_fileInfo.absFilePath() + ".tex").exists() )
    m_fileInfo.setFile(m_fileInfo.absFilePath() + ".tex");
    
  //if that doesn't help either, perhaps the file started with a 
  //number: move the numbers from the sourceline to the filename
  //one by one (also try to add .tex repeatedly)
  if ( possibleNumberMixUp && !fiExists )
  {
    TQFileInfo tempInfo(m_fileInfo);
    TQString tempFileName = tempInfo.fileName();
    Q_UINT32 index, maxindex = linepart.length();
    bool found = false;
    for ( index = 1; index < maxindex; ++index)
    {
      tempInfo.setFile(linepart.right(index) + tempFileName);
#ifdef DEBUG_SOURCESPLITTER
      kdDebug() << "DVI_SourceSplitter: trying " << tempInfo.fileName() << endl;
#endif
      if ( tempInfo.exists() ) { found = true; break;}
      tempInfo.setFile(linepart.right(index) + tempFileName + ".tex");
#ifdef DEBUG_SOURCESPLITTER
      kdDebug() << "DVI_SourceSplitter: trying " << tempInfo.fileName() << endl;
#endif
      if ( tempInfo.exists() ) { found = true; break;}
    }
    
    if (found)
    {
      m_fileInfo = tempInfo;
      linepart = linepart.left(maxindex - index);
    }
  }
  
  bool ok;
  m_line = linepart.toInt(&ok);
  if (!ok) m_line = 0;
  
#ifdef DEBUG_SOURCESPLITTER
  kdDebug() << "DVI_SourceSplitter: result: file " << m_fileInfo.absFilePath() << " line " << m_line << endl;
#endif
}