static void ClipboardWriteHistory(FcitxClipboard *clipboard) { FILE *fp; fp = FcitxXDGGetFileUserWithPrefix("clipboard", "history.dat", "w", NULL); if (!fp) return; if (!clipboard->config.save_history) goto out; fcitx_utils_write_uint32(fp, clipboard->clp_hist_len); fcitx_utils_write_uint32(fp, clipboard->primary.len); unsigned int i; for (i = 0;i < clipboard->clp_hist_len;i++) { fcitx_utils_write_uint32(fp, clipboard->clp_hist_lst[i].len); } if (clipboard->primary.len) fwrite(clipboard->primary.str, 1, clipboard->primary.len, fp); for (i = 0;i < clipboard->clp_hist_len;i++) { if (clipboard->clp_hist_lst[i].len) fwrite(clipboard->clp_hist_lst[i].str, 1, clipboard->clp_hist_lst[i].len, fp); } out: fclose(fp); }
void SaveTableDict(TableMetaData *tableMetaData) { RECORD *recTemp; char *pstr, *tempfile; FILE *fpDict; uint32_t iTemp; unsigned int i; int fd; int8_t cTemp; TableDict *tableDict = tableMetaData->tableDict; if (!tableDict->iTableChanged) return; // make ~/.config/fcitx/table/ dir FcitxXDGGetFileUserWithPrefix("table", "", "w", NULL); FcitxXDGGetFileUserWithPrefix("table", TABLE_TEMP_FILE, NULL, &tempfile); fd = mkstemp(tempfile); fpDict = NULL; if (fd > 0) fpDict = fdopen(fd, "w"); if (!fpDict) { FcitxLog(ERROR, _("Save dict error")); free(tempfile); return; } // write version number fcitx_utils_write_uint32(fpDict, 0); fwrite(&iInternalVersion, sizeof(char), 1, fpDict); iTemp = strlen(tableDict->strInputCode); fcitx_utils_write_uint32(fpDict, iTemp); fwrite(tableDict->strInputCode, sizeof(char), iTemp + 1, fpDict); fwrite(&(tableDict->iCodeLength), sizeof(char), 1, fpDict); fwrite(&(tableDict->iPYCodeLength), sizeof(char), 1, fpDict); iTemp = strlen(tableDict->strIgnoreChars); fcitx_utils_write_uint32(fpDict, iTemp); fwrite(tableDict->strIgnoreChars, sizeof(char), iTemp + 1, fpDict); fwrite(&(tableDict->bRule), sizeof(unsigned char), 1, fpDict); if (tableDict->bRule) { // table contains rule for (i = 0; i < tableDict->iCodeLength - 1; i++) { fwrite(&(tableDict->rule[i].iFlag), sizeof(unsigned char), 1, fpDict); fwrite(&(tableDict->rule[i].iWords), sizeof(unsigned char), 1, fpDict); for (iTemp = 0; iTemp < tableDict->iCodeLength; iTemp++) { fwrite(&(tableDict->rule[i].rule[iTemp].iFlag), sizeof(unsigned char), 1, fpDict); fwrite(&(tableDict->rule[i].rule[iTemp].iWhich), sizeof(unsigned char), 1, fpDict); fwrite(&(tableDict->rule[i].rule[iTemp].iIndex), sizeof(unsigned char), 1, fpDict); } } } fcitx_utils_write_uint32(fpDict, tableDict->iRecordCount); recTemp = tableDict->recordHead->next; while (recTemp != tableDict->recordHead) { fwrite(recTemp->strCode, sizeof(char), tableDict->iPYCodeLength + 1, fpDict); iTemp = strlen(recTemp->strHZ) + 1; fcitx_utils_write_uint32(fpDict, iTemp); fwrite(recTemp->strHZ, sizeof(char), iTemp, fpDict); cTemp = recTemp->type; fwrite(&cTemp, sizeof(int8_t), 1, fpDict); fcitx_utils_write_uint32(fpDict, recTemp->iHit); fcitx_utils_write_uint32(fpDict, recTemp->iIndex); recTemp = recTemp->next; } fclose(fpDict); fpDict = FcitxXDGGetFileUserWithPrefix("table", tableMetaData->strPath, NULL, &pstr); if (access(pstr, 0)) unlink(pstr); rename(tempfile, pstr); free(pstr); free(tempfile); FcitxLog(DEBUG, _("Rename OK")); tableDict->iTableChanged = 0; if (tableDict->autoPhrase) { // Save auto phrase // make ~/.config/fcitx/table/ dir FcitxXDGGetFileUserWithPrefix("table", "", "w", NULL); FcitxXDGGetFileUserWithPrefix("table", TABLE_TEMP_FILE, NULL, &tempfile); fd = mkstemp(tempfile); fpDict = NULL; if (fd > 0) fpDict = fdopen(fd, "w"); if (fpDict) { fcitx_utils_write_uint32(fpDict, tableDict->iAutoPhrase); for (i = 0; i < tableDict->iAutoPhrase; i++) { fwrite(tableDict->autoPhrase[i].strCode, tableDict->iCodeLength + 1, 1, fpDict); fwrite(tableDict->autoPhrase[i].strHZ, PHRASE_MAX_LENGTH * UTF8_MAX_LENGTH + 1, 1, fpDict); fcitx_utils_write_int32( fpDict, tableDict->autoPhrase[i].iSelected); } fclose(fpDict); } char *strPath; fcitx_utils_alloc_cat_str(strPath, tableMetaData->uniqueName, "_LastAutoPhrase.tmp"); fpDict = FcitxXDGGetFileUserWithPrefix("table", strPath, NULL, &pstr); free(strPath); if (access(pstr, F_OK)) unlink(pstr); rename(tempfile, pstr); free(pstr); free(tempfile); } }
void CreatePYPhrase(void) { char strPY[256]; char strPhrase[256]; char strMap[256]; ParsePYStruct strTemp; int32_t iIndex, i, s1, s2, j, k; _PyPhrase *phrase, *t, *tt; FILE *f = fopen("pyERROR", "w"); FILE *fg = fopen("pyPhrase.ok", "w"); int kkk; uint32_t uIndex; FcitxPinyinConfig pyconfig; memset(&pyconfig, 0 , sizeof(pyconfig)); InitMHPY(&pyconfig.MHPY_C, MHPY_C_TEMPLATE); InitMHPY(&pyconfig.MHPY_S, MHPY_S_TEMPLATE); InitPYTable(&pyconfig); s1 = 0; s2 = 0; uIndex = 0; printf("Start Loading Phrase...\n"); while (!feof(fpt)) { fscanf(fpt, "%s", strPY); fscanf(fpt, "%s\n", strPhrase); if (strlen(strPhrase) < 3) continue; ParsePY(&pyconfig, strPY, &strTemp, PY_PARSE_INPUT_SYSTEM, false); s2++; kkk = 0; if (strTemp.iHZCount != fcitx_utf8_strlen(strPhrase) || (strTemp.iMode & PARSE_ABBR)) { fprintf(f, "%s %s\n", strPY, strPhrase); continue; } strMap[0] = '\0'; for (iIndex = 0; iIndex < strTemp.iHZCount; iIndex++) strcat(strMap, strTemp.strMap[iIndex]); for (iIndex = 0; iIndex < iPYFACount; iIndex++) { if (!strncmp(PYFAList[iIndex].strMap, strMap, 2)) { for (i = 0; i < PYFAList[iIndex].iHZCount; i++) { if (!fcitx_utf8_strncmp(PYFAList[iIndex].pyBase[i].strHZ, strPhrase, 1)) { t = PYFAList[iIndex].pyBase[i].phrase; for (j = 0; j < PYFAList[iIndex].pyBase[i].iPhraseCount; j++) { tt = t; t = t->next; if (!strcmp(t->strMap, strMap + 2) && !strcmp(t->strPhrase, strPhrase + fcitx_utf8_char_len(strPhrase))) { printf("\n\t%d: %s %s ----->deleted.\n", s2, strPY, strPhrase); goto _next; } if (strcmp(t->strMap, strMap + 2) > 0) { t = tt; break; } } phrase = (_PyPhrase *) malloc(sizeof(_PyPhrase)); phrase->strPhrase = (char *) malloc(sizeof(char) * (strlen(strPhrase) - fcitx_utf8_char_len(strPhrase) + 1)); phrase->strMap = (char *) malloc(sizeof(char) * ((strTemp.iHZCount - 1) * 2 + 1)); phrase->uIndex = uIndex++; strcpy(phrase->strPhrase, strPhrase + fcitx_utf8_char_len(strPhrase)); strcpy(phrase->strMap, strMap + 2); tt = t->next; t->next = phrase; phrase->next = tt; PYFAList[iIndex].pyBase[i].iPhraseCount++; s1++; kkk = 1; _next: ; } } } } if (!kkk) fprintf(f, "%s %s %s\n", strPY, strPhrase, (char *)(strTemp.strPYParsed)); else fprintf(fg, "%s %s\n", strPY, strPhrase); } printf("%d Phrases, %d Converted!\nWriting Phrase file ...", s2, s1); for (i = 0; i < iPYFACount; i++) { for (j = 0; j < PYFAList[i].iHZCount; j++) { iIndex = PYFAList[i].pyBase[j].iPhraseCount; if (iIndex) { int8_t clen = strlen(PYFAList[i].pyBase[j].strHZ); fcitx_utils_write_int32(fp2, i); fwrite(&clen, sizeof(int8_t), 1, fp2); fwrite(PYFAList[i].pyBase[j].strHZ, sizeof(char) * clen, 1, fp2); fcitx_utils_write_int32(fp2, iIndex); t = PYFAList[i].pyBase[j].phrase->next; for (k = 0; k < PYFAList[i].pyBase[j].iPhraseCount; k++) { int32_t slen = strlen(t->strPhrase); iIndex = strlen(t->strMap); fcitx_utils_write_int32(fp2, iIndex); fwrite(t->strMap, sizeof(char), iIndex, fp2); fcitx_utils_write_int32(fp2, slen); fwrite(t->strPhrase, sizeof(char), strlen(t->strPhrase), fp2); fcitx_utils_write_uint32(fp2, uIndex - 1 - t->uIndex); t = t->next; } } } } printf("\nOK!\n"); fclose(fp2); fclose(fpt); }
void CreatePYBase(void) { _PyStruct *head, *pyList, *temp, *t; char strPY[7], strHZ[UTF8_MAX_LENGTH * 80 + 1], strMap[3]; int32_t iIndex, iCount, i; int32_t iBaseCount; int32_t s = 0; int32_t tt = 0; head = (_PyStruct *) malloc(sizeof(_PyStruct)); head->prev = head; head->next = head; iBaseCount = 0; while (PYTable_template[iBaseCount].strPY[0] != '\0') iBaseCount++; for (iIndex = 0; iIndex < iBaseCount; iIndex++) YY[iIndex] = 0; iIndex = 0; while (!feof(fps)) { fscanf(fps, "%s", strPY); fscanf(fps, "%s\n", strHZ); if (MapPY(&pyconfig, strPY, strMap, PY_PARSE_INPUT_SYSTEM)) { for (i = 0; i < iBaseCount; i++) if ((!strcmp(PYTable_template[i].strPY, strPY)) && PYTable_template[i].control == PYTABLE_NONE) YY[i] += 1; iIndex++; if (fcitx_utf8_strlen(strHZ) > 1) { int8_t charLen = fcitx_utf8_char_len(strHZ); fprintf(stderr, "%s length is larger that 1, truncated to ", strHZ); strHZ[charLen] = '\0'; fprintf(stderr, "%s.\n", strHZ); } temp = (_PyStruct *) malloc(sizeof(_PyStruct)); strcpy(temp->strHZ, strHZ); strcpy(temp->strPY, strMap); pyList = head->prev; while (pyList != head) { if (strcmp(pyList->strPY, strMap) <= 0) break; pyList = pyList->prev; } temp->next = pyList->next; temp->prev = pyList; pyList->next->prev = temp; pyList->next = temp; } else fprintf(stderr, "%s Error!!!!\n", strPY); } iCount = 0; for (i = 0; i < iBaseCount; i++) { if (YY[i]) iCount++; } fcitx_utils_write_uint32(fp1, iCount); printf("Groups: %d\n", iCount); iAllCount = iIndex; pyList = head->next; strcpy(strPY, pyList->strPY); iCount = 0; t = pyList; while (pyList != head) { if (!strcmp(strPY, pyList->strPY)) { iCount++; } else { tt++; fwrite(strPY, sizeof(char) * 2, 1, fp1); fcitx_utils_write_uint32(fp1, iCount); for (i = 0; i < iCount; i++) { int8_t len = strlen(t->strHZ); fwrite(&len, sizeof(int8_t), 1, fp1); fwrite(t->strHZ, sizeof(char) * len , 1, fp1); t = t->next; } s += iCount; t = pyList; iCount = 1; strcpy(strPY, pyList->strPY); } pyList = pyList->next; } fwrite(strPY, sizeof(char) * 2, 1, fp1); fcitx_utils_write_uint32(fp1, iCount); for (i = 0; i < iCount; i++) { int8_t len = strlen(t->strHZ); fwrite(&len, sizeof(int8_t), 1, fp1); fwrite(t->strHZ, sizeof(char) * len , 1, fp1); t = t->next; } s += iCount; fclose(fp1); fclose(fps); }
int main(int argc, char *argv[]) { FILE *fpDict, *fpNew; RECORD *temp, *head, *newRec, *current; uint32_t s = 0; int i; uint32_t iTemp; char *pstr = 0; char strTemp[10]; unsigned char bRule; RULE *rule = NULL; unsigned int l; unsigned char iCodeLength = 0; unsigned char iPYCodeLength = 0; int8_t type; if (argc != 3) { printf("\nUsage: txt2mb <Source File> <IM File>\n\n"); exit(1); } fpDict = fopen(argv[1], "r"); if (!fpDict) { printf("\nCannot read source file!\n\n"); exit(2); } head = (RECORD *) malloc(sizeof(RECORD)); head->next = head; head->prev = head; current = head; bRule = 0; l = 0; char* buf = NULL, *buf1 = NULL; size_t len; for (;;) { l++; if (getline(&buf, &len, fpDict) == -1) break; i = strlen(buf) - 1; while ((i >= 0) && (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\r')) buf[i--] = '\0'; pstr = buf; if (*pstr == ' ') pstr++; if (pstr[0] == '#') continue; if (CHECK_OPTION(pstr, STR_KEYCODE)) { pstr += ADD_LENGTH(pstr, STR_KEYCODE); strcpy(strInputCode, pstr); } else if (CHECK_OPTION(pstr, STR_CODELEN)) { pstr += ADD_LENGTH(pstr, STR_CODELEN); iCodeLength = atoi(pstr); if (iCodeLength > MAX_CODE_LENGTH) { iCodeLength = MAX_CODE_LENGTH; printf("Max Code Length is %d\n", MAX_CODE_LENGTH); } } else if (CHECK_OPTION(pstr, STR_IGNORECHAR)) { pstr += ADD_LENGTH(pstr, STR_IGNORECHAR); strcpy(strIgnoreChars, pstr); } else if (CHECK_OPTION(pstr, STR_PINYIN)) { pstr += ADD_LENGTH(pstr, STR_PINYIN); while (*pstr == ' ' && *pstr != '\0') pstr++; cPinyinKey = *pstr; } else if (CHECK_OPTION(pstr, STR_PROMPT)) { pstr += ADD_LENGTH(pstr, STR_PROMPT); while (*pstr == ' ' && *pstr != '\0') pstr++; cPromptKey = *pstr; } else if (CHECK_OPTION(pstr, STR_CONSTRUCTPHRASE)) { pstr += ADD_LENGTH(pstr, STR_CONSTRUCTPHRASE); while (*pstr == ' ' && *pstr != '\0') pstr++; cPhraseKey = *pstr; } else if (CHECK_OPTION(pstr, STR_PINYINLEN)) { pstr += ADD_LENGTH(pstr, STR_PINYINLEN); iPYCodeLength = atoi(pstr); } else if (CHECK_OPTION(pstr, STR_DATA)) break; else if (CHECK_OPTION(pstr, STR_RULE)) { bRule = 1; break; } } if (iCodeLength <= 0 || !strInputCode[0]) { printf("Source File Format Error!\n"); exit(1); } if (bRule) { /* * 组词规则数应该比键码长度小1 */ rule = (RULE *) malloc(sizeof(RULE) * (iCodeLength - 1)); for (iTemp = 0; iTemp < (iCodeLength - 1); iTemp++) { l++; if (getline(&buf, &len, fpDict) == -1) break; rule[iTemp].rule = (RULE_RULE *) malloc(sizeof(RULE_RULE) * iCodeLength); i = strlen(buf) - 1; while ((i >= 0) && (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\r')) buf[i--] = '\0'; pstr = buf; if (*pstr == ' ') pstr++; if (pstr[0] == '#') continue; if (CHECK_OPTION(pstr, STR_DATA)) break; switch (*pstr) { case 'e': case 'E': rule[iTemp].iFlag = 0; break; case 'a': case 'A': rule[iTemp].iFlag = 1; break; default: printf("2 Phrase rules are not suitable!\n"); printf("\t\t%s\n", buf); exit(1); } pstr++; char* p = pstr; while (*p && *p != '=') p++; if (!(*p)) { printf("3 Phrase rules are not suitable!\n"); printf("\t\t%s\n", buf); exit(1); } strncpy(strTemp, pstr, p - pstr); strTemp[p - pstr] = '\0'; rule[iTemp].iWords = atoi(strTemp); p++; for (i = 0; i < iCodeLength; i++) { while (*p == ' ') p++; switch (*p) { case 'p': case 'P': rule[iTemp].rule[i].iFlag = 1; break; case 'n': case 'N': rule[iTemp].rule[i].iFlag = 0; break; default: printf("4 Phrase rules are not suitable!\n"); printf("\t\t%s\n", buf); exit(1); } p++; rule[iTemp].rule[i].iWhich = *p++ - '0'; rule[iTemp].rule[i].iIndex = *p++ - '0'; while (*p == ' ') p++; if (i != (iCodeLength - 1)) { if (*p != '+') { printf("5 Phrase rules are not suitable!\n"); printf("\t\t%s %d\n", buf, iCodeLength); exit(1); } p++; } } } if (iTemp != iCodeLength - 1) { printf("6 Phrase rules are not suitable!\n"); exit(1); } for (iTemp = 0; iTemp < (iCodeLength - 1); iTemp++) { l++; if (getline(&buf, &len, fpDict) == -1) break; i = strlen(buf) - 1; while ((i >= 0) && (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\r')) buf[i--] = '\0'; pstr = buf; if (*pstr == ' ') pstr++; if (pstr[0] == '#') continue; if (CHECK_OPTION(pstr, STR_DATA)) break; } } if (iPYCodeLength < iCodeLength) iPYCodeLength = iCodeLength; if (!CHECK_OPTION(pstr, STR_DATA)) { printf("Source File Format Error!\n"); exit(1); } while (getline(&buf, &len, fpDict) != -1) { l++; if (buf1) free(buf1); buf1 = fcitx_utils_trim(buf); char *p = buf1; while (*p && !isspace(*p)) p ++; if (*p == '\0') continue; while (isspace(*p)) { *p = '\0'; p ++; } char* strHZ = p; if (!IsValidCode(buf1[0])) { printf("Invalid Format: Line-%d %s %s\n", l, buf1, strHZ); exit(1); } if (((buf1[0] != cPinyinKey) && (strlen(buf1) > iCodeLength)) || ((buf1[0] == cPinyinKey) && (strlen(buf1) > (iPYCodeLength + 1))) || ((buf1[0] == cPhraseKey) && (strlen(buf1) > (iCodeLength + 1))) || ((buf1[0] == cPromptKey) && (strlen(buf1) > (iPYCodeLength + 1))) ) { printf("Delete: %s %s, Too long\n", buf1, strHZ); continue; } size_t hzLen = fcitx_utf8_strlen(strHZ); if (buf1[0] == cPhraseKey && hzLen != 1) { printf("Delete: %s %s, Too long\n", buf1, strHZ); continue; } type = RECORDTYPE_NORMAL; pstr = buf1; if (buf1[0] == cPinyinKey) { pstr ++; type = RECORDTYPE_PINYIN; } else if (buf1[0] == cPhraseKey) { pstr ++; type = RECORDTYPE_CONSTRUCT; } else if (buf1[0] == cPromptKey) { pstr ++; type = RECORDTYPE_PROMPT; } //查找是否重复 temp = current; if (temp != head) { if (strcmp(temp->strCode, pstr) >= 0) { while (temp != head && strcmp(temp->strCode, pstr) >= 0) { if (!strcmp(temp->strHZ, strHZ) && !strcmp(temp->strCode, pstr) && temp->type == type) { printf("Delete: %s %s\n", pstr, strHZ); goto _next; } temp = temp->prev; } if (temp == head) temp = temp->next; while (temp != head && strcmp(temp->strCode, pstr) <= 0) temp = temp->next; } else { while (temp != head && strcmp(temp->strCode, pstr) <= 0) { if (!strcmp(temp->strHZ, strHZ) && !strcmp(temp->strCode, pstr) && temp->type == type) { printf("Delete: %s %s\n", pstr, strHZ); goto _next; } temp = temp->next; } } } //插在temp的前面 newRec = (RECORD *) fcitx_utils_malloc0(sizeof(RECORD)); newRec->strCode = (char *) fcitx_utils_malloc0(sizeof(char) * (iPYCodeLength + 1)); newRec->strHZ = (char *) fcitx_utils_malloc0(sizeof(char) * strlen(strHZ) + 1); strcpy(newRec->strCode, pstr); strcpy(newRec->strHZ, strHZ); newRec->type = type; newRec->iHit = 0; newRec->iIndex = 0; temp->prev->next = newRec; newRec->next = temp; newRec->prev = temp->prev; temp->prev = newRec; current = newRec; s++; _next: continue; } if (buf) free(buf); if (buf1) free(buf1); fclose(fpDict); printf("\nReading %d records.\n\n", s); fpNew = fopen(argv[2], "w"); if (!fpNew) { printf("\nCannot create target file!\n\n"); exit(3); } int8_t iInternalVersion = INTERNAL_VERSION; //写入版本号--如果第一个字为0,表示后面那个字节为版本号 fcitx_utils_write_uint32(fpNew, 0); fwrite(&iInternalVersion, sizeof(int8_t), 1, fpNew); iTemp = (uint32_t)strlen(strInputCode); fcitx_utils_write_uint32(fpNew, iTemp); fwrite(strInputCode, sizeof(char), iTemp + 1, fpNew); fwrite(&iCodeLength, sizeof(unsigned char), 1, fpNew); fwrite(&iPYCodeLength, sizeof(unsigned char), 1, fpNew); iTemp = (uint32_t)strlen(strIgnoreChars); fcitx_utils_write_uint32(fpNew, iTemp); fwrite(strIgnoreChars, sizeof(char), iTemp + 1, fpNew); fwrite(&bRule, sizeof(unsigned char), 1, fpNew); if (bRule) { for (i = 0; i < iCodeLength - 1; i++) { fwrite(&(rule[i].iFlag), sizeof(unsigned char), 1, fpNew); fwrite(&(rule[i].iWords), sizeof(unsigned char), 1, fpNew); for (iTemp = 0; iTemp < iCodeLength; iTemp++) { fwrite(&(rule[i].rule[iTemp].iFlag), sizeof(unsigned char), 1, fpNew); fwrite(&(rule[i].rule[iTemp].iWhich), sizeof(unsigned char), 1, fpNew); fwrite(&(rule[i].rule[iTemp].iIndex), sizeof(unsigned char), 1, fpNew); } } } fcitx_utils_write_uint32(fpNew, s); current = head->next; while (current != head) { fwrite(current->strCode, sizeof(char), iPYCodeLength + 1, fpNew); s = strlen(current->strHZ) + 1; fcitx_utils_write_uint32(fpNew, s); fwrite(current->strHZ, sizeof(char), s, fpNew); fwrite(&(current->type), sizeof(int8_t), 1, fpNew); fcitx_utils_write_uint32(fpNew, current->iHit); fcitx_utils_write_uint32(fpNew, current->iIndex); current = current->next; } fclose(fpNew); return 0; }