void py_enhance_stroke_load_tree(PyEnhanceStrokeTree *tree, FILE *fp) { char *buff = NULL; char *key; char *word; unsigned int key_l; int word_l; size_t len; memset(tree, 0, sizeof(PyEnhanceStrokeTree)); /** * init each entry to the key_id (single and double) * or other odd numbers (multiple). **/ unsigned int i; for (i = 0;i < sizeof(tree->table) / sizeof(uint32_t);i++) tree->table[i] = i * 2 + 1; /** * reserve some space before loading to avoid repeating realloc **/ py_enhance_buff_reserve(&tree->keys, 1024 * 1024 / 2 * 3); py_enhance_buff_reserve(&tree->words, 1024 * 1024); while (getline(&buff, &len, fp) != -1) { /* remove leading spaces */ key = buff + strspn(buff, PYENHANCE_MAP_BLANK); /* empty line or comment */ if (*key == '\0' || *key == '#') continue; /* find delimiter */ key_l = strspn(key, "12345"); if (fcitx_unlikely(key_l == 0 || key_l > 0xff)) continue; word = key + key_l; word_l = strspn(word, PYENHANCE_MAP_BLANK); if (!word_l) continue; *word = '\0'; word += word_l; word_l = strcspn(word, PYENHANCE_MAP_BLANK); if (fcitx_unlikely(word_l == 0 || word_l > UTF8_MAX_LENGTH)) continue; word[word_l] = '\0'; word_l++; for (i = 0;i < key_l;i++) key[i] -= '1'; py_enhance_stroke_add_word(tree, (uint8_t*)key, key_l, word, word_l); } py_enhance_stroke_load_finish(tree); py_enhance_buff_shrink(&tree->keys); py_enhance_buff_shrink(&tree->words); fcitx_utils_free(buff); }
void py_enhance_stroke_load_tree(PyEnhanceStrokeTree *tree, FILE *fp, FcitxMemoryPool *pool) { char *buff = NULL; char *key; char *word; int key_l; int word_l; size_t len; memset(tree, 0, sizeof(PyEnhanceStrokeTree)); while (getline(&buff, &len, fp) != -1) { /* remove leading spaces */ key = buff + strspn(buff, PYENHANCE_MAP_BLANK); /* empty line or comment */ if (*key == '\0' || *key == '#') continue; /* find delimiter */ key_l = strspn(key, "12345"); if (!key_l) continue; word = key + key_l; word_l = strspn(word, PYENHANCE_MAP_BLANK); if (!word_l) continue; *word = '\0'; word += word_l; word_l = strcspn(word, PYENHANCE_MAP_BLANK); if (!word_l) continue; word[word_l] = '\0'; word_l++; py_enhance_stroke_add_word(tree, pool, key, key_l, word, word_l); } fcitx_utils_free(buff); }