void Converter::setConfig(const char *config) { if (!s_loaded) return; if (handle != NULL) opencc_close(handle); handle = opencc_open(config); if (handle == (opencc_t) -1) { opencc_perror("Opencc loading:"); return; } m_loaded = true; }
const char *Convert(const char *input, const char *config) { if(strlen(config) > 16) { return 0; } char configFile[256] = "/usr/share/opencc/"; strcat(configFile, config); strcat(configFile, ".json"); opencc_t p = opencc_open(configFile); char *out = opencc_convert_utf8(p, input, strlen(input)); out[strlen(input)] = '\0'; opencc_close(p); return out; }
static PyObject * new_opencc_object(char *config) { openccobject *dp; dp = PyObject_New(openccobject, &Opencctype); if (dp == NULL) return NULL; if (!(dp->opencc = opencc_open(config))) { PyErr_SetString(PyExc_IOError, "Cannot open config file."); Py_DECREF(dp); return NULL; } return (PyObject *)dp; }
void convert(const char * input_file, const char * output_file, const char * config_file) { opencc_t od = opencc_open(config_file); if (od == (opencc_t) -1) { opencc_perror(_("OpenCC initialization error")); exit(1); } FILE * fp = stdin; FILE * fpo = stdout; if (input_file) { fp = fopen(input_file, "r"); if (!fp) { fprintf(stderr, _("Can not read file: %s\n"), input_file); exit(1); } } if (output_file) { fpo = fopen(output_file, "w"); if (!fpo) { fprintf(stderr, _("Can not write file: %s\n"), output_file); exit(1); } } size_t size = BUFFER_SIZE; char * buffer_in = NULL, * buffer_out = NULL; buffer_in = (char *) malloc(size * sizeof(char)); while (fgets(buffer_in, size, fp) != NULL) { size_t freesize = size; char * buffer_in_p = buffer_in; size_t line_length = strlen(buffer_in_p); while (line_length + 1 == freesize && buffer_in_p[line_length - 2] != '\n') { //如果一行沒讀完,則最後一個字符不是換行,且讀滿緩衝區 buffer_in_p += size - 1; freesize = size + 1; size += size; size_t offset = buffer_in_p - buffer_in; buffer_in = (char *) realloc(buffer_in, size * sizeof(char)); buffer_in_p = buffer_in + offset; if (fgets(buffer_in_p, freesize, fp) == NULL) break; line_length = strlen(buffer_in_p); } buffer_out = opencc_convert_utf8(od, buffer_in, (size_t) -1); if (buffer_out != (char *) -1) { fprintf(fpo, "%s", buffer_out); } else { opencc_perror(_("OpenCC error")); break; } } opencc_close(od); free(buffer_in); free(buffer_out); fclose(fp); fclose(fpo); }
ChineseConvertor::ChineseConvertor(const char *config) { opencc = opencc_open(config); }
/** * 该函数装载data/gbks2t.tab的简体转繁体的码表, * 然后按码表将GBK字符转换成GBK繁体字符。 * * WARNING: 该函数返回新分配内存字符串,请调用者 * 注意释放。 */ char *ConvertGBKTradition2Simple(FcitxChttrans* transState, const char *strHZ) { if (strHZ == NULL) return NULL; switch (transState->engine) { case ENGINE_OPENCC: #ifdef _ENABLE_OPENCC { if (transState->odt2s == NULL) { transState->odt2s = opencc_open(OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP); if (transState->odt2s == NULL) { opencc_perror(_("OpenCC initialization error")); return NULL; } } char * res = opencc_convert_utf8(transState->odt2s, strHZ, (size_t) - 1); if (res == (char *) - 1) { opencc_perror(_("OpenCC error")); return NULL; } return res; } #endif case ENGINE_NATIVE: { FILE *fp; char *ret; int i, len, ret_len; char *strBuf = NULL; size_t bufLen = 0; const char *ps; if (!transState->t2s_table) { len = 0; fp = FcitxXDGGetFileWithPrefix("data", TABLE_GBKS2T, "r", NULL); if (!fp) { ret = (char *) malloc(sizeof(char) * (strlen(strHZ) + 1)); strcpy(ret, strHZ); return ret; } while (getline(&strBuf, &bufLen, fp) != -1) { simple2trad_t *t2s; char *ps; unsigned int wc; ps = fcitx_utf8_get_char(strBuf, &wc); t2s = (simple2trad_t*) malloc(sizeof(simple2trad_t)); fcitx_utf8_get_char(ps, &wc); t2s->wc = wc; t2s->len = fcitx_utf8_char_len(strBuf); strncpy(t2s->str, strBuf, t2s->len); t2s->str[t2s->len] = '\0'; HASH_ADD_INT(transState->t2s_table, wc, t2s); } if (strBuf) free(strBuf); } i = 0; len = fcitx_utf8_strlen(strHZ); ret_len = 0; ret = (char *) fcitx_utils_malloc0(sizeof(char) * (UTF8_MAX_LENGTH * len + 1)); ps = strHZ; ret[0] = '\0'; for (; i < len; ++i) { unsigned int wc; simple2trad_t *t2s = NULL; int chr_len = fcitx_utf8_char_len(ps); char *nps; nps = fcitx_utf8_get_char(ps , &wc); HASH_FIND_INT(transState->t2s_table, &wc, t2s); if (t2s) { strcat(ret, t2s->str); ret_len += t2s->len; } else { strncat(ret, ps, chr_len); ret_len += chr_len; } ps = nps; } ret[ret_len] = '\0'; return ret; } } return NULL; }
void* Opencc_New(const char *configFile) { return opencc_open(configFile); }
void convert(const char * input_file, const char * output_file, const char * config_file) { opencc_t od = opencc_open(config_file); if (od == (opencc_t) -1) { opencc_perror(_("OpenCC initialization error")); exit(1); } FILE * fp = stdin; FILE * fpo = stdout; if (input_file) { fp = fopen(input_file, "r"); if (!fp) { fprintf(stderr, _("Can not read file: %s\n"), input_file); exit(1); } } if (output_file) { fpo = fopen(output_file, "w"); if (!fpo) { fprintf(stderr, _("Can not write file: %s\n"), output_file); exit(1); } } size_t size = BUFFER_SIZE; char * buffer_in = NULL, * buffer_out = NULL; buffer_in = (char *) malloc(size * sizeof(char)); while (!feof(fp)) { size_t read = fread(buffer_in, 1, size, fp); // If we haven't finished reading after filling the entire buffer, // then it could be that we broke within an UTF-8 character, in // that case we must backtrack and find the boundary if (read == size) { // Find the boundary of last UTF-8 character int i; for (i = read - 1; i >= 0; i--) { char c = buffer_in[i]; if (!(c & 0x80) || ((c & 0xC0) == 0xC0)) break; } if (i >= 0) { buffer_in[i] = '\0'; // Since we read a bit too much, move file pointer back // just that much so that we can continue reading size_t n = read - i; fseek(fp, -n, SEEK_CUR); } } buffer_out = opencc_convert_utf8(od, buffer_in, (size_t) -1); if (buffer_out != (char *) -1) { fprintf(fpo, "%s", buffer_out); free(buffer_out); } else { opencc_perror(_("OpenCC error")); break; } } opencc_close(od); free(buffer_in); fclose(fp); fclose(fpo); }