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 "datapaths.h"
30 #include "log.h"
31 #include "strings.h"
32 #include <QDir>
33 #include <QFileInfo>
34 #include <cstdlib>
35 
36 DClass<PathFinderResult>
37 {
38  public:
39  QStringList foundFiles;
40  QStringList missingFiles;
41 };
42 
43 
44 DPointered(PathFinderResult)
45 
46 
48 {
49 }
50 
51 PathFinderResult::~PathFinderResult()
52 {
53 }
54 
56 {
57  return d->foundFiles;
58 }
59 
60 const QStringList& PathFinderResult::foundFiles() const
61 {
62  return d->foundFiles;
63 }
64 
66 {
67  return d->missingFiles;
68 }
69 
70 const QStringList& PathFinderResult::missingFiles() const
71 {
72  return d->missingFiles;
73 }
74 
76 
77 DClass<PathFinder>
78 {
79  public:
80  QList<FileSearchPath> searchPaths;
81 };
82 
83 
84 DPointered(PathFinder)
85 
86 
88 {
89  d->searchPaths = gConfig.combinedWadseekPaths();
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 PathFinder PathFinder::genericPathFinder(const QStringList &suffixes)
105 {
106  QStringList paths;
107 #if defined(Q_OS_WIN32)
108  paths << "." << ".."
109  << gDefaultDataPaths->workingDirectory()
110  << gDefaultDataPaths->workingDirectory() + "/.."
111  << DataPaths::programFilesDirectory(DataPaths::x64)
112  << DataPaths::programFilesDirectory(DataPaths::x86);
113 #else
114  paths << "/usr/bin" << "/usr/local/bin" << "/usr/share/bin"
115  << "/usr/games/" << "/usr/local/games/"
116  << "/usr/share/games/" << gDefaultDataPaths->workingDirectory() << ".";
117 #endif
118  QStringList pathsCopy(paths);
119  foreach (const QString &path, pathsCopy)
120  {
121  foreach (const QString &suffix, suffixes)
122  {
123  paths << Strings::combinePaths(path, suffix);
124  }
125  }
126  return PathFinder(paths);
127 }
128 
129 void PathFinder::addPrioritySearchDir(const QString& dir)
130 {
131  QFileInfo fileInfo(dir);
132  if(fileInfo.isSymLink())
133  fileInfo = QFileInfo(fileInfo.symLinkTarget());
134 
135  if(fileInfo.isBundle())
136  d->searchPaths.prepend(fileInfo.absoluteFilePath() + "/Contents/MacOS");
137  else
138  {
139  if(fileInfo.isFile())
140  d->searchPaths.prepend(fileInfo.absoluteDir().absolutePath());
141  else
142  d->searchPaths.prepend(fileInfo.absoluteFilePath());
143  }
144 }
145 
146 void PathFinder::addSearchDir(const QString &dir)
147 {
148  QFileInfo fileInfo(dir);
149  if(fileInfo.isSymLink())
150  fileInfo = QFileInfo(fileInfo.symLinkTarget());
151 
152  if(fileInfo.isBundle())
153  d->searchPaths << fileInfo.absoluteFilePath() + "/Contents/MacOS";
154  else
155  {
156  if(fileInfo.isFile())
157  d->searchPaths << fileInfo.absoluteDir().absolutePath();
158  else
159  d->searchPaths << fileInfo.absoluteFilePath();
160  }
161 }
162 
163 QString PathFinder::findFile(const QString& fileName) const
164 {
165  if (d->searchPaths.count() == 0)
166  {
167  return QString();
168  }
169 
170  BaseFileSeeker* seeker = NULL;
171  #ifdef Q_OS_WIN32
172  seeker = new CaseInsensitiveFSFileSeeker();
173  #else
174  seeker = new CaseSensitiveFSFileSeeker();
175  #endif
176  QString result = seeker->findFile(fileName, d->searchPaths);
177  delete seeker;
178  return result;
179 }
180 
181 PathFinderResult PathFinder::findFiles(const QStringList& files) const
182 {
183  PathFinderResult result;
184  foreach(const QString file, files)
185  {
186  QString filePath = findFile(file);
187  if (filePath.isNull())
188  {
189  result.missingFiles() << file;
190  }
191  else
192  {
193  result.foundFiles() << filePath;
194  }
195  }
196 
197  return result;
198 }
static QString combinePaths(QString pathFront, QString pathEnd)
Definition: strings.cpp:147
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:65
void addSearchDir(const QString &dir)
Adds directory where search will be performed.
Definition: pathfinder.cpp:146
QStringList & foundFiles()
Paths to found files.
Definition: pathfinder.cpp:55
PathFinderResult findFiles(const QStringList &files) const
Performs a search for multiple files, marking them as found or missing.
Definition: pathfinder.cpp:181
PathFinder()
Constructs PathFinder where paths are read from program configuration from file (WAD) path list setti...
static QString programFilesDirectory(MachineType machineType)
Definition: datapaths.cpp:235
QString findFile(const QString &fileName) const
Performs a search for a single file.
Definition: pathfinder.cpp:163
For case-sensitive file systems (like ext).
static PathFinder genericPathFinder(const QStringList &suffixes)
Generic PathFinder that looks in PATH and other common dirs.
Definition: pathfinder.cpp:104
void addPrioritySearchDir(const QString &dir)
Definition: pathfinder.cpp:129
For case-insensitive file systems (like NTFS).