]> Dogcows Code - chaz/yoink/blob - src/Moof/Resource.cc
reformatting
[chaz/yoink] / src / Moof / Resource.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <unistd.h>
13
14 #include <boost/algorithm/string.hpp>
15
16 #include "Log.hh"
17 #include "Resource.hh"
18
19
20 namespace Mf {
21
22
23 // static member
24 std::vector<std::string> Resource::gSearchPaths;
25
26
27 void Resource::addSearchPaths(const std::string& path)
28 {
29 std::vector<std::string> paths;
30 boost::split(paths, path, boost::is_any_of(":"));
31
32 addSearchPaths(paths);
33 }
34
35 void Resource::addSearchPaths(const std::vector<std::string>& path)
36 {
37 std::vector<std::string>::const_iterator it;
38
39 for (it = path.begin(); it != path.end(); ++it)
40 {
41 std::string onePath(*it);
42
43 ASSERT(!onePath.empty() && "empty search path string");
44
45 // add a slash if there isn't one already
46 if (*onePath.rbegin() != '/')
47 {
48 onePath += '/';
49 }
50
51 #if defined(_WIN32) || defined(__WIN32__)
52 boost::replace_all(onePath, "/", "\\");
53 #endif
54
55 gSearchPaths.push_back(onePath);
56 logInfo << "added search path " << onePath << std::endl;
57 }
58 }
59
60
61 std::string Resource::getPath(const std::string& name)
62 {
63 std::vector<std::string>::iterator it;
64
65 std::string path(name);
66
67 #if defined(_WIN32) || defined(__WIN32__)
68 boost::replace_all(path, "/", "\\");
69 #endif
70
71 for (it = gSearchPaths.begin(); it != gSearchPaths.end(); ++it)
72 {
73 std::string fullPath(*it);
74 fullPath += path;
75
76 if (access(fullPath.c_str(), R_OK) == 0) return fullPath;
77 }
78
79 logWarning << "cannot find resource " << name << std::endl;
80
81 // empty string
82 return std::string();
83 }
84
85
86 } // namespace Mf
87
This page took 0.034258 seconds and 4 git commands to generate.