pathfinder.cpp
1 //------------------------------------------------------------------------------
2 // pathfinder.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "pathfinder.h"
24 
25 #include "configuration/doomseekerconfig.h"
26 #include "pathfinder/caseinsensitivefsfileseeker.h"
27 #include "pathfinder/casesensitivefsfileseeker.h"
28 #include "pathfinder/filesearchpath.h"
29 #include "log.h"
30 #include "strings.h"
31 #include <QDir>
32 #include <QFileInfo>
33 #include <cstdlib>
34 
35 DClass<PathFinderResult>
36 {
37  public:
38  QStringList foundFiles;
39  QStringList missingFiles;
40 };
41 
42 
43 DPointered(PathFinderResult)
44 
45 
47 {
48 }
49 
50 PathFinderResult::~PathFinderResult()
51 {
52 }
53 
55 {
56  return d->foundFiles;
57 }
58 
59 const QStringList& PathFinderResult::foundFiles() const
60 {
61  return d->foundFiles;
62 }
63 
65 {
66  return d->missingFiles;
67 }
68 
69 const QStringList& PathFinderResult::missingFiles() const
70 {
71  return d->missingFiles;
72 }
73 
75 
76 DClass<PathFinder>
77 {
78  public:
79  QList<FileSearchPath> searchPaths;
80 };
81 
82 
83 DPointered(PathFinder)
84 
85 
87 {
88  d->searchPaths = gConfig.doomseeker.wadPaths;
89  d->searchPaths << gConfig.wadseeker.targetDirectory;
90 }
91 
92 PathFinder::PathFinder(const QStringList& paths)
93 {
94  foreach (const QString& path, paths)
95  {
96  d->searchPaths << path;
97  }
98 }
99 
100 PathFinder::~PathFinder()
101 {
102 }
103 
104 void PathFinder::addPrioritySearchDir(const QString& dir)
105 {
106  QFileInfo fileInfo(dir);
107  if(fileInfo.isSymLink())
108  fileInfo = QFileInfo(fileInfo.symLinkTarget());
109 
110 #ifdef Q_OS_MAC
111  if(fileInfo.isBundle())
112  d->searchPaths.prepend(fileInfo.absoluteFilePath() + "/Contents/MacOS");
113  else
114 #endif
115  if(fileInfo.isFile())
116  d->searchPaths.prepend(fileInfo.absoluteDir().absolutePath());
117  else
118  d->searchPaths.prepend(fileInfo.absoluteFilePath());
119 
120 }
121 
122 QString PathFinder::findFile(const QString& fileName) const
123 {
124  if (d->searchPaths.count() == 0)
125  {
126  return QString();
127  }
128 
129  BaseFileSeeker* seeker = NULL;
130  #ifdef Q_OS_WIN32
131  seeker = new CaseInsensitiveFSFileSeeker();
132  #else
133  seeker = new CaseSensitiveFSFileSeeker();
134  #endif
135  QString result = seeker->findFile(fileName, d->searchPaths);
136  delete seeker;
137  return result;
138 }
139 
140 PathFinderResult PathFinder::findFiles(const QStringList& files) const
141 {
142  PathFinderResult result;
143  foreach(const QString file, files)
144  {
145  QString filePath = findFile(file);
146  if (filePath.isNull())
147  {
148  result.missingFiles() << file;
149  }
150  else
151  {
152  result.foundFiles() << filePath;
153  }
154  }
155 
156  return result;
157 }
Result of multiple file search operation done by PathFinder.
Definition: pathfinder.h:41
Performs a case-insensitive (OS independent) file searches.
Definition: pathfinder.h:81
QStringList & missingFiles()
Names of not found files.
Definition: pathfinder.cpp:64
QStringList & foundFiles()
Paths to found files.
Definition: pathfinder.cpp:54
PathFinderResult findFiles(const QStringList &files) const
Performs a search for multiple files, marking them as found or missing.
Definition: pathfinder.cpp:140
PathFinder()
Constructs PathFinder where paths are read from program configuration.
QString findFile(const QString &fileName) const
Performs a search for a single file.
Definition: pathfinder.cpp:122
For case-sensitive file systems (like ext).
void addPrioritySearchDir(const QString &dir)
Definition: pathfinder.cpp:104
For case-insensitive file systems (like NTFS).