diff --git a/src/rime/resource_resolver.cc b/src/rime/resource_resolver.cc new file mode 100644 index 0000000000..507e36bdb3 --- /dev/null +++ b/src/rime/resource_resolver.cc @@ -0,0 +1,16 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// + +#include + +namespace rime { + +boost::filesystem::path ResourceResolver::ResolvePath(const string& resource_id) { + return boost::filesystem::absolute( + boost::filesystem::path(type_.prefix + resource_id + type_.suffix), + root_path_); +} + +} // namespace rime diff --git a/src/rime/resource_resolver.h b/src/rime/resource_resolver.h new file mode 100644 index 0000000000..8837086c41 --- /dev/null +++ b/src/rime/resource_resolver.h @@ -0,0 +1,40 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// + +#ifndef RIME_RESOURCE_RESOLVER_H_ +#define RIME_RESOURCE_RESOLVER_H_ + +#include +#include + +namespace rime { + +class ResourceResolver; + +struct ResourceType { + string name; + string prefix; + string suffix; +}; + +class ResourceResolver { + public: + explicit ResourceResolver(const ResourceType type) : type_(type) { + } + boost::filesystem::path ResolvePath(const string& resource_id); + void set_root_path(const boost::filesystem::path& root_path) { + root_path_ = root_path; + } + boost::filesystem::path root_path() const { + return root_path_; + } + private: + const ResourceType type_; + boost::filesystem::path root_path_; +}; + +} // namespace rime + +#endif // RIME_RESOURCE_RESOLVER_H_ diff --git a/test/resource_resolver_test.cc b/test/resource_resolver_test.cc new file mode 100644 index 0000000000..9a0aafcab9 --- /dev/null +++ b/test/resource_resolver_test.cc @@ -0,0 +1,17 @@ +#include +#include + +using namespace rime; + +TEST(RimeResourceResolverTest, ResolvePath) { + const auto type = ResourceType{ + "minerals", + "not_", + ".minerals", + }; + the rr(new ResourceResolver(type)); + rr->set_root_path("/starcraft"); + auto actual = rr->ResolvePath("enough"); + boost::filesystem::path expected = "/starcraft/not_enough.minerals"; + EXPECT_TRUE(expected == actual); +}