| 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 | #include <cstring> |
| 20 | |
| 21 | #include "Config.hpp" |
| 22 | #include "Converter.hpp" |
| 23 | #include "UTF8Util.hpp" |
| 24 | #include "opencc.h" |
| 25 | |
| 26 | #ifdef BAZEL |
| 27 | #include "tools/cpp/runfiles/runfiles.h" |
| 28 | using bazel::tools::cpp::runfiles::Runfiles; |
| 29 | #endif |
| 30 | |
| 31 | using namespace opencc; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | struct InternalData { |
| 36 | const ConverterPtr converter; |
| 37 | |
| 38 | InternalData(const ConverterPtr& _converter) : converter(_converter) {} |
| 39 | |
| 40 | static InternalData* NewInternalData(const std::string& configFileName, |
| 41 | const std::vector<std::string>& paths, |
| 42 | const char* argv0, |
| 43 | const ConfigLoadOptions& options) { |
| 44 | try { |
| 45 | Config config; |
| 46 | #ifdef BAZEL |
| 47 | std::string err; |
| 48 | std::unique_ptr<Runfiles> bazel_runfiles( |
| 49 | Runfiles::Create(argv0 != nullptr ? argv0 : "", &err)); |
| 50 | if (bazel_runfiles != nullptr) { |
| 51 | std::vector<std::string> paths_with_runfiles = paths; |
| 52 | paths_with_runfiles.push_back( |
| 53 | bazel_runfiles->Rlocation("opencc~/data/config")); |
| 54 | paths_with_runfiles.push_back( |
| 55 | bazel_runfiles->Rlocation("opencc~/data/dictionary")); |
| 56 | paths_with_runfiles.push_back( |
| 57 | bazel_runfiles->Rlocation("_main/data/config")); |
| 58 | paths_with_runfiles.push_back( |
| 59 | bazel_runfiles->Rlocation("_main/data/dictionary")); |
| 60 | return new InternalData( |
| 61 | config.NewFromFile(configFileName, paths_with_runfiles, argv0, |
| 62 | options)); |
| 63 | } |
| 64 | #endif |
| 65 | return new InternalData( |
| 66 | config.NewFromFile(configFileName, paths, argv0, options)); |
| 67 | } catch (Exception& ex) { |
| 68 | throw std::runtime_error(ex.what()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static InternalData* NewInternalData( |
| 73 | const std::string& configFileName, |
| 74 | const std::shared_ptr<ResourceProvider>& provider, |
| 75 | const ConfigLoadOptions& options) { |
| 76 | try { |
| 77 | Config config; |
| 78 | return new InternalData( |
| 79 | config.NewFromFile(configFileName, provider, options)); |
| 80 | } catch (Exception& ex) { |
| 81 | throw std::runtime_error(ex.what()); |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | SimpleConverter::SimpleConverter(const std::string& configFileName) |
| 89 | : SimpleConverter(configFileName, ConfigLoadOptions()) {} |
| 90 | |
| 91 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 92 | const ConfigLoadOptions& options) |
| 93 | : SimpleConverter(configFileName, std::vector<std::string>(), options) {} |
| 94 | |
| 95 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 96 | const std::vector<std::string>& paths) |
| 97 | : SimpleConverter(configFileName, paths, ConfigLoadOptions()) {} |
| 98 | |
| 99 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 100 | const std::vector<std::string>& paths, |
| 101 | const ConfigLoadOptions& options) |
| 102 | : SimpleConverter(configFileName, paths, nullptr, options) {} |
| 103 | |
| 104 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 105 | const std::vector<std::string>& paths, |
| 106 | const char* argv0) |
| 107 | : SimpleConverter(configFileName, paths, argv0, ConfigLoadOptions()) {} |
| 108 | |
| 109 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 110 | const std::vector<std::string>& paths, |
| 111 | const char* argv0, |
| 112 | const ConfigLoadOptions& options) |
| 113 | : internalData( |
| 114 | InternalData::NewInternalData(configFileName, paths, argv0, |
| 115 | options)) {} |
| 116 | |
| 117 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 118 | std::shared_ptr<ResourceProvider> provider) |
| 119 | : SimpleConverter(configFileName, provider, ConfigLoadOptions()) {} |
| 120 | |
| 121 | SimpleConverter::SimpleConverter(const std::string& configFileName, |
| 122 | std::shared_ptr<ResourceProvider> provider, |
| 123 | const ConfigLoadOptions& options) |
| 124 | : internalData( |
| 125 | InternalData::NewInternalData(configFileName, provider, options)) {} |
| 126 | |
| 127 | SimpleConverter::~SimpleConverter() { delete (InternalData*)internalData; } |
| 128 | |
| 129 | std::string SimpleConverter::Convert(const std::string& input) const { |
| 130 | return Convert(std::string_view(input)); |
| 131 | } |
| 132 | |
| 133 | std::string SimpleConverter::Convert(std::string_view input) const { |
| 134 | try { |
| 135 | const InternalData* data = (InternalData*)internalData; |
| 136 | return data->converter->Convert(input); |
| 137 | } catch (Exception& ex) { |
| 138 | throw std::runtime_error(ex.what()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | std::string SimpleConverter::Convert(const char* input) const { |
| 143 | return Convert(std::string_view(input)); |
| 144 | } |
| 145 | |
| 146 | std::string SimpleConverter::Convert(const char* input, size_t length) const { |
| 147 | if (length == static_cast<size_t>(-1)) { |
| 148 | return Convert(std::string_view(input)); |
| 149 | } else { |
| 150 | return Convert(std::string_view(input, length)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | size_t SimpleConverter::Convert(const char* input, char* output) const { |
| 155 | try { |
| 156 | const InternalData* data = (InternalData*)internalData; |
| 157 | const std::string converted = data->converter->Convert(input); |
| 158 | strcpy(output, converted.c_str()); |
| 159 | return converted.length(); |
| 160 | } catch (Exception& ex) { |
| 161 | throw std::runtime_error(ex.what()); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | size_t SimpleConverter::Convert(const char* input, size_t length, |
| 166 | char* output) const { |
| 167 | if (length == static_cast<size_t>(-1)) { |
| 168 | return Convert(input, output); |
| 169 | } else { |
| 170 | std::string trimmed = UTF8Util::FromSubstr(input, length); |
| 171 | return Convert(trimmed.c_str(), output); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | ConversionInspectionResult |
| 176 | SimpleConverter::Inspect(std::string_view input) const { |
| 177 | try { |
| 178 | const InternalData* data = (InternalData*)internalData; |
| 179 | return data->converter->Inspect(input); |
| 180 | } catch (Exception& ex) { |
| 181 | throw std::runtime_error(ex.what()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static std::string cError; |
| 186 | |
| 187 | opencc_t opencc_open_internal(const char* configFileName) { |
| 188 | try { |
| 189 | if (configFileName == nullptr) { |
| 190 | configFileName = OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD; |
| 191 | } |
| 192 | SimpleConverter* instance = new SimpleConverter(configFileName); |
| 193 | return instance; |
| 194 | } catch (std::runtime_error& ex) { |
| 195 | cError = ex.what(); |
| 196 | return reinterpret_cast<opencc_t>(-1); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | #ifdef _MSC_VER |
| 201 | opencc_t opencc_open_w(const wchar_t* configFileName) { |
| 202 | try { |
| 203 | if (configFileName == nullptr) { |
| 204 | return opencc_open_internal(nullptr); |
| 205 | } |
| 206 | std::string utf8fn = UTF8Util::U16ToU8(configFileName); |
| 207 | return opencc_open_internal(utf8fn.c_str()); |
| 208 | } catch (std::runtime_error& ex) { |
| 209 | cError = ex.what(); |
| 210 | return reinterpret_cast<opencc_t>(-1); |
| 211 | } |
| 212 | } |
| 213 | opencc_t opencc_open(const char* configFileName) { |
| 214 | if (configFileName == nullptr) { |
| 215 | return opencc_open_internal(nullptr); |
| 216 | } |
| 217 | std::wstring wFileName; |
| 218 | int convcnt = MultiByteToWideChar(CP_ACP, 0, configFileName, -1, NULL, 0); |
| 219 | if (convcnt > 0) { |
| 220 | wFileName.resize(convcnt); |
| 221 | MultiByteToWideChar(CP_ACP, 0, configFileName, -1, &wFileName[0], convcnt); |
| 222 | } |
| 223 | return opencc_open_w(wFileName.c_str()); |
| 224 | } |
| 225 | #else |
| 226 | opencc_t opencc_open(const char* configFileName) { |
| 227 | return opencc_open_internal(configFileName); |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | int opencc_close(opencc_t opencc) { |
| 232 | SimpleConverter* instance = reinterpret_cast<SimpleConverter*>(opencc); |
| 233 | delete instance; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | size_t opencc_convert_utf8_to_buffer(opencc_t opencc, const char* input, |
| 238 | size_t length, char* output) { |
| 239 | try { |
| 240 | SimpleConverter* instance = reinterpret_cast<SimpleConverter*>(opencc); |
| 241 | return instance->Convert(input, length, output); |
| 242 | } catch (std::runtime_error& ex) { |
| 243 | cError = ex.what(); |
| 244 | return static_cast<size_t>(-1); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | char* opencc_convert_utf8(opencc_t opencc, const char* input, size_t length) { |
| 249 | try { |
| 250 | SimpleConverter* instance = reinterpret_cast<SimpleConverter*>(opencc); |
| 251 | std::string converted = instance->Convert(input, length); |
| 252 | char* output = new char[converted.length() + 1]; |
| 253 | memcpy(output, converted.c_str(), converted.length()); |
| 254 | output[converted.length()] = '\0'; |
| 255 | return output; |
| 256 | } catch (std::runtime_error& ex) { |
| 257 | cError = ex.what(); |
| 258 | return nullptr; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void opencc_convert_utf8_free(char* str) { delete[] str; } |
| 263 | |
| 264 | const char* opencc_error(void) { return cError.c_str(); } |