pathfinder.cpp
1 //------------------------------------------------------------------------------
2 // pathfinder.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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.hpp"
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  QString resolveDir(const QString &dir)
83  {
84  QFileInfo fileInfo(dir);
85  if(fileInfo.isSymLink())
86  fileInfo = QFileInfo(fileInfo.symLinkTarget());
87 
88  if(fileInfo.isBundle())
89  return fileInfo.absoluteFilePath() + "/Contents/MacOS";
90  else
91  {
92  if(fileInfo.isFile())
93  return fileInfo.absoluteDir().absolutePath();
94  else
95  return fileInfo.absoluteFilePath();
96  }
97  }
98 };
99 
100 
101 DPointered(PathFinder)
102 
103 
105 {
106  d->searchPaths = gConfig.combinedWadseekPaths();
107 }
108 
109 PathFinder::PathFinder(const QStringList& paths)
110 {
111  foreach (const QString& path, paths)
112  {
113  d->searchPaths << path;
114  }
115 }
116 
117 PathFinder::~PathFinder()
118 {
119 }
120 
121 PathFinder PathFinder::genericPathFinder(const QStringList &suffixes)
122 {
123  QStringList paths;
124 #if defined(Q_OS_WIN32)
125  paths << "." << ".."
126  << gDefaultDataPaths->workingDirectory()
127  << gDefaultDataPaths->workingDirectory() + "/.."
128  << DataPaths::programFilesDirectory(DataPaths::x64)
129  << DataPaths::programFilesDirectory(DataPaths::x86);
130 #else
131  paths << "/usr/bin" << "/usr/local/bin" << "/usr/share/bin"
132  << "/usr/games/" << "/usr/local/games/"
133  << "/usr/share/games/" << gDefaultDataPaths->workingDirectory() << ".";
134 #endif
135  QStringList pathsCopy(paths);
136  foreach (const QString &path, pathsCopy)
137  {
138  foreach (const QString &suffix, suffixes)
139  {
140  paths << Strings::combinePaths(path, suffix);
141  }
142  }
143  return PathFinder(paths);
144 }
145 
146 void PathFinder::addPrioritySearchDir(const QString& dir)
147 {
148  d->searchPaths.prepend(d->resolveDir(dir));
149 }
150 
151 void PathFinder::addSearchDir(const QString &dir)
152 {
153  d->searchPaths << d->resolveDir(dir);
154 }
155 
156 QString PathFinder::findFile(const QString& fileName) const
157 {
158  if (d->searchPaths.count() == 0)
159  {
160  return QString();
161  }
162 
163  BaseFileSeeker* seeker = NULL;
164  #ifdef Q_OS_WIN32
165  seeker = new CaseInsensitiveFSFileSeeker();
166  #else
167  seeker = new CaseSensitiveFSFileSeeker();
168  #endif
169  QString result = seeker->findFile(fileName, d->searchPaths);
170  delete seeker;
171  return result;
172 }
173 
174 PathFinderResult PathFinder::findFiles(const QStringList& files) const
175 {
176  PathFinderResult result;
177  foreach(const QString file, files)
178  {
179  QString filePath = findFile(file);
180  if (filePath.isNull())
181  {
182  result.missingFiles() << file;
183  }
184  else
185  {
186  result.foundFiles() << filePath;
187  }
188  }
189 
190  return result;
191 }
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:151
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:174
PathFinder()
Constructs PathFinder where paths are read from program configuration from file (WAD) path list setti...
static QString programFilesDirectory(MachineType machineType)
Definition: datapaths.cpp:372
QString findFile(const QString &fileName) const
Performs a search for a single file.
Definition: pathfinder.cpp:156
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:121
void addPrioritySearchDir(const QString &dir)
Definition: pathfinder.cpp:146
For case-insensitive file systems (like NTFS).