]> Dogcows Code - chaz/yoink/blob - src/Moof/Resource.cc
bugfix: win32 packaging script temp directories
[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 <boost/algorithm/string.hpp>
13
14 #include "Log.hh"
15 #include "Resource.hh"
16
17
18 namespace Mf {
19
20
21 // static member
22 std::vector<std::string> Resource::gSearchPaths;
23
24
25 void Resource::addSearchPaths(const std::string& paths)
26 {
27 std::vector<std::string> pathList;
28 boost::split(pathList, paths, boost::is_any_of(":"));
29
30 addSearchPaths(pathList);
31 }
32
33 void Resource::addSearchPaths(const std::vector<std::string>& pathList)
34 {
35 std::vector<std::string>::const_iterator it;
36 for (it = pathList.begin(); it != pathList.end(); ++it)
37 {
38 std::string path(*it);
39
40 ASSERT(!path.empty() && "empty search path string");
41
42 // add a slash if there isn't one already
43 if (*path.rbegin() != '/') path += '/';
44
45 #if defined(_WIN32)
46 boost::replace_all(path, "/", "\\");
47 #endif
48
49 gSearchPaths.push_back(path);
50 logInfo << "added search path " << path << std::endl;
51 }
52 }
53
54
55 std::string Resource::getPath(const std::string& path,
56 const std::string& prefix,
57 const std::string& extension)
58 {
59 std::string realPath(path);
60 if (getPath(realPath, prefix, extension)) return realPath;
61
62 return std::string();
63 }
64
65 bool Resource::getPath(std::string& path,
66 const std::string& prefix,
67 const std::string& extension)
68 {
69 FILE* file = openFile(path, prefix, extension);
70 if (file)
71 {
72 fclose(file);
73 return true;
74 }
75
76 return false;
77 }
78
79 FILE* Resource::openFile(std::string& path,
80 std::string prefix,
81 const std::string& extension,
82 const std::string& mode)
83 {
84 #if defined(_WIN32)
85 // Windows always has to be the odd one.
86 boost::replace_all(path, "/", "\\");
87 boost::replace_all(prefix, "/", "\\");
88 #endif
89
90 std::vector<std::string> preList;
91 boost::split(preList, prefix, boost::is_any_of(":"));
92 std::vector<std::string> postList;
93 boost::split(postList, extension, boost::is_any_of(":"));
94
95 std::vector<std::string>::iterator it;
96 for (it = gSearchPaths.begin(); it != gSearchPaths.end(); ++it)
97 {
98 std::vector<std::string>::iterator jt;
99 for (jt = preList.begin(); jt != preList.end(); ++jt)
100 {
101 std::vector<std::string>::iterator kt;
102 for (kt = postList.begin(); kt != postList.end(); ++kt)
103 {
104 std::string realPath(*it);
105 realPath += *jt;
106 realPath += path;
107 realPath += ".";
108 realPath += *kt;
109
110 FILE* file = fopen(realPath.c_str(), mode.c_str());
111 if (file)
112 {
113 path = realPath;
114 return file;
115 }
116 }
117 }
118
119 // check path relative to search path
120 std::string realPath(*it);
121 realPath += path;
122
123 FILE* file = fopen(realPath.c_str(), mode.c_str());
124 if (file)
125 {
126 path = realPath;
127 return file;
128 }
129 }
130
131 // last ditch effort; maybe it's already a path to a valid resource
132 return fopen(path.c_str(), mode.c_str());
133 }
134
135
136 } // namespace Mf
137
This page took 0.039246 seconds and 4 git commands to generate.