Example #1
0
Language Language::TryParse(const std::wstring& s)
{
    if (IsValidCode(s))
        return Language(s);

    // Is it a standard language code?
    if (std::regex_match(s, RE_LANG_CODE_PERMISSIVE))
    {
        std::wstring s2(s);
        TryNormalize(s2);
        if (IsValidCode(s2))
            return Language(s2);
    }

    // If not, perhaps it's a human-readable name (perhaps coming from the language control)?
    auto names = GetDisplayNamesData();
    icu::UnicodeString s_icu = ToIcuStr(s);
    s_icu.foldCase();
    std::wstring folded = StdFromIcuStr(s_icu);
    auto i = names.names.find(folded);
    if (i != names.names.end())
        return Language(i->second);

    // Maybe it was in English?
    i = names.namesEng.find(folded);
    if (i != names.namesEng.end())
        return Language(i->second);

    return Language(); // invalid
}
Example #2
0
NOAAStore::iterator
NOAAStore::AddStation(const TCHAR *code)
{
    assert(IsValidCode(code));

    WideToUTF8Converter code2(code);
    assert(code2.IsValid());
    return AddStation(code2);
}
Example #3
0
bool
NOAAStore::LoadFromString(const char *string)
{
  const char *s = string;
  while (s != NULL && *s) {
    const char *next = strchr(s, _T(','));
    if ((next != NULL && next - s == 4) || (next == NULL && strlen(s) == 4)) {
      char code[5];
      std::copy_n(s, 4, code);
      code[4] = '\0';
      if (IsValidCode(code))
        AddStation(code);
    }
    s = (next == NULL) ? NULL : next + 1;
  }
  return true;
}
Example #4
0
bool
NOAAStore::LoadFromString(const TCHAR *string)
{
  const TCHAR *s = string;
  while (s != NULL && *s) {
    const TCHAR *next = _tcschr(s, _T(','));
    if ((next != NULL && next - s == 4) || (next == NULL && _tcslen(s) == 4)) {
      TCHAR code[5];
      std::copy(s, s+4, code);
      code[4] = '\0';
      if (IsValidCode(code))
        AddStation(code);
    }
    s = (next == NULL) ? NULL : next + 1;
  }
  return true;
}
Example #5
0
NOAAStore::iterator
NOAAStore::AddStation(const char *code)
{
    assert(IsValidCode(code));

    Item item;

    // Copy station code
    strncpy(item.code, code, 4);
    item.code[4] = 0;

    // Reset available flags
    item.metar_available = false;
    item.parsed_metar_available = false;
    item.taf_available = false;

    stations.push_back(item);
    return --end();
}
Example #6
0
int main (int argc, char *argv[])
{
    char            strCode[100];
    char            strHZ[100];
    char           *p;
    FILE           *fpDict, *fpNew;
    RECORD         *temp, *head, *newRec, *current;
    unsigned int    s = 0;
    int             i;
    unsigned int    iTemp;
    char           *pstr = 0;
    char            strTemp[10];
    unsigned char   bRule;
    RULE           *rule = NULL;
    unsigned int    l;

    unsigned char   iCodeLength = 0;
    unsigned char   iPYCodeLength = 0;

    Bool            bPY;

    if (argc != 3) {
	printf ("\nUsage: txt2mb <Source File> <IM File>\n\n");
	exit (1);
    }

    fpDict = fopen (argv[1], "rt");
    if (!fpDict) {
	printf ("\nCan not read source file!\n\n");
	exit (2);
    }

    head = (RECORD *) malloc (sizeof (RECORD));
    head->next = head;
    head->prev = head;
    current = head;

    bRule = 0;
    l = 0;
    for (;;) {
	l++;
	if (!fgets (strCode, 100, fpDict))
	    break;

	i = strlen (strCode) - 1;
	while ((i >= 0) && (strCode[i] == ' ' || strCode[i] == '\n' || strCode[i] == '\r'))
	    strCode[i--] = '\0';

	pstr = strCode;
	if (*pstr == ' ')
	    pstr++;
	if (pstr[0] == '#')
	    continue;

	if (strstr (pstr, "键码=")) {
	    pstr += 5;
	    strcpy (strInputCode, pstr);
	}
	else if (strstr (pstr, "码长=")) {
	    pstr += 5;
	    iCodeLength = atoi (pstr);
	}
	else if (strstr (pstr, "规避字符=")) {
	    pstr += 9;
	    strcpy (strIgnoreChars, pstr);
	}
	else if (strstr (pstr, "拼音=")) {
	    pstr += 5;
	    while (*pstr == ' ')
		pstr++;
	    cPinyinKey = *pstr;
	}
	else if (strstr (pstr, "拼音长度=")) {
	    pstr += 9;
	    iPYCodeLength = atoi (pstr);
	}

	else if (strstr (pstr, "[数据]"))
	    break;
	else if (strstr (pstr, "[组词规则]")) {
	    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 (!fgets (strCode, 100, fpDict))
		break;

	    rule[iTemp].rule = (RULE_RULE *) malloc (sizeof (RULE_RULE) * iCodeLength);

	    i = strlen (strCode) - 1;
	    while ((i >= 0) && (strCode[i] == ' ' || strCode[i] == '\n' || strCode[i] == '\r'))
		strCode[i--] = '\0';

	    pstr = strCode;
	    if (*pstr == ' ')
		pstr++;
	    if (pstr[0] == '#')
		continue;

	    if (strstr (pstr, "[数据]"))
		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", strCode);
		exit (1);
	    }
	    pstr++;
	    p = pstr;
	    while (*p && *p != '=')
		p++;
	    if (!(*p)) {
		printf ("3   Phrase rules are not suitable!\n");
		printf ("\t\t%s\n", strCode);
		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", strCode);
		    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", strCode, 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 (!fgets (strCode, 100, fpDict))
		break;

	    i = strlen (strCode) - 1;
	    while ((i >= 0) && (strCode[i] == ' ' || strCode[i] == '\n' || strCode[i] == '\r'))
		strCode[i--] = '\0';

	    pstr = strCode;
	    if (*pstr == ' ')
		pstr++;
	    if (pstr[0] == '#')
		continue;

	    if (strstr (pstr, "[数据]"))
		break;
	}
    }

    if (iPYCodeLength < iCodeLength)
	iPYCodeLength = iCodeLength;

    if (!strstr (pstr, "[数据]")) {
	printf ("Source File Format Error!\n");
	exit (1);
    }

    for (;;) {
	l++;
	if (EOF == fscanf (fpDict, "%s %s\n", strCode, strHZ))
	    break;

	if (!IsValidCode (strCode[0])) {
	    printf ("Invalid Format: Line-%d  %s %s\n", l, strCode, strHZ);

	    exit (1);
	}

	if (((strCode[0] != cPinyinKey) && (strlen (strCode) > iCodeLength)) || ((strCode[0] == cPinyinKey) && (strlen (strCode) > (iPYCodeLength + 1))))
	    continue;
	if (strlen (strHZ) > 20)	//最长词组长度为10个汉字
	    continue;

	bPY = False;
	if (strCode[0] == cPinyinKey) {
	    strcpy (strCode, strCode + 1);
	    bPY = True;
	}

	//查找是否重复
	temp = current;
	if (temp != head) {
	    if (strcmp (temp->strCode, strCode) >= 0) {
		while (temp != head && strcmp (temp->strCode, strCode) >= 0) {
		    if (!strcmp (temp->strHZ, strHZ) && !strcmp (temp->strCode, strCode)) {
			printf ("Delete:  %s %s\n", strCode, strHZ);
			goto _next;
		    }
		    temp = temp->prev;
		}

		if (temp == head)
		    temp = temp->next;

		while (temp != head && strcmp (temp->strCode, strCode) <= 0)
		    temp = temp->next;
	    }
	    else {
		while (temp != head && strcmp (temp->strCode, strCode) <= 0) {
		    if (!strcmp (temp->strHZ, strHZ) && !strcmp (temp->strCode, strCode)) {
			printf ("Delete:  %s %s\n", strCode, strHZ);
			goto _next;
		    }
		    temp = temp->next;
		}
	    }
	}

	//插在temp的前面
	newRec = (RECORD *) malloc (sizeof (RECORD));
	newRec->strCode = (char *) malloc (sizeof (char) * (iPYCodeLength + 1));
	newRec->strHZ = (char *) malloc (sizeof (char) * strlen (strHZ) + 1);
	strcpy (newRec->strCode, strCode);
	strcpy (newRec->strHZ, strHZ);
	newRec->bPinyin = bPY;
	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;
    }

    fclose (fpDict);

    printf ("\nReading %d records.\n\n", s);

    fpNew = fopen (argv[2], "wb");
    if (!fpNew) {
	printf ("\nCan not create target file!\n\n");
	exit (3);
    }

    //写入版本号--如果第一个字为0,表示后面那个字节为版本号
    iTemp = 0;
    fwrite (&iTemp, sizeof (unsigned int), 1, fpDict);
    fwrite (&iInternalVersion, sizeof (INT8), 1, fpDict);

    iTemp = (unsigned int) strlen (strInputCode);
    fwrite (&iTemp, sizeof (unsigned int), 1, fpNew);
    fwrite (strInputCode, sizeof (char), iTemp + 1, fpNew);
    fwrite (&iCodeLength, sizeof (unsigned char), 1, fpNew);
    fwrite (&iPYCodeLength, sizeof (unsigned char), 1, fpNew);
    iTemp = (unsigned int) strlen (strIgnoreChars);
    fwrite (&iTemp, sizeof (unsigned int), 1, fpNew);
    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);
	    }
	}
    }

    fwrite (&s, sizeof (unsigned int), 1, fpNew);
    current = head->next;

    while (current != head) {
	fwrite (current->strCode, sizeof (char), iPYCodeLength + 1, fpNew);
	s = strlen (current->strHZ) + 1;
	fwrite (&s, sizeof (unsigned int), 1, fpNew);
	fwrite (current->strHZ, sizeof (char), s, fpNew);
	fwrite (&(current->bPinyin), sizeof (char), 1, fpDict);
	fwrite (&(current->iHit), sizeof (unsigned int), 1, fpNew);
	fwrite (&(current->iIndex), sizeof (unsigned int), 1, fpNew);
	current = current->next;
    }
    fclose (fpNew);

    return 0;
}
Example #7
0
//---------------------------------------------------------------------------
void __fastcall TFMain_11011981::lbUnitItemsDblClick(TObject *Sender)
{
    int         idx = -1, len, size, refCnt;
    WORD*       uses;
    DWORD       adr;
    char        *tmpBuf;
    PInfoRec    recN;
    MTypeInfo   tInfo;
    String      str;
    char        tkName[32], typeName[1024];

    if (lbUnitItems->ItemIndex == -1) return;

    String item = lbUnitItems->Items->Strings[lbUnitItems->ItemIndex];
    //Xrefs?
    if (item[11] == '<' || item[11] == '?')
        sscanf(item.c_str() + 1, "%lX%s%s", &adr, tkName, typeName);
    else
        sscanf(item.c_str() + 1, "%lX%d%s%s", &adr, &refCnt, tkName, typeName);
    String name = String(tkName);

    if (SameText(name, "????"))
    {
        //Find end of unexplored Data
        int bytes = 1024, pos = Adr2Pos(adr);
        //Get first byte (use later for filtering code?data)
        BYTE db = *(Code + pos);

        FExplorer_11011981->tsCode->TabVisible = true;
        FExplorer_11011981->ShowCode(adr, bytes);
        FExplorer_11011981->tsData->TabVisible = true;
        FExplorer_11011981->ShowData(adr, bytes);
        FExplorer_11011981->tsString->TabVisible = true;
        FExplorer_11011981->ShowString(adr, 1024);
        FExplorer_11011981->tsText->TabVisible = false;
        FExplorer_11011981->WAlign = 0;

        FExplorer_11011981->btnDefCode->Enabled = true;
        if (IsFlagSet(cfCode, pos)) FExplorer_11011981->btnDefCode->Enabled = false;
        FExplorer_11011981->btnUndefCode->Enabled = false;
        if (IsFlagSet(cfCode | cfData, pos)) FExplorer_11011981->btnUndefCode->Enabled = true;

        if (IsValidCode(adr) != -1 && db >= 0xF)
        	FExplorer_11011981->pc1->ActivePage = FExplorer_11011981->tsCode;
        else
        	FExplorer_11011981->pc1->ActivePage = FExplorer_11011981->tsData;

        if (FExplorer_11011981->ShowModal() == mrOk)
        {
            switch (FExplorer_11011981->DefineAs)
            {
            case DEFINE_AS_CODE:
                recN = GetInfoRec(adr);
                if (!recN)
                    recN = new InfoRec(pos, ikRefine);
                else if (recN->kind < ikRefine || recN->kind > ikFunc)
                {
                    delete recN;
                    recN = new InfoRec(pos, ikRefine);
                }

                //AnalyzeProcInitial(adr);
                AnalyzeProc1(adr, 0, 0, 0, false);
                AnalyzeProc2(adr, true, true);
                AnalyzeArguments(adr);
                AnalyzeProc2(adr, true, true);

                if (!ContainsUnexplored(GetUnit(adr))) ShowUnits(true);
                ShowUnitItems(GetUnit(adr), lbUnitItems->TopIndex, lbUnitItems->ItemIndex);
                ShowCode(adr, 0, -1, -1);
                break;
            case DEFINE_AS_STRING:
                break;
            }
        }
        return;
    }

    if (SameText(name, "<VMT>") && tsClassView->TabVisible)
    {
        ShowClassViewer(adr);
        return;
    }
    if (SameText(name, "<ResString>"))
    {
        FStringInfo_11011981->memStringInfo->Clear();
        FStringInfo_11011981->Caption = "ResString";
        recN = GetInfoRec(adr);
        FStringInfo_11011981->memStringInfo->Lines->Add(recN->rsInfo->value);
        FStringInfo_11011981->ShowModal();
        return;
    }
    if (SameText(name, "<ShortString>") ||
        SameText(name, "<AnsiString>")  ||
        SameText(name, "<WideString>")  ||
        SameText(name, "<PAnsiChar>")   ||
        SameText(name, "<PWideChar>"))
    {
        FStringInfo_11011981->memStringInfo->Clear();
        FStringInfo_11011981->Caption = "String";
        recN = GetInfoRec(adr);
        FStringInfo_11011981->memStringInfo->Lines->Add(recN->GetName());
        FStringInfo_11011981->ShowModal();
        return;
    }
    if (SameText(name, "<UString>"))
    {
        FStringInfo_11011981->memStringInfo->Clear();
        FStringInfo_11011981->Caption = "String";
        recN = GetInfoRec(adr);
        len = wcslen((wchar_t*)(Code + Adr2Pos(adr)));
        size = WideCharToMultiByte(CP_ACP, 0, (wchar_t*)(Code + Adr2Pos(adr)), len, 0, 0, 0, 0);
        if (size)
        {
            tmpBuf = new char[size + 1];
            WideCharToMultiByte(CP_ACP, 0, (wchar_t*)(Code + Adr2Pos(adr)), len, tmpBuf, len, 0, 0);
            FStringInfo_11011981->memStringInfo->Lines->Add(String(tmpBuf, len));
            delete[] tmpBuf;
            FStringInfo_11011981->ShowModal();
        }
        return;
    }
    if (SameText(name, "<Integer>")     ||
        SameText(name, "<Char>")        ||
        SameText(name, "<Enumeration>") ||
        SameText(name, "<Float>")       ||
        SameText(name, "<Set>")         ||
        SameText(name, "<Class>")       ||
        SameText(name, "<Method>")      ||
        SameText(name, "<WChar>")       ||
        SameText(name, "<Array>")       ||
        SameText(name, "<Record>")      ||
        SameText(name, "<Interface>")   ||
        SameText(name, "<Int64>")       ||
        SameText(name, "<DynArray>")    ||
        SameText(name, "<ClassRef>")    ||
        SameText(name, "<Pointer>")     ||
        SameText(name, "<Procedure>"))
    {
        uses = KnowledgeBase.GetTypeUses(typeName);
        idx = KnowledgeBase.GetTypeIdxByModuleIds(uses, typeName);
        if (uses) delete[] uses;

        if (idx != -1)
        {
            idx = KnowledgeBase.TypeOffsets[idx].NamId;
            if (KnowledgeBase.GetTypeInfo(idx, INFO_FIELDS | INFO_PROPS | INFO_METHODS, &tInfo))
            {
                FTypeInfo_11011981->ShowKbInfo(&tInfo);
                //as delete tInfo;
            }
        }
        else
        {
            FTypeInfo_11011981->ShowRTTI(adr);
        }
        return;
    }
    if (SameText(name, "<Proc>")    	||
        SameText(name, "<Func>")     	||
        SameText(name, "<Constructor>") ||
        SameText(name, "<Destructor>")  ||
        SameText(name, "<EmbProc>") 	||
        SameText(name, "<EmbFunc>")  	||
        SameText(name, "<Emb?>")        ||
        SameText(name, "<ImpProc>")  	||
        SameText(name, "<ExpProc>")  	||
        SameText(name, "<ImpFunc>")  	||
        SameText(name, "<ExpFunc>")     ||
        SameText(name, "<Imp?>")        ||
        SameText(name, "<Exp?>")        ||
        SameText(name, "<?>"))
    {
        PROCHISTORYREC  rec;
        rec.adr = CurProcAdr;
        rec.itemIdx = lbCode->ItemIndex;
        rec.xrefIdx = lbCXrefs->ItemIndex;
        rec.topIdx = lbCode->TopIndex;
        ShowCode(adr, 0, -1, -1);
        CodeHistoryPush(&rec);
        pcWorkArea->ActivePage = tsCodeView;
    }
}
Example #8
0
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;
}