24 #include "config_reader.h"
25 #include "converter.h"
26 #include "dict_group.h"
27 #include "dict_chain.h"
36 static opencc_error errnum = OPENCC_ERROR_VOID;
37 static int lib_initialized = 0;
39 static void lib_initialize(
void) {
41 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
50 size_t* outbuf_left) {
51 if (!lib_initialized) {
55 size_t retval = converter_convert(opencc->converter,
60 if (retval == (
size_t)-1) {
61 errnum = OPENCC_ERROR_CONVERTER;
67 if (!lib_initialized) {
70 size_t actual_length = strlen(inbuf);
71 if ((length == (
size_t)-1) || (length > actual_length)) {
72 length = actual_length;
75 if (winbuf == (ucs4_t*)-1) {
77 errnum = OPENCC_ERROR_ENCODING;
81 size_t outbuf_len = length;
82 size_t outsize = outbuf_len;
83 char* original_outbuf = (
char*)malloc(
sizeof(
char) * (outbuf_len + 1));
84 char* outbuf = original_outbuf;
85 original_outbuf[0] =
'\0';
87 size_t wbufsize = length + 64;
88 ucs4_t* woutbuf = (ucs4_t*)malloc(
sizeof(ucs4_t) * (wbufsize + 1));
89 ucs4_t* pinbuf = winbuf;
90 ucs4_t* poutbuf = woutbuf;
91 size_t inbuf_left, outbuf_left;
92 inbuf_left = ucs4len(winbuf);
93 outbuf_left = wbufsize;
94 while (inbuf_left > 0) {
100 if (retval == (
size_t)-1) {
108 if (ubuff == (
char*)-1) {
112 errnum = OPENCC_ERROR_ENCODING;
115 size_t ubuff_len = strlen(ubuff);
116 while (ubuff_len > outsize) {
117 size_t outbuf_offset = outbuf - original_outbuf;
118 outsize += outbuf_len;
119 outbuf_len += outbuf_len;
121 (
char*)realloc(original_outbuf,
sizeof(
char) * outbuf_len);
122 outbuf = original_outbuf + outbuf_offset;
124 strncpy(outbuf, ubuff, ubuff_len);
128 outbuf_left = wbufsize;
133 original_outbuf = (
char*)realloc(original_outbuf,
134 sizeof(
char) * (strlen(original_outbuf) + 1));
135 return original_outbuf;
143 if (!lib_initialized) {
148 opencc->dict_chain = NULL;
149 opencc->converter = converter_open();
150 converter_set_conversion_mode(opencc->converter, OPENCC_CONVERSION_FAST);
151 if (config_file == NULL) {
156 Config* config = config_open(config_file);
157 if (config == (
Config*)-1) {
158 errnum = OPENCC_ERROR_CONFIG;
161 opencc->dict_chain = config_get_dict_chain(config);
162 converter_assign_dictionary(opencc->converter, opencc->dict_chain);
163 config_close(config);
165 return (opencc_t)opencc;
169 if (!lib_initialized) {
173 converter_close(opencc->converter);
174 if (opencc->dict_chain != NULL) {
175 dict_chain_delete(opencc->dict_chain);
182 const char* dict_filename,
183 opencc_dictionary_type dict_type) {
184 if (!lib_initialized) {
189 if (opencc->dict_chain == NULL) {
190 opencc->dict_chain = dict_chain_new(NULL);
191 DictGroup = dict_chain_add_group(opencc->dict_chain);
193 DictGroup = dict_chain_get_group(opencc->dict_chain, 0);
195 int retval = dict_group_load(DictGroup, dict_filename, dict_type);
197 errnum = OPENCC_ERROR_DICTLOAD;
200 converter_assign_dictionary(opencc->converter, opencc->dict_chain);
205 opencc_conversion_mode conversion_mode) {
206 if (!lib_initialized) {
210 converter_set_conversion_mode(opencc->converter, conversion_mode);
214 if (!lib_initialized) {
221 if (!lib_initialized) {
227 case OPENCC_ERROR_VOID:
229 case OPENCC_ERROR_DICTLOAD:
230 dictionary_perror(_(
"Dictionary loading error"));
232 case OPENCC_ERROR_CONFIG:
233 config_perror(_(
"Configuration error"));
235 case OPENCC_ERROR_CONVERTER:
236 converter_perror(_(
"Converter error"));
238 case OPENCC_ERROR_ENCODING:
239 perr(_(
"Encoding error"));
char * ucs4_to_utf8(const ucs4_t *ucs4, size_t length)
Converts a UCS-4 string into UTF-8.
char * opencc_convert_utf8(opencc_t t_opencc, const char *inbuf, size_t length)
Converts UTF-8 string from inbuf.
opencc_t opencc_open(const char *config_file)
Makes an instance of opencc.
void opencc_convert_utf8_free(char *buf)
Releases allocated buffer by opencc_convert_utf8.
ucs4_t * utf8_to_ucs4(const char *utf8, size_t length)
Converts a UTF-8 string into UCS-4.
UCS4-UTF8 Encoding module.
int opencc_dict_load(opencc_t t_opencc, const char *dict_filename, opencc_dictionary_type dict_type)
Loads a dictionary to default dictionary chain.
int opencc_close(opencc_t t_opencc)
Destroys an instance of opencc.
void opencc_perror(const char *spec)
Prints the error message to stderr.
opencc_error opencc_errno(void)
Returns an opencc_convert_errno_t which describes the last error.
void opencc_set_conversion_mode(opencc_t t_opencc, opencc_conversion_mode conversion_mode)
Changes the mode of conversion.
size_t opencc_convert(opencc_t t_opencc, ucs4_t **inbuf, size_t *inbuf_left, ucs4_t **outbuf, size_t *outbuf_left)
Converts a UCS-4 string from *inbuf to *outbuf.