| 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 | #ifndef __OPENCC_H_ |
| 20 | #define __OPENCC_H_ |
| 21 | |
| 22 | #ifdef __cplusplus |
| 23 | |
| 24 | #include "Export.hpp" |
| 25 | #include "SimpleConverter.hpp" |
| 26 | #include <string> |
| 27 | |
| 28 | extern "C" { |
| 29 | #else |
| 30 | #include <stddef.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifndef OPENCC_EXPORT |
| 34 | #define OPENCC_EXPORT |
| 35 | #endif |
| 36 | |
| 37 | /** |
| 38 | * @defgroup opencc_c_api OpenCC C API |
| 39 | * |
| 40 | * API in C language |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * Filename of default Simplified to Traditional configuration |
| 45 | * |
| 46 | * @ingroup opencc_c_api |
| 47 | */ |
| 48 | #define OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD "s2t.json" |
| 49 | |
| 50 | /** |
| 51 | * Filename of default Traditional to Simplified configuration |
| 52 | * |
| 53 | * @ingroup opencc_c_api |
| 54 | */ |
| 55 | #define OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP "t2s.json" |
| 56 | |
| 57 | /** |
| 58 | * Type of opencc descriptor |
| 59 | * |
| 60 | * @ingroup opencc_c_api |
| 61 | */ |
| 62 | typedef void* opencc_t; |
| 63 | |
| 64 | /** |
| 65 | * Makes an instance of opencc |
| 66 | * |
| 67 | * @param configFileName Location of configuration file. If this is set to NULL, |
| 68 | * OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD will be loaded. |
| 69 | * @return A description pointer of the newly allocated instance of |
| 70 | * opencc. On error the return value will be (opencc_t) -1. |
| 71 | * @ingroup opencc_c_api |
| 72 | */ |
| 73 | OPENCC_EXPORT opencc_t opencc_open(const char* configFileName); |
| 74 | #ifdef _MSC_VER |
| 75 | /** |
| 76 | * Makes an instance of opencc (wide char / Unicode) |
| 77 | * |
| 78 | * @param configFileName Location of configuration file. If this is set to NULL, |
| 79 | * OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD will be loaded. |
| 80 | * @return A description pointer of the newly allocated instance of |
| 81 | * opencc. On error the return value will be (opencc_t) -1. |
| 82 | * @ingroup opencc_c_api |
| 83 | */ |
| 84 | OPENCC_EXPORT opencc_t opencc_open_w(const wchar_t* configFileName); |
| 85 | #endif /* _MSC_VER */ |
| 86 | |
| 87 | /** |
| 88 | * Destroys an instance of opencc |
| 89 | * |
| 90 | * @param opencc The description pointer. |
| 91 | * @return 0 on success or non-zero number on failure. |
| 92 | * @ingroup opencc_c_api |
| 93 | */ |
| 94 | OPENCC_EXPORT int opencc_close(opencc_t opencc); |
| 95 | |
| 96 | /** |
| 97 | * Converts UTF-8 std::string |
| 98 | * |
| 99 | * @param opencc The opencc description pointer. |
| 100 | * @param input The UTF-8 encoded std::string. |
| 101 | * @param length The maximum length in byte to convert. If length is (size_t)-1, |
| 102 | * the whole std::string (terminated by '\0') will be converted. |
| 103 | * @param output The buffer to store converted text. You MUST make sure this |
| 104 | * buffer has sufficient space. |
| 105 | * |
| 106 | * @return The length of converted std::string or (size_t)-1 on error. |
| 107 | * |
| 108 | * @ingroup opencc_c_api |
| 109 | */ |
| 110 | OPENCC_EXPORT size_t opencc_convert_utf8_to_buffer(opencc_t opencc, |
| 111 | const char* input, |
| 112 | size_t length, char* output); |
| 113 | |
| 114 | /** |
| 115 | * Converts UTF-8 std::string |
| 116 | * This function returns an allocated C-Style std::string, which stores |
| 117 | * the converted std::string. |
| 118 | * You MUST call opencc_convert_utf8_free() to release allocated memory. |
| 119 | * |
| 120 | * @param opencc The opencc description pointer. |
| 121 | * @param input The UTF-8 encoded std::string. |
| 122 | * @param length The maximum length in byte to convert. If length is (size_t)-1, |
| 123 | * the whole std::string (terminated by '\0') will be converted. |
| 124 | * |
| 125 | * @return The newly allocated UTF-8 std::string that stores text |
| 126 | * converted, or NULL on error. |
| 127 | * @ingroup opencc_c_api |
| 128 | */ |
| 129 | OPENCC_EXPORT char* opencc_convert_utf8(opencc_t opencc, const char* input, |
| 130 | size_t length); |
| 131 | |
| 132 | /** |
| 133 | * Releases allocated buffer by opencc_convert_utf8 |
| 134 | * |
| 135 | * @param str Pointer to the allocated std::string buffer by |
| 136 | * opencc_convert_utf8. |
| 137 | * |
| 138 | * @ingroup opencc_c_api |
| 139 | */ |
| 140 | OPENCC_EXPORT void opencc_convert_utf8_free(char* str); |
| 141 | |
| 142 | /** |
| 143 | * Returns the last error message |
| 144 | * |
| 145 | * Note that this function is the only one which is NOT thread-safe. |
| 146 | * |
| 147 | * @ingroup opencc_c_api |
| 148 | */ |
| 149 | OPENCC_EXPORT const char* opencc_error(void); |
| 150 | |
| 151 | #ifdef __cplusplus |
| 152 | } // extern "C" |
| 153 | #endif |
| 154 | |
| 155 | /** |
| 156 | * @defgroup opencc_cpp_api OpenCC C++ Comprehensive API |
| 157 | * |
| 158 | * Comprehensive API in C++ language |
| 159 | */ |
| 160 | |
| 161 | #endif |