const int8_t* py_enhance_py_find_py(PinyinEnhance *pyenhance, const char *str) { py_enhance_load_py(pyenhance); if (!utarray_len(&pyenhance->py_list)) return NULL; int8_t **py_list; py_list = bsearch(str, _utarray_eltptr(&pyenhance->py_list, 0), utarray_len(&pyenhance->py_list), sizeof(int8_t*), (int (*)(const void*, const void*))compare_func); if (!py_list) return NULL; int8_t *res = *py_list; return res + *(res - 1); }
const FcitxPYEnhancePYList* py_enhance_py_find_py(PinyinEnhance *pyenhance, const char *str) { py_enhance_load_py(pyenhance); if (!utarray_len(&pyenhance->py_list)) return NULL; FcitxPYEnhancePYList **py_list; py_list = bsearch(str, _utarray_eltptr(&pyenhance->py_list, 0), utarray_len(&pyenhance->py_list), sizeof(FcitxPYEnhancePYList*), (int (*)(const void*, const void*))compare_func); if (py_list) return *py_list; return NULL; }
static void py_enhance_load_py(PinyinEnhance *pyenhance) { UT_array *array = &pyenhance->py_list; if (array->icd) return; utarray_init(array, fcitx_ptr_icd); FILE *fp; char *fname; fname = fcitx_utils_get_fcitx_path_with_filename( "pkgdatadir", "py-enhance/"PY_TABLE_FILE); fp = fopen(fname, "r"); free(fname); if (fp) { FcitxMemoryPool *pool = pyenhance->static_pool; char buff[UTF8_MAX_LENGTH + 1]; int buff_size = 33; int8_t *list_buff = malloc(buff_size); size_t res; int8_t word_l; int8_t count; int8_t py_size; int i; int8_t *py_list; int8_t *tmp; /** * Format: * int8_t word_l; * char word[word_l]; * int8_t count; * int8_t py[count][3]; **/ while (true) { res = fread(&word_l, 1, 1, fp); if (!res || word_l < 0 || word_l > UTF8_MAX_LENGTH) break; res = fread(buff, word_l + 1, 1, fp); if (!res) break; count = buff[word_l]; if (count < 0) break; if (count == 0) continue; py_size = count * 3; if (buff_size < py_size) { buff_size = py_size; list_buff = realloc(list_buff, buff_size); } res = fread(list_buff, py_size, 1, fp); if (!res) break; py_list = fcitx_memory_pool_alloc(pool, word_l + py_size + 3); py_list[0] = word_l + 1; py_list++; memcpy(py_list, buff, word_l); tmp = py_list + word_l; *tmp = '\0'; tmp++; *tmp = count; memcpy(tmp + 1, list_buff, py_size); for (i = utarray_len(array) - 1;i >= 0;i--) { if (strcmp(*(char**)_utarray_eltptr(array, i), (char*)py_list) < 0) { break; } } utarray_insert(array, &py_list, i + 1); } free(list_buff); fclose(fp); } }
static int fxaddon_scan_addon(FILE *ifp, FILE *ofp) { FcitxDesktopFile dfile; char *buff = NULL; unsigned int i; char **p; if (!fcitx_desktop_file_init(&dfile, NULL, NULL)) return 1; if (!fcitx_desktop_file_load_fp(&dfile, ifp)) return 1; fclose(ifp); FcitxDesktopGroup *addon_grp; FcitxDesktopEntry *tmp_ety; addon_grp = fcitx_desktop_file_find_group(&dfile, "FcitxAddon"); if (!addon_grp) return 1; tmp_ety = fcitx_desktop_group_find_entry(addon_grp, "Name"); if (!tmp_ety) return 1; const char *name = tmp_ety->value; tmp_ety = fcitx_desktop_group_find_entry(addon_grp, "Prefix"); if (!tmp_ety) return 1; const char *prefix = tmp_ety->value; UT_array macros; fxaddon_load_numbered_entries(¯os, addon_grp, "Macro", false); UT_array includes; fxaddon_load_numbered_entries(&includes, addon_grp, "Include", false); UT_array functions; fxaddon_load_numbered_entries(&functions, addon_grp, "Function", true); fxaddon_write_copyright(ofp); size_t name_len = strlen(name); buff = fcitx_utils_set_str_with_len(buff, name, name_len); fxaddon_name_to_macro(buff); _write_str(ofp, "\n#ifndef __FCITX_MODULE_"); _write_len(ofp, buff, name_len); _write_str(ofp, "_H\n"); _write_str(ofp, "#define __FCITX_MODULE_"); _write_len(ofp, buff, name_len); _write_str(ofp, "_H\n" "\n" "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n" "\n"); for (i = 0;i < utarray_len(¯os);i++) { p = (char**)_utarray_eltptr(¯os, i); fxaddon_write_macro(ofp, &dfile, *p); } fxaddon_write_includes(ofp, &includes); utarray_done(&includes); _write_str(ofp, "DEFINE_GET_ADDON(\""); _write_len(ofp, name, name_len); _write_str(ofp, "\", "); _write_str(ofp, prefix); _write_str(ofp, ")\n\n"); for (i = 0;i < utarray_len(&functions);i++) { p = (char**)_utarray_eltptr(&functions, i); fxaddon_write_function(ofp, &dfile, prefix, *p, i); } _write_str(ofp, "\n" "#ifdef __cplusplus\n" "}\n" "#endif\n" "\n" "#endif\n"); fclose(ofp); fcitx_utils_free(buff); fcitx_desktop_file_done(&dfile); utarray_done(&functions); return 0; }
static void fxaddon_write_function(FILE *ofp, FcitxDesktopFile *dfile, const char *prefix, const char *func_name, int id) { FcitxDesktopGroup *grp; unsigned int i; grp = fcitx_desktop_file_find_group(dfile, func_name); if (!grp) return; /* require the Name entry although not used now. */ if (!fcitx_desktop_group_find_entry(grp, "Name")) return; UT_array args; fxaddon_load_numbered_entries(&args, grp, "Arg", true); const char *type = fxaddon_function_get_return(grp); const char *err_ret = fxaddon_function_get_error_return(grp); boolean cache = fxaddon_function_get_cache_result(grp); boolean enable_wrapper = fxaddon_function_get_enable_wrapper(grp); if (cache && !type) { FcitxLog(WARNING, "Cannot cache result of type void."); cache = false; } if (!err_ret) { _write_str(ofp, "DEFINE_GET_AND_INVOKE_FUNC("); _write_str(ofp, prefix); _write_str(ofp, ", "); _write_str(ofp, func_name); _write_str(ofp, ", "); fprintf(ofp, "%d", id); _write_str(ofp, ")\n"); } else { _write_str(ofp, "DEFINE_GET_AND_INVOKE_FUNC_WITH_ERROR("); _write_str(ofp, prefix); _write_str(ofp, ", "); _write_str(ofp, func_name); _write_str(ofp, ", "); fprintf(ofp, "%d", id); _write_str(ofp, ", "); _write_str(ofp, err_ret); _write_str(ofp, ")\n"); } if (!enable_wrapper) _write_str(ofp, "#if 0\n"); _write_str(ofp, "static inline "); _write_str(ofp, type ? type : "void"); _write_str(ofp, "\nFcitx"); _write_str(ofp, prefix); _write_str(ofp, func_name); _write_str(ofp, "(FcitxInstance *instance"); char **p; for (i = 0;i < utarray_len(&args);i++) { p = (char**)_utarray_eltptr(&args, i); _write_str(ofp, ", "); _write_str(ofp, *p); _write_str(ofp, " arg"); fprintf(ofp, "%d", i); } _write_str(ofp, ")\n" "{\n"); if (cache) { _write_str(ofp, " static boolean _init = false;\n" " static void *result = NULL;\n" " if (fcitx_likely(_init))\n" " return ("); _write_str(ofp, type); _write_str(ofp, ")(intptr_t)result;\n" " _init = true;\n"); } else if (type) { _write_str(ofp, " void *result;\n"); } _write_str(ofp, " FCITX_DEF_MODULE_ARGS(args"); for (i = 0;i < utarray_len(&args);i++) { _write_str(ofp, ", (void*)(intptr_t)arg"); fprintf(ofp, "%d", i); } _write_str(ofp, ");\n" " "); if (type) { _write_str(ofp, "result = "); } _write_str(ofp, "Fcitx"); _write_str(ofp, prefix); _write_str(ofp, "Invoke"); _write_str(ofp, func_name); _write_str(ofp, "(instance, args);\n"); if (type) { _write_str(ofp, " return ("); _write_str(ofp, type); _write_str(ofp, ")(intptr_t)result;\n"); } if (enable_wrapper) { _write_str(ofp, "}\n\n"); } else { _write_str(ofp, "}\n" "#endif\n\n"); } utarray_done(&args); }
static void py_enhance_load_py(PinyinEnhance *pyenhance) { UT_array *array = &pyenhance->py_list; if (array->icd) return; utarray_init(array, fcitx_ptr_icd); FILE *fp; char *fname; fname = fcitx_utils_get_fcitx_path_with_filename( "pkgdatadir", "py-enhance/"PY_TABLE_FILE); fp = fopen(fname, "r"); free(fname); if (fp) { FcitxMemoryPool *pool = pyenhance->static_pool; char buff[UTF8_MAX_LENGTH + 1]; int buff_size = 33; int8_t *list_buff = malloc(buff_size); size_t res; int8_t word_l; int8_t count; int8_t min_size; FcitxPYEnhancePYList *py_list; int i; FcitxPYEnhancePY *py; while (true) { res = fread(&word_l, 1, 1, fp); if (!res || word_l < 0 || word_l > UTF8_MAX_LENGTH) break; res = fread(buff, word_l, 1, fp); if (!res) break; res = fread(&count, 1, 1, fp); if (!res || count < 0) break; if (count == 0) continue; min_size = count * 3; if (buff_size < min_size) { buff_size = min_size; list_buff = realloc(list_buff, buff_size); } res = fread(list_buff, min_size, 1, fp); if (!res) break; py_list = fcitx_memory_pool_alloc_align( pool, (sizeof(FcitxPYEnhancePYList) + sizeof(FcitxPYEnhancePY) * count), 1); memcpy(py_list->word, buff, word_l); py_list->word[word_l] = '\0'; py_list->count = count; for (i = 0;i < count;i++) { py = pinyin_enhance_pylist_get(py_list, i); int8_t *tmp = list_buff + i * 3; py->konsonant = tmp[0]; py->vokal = tmp[1]; py->tone = tmp[2]; } for (i = utarray_len(array) - 1;i >= 0;i--) { FcitxPYEnhancePYList *ele; ele = *(FcitxPYEnhancePYList**)_utarray_eltptr(array, i); if (strcmp(ele->word, py_list->word) < 0) { break; } } utarray_insert(array, &py_list, i + 1); } free(list_buff); fclose(fp); } }