void ClipboardPushClipboard(FcitxClipboard *clipboard, uint32_t len, const char *str) { if (!(len && str && *str)) return; if (!ClipboardCheckBlank(clipboard, str)) return; int i = ClipboardSelectionClipboardFind(clipboard, str, len); if (i == 0) { return; } else if (i > 0) { ClipboardSelectionStr sel = clipboard->clp_hist_lst[i]; memmove(clipboard->clp_hist_lst + 1, clipboard->clp_hist_lst, i * sizeof(ClipboardSelectionStr)); clipboard->clp_hist_lst[0] = sel; return; } char *new_str; if (clipboard->clp_hist_len < (uint32_t)clipboard->config.history_len) { clipboard->clp_hist_len++; new_str = NULL; } else { new_str = clipboard->clp_hist_lst[clipboard->clp_hist_len - 1].str; } memmove(clipboard->clp_hist_lst + 1, clipboard->clp_hist_lst, (clipboard->clp_hist_len - 1) * sizeof(ClipboardSelectionStr)); new_str = fcitx_utils_set_str_with_len(new_str, str, len); clipboard->clp_hist_lst->len = len; clipboard->clp_hist_lst->str = new_str; }
FCITX_EXPORT_API UT_array* fcitx_utils_string_list_append_len(UT_array *list, const char *str, size_t len) { char *buff = fcitx_utils_set_str_with_len(NULL, str, len); fcitx_utils_string_list_append_no_copy(list, buff); return list; }
static void ClipboardSetCandWord(FcitxClipboard *clipboard, FcitxCandidateWord *cand_word, ClipboardSelectionStr *str) { cand_word->strWord = ClipboardSelectionStrip(clipboard, str->str, str->len); cand_word->priv = fcitx_utils_set_str_with_len(NULL, str->str, str->len); }
FCITX_EXPORT_API void fcitx_utils_string_swap_with_len(char** obj, const char* str, size_t len) { if (str) { *obj = fcitx_utils_set_str_with_len(*obj, str, len); } else if (*obj) { free(*obj); *obj = NULL; } }
static char* ClipboardSelectionStrip(FcitxClipboard *clipboard, const char *str, uint32_t len) { const char *begin = str + strspn(str, " \t\b\n\f\v\r"); const char *end = str + len; for (;end >= begin;end--) { switch (*(end - 1)) { case_blank: continue; default: break; } break; } if (end <= begin) return strdup(""); len = end - begin; char *res; char *p; if (len < (uint32_t)clipboard->config.cand_max_len) { res = fcitx_utils_set_str_with_len(NULL, begin, len); goto out; } const char *begin_end = begin + clipboard->cand_half_len; const char *end_begin = end - clipboard->cand_half_len; for (;begin_end < end_begin;begin_end++) { if (fcitx_utf8_valid_start(*begin_end)) break; } for (;begin_end < end_begin;end_begin--) { if (fcitx_utf8_valid_start(*end_begin)) break; } int begin_len = begin_end - begin; int end_len = end - end_begin; res = malloc(begin_len + end_len + strlen(CLIPBOARD_CAND_SEP) + 1); p = res; memcpy(p, begin, begin_len); p += begin_len; memcpy(p, CLIPBOARD_CAND_SEP, strlen(CLIPBOARD_CAND_SEP)); p += strlen(CLIPBOARD_CAND_SEP); memcpy(p, end_begin, end_len); p += end_len; *p = '\0'; out: for (p = res;*p;p++) { switch (*p) { case_blank: *p = ' '; } } return res; }
FCITX_EXPORT_API char* fcitx_utils_get_current_langcode() { /* language[_territory][.codeset][@modifier]" or "C" */ const char* p; p = getenv("LC_CTYPE"); if (!p) { p = getenv("LC_ALL"); if (!p) p = getenv("LANG"); } if (p) return fcitx_utils_set_str_with_len(NULL, p, strcspn(p, ".@")); return strdup("C"); }
FCITX_EXPORT_API int fcitx_utils_get_display_number() { const char *display = getenv("DISPLAY"); if (!display) return 0; size_t len; const char *p = display + strcspn(display, ":"); if (*p != ':') return 0; p++; len = strcspn(p, "."); char *str_disp_num = fcitx_utils_set_str_with_len(NULL, p, len); int displayNumber = atoi(str_disp_num); free(str_disp_num); return displayNumber; }
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; }