| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2014 Carbo Kuo <[email protected]> |
| 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 | #include "Export.hpp" |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <string_view> |
| 23 | #include <vector> |
| 24 | |
| 25 | #ifndef __OPENCC_SIMPLECONVERTER_HPP_ |
| 26 | #define __OPENCC_SIMPLECONVERTER_HPP_ |
| 27 | |
| 28 | #include "ConversionInspection.hpp" |
| 29 | #include "ResourceProvider.hpp" |
| 30 | |
| 31 | /** |
| 32 | * @defgroup opencc_simple_api OpenCC C++ Simple API |
| 33 | * |
| 34 | * Simple API in C++ language |
| 35 | */ |
| 36 | |
| 37 | namespace opencc { |
| 38 | |
| 39 | struct ConfigLoadOptions; |
| 40 | |
| 41 | /** |
| 42 | * A high-level converter. |
| 43 | * @ingroup opencc_simple_api |
| 44 | */ |
| 45 | class OPENCC_EXPORT SimpleConverter { |
| 46 | public: |
| 47 | /** |
| 48 | * Constructor of SimpleConverter |
| 49 | * @param configFileName File name of configuration. |
| 50 | */ |
| 51 | explicit SimpleConverter(const std::string& configFileName); |
| 52 | |
| 53 | /** |
| 54 | * Constructor of SimpleConverter |
| 55 | * @param configFileName File name of configuration. |
| 56 | * @param options Configuration loading options. |
| 57 | */ |
| 58 | SimpleConverter(const std::string& configFileName, |
| 59 | const ConfigLoadOptions& options); |
| 60 | |
| 61 | /** |
| 62 | * Constructor of SimpleConverter |
| 63 | * @param configFileName File name of configuration. |
| 64 | * @param paths Additional paths to locate configuration and dictionary files. |
| 65 | */ |
| 66 | SimpleConverter(const std::string& configFileName, |
| 67 | const std::vector<std::string>& paths); |
| 68 | |
| 69 | /** |
| 70 | * Constructor of SimpleConverter |
| 71 | * @param configFileName File name of configuration. |
| 72 | * @param paths Additional paths to locate configuration and dictionary files. |
| 73 | * @param options Configuration loading options. |
| 74 | */ |
| 75 | SimpleConverter(const std::string& configFileName, |
| 76 | const std::vector<std::string>& paths, |
| 77 | const ConfigLoadOptions& options); |
| 78 | |
| 79 | /** |
| 80 | * Constructor of SimpleConverter |
| 81 | * @param configFileName File name of configuration. |
| 82 | * @param paths Additional paths to locate configuration and dictionary files. |
| 83 | * @param argv0 Path of the executable (argv[0]), in addition to additional |
| 84 | * paths. |
| 85 | */ |
| 86 | SimpleConverter(const std::string& configFileName, |
| 87 | const std::vector<std::string>& paths, const char* argv0); |
| 88 | |
| 89 | /** |
| 90 | * Constructor of SimpleConverter |
| 91 | * @param configFileName File name of configuration. |
| 92 | * @param paths Additional paths to locate configuration and dictionary files. |
| 93 | * @param argv0 Path of the executable (argv[0]), in addition to additional |
| 94 | * paths. |
| 95 | * @param options Configuration loading options. |
| 96 | */ |
| 97 | SimpleConverter(const std::string& configFileName, |
| 98 | const std::vector<std::string>& paths, const char* argv0, |
| 99 | const ConfigLoadOptions& options); |
| 100 | |
| 101 | /** |
| 102 | * Constructor of SimpleConverter |
| 103 | * @param configFileName File name of configuration. |
| 104 | * @param provider Provider used to locate dictionary/resource files |
| 105 | * referenced by the configuration. |
| 106 | */ |
| 107 | SimpleConverter(const std::string& configFileName, |
| 108 | std::shared_ptr<ResourceProvider> provider); |
| 109 | |
| 110 | /** |
| 111 | * Constructor of SimpleConverter |
| 112 | * @param configFileName File name of configuration. |
| 113 | * @param provider Provider used to locate dictionary/resource files |
| 114 | * referenced by the configuration. |
| 115 | * @param options Configuration loading options. |
| 116 | */ |
| 117 | SimpleConverter(const std::string& configFileName, |
| 118 | std::shared_ptr<ResourceProvider> provider, |
| 119 | const ConfigLoadOptions& options); |
| 120 | |
| 121 | ~SimpleConverter(); |
| 122 | |
| 123 | /** |
| 124 | * Converts a text |
| 125 | * @param input Text to be converted. |
| 126 | */ |
| 127 | std::string Convert(const std::string& input) const; |
| 128 | |
| 129 | /** |
| 130 | * Converts a text without requiring a null-terminated buffer. |
| 131 | * New code should prefer this overload when a string_view is already |
| 132 | * available. |
| 133 | * @param input Text to be converted. |
| 134 | */ |
| 135 | std::string Convert(std::string_view input) const; |
| 136 | |
| 137 | /** |
| 138 | * Converts a text |
| 139 | * @param input A C-Style std::string (terminated by '\0') to be converted. |
| 140 | */ |
| 141 | std::string Convert(const char* input) const; |
| 142 | |
| 143 | /** |
| 144 | * Converts a text |
| 145 | * @param input A C-Style std::string limited by a given length to be |
| 146 | * converted. |
| 147 | * @param length Maximal length in byte of the input std::string. |
| 148 | */ |
| 149 | std::string Convert(const char* input, size_t length) const; |
| 150 | |
| 151 | /** |
| 152 | * Converts a text and writes to an allocated buffer |
| 153 | * Please make sure the buffer has sufficient space. |
| 154 | * @param input A C-Style std::string (terminated by '\0') to be converted. |
| 155 | * @param output Buffer to write the converted text. |
| 156 | * @return Length of converted text. |
| 157 | */ |
| 158 | size_t Convert(const char* input, char* output) const; |
| 159 | |
| 160 | /** |
| 161 | * Converts a text and writes to an allocated buffer |
| 162 | * Please make sure the buffer has sufficient space. |
| 163 | * @param input A C-Style std::string limited by a given length to be |
| 164 | * converted. |
| 165 | * @param length Maximal length in byte of the input std::string. |
| 166 | * @param output Buffer to write the converted text. |
| 167 | * @return Length of converted text. |
| 168 | */ |
| 169 | size_t Convert(const char* input, size_t length, char* output) const; |
| 170 | |
| 171 | /** |
| 172 | * Inspects the conversion process for a given text. |
| 173 | * Returns the segmentation result, per-stage conversion outputs, and final |
| 174 | * output string. |
| 175 | * @param input Text to be inspected. |
| 176 | */ |
| 177 | ConversionInspectionResult Inspect(std::string_view input) const; |
| 178 | |
| 179 | private: |
| 180 | const void* internalData; |
| 181 | }; |
| 182 | |
| 183 | } // namespace opencc |
| 184 | |
| 185 | #endif |