]>
Dogcows Code - chaz/yoink/blob - src/Moof/Resource.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #include <boost/algorithm/string.hpp>
15 #include "Resource.hh"
22 std::vector
<std::string
> Resource::gSearchPaths
;
25 void Resource::addSearchPaths(const std::string
& paths
)
27 std::vector
<std::string
> pathList
;
28 boost::split(pathList
, paths
, boost::is_any_of(":"));
30 addSearchPaths(pathList
);
33 void Resource::addSearchPaths(const std::vector
<std::string
>& pathList
)
35 std::vector
<std::string
>::const_iterator it
;
36 for (it
= pathList
.begin(); it
!= pathList
.end(); ++it
)
38 std::string
path(*it
);
40 ASSERT(!path
.empty() && "empty search path string");
42 // add a slash if there isn't one already
43 if (*path
.rbegin() != '/') path
+= '/';
46 boost::replace_all(path
, "/", "\\");
49 gSearchPaths
.push_back(path
);
50 logInfo
<< "added search path " << path
<< std::endl
;
55 std::string
Resource::getPath(const std::string
& path
,
56 const std::string
& prefix
,
57 const std::string
& extension
)
59 std::string
realPath(path
);
60 if (getPath(realPath
, prefix
, extension
)) return realPath
;
65 bool Resource::getPath(std::string
& path
,
66 const std::string
& prefix
,
67 const std::string
& extension
)
69 FILE* file
= openFile(path
, prefix
, extension
);
79 FILE* Resource::openFile(std::string
& path
,
81 const std::string
& extension
,
82 const std::string
& mode
)
85 // Windows always has to be the odd one.
86 boost::replace_all(path
, "/", "\\");
87 boost::replace_all(prefix
, "/", "\\");
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(":"));
95 std::vector
<std::string
>::iterator it
;
96 for (it
= gSearchPaths
.begin(); it
!= gSearchPaths
.end(); ++it
)
98 std::vector
<std::string
>::iterator jt
;
99 for (jt
= preList
.begin(); jt
!= preList
.end(); ++jt
)
101 std::vector
<std::string
>::iterator kt
;
102 for (kt
= postList
.begin(); kt
!= postList
.end(); ++kt
)
104 std::string
realPath(*it
);
110 FILE* file
= fopen(realPath
.c_str(), mode
.c_str());
119 // check path relative to search path
120 std::string
realPath(*it
);
123 FILE* file
= fopen(realPath
.c_str(), mode
.c_str());
131 // last ditch effort; maybe it's already a path to a valid resource
132 return fopen(path
.c_str(), mode
.c_str());
This page took 0.041622 seconds and 4 git commands to generate.