| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <string> |
| 22 | #include <string_view> |
| 23 | #include <memory> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "Export.hpp" |
| 27 | |
| 28 | namespace opencc { |
| 29 | |
| 30 | class OPENCC_EXPORT ResourceProvider { |
| 31 | public: |
| 32 | class OPENCC_EXPORT Resource { |
| 33 | public: |
| 34 | Resource(std::string name_, const char* data_, size_t size_, |
| 35 | std::shared_ptr<const void> owner_, std::string cacheKey_); |
| 36 | |
| 37 | const std::string& Name() const { return name; } |
| 38 | const char* Data() const { return data; } |
| 39 | size_t Size() const { return size; } |
| 40 | const std::string& CacheKey() const { return cacheKey; } |
| 41 | |
| 42 | private: |
| 43 | std::string name; |
| 44 | const char* data; |
| 45 | size_t size; |
| 46 | std::shared_ptr<const void> owner; |
| 47 | std::string cacheKey; |
| 48 | }; |
| 49 | |
| 50 | virtual ~ResourceProvider() = default; |
| 51 | |
| 52 | virtual std::string Resolve(std::string_view resourceName) const = 0; |
| 53 | |
| 54 | virtual std::shared_ptr<const Resource> |
| 55 | GetResource(std::string_view resourceName) const; |
| 56 | }; |
| 57 | |
| 58 | class OPENCC_EXPORT FilesystemResourceProvider : public ResourceProvider { |
| 59 | public: |
| 60 | explicit FilesystemResourceProvider(std::vector<std::string> searchPaths); |
| 61 | |
| 62 | std::string Resolve(std::string_view resourceName) const override; |
| 63 | |
| 64 | private: |
| 65 | std::vector<std::string> searchPaths; |
| 66 | }; |
| 67 | |
| 68 | class OPENCC_EXPORT ZipResourceProvider : public ResourceProvider { |
| 69 | public: |
| 70 | explicit ZipResourceProvider(std::string zipFileName); |
| 71 | ~ZipResourceProvider(); |
| 72 | |
| 73 | ZipResourceProvider(const ZipResourceProvider&) = delete; |
| 74 | ZipResourceProvider& operator=(const ZipResourceProvider&) = delete; |
| 75 | |
| 76 | std::string Resolve(std::string_view resourceName) const override; |
| 77 | |
| 78 | std::shared_ptr<const Resource> |
| 79 | GetResource(std::string_view resourceName) const override; |
| 80 | |
| 81 | private: |
| 82 | struct Internal; |
| 83 | std::unique_ptr<Internal> internal; |
| 84 | }; |
| 85 | |
| 86 | } // namespace opencc |