-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindfile.cpp
77 lines (71 loc) · 1.63 KB
/
findfile.cpp
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
#include "prec.h"
#include "findfile.h"
#include "fileutil.h"
#include "config.h"
FileFinder* FileFinder::instance = NULL;
FileFinder* FileFinder::getSingleton()
{
if (!instance)
{
instance = new FileFinder();
instance->addDir(Config::getString("root"));
}
return instance;
}
FileFinder::FileFinder()
{
}
FileFinder::~FileFinder()
{
}
bool FileFinder::find(const std::string& name, std::string& found)
{
if (name.size())
{
//take local path/cwd as priority
if (fileExists(name.c_str()))
{
found = name;
return true;
}
//try all search paths
for (std::set<std::string>::iterator it = getSingleton()->paths.begin(); it != getSingleton()->paths.end(); ++it)
{
std::string test = joinPath(*it, name);
//printf("looking for %s in %s\n", name.c_str(), test.c_str());
if (fileExists(test.c_str()))
{
//printf("FOUND\n");
found = test;
return true;
}
}
}
return false;
}
std::string FileFinder::find(const std::string& name)
{
std::string result;
find(name, result);
return result;
}
bool FileFinder::addDir(std::string path, bool recursive)
{
path = expanduser(path);
//try appending the apps root directory if the relative path doesn't exist
if (!pathExists(path.c_str()))
{
//printf("%s doesn't exist. ", path.c_str());
path = joinPath(Config::getString("root"), path);
//printf("trying %s.\n", path.c_str());
}
if (!pathExists(path.c_str()))
{
printf("NOTE: Path %s doesn't exist. Not adding to FileFinder\n", path.c_str());
return false;
}
getSingleton()->paths.insert(path);
if (recursive)
printf("Warning: FileFinder::addDir(path, true) not implemented\n");
return true;
}