]> Dogcows Code - chaz/yoink/blob - src/moof/resource.cc
the massive refactoring effort
[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 moof {
19
20
21 // static member
22 std::vector<std::string> resource::search_paths_;
23
24
25 void resource::add_search_paths(const std::string& paths)
26 {
27 std::vector<std::string> pathList;
28 boost::split(pathList, paths, boost::is_any_of(":"));
29
30 add_search_paths(pathList);
31 }
32
33 void resource::add_search_paths(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 search_paths_.push_back(path);
50 log_info << "added search path " << path << std::endl;
51 }
52 }
53
54
55 bool resource::find_path(std::string& path,
56 const std::string& prefix,
57 const std::string& extension)
58 {
59 FILE* file = open_file(path, prefix, extension);
60 if (file)
61 {
62 fclose(file);
63 return true;
64 }
65
66 return false;
67 }
68
69 FILE* resource::open_file(std::string& path,
70 const std::string& prefix,
71 const std::string& extension,
72 const std::string& mode)
73 {
74 #if defined(_WIN32)
75 // windows always has to be a little different
76 boost::replace_all(path, "/", "\\");
77 std::string temp_prefix(prefix);
78 boost::replace_all(temp_prefix, "/", "\\");
79 std::vector<std::string> preList;
80 boost::split(preList, temp_prefix, boost::is_any_of(":"));
81 #else
82 std::vector<std::string> preList;
83 boost::split(preList, prefix, boost::is_any_of(":"));
84 #endif
85
86 std::vector<std::string> postList;
87 boost::split(postList, extension, boost::is_any_of(":"));
88
89 std::vector<std::string>::iterator it;
90 for (it = search_paths_.begin(); it != search_paths_.end(); ++it)
91 {
92 std::vector<std::string>::iterator jt;
93 for (jt = preList.begin(); jt != preList.end(); ++jt)
94 {
95 std::vector<std::string>::iterator kt;
96 for (kt = postList.begin(); kt != postList.end(); ++kt)
97 {
98 std::string realPath(*it);
99 realPath += *jt;
100 realPath += path;
101 realPath += ".";
102 realPath += *kt;
103
104 FILE* file = fopen(realPath.c_str(), mode.c_str());
105 if (file)
106 {
107 path = realPath;
108 return file;
109 }
110 }
111 }
112
113 // check path relative to search path
114 std::string realPath(*it);
115 realPath += path;
116
117 FILE* file = fopen(realPath.c_str(), mode.c_str());
118 if (file)
119 {
120 path = realPath;
121 return file;
122 }
123 }
124
125 // last ditch effort; maybe it's already a path to a valid resource
126 return fopen(path.c_str(), mode.c_str());
127 }
128
129
130 } // namespace moof
131
This page took 0.043596 seconds and 5 git commands to generate.