boolean LoadPY(void) { FILE *fp; int i, j; int iSW;; fp = fopen("pybase.mb", "r"); if (!fp) return false; fcitx_utils_read_int32(fp, &iPYFACount); PYFAList = (__PYFA *) malloc(sizeof(__PYFA) * iPYFACount); for (i = 0; i < iPYFACount; i++) { fread(PYFAList[i].strMap, sizeof(char) * 2, 1, fp); PYFAList[i].strMap[2] = '\0'; fcitx_utils_read_int32(fp, &(PYFAList[i].iHZCount)); PYFAList[i].pyBase = (_PyBase *) malloc(sizeof(_PyBase) * PYFAList[i].iHZCount); for (j = 0; j < PYFAList[i].iHZCount; j++) { int8_t len; fread(&len, sizeof(int8_t) , 1, fp); fread(PYFAList[i].pyBase[j].strHZ, sizeof(char) * len, 1, fp); PYFAList[i].pyBase[j].strHZ[len] = '\0'; PYFAList[i].pyBase[j].phrase = (_PyPhrase *) malloc(sizeof(_PyPhrase)); PYFAList[i].pyBase[j].phrase->next = NULL; PYFAList[i].pyBase[j].iPhraseCount = 0; } } fclose(fp); i = 0; while (1) { iSW = 0; for (j = 0; j < iPYFACount; j++) { if (i < PYFAList[j].iHZCount) { PYFAList[j].pyBase[i].iIndex = iAllCount--; iSW = 1; } } if (!iSW) break; i++; } fp = fopen("pybase.mb", "w"); if (!fp) return false; fcitx_utils_write_int32(fp, iPYFACount); for (i = 0; i < iPYFACount; i++) { fwrite(PYFAList[i].strMap, sizeof(char) * 2, 1, fp); fcitx_utils_write_int32(fp, PYFAList[i].iHZCount); for (j = 0; j < PYFAList[i].iHZCount; j++) { int8_t len = strlen(PYFAList[i].pyBase[j].strHZ); fwrite(&len, sizeof(int8_t), 1, fp); fwrite(PYFAList[i].pyBase[j].strHZ, sizeof(char) * len, 1, fp); fcitx_utils_write_int32(fp, PYFAList[i].pyBase[j].iIndex); } } fclose(fp); return true; }
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); }