Back to index

im-sdk  12.3.91
UdCikuOper.c
Go to the documentation of this file.
00001 #include  <stdio.h>
00002 #include  <string.h>
00003 #include  <stdlib.h>
00004 #include  "GeneType.h"
00005 #include  "PyBasic.h"
00006 #include  "ciku.h"
00007 
00008 #define          UDCMEM_ALIGN         128
00009 UdcMemAll  udcAll;
00010 
00011 JINT   Hzcode2244ToYjcode(JINT nHzcode);
00012 JINT   JwordNCmp(JWORD* pwSrc1, JWORD* pwSrc2, JINT nNum);
00013 
00014 VOID   SetUdcfh(UdCikuHeader *pUdcfh);
00015 VOID   SetSpecHzData(JWORD* pwSpecHz, JINT nMaxLen);
00016 VOID   SetUdcIndex(UdcIndex *pUdci);
00017 VOID   CreateUdCikuFile();
00018 
00019 JINT   ReadUdcData(CHAR* szUdcName);
00020 JINT   WriteUdcData(CHAR* szUdcName, JINT nTimeStamp);
00021 JINT   AddUdc(JWORD* pwHz2244, JINT nLen);
00022 JINT   DelUdc(JWORD* pwHz2244, JINT nLen);
00023 JINT   PureUdc(VOID);
00024 
00025 /*
00026 **  Read out all those data in System Ciku file to pCkAll.
00027 */
00028 JINT ReadUdcData(CHAR* szUdcName)
00029 {
00030        FILE*  pfUdc;
00031        JINT   i, k, nTmp;
00032        JINT   nFileSize;
00033 
00034        pfUdc = fopen(szUdcName, "rb");
00035        if (pfUdc == NULL)
00036        {
00037               fprintf(stderr, "Failed to fopen() UserDefinedCiku File: %s\n", szUdcName);
00038               return FALSE;
00039        }
00040 
00041        fseek(pfUdc, 0, SEEK_SET);
00042        if ((JINT)fread (&(udcAll.udcfh), 1, sizeof(UdCikuHeader), pfUdc) != sizeof(UdCikuHeader) )
00043        {
00044               fprintf(stderr, "Failed to fread() UdCiku Header.\n");
00045               fclose(pfUdc);
00046               return FALSE;
00047        }
00048 
00049        /* Check Magic Word in Header */
00050        if ((udcAll.udcfh.nMagicDescHi != 0x35303539) || (udcAll.udcfh.nMagicDescLow != 0x34333442))
00051        {
00052               fprintf(stderr, "This is not a valid UdCiku File.\n");
00053               fclose(pfUdc);
00054               return FALSE;
00055        }
00056        nFileSize = udcAll.udcfh.nFileSize;
00057 
00058        fseek(pfUdc, 0, SEEK_END);
00059        if (nFileSize != ftell(pfUdc))
00060        {
00061               fprintf(stderr, "File UdCiku was demaged.\n");
00062               fclose(pfUdc);
00063               return FALSE;
00064        }
00065 
00066        /* Read in UdcIndex Data */
00067        nTmp = udcAll.udcfh.nIdxUdcPos;
00068        fseek(pfUdc, nTmp, SEEK_SET);
00069        if ((JINT)fread (&(udcAll.udci), 1, sizeof(UdcIndex), pfUdc) != sizeof(UdcIndex))
00070        {
00071               fprintf(stderr, "Failed to fread() UdCiku UdcIndex.\n");
00072               fclose(pfUdc);
00073               return FALSE;
00074        }
00075 
00076        /* Alloc Memory for SpecHz, and fill these data to it */
00077        nTmp = udcAll.udcfh.nSizeSpecHz;   /* In BYTE */
00078        udcAll.pwUdcSh = (JWORD*)malloc(nTmp);
00079        if (udcAll.pwUdcSh == NULL)
00080        {
00081               fprintf(stderr, "Failed to malloc() for pwUdcSh.\n");
00082               fclose(pfUdc);
00083               return FALSE;
00084        }
00085 
00086        nTmp = (udcAll.udcfh.nSizeSpecHz) / sizeof(JWORD);   /* In JWORD */
00087        fseek(pfUdc, udcAll.udcfh.nSpecHzStartPos, SEEK_SET);
00088        if ((JINT)fread (udcAll.pwUdcSh, sizeof(JWORD), nTmp, pfUdc) != nTmp )
00089        {
00090               fprintf(stderr, "Failed to fread() UdCiku File.\n");
00091               fclose(pfUdc);
00092               return FALSE;
00093        }
00094 
00095        /* Alloc Memory for pwUdc28[NUM_YINJIE], align each with UDCMEM_ALIGN BYTE (64 JWORD) */
00096        for (i = 0; i < NUM_YINJIE; i++)
00097        {
00098               /* Get the Actual Size to nTmp and upper align it to UDCMEM_ALIGN */
00099               nTmp = udcAll.udci.nYjOff[i + 1] - udcAll.udci.nYjOff[i];
00100               nTmp = ((nTmp + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;
00101               udcAll.pwUdc28[i] = (JWORD*)malloc(nTmp);
00102        }
00103 
00104        /* Is there any failure in Memory Allocation? If no, Init it to 0x0000 */
00105        for (i = 0; i < NUM_YINJIE; i++)
00106        {
00107               if (udcAll.pwUdc28[i] == NULL)
00108               {
00109                      for(k = 0; k < NUM_YINJIE; k++)
00110                      {
00111                             free (udcAll.pwUdc28[i]);
00112                             udcAll.pwUdc28[i] = NULL;
00113                      }
00114                      fprintf(stderr, "Failed in Alloc Mem for pwUdc28. %d\n", i);
00115                      return FALSE;
00116               }
00117 
00118               nTmp = udcAll.udci.nYjOff[i + 1] - udcAll.udci.nYjOff[i];
00119               nTmp = ((nTmp + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;
00120               for (k = 0; k < (nTmp / 2); k++)
00121                      udcAll.pwUdc28[i][k] = 0x0000;
00122        }
00123 
00124        /* Read in pwUdc28 data */
00125        nTmp = udcAll.udci.nStartPos;
00126        fseek(pfUdc, nTmp, SEEK_SET);
00127 
00128        for(i = 0; i < NUM_YINJIE; i++)
00129        {
00130               nTmp = (udcAll.udci.nYjOff[i + 1] - udcAll.udci.nYjOff[i]) / sizeof(JWORD);
00131               if ((JINT)fread (udcAll.pwUdc28[i], sizeof(JWORD), nTmp, pfUdc) != nTmp)
00132               {
00133                      fprintf(stderr, "Failed to fread() UdCiku pwUdc28.\n");
00134                      fclose(pfUdc);
00135                      return FALSE;
00136               }
00137        }
00138 
00139        fclose(pfUdc);
00140        return TRUE;
00141 }
00142 
00143 
00144 JINT WriteUdcData(CHAR* szUdcName, JINT nTimeStamp)
00145 {
00146        FILE*  pfUdc;
00147        JINT   i, nTmp;
00148        JINT   nFileSize;
00149 
00150        udcAll.udcfh.nLatestTime  = nTimeStamp;
00151 
00152        nFileSize = sizeof(UdCikuHeader) + sizeof(UdcIndex) +
00153                   udcAll.udcfh.nSizeSpecHz + udcAll.udci.nYjOff[NUM_YINJIE];
00154        udcAll.udcfh.nFileSize        = nFileSize;
00155        udcAll.udci.nStartPos         = sizeof(UdCikuHeader) + sizeof(UdcIndex) + udcAll.udcfh.nSizeSpecHz;
00156        udcAll.udci.nEndPos    = sizeof(UdCikuHeader) + sizeof(UdcIndex) +
00157                   udcAll.udcfh.nSizeSpecHz + udcAll.udci.nYjOff[NUM_YINJIE];
00158 
00159        /* Write These data back to file */
00160        pfUdc = fopen(szUdcName, "wb");
00161        if (pfUdc == NULL)
00162        {
00163               fprintf(stderr, "Failed to Open UdCiku File to Write.\n");
00164               return FALSE;
00165        }
00166 
00167        if ((JINT)fwrite (&(udcAll.udcfh), 1, sizeof(UdCikuHeader), pfUdc) != sizeof(UdCikuHeader) )
00168        {
00169               fprintf(stderr, "Failed to fwrite() Ud Ciku File11.\n");
00170               return FALSE;
00171        }
00172 
00173        if ((JINT)fwrite (&(udcAll.udci),  1, sizeof(UdcIndex), pfUdc) != sizeof(UdcIndex) )
00174        {
00175               fprintf(stderr, "Failed to fwrite() Ud Ciku File22.\n");
00176               return FALSE;
00177        }
00178 
00179        nTmp = (udcAll.udcfh.nSizeSpecHz) / sizeof(JWORD);
00180        if ((JINT)fwrite (udcAll.pwUdcSh, sizeof(JWORD), nTmp, pfUdc) != nTmp )
00181        {
00182               fprintf(stderr, "Failed to fwrite() Ud Ciku File33.\n");
00183               return FALSE;
00184        }
00185 
00186        for(i = 0; i < NUM_YINJIE; i++)
00187        {
00188               nTmp = (udcAll.udci.nYjOff[i + 1] - udcAll.udci.nYjOff[i]) / sizeof(JWORD);
00189               if ((JINT)fwrite (udcAll.pwUdc28[i], sizeof(JWORD), nTmp, pfUdc) != nTmp )
00190               {
00191                      fprintf(stderr, "Failed to fwrite() Ud Ciku File44.\n");
00192                      return FALSE;
00193               }
00194        }
00195 
00196        fclose(pfUdc);
00197        return TRUE;
00198 }
00199 
00200 
00201 /*
00202 **  Add an User Defined Cizu to structure udcAll.pwUdc28[i] AND
00203 **  realloc the related size of udcAll.udci.nYjOff[]
00204 **  nLen is in JWORD.
00205 */
00206 JINT AddUdc(JWORD* pwHz2244, JINT nLen)
00207 {
00208        JINT   i, nTmp1, nTmp2;
00209        JINT   nCurSize;
00210        JINT   nFreqLen, nTmpLen;
00211        JINT   nFirstYj;
00212 
00213        if (nLen <= 1)
00214               return FALSE;
00215 
00216        nFirstYj = Hzcode2244ToYjcode(pwHz2244[0]);
00217        if (nFirstYj == 0xFFFF)
00218        {
00219               fprintf(stderr, "Error in AddUdc.\n");
00220               return FALSE;
00221        }
00222 
00223        /*
00224        **  STEP1: Is it necessary to realloc() for this Yinjie's space?
00225        */
00226        nCurSize = udcAll.udci.nYjOff[nFirstYj + 1] - udcAll.udci.nYjOff[nFirstYj];    /* In BYTE */
00227        nTmp1   = ((nCurSize + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;
00228        nTmp2   = ((nCurSize + ((nLen + 1) * sizeof(JWORD)) + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;
00229        if (nTmp2 > nTmp1)
00230        {
00231               udcAll.pwUdc28[nFirstYj] = (JWORD*)realloc(udcAll.pwUdc28[nFirstYj], nTmp2);
00232               if (udcAll.pwUdc28[nFirstYj] == NULL)
00233               {
00234                      fprintf(stderr, "Failed to realloc() in AddUdc().\n");
00235                      return FALSE;
00236               }
00237               for(i = 0; i < (UDCMEM_ALIGN) / 2; i++)
00238                      udcAll.pwUdc28[nFirstYj][nTmp1 + i] = 0x0000;
00239        }
00240 
00241        /*
00242        ** STEP2:  Reduce the Frequence of other Udc for FUTURE FUNCTION: DiscardUdc().
00243        ** Notice: Cannot DiscardUdc() here, that will make the LOGIC confusion.
00244        */
00245        /* NO i++ in this FOR sentence */
00246        for (i = 0; i < nCurSize / 2; )
00247        {
00248               nFreqLen = (JINT)udcAll.pwUdc28[nFirstYj][i];
00249               nTmpLen        = (nFreqLen & 0x0007) + 2;
00250               if (nFreqLen >= 0x0010)
00251                      udcAll.pwUdc28[nFirstYj][i] -= 8;
00252               i += (nTmpLen + 1);
00253        }
00254 
00255        /*
00256        ** STEP3:  Append this pwHz2244 just after pwUdc28[nFirstYj][nCurSize / 2]
00257        ** Set the Frequence of this pwHz2244 to the Highest: 11111xxx.
00258        ** xxx from 000 to 111, indicates length from 2 to 9
00259        */
00260        nFreqLen = 0x00F8 + (nLen - 2);
00261        udcAll.pwUdc28[nFirstYj][nCurSize / 2] = (JWORD)nFreqLen;
00262        for (i = 0; i < nLen; i++)
00263               udcAll.pwUdc28[nFirstYj][(nCurSize / 2) + 1 + i] = pwHz2244[i];
00264 
00265        /*
00266        ** STEP4:  Adjust udcAll.udci.nYjOff[nFirstYj + 1] to nYjOff[NUM_YINJIE]
00267        */
00268        for (i = nFirstYj; i < NUM_YINJIE; i++)
00269               udcAll.udci.nYjOff[i + 1] += 2 * (nLen + 1);
00270 
00271        return TRUE;
00272 }
00273 
00274 
00275 /*
00276 **  Del an User Defined Cizu to structure udcAll.pwUdc28[i] AND
00277 **  realloc the related size of udcAll.udci.nYjOff[]
00278 **  If cannot find this Udc, just return FALSE.
00279 */
00280 JINT DelUdc(JWORD* pwHz2244, JINT nLen)
00281 {
00282        JINT   i, k, nTmp1, nTmp2;
00283        JINT   nCurSize;
00284        JINT   nFreqLen, nTmpLen;
00285        JINT   nFirstYj;
00286        JINT   nFindFlag;
00287 
00288        if (nLen <= 1)
00289               return FALSE;
00290 
00291        nFirstYj = Hzcode2244ToYjcode(pwHz2244[0]);
00292        if (nFirstYj == 0xFFFF)
00293        {
00294               fprintf(stderr, "Error in DelUdc.\n");
00295               return FALSE;
00296        }
00297 
00298        /*
00299        **  STEP1: Find this pwHz2244, Forward other Udcdata to (nLen + 1) JWORD.
00300        */
00301        nFindFlag = FALSE;
00302        nCurSize  = udcAll.udci.nYjOff[nFirstYj + 1] - udcAll.udci.nYjOff[nFirstYj]; /* In BYTE */
00303 
00304        /* NO i++ in this FOR sentence */
00305        for (i = 0; i < nCurSize / 2; )
00306        {
00307               nFreqLen = (JINT)udcAll.pwUdc28[nFirstYj][i];
00308               nTmpLen        = (nFreqLen & 0x0007) + 2;
00309               /* DON'T exchange the sequence of two judgement in the following IF sentence */
00310               if ((nTmpLen == nLen) && (JwordNCmp(pwHz2244, &(udcAll.pwUdc28[nFirstYj][i + 1]), nTmpLen) == 0))
00311               {
00312                      nFindFlag = TRUE;
00313                      for (k = i; k < (nCurSize / 2) - (nTmpLen + 1); k++)
00314                             udcAll.pwUdc28[nFirstYj][k] = udcAll.pwUdc28[nFirstYj][k + nTmpLen + 1];
00315                      for (k = (nCurSize / 2) - (nTmpLen + 1); k < nCurSize / 2; k++)
00316                             udcAll.pwUdc28[nFirstYj][k] = 0x0000;
00317 
00318                      break;       /* BREAK OUT FOR_i sentence */
00319               }
00320               i += (nTmpLen + 1);
00321        }
00322 
00323        if (nFindFlag == FALSE)
00324        {
00325               fprintf (stderr, "Failed to find given Udcizu in DelUdc().\n");
00326               return FALSE;
00327        }
00328 
00329        /*
00330        **  STEP2: Is it necessary to realloc() for this Yinjie's space?
00331        */
00332        nTmp1    = ((nCurSize + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;
00333        nTmp2    = ((nCurSize - ((nLen + 1) * sizeof(JWORD)) + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;
00334        if (nTmp2 < nTmp1)
00335        {
00336               udcAll.pwUdc28[nFirstYj] = (JWORD*)realloc(udcAll.pwUdc28[nFirstYj], nTmp2);
00337               if (udcAll.pwUdc28[nFirstYj] == NULL)
00338               {
00339                      fprintf(stderr, "Failed to realloc() in DelUdc().\n");
00340                      return FALSE;
00341               }
00342        }
00343 
00344        /*
00345        ** STEP3:  Adjust udcAll.udci.nYjOff[nFirstYj + 1] to nYjOff[NUM_YINJIE]
00346        */
00347        for (i = nFirstYj; i < NUM_YINJIE; i++)
00348               udcAll.udci.nYjOff[i + 1] -= 2 * (nLen + 1);
00349 
00350        return TRUE;
00351 }
00352 
00353 
00354 /*
00355 **  Pure (Discard or Clean) these rarely used Udc in UdCiku.
00356 **  BEST WAY to use:
00357 **          CALL this function just before the SAVING UDC operation!!!
00358 */
00359 JINT PureUdc(VOID)
00360 {
00361        JINT   i, k, nYj, nTmp1, nTmp2;
00362        JINT   nCurSize;
00363        JINT   nTmpFreq, nTmpLen;
00364        JINT   nPuredSize;
00365 
00366        JINT   nPureNum;
00367        nPureNum = 0;
00368 
00369        for (nYj = 0; nYj < NUM_YINJIE; nYj++)
00370        {
00371               nCurSize   = udcAll.udci.nYjOff[nYj + 1] - udcAll.udci.nYjOff[nYj];    /* In BYTE */
00372               nTmp1     = ((nCurSize + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN;        /* Old Buf Size */
00373 
00374               nPuredSize = 0;
00375               /* NO i++ in this FOR sentence */
00376               for (i = 0; i < nCurSize / 2; )
00377               {
00378                      nTmpFreq = (JUINT)udcAll.pwUdc28[nYj][i] & 0x00F8;
00379                      nTmpLen        = (udcAll.pwUdc28[nYj][i] & 0x0007) + 2;
00380 
00381                      if (nTmpFreq != 0x0008)         /* Lowest Frequence */
00382                      {
00383                             for (k = i; k < (nCurSize / 2) - (nTmpLen + 1); k++)
00384                                    udcAll.pwUdc28[nYj][k] = udcAll.pwUdc28[nYj][k + nTmpLen + 1];
00385                             for (k = (nCurSize / 2) - (nTmpLen + 1); k < nCurSize / 2; k++)
00386                                    udcAll.pwUdc28[nYj][k] = 0x0000;
00387 
00388                             nPureNum   += 1;
00389                             nCurSize   -= 2 * (nTmpLen + 1);
00390                             nPuredSize += 2 * (nTmpLen + 1);
00391                      }
00392                      else   /* If an Udcizu is already deleted, DON'T CHANGE i HERE!! MXL */
00393                             i += (nTmpLen + 1);
00394               }
00395 
00396               /* Adjust udcAll.udci.nYjOff[nFirstYj + 1] to nYjOff[NUM_YINJIE] */
00397               for (k = nYj; k < NUM_YINJIE; k++)
00398                      udcAll.udci.nYjOff[k + 1] -= nPuredSize;
00399 
00400               /* Is it necessary to resize the buffer allocated for udcAll.pwUdc28[nYj] ? */
00401               nTmp2    = ((nCurSize + UDCMEM_ALIGN) / UDCMEM_ALIGN) * UDCMEM_ALIGN; /* New Buf Size */
00402               if (nTmp2 < nTmp1)
00403               {
00404                      udcAll.pwUdc28[nYj] = (JWORD*)realloc(udcAll.pwUdc28[nYj], nTmp2);
00405                      if (udcAll.pwUdc28[nYj] == NULL)
00406                      {
00407                             fprintf(stderr, "Failed to realloc() in PureUdc().\n");
00408                             return FALSE;
00409                      }
00410               }
00411        }
00412 
00413 #ifdef _DEBUG
00414        fprintf (stderr, "PureNum is %d\n", nPureNum);
00415 #endif
00416 
00417        return TRUE;
00418 }
00419 
00420 
00421 /*
00422 **  Create UdFileHeader.
00423 **  Data Sequence:
00424 **
00425 **  UdCikuHeader(UDCMEM_ALIGN) + UdcIndex (1676) + SpecHzData (2048 = 1024 * sizeof(JWORD)) + Ud2~8CizuData
00426 **
00427 **  Notice: In Memory, Ud2~8CizuData is not same as in disk file. They are realloced.
00428 */
00429 VOID SetUdcfh(UdCikuHeader *pUdcfh)
00430 {
00431        JINT   i, nLen;
00432        CHAR   szName[] = " SUN TDC P.R.C. 用户拼音词库 1.0";
00433        CHAR   szCopyright[] = "版权所有(C) 太阳计算机系统(中国)有限公司 1997_11";
00434 
00435        for (i = 0; i < 32; i++)
00436               pUdcfh->szName[i]      = '\0';
00437        for (i = 0; i < 48; i++)
00438               pUdcfh->szCopyright[i] = '\0';
00439 
00440        pUdcfh->nMagicDescHi = 0x35303539;
00441        pUdcfh->nMagicDescLow       = 0x34333442;
00442        pUdcfh->nSize        = sizeof(UdCikuHeader);
00443        pUdcfh->nFileSize    = 0;
00444 
00445        nLen = strlen(szName);
00446        for (i = 0; i < nLen; i++)
00447               pUdcfh->szName[i]      = szName[i];
00448 
00449        nLen = strlen(szCopyright);
00450        for (i = 0; i < nLen; i++)
00451               pUdcfh->szCopyright[i] = szCopyright[i];
00452 
00453        pUdcfh->nVersion     = 0x00010000;        /* Version 1.0 */
00454        pUdcfh->nLatestTime  = 0;
00455 
00456        pUdcfh->nSpecHzStartPos = 0;
00457        pUdcfh->nSizeSpecHz  = 0;           /* 2 KiloBytes for all spec Hanzi, each seperated by a 0x0000(JWORD) */
00458        pUdcfh->nIdxUdcPos   = 0;           /* Index Position of User defined 2 ~ 8 Hanzi Cizu */
00459 
00460        pUdcfh->nReserve1    = 0;
00461        pUdcfh->nReserve2    = 0;
00462        pUdcfh->nReserve3    = 0;
00463 
00464        printf("Sizeof structure UdCikuHeader is %d\n", sizeof(UdCikuHeader));
00465 }
00466 
00467 /*
00468 **  Here: nMaxLen in JWORD.
00469 **  Data Sequence of pwSpecHz ==>
00470 **                 0HHHHHHHHHH00HHHHH0H00000HHHH
00471 */
00472 VOID SetSpecHzData(JWORD* pwSpecHz, JINT nMaxLen)
00473 {
00474        JINT   nHzcode, nYjcode;
00475        JINT   i, k, m, nLen, nOff;
00476 
00477        JWORD  wSpecYjHanzi[NUM_YINJIE][6];
00478        JINT   nSpecCount[NUM_YINJIE];
00479 
00480 /*     UCHAR  szTmp[20], nTmpYj, nFlag; */
00481 
00482        UCHAR   szSpecHz[400] =
00483        "之节或子出也能对大百太且用及过无才开到于下不来占此小学少最虽叫只因加边同由则几必然名多反后年可长秒向第并前半间曾好会全个从化给比让说以各有为门要人经是的了而你我他与其就和在这那每某本总如被更但将仅应该很都性上中内外分原";
00484 /*    1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 */
00485 
00486        memset(wSpecYjHanzi, 0x00, sizeof(JWORD) * NUM_YINJIE * 6);
00487        memset(nSpecCount,   0x00, sizeof(JINT) * NUM_YINJIE);
00488 
00489        /* 是0xCAC7 不0xB2BB */
00490        nLen = strlen((CHAR*)szSpecHz);
00491        for (i = 0; (i < nLen / 2) && (i < nMaxLen); i++)
00492        {
00493               nHzcode = (szSpecHz[2 * i] << 8) + szSpecHz[2 * i + 1];
00494               nYjcode = Hzcode2244ToYjcode(nHzcode);
00495 
00496               wSpecYjHanzi[nYjcode][nSpecCount[nYjcode]] = (JWORD)nHzcode;
00497               nSpecCount[nYjcode]++;
00498        }
00499 
00500        k = 0;
00501        for (i = 0; i < NUM_YINJIE; i++)
00502        {
00503               pwSpecHz[k++] = 0x0000;
00504               for (m = 0; m < nSpecCount[i]; m++)
00505                      pwSpecHz[k++] = wSpecYjHanzi[i][m];
00506        }
00507 }
00508 
00509 
00510 VOID SetUdcIndex(UdcIndex *pUdci)
00511 {
00512        JINT   i, k;
00513        JINT   nM28Len[NUM_YINJIE];
00514 
00515        pUdci->nSize    = sizeof(UdcIndex);
00516        pUdci->nStartPos  = 0;                           /* To be added by a BaseOffset */
00517 
00518        for (i = 0; i < (NUM_YINJIE + 1); i++)
00519               pUdci->nYjOff[i]  = 0;
00520 
00521        /* Assume that there is a FreqLen(JWORD), and a 0xBBBB(换) and 0xCCCC (烫) */
00522        for (i = 0; i < NUM_YINJIE; i++)
00523               nM28Len[i] = 0;
00524 
00525        for (i = 0; i < NUM_YINJIE; i++)
00526               for (k = i; k < NUM_YINJIE; k++)
00527                      pUdci->nYjOff[k + 1] += nM28Len[i];
00528 
00529        pUdci->nEndPos          = pUdci->nYjOff[NUM_YINJIE];       /* To be added by a BaseOffset */
00530 }
00531 
00532 
00533 VOID CreateUdCikuFile()
00534 {
00535        JINT   i, nTmp;
00536        JINT   nFileSize;
00537        JWORD  wSpecHz[1024];
00538        FILE*  pfUdCiku;
00539        JWORD* pwUdCizu[NUM_YINJIE];
00540 
00541        UdCikuHeader    udcfh;
00542        UdcIndex        udci;
00543 
00544        SetUdcfh(&udcfh);
00545 
00546        memset(wSpecHz, '\0', 1024 * sizeof(JWORD));
00547        SetSpecHzData(wSpecHz, 1024);
00548        SetUdcIndex(&udci);
00549 
00550        nFileSize = sizeof(UdCikuHeader) + sizeof(UdcIndex) + (1024 * sizeof(JWORD)) + udci.nEndPos;
00551        udcfh.nFileSize            = nFileSize;
00552        fprintf (stderr, "File Size of UdCiku.dat should be %d 0x%X\n", nFileSize, nFileSize);
00553 
00554        udcfh.nSpecHzStartPos  = sizeof(UdCikuHeader) + sizeof(UdcIndex);
00555        udcfh.nSizeSpecHz      = 1024 * sizeof(JWORD);        /* 2 KiloBytes for all spec Hanzi, each seperated by a 0x0000(JWORD) */
00556        udcfh.nIdxUdcPos       = sizeof(UdCikuHeader);        /* Index Position of User defined 2 ~ 8 Hanzi Cizu */
00557 
00558        udci.nStartPos             += sizeof(UdCikuHeader) + sizeof(UdcIndex) + (1024 * sizeof(JWORD));
00559        udci.nEndPos        += sizeof(UdCikuHeader) + sizeof(UdcIndex) + (1024 * sizeof(JWORD));
00560 
00561        pfUdCiku = fopen("UdCiku.dat", "wb");
00562        if (pfUdCiku == NULL)
00563        {
00564               fprintf (stderr, "Failed to Create User Defined Ciku [UdCiku.dat]\n");
00565               exit (FALSE);
00566        }
00567 
00568        /* Alloc for each Yinjie 64 * 2 bytes space to contain User Defined Cizu */
00569        for (i = 0; i < NUM_YINJIE; i++)
00570        {
00571               pwUdCizu[i] = (JWORD*)malloc(64 * sizeof(JWORD));
00572               if (pwUdCizu == NULL)
00573               {
00574                      printf("Failed to alloc mem in CreateUdCikuFile().\n");
00575                      exit (FALSE);
00576               }
00577        }
00578 
00579        /* WriteData to file */
00580        if ((JINT)fwrite (&udcfh, 1, sizeof(UdCikuHeader), pfUdCiku) != sizeof(UdCikuHeader) )
00581        {
00582               fprintf(stderr, "Failed to fwrite() Ud Ciku File11.\n");
00583               exit (FALSE);
00584        }
00585 
00586        if ((JINT)fwrite (&udci,  1, sizeof(UdcIndex), pfUdCiku) != sizeof(UdcIndex) )
00587        {
00588               fprintf(stderr, "Failed to fwrite() Ud Ciku File22.\n");
00589               exit (FALSE);
00590        }
00591 
00592        if ((JINT)fwrite (wSpecHz,  sizeof(JWORD), 1024, pfUdCiku) != 1024 )
00593        {
00594               fprintf(stderr, "Failed to fwrite() Ud Ciku File33.\n");
00595               exit (FALSE);
00596        }
00597 
00598        for(i = 0; i < NUM_YINJIE; i++)
00599        {
00600               nTmp = (udci.nYjOff[i + 1] - udci.nYjOff[i]) / 2;
00601               if ((JINT)fwrite (pwUdCizu[i], sizeof(JWORD), nTmp, pfUdCiku) != nTmp )
00602               {
00603                      fprintf(stderr, "Failed to fwrite() Ud Ciku File44.\n");
00604                      exit (FALSE);
00605               }
00606        }
00607 
00608        fclose(pfUdCiku);
00609 
00610        for(i = 0; i < NUM_YINJIE; i++)
00611               free (pwUdCizu[i]);
00612 
00613 }
00614 
00615 
00616 /*
00617 **  This Function similar to strncmp() in standard ANSI library.
00618 **  RETURN:  0             pwSrc1 == pwSrc2
00619 **         -1       pwSrc1 <       pwSrc2
00620 **          1       pwSrc1 >       pwSrc2
00621 **
00622 **  Notice: because Jword hava no StringZero characteristics,
00623 **         SO, must give a proper nNum value!!!
00624 **         nNum is in JWORD, not in BYTE!!
00625 */
00626 JINT JwordNCmp(JWORD* pwSrc1, JWORD* pwSrc2, JINT nNum)
00627 {
00628        JINT   i;
00629        JINT   nRet;
00630 
00631        nRet = 0;
00632        for(i = 0; (i < nNum) && (nRet == 0); i++)
00633        {
00634               if(pwSrc1[i] > pwSrc2[i])
00635               {
00636                      nRet = 1;
00637                      break;
00638               }
00639               else if(pwSrc1[i] < pwSrc2[i])
00640               {
00641                      nRet = -1;
00642                      break;
00643               }
00644        }
00645 
00646        return nRet;
00647 }
00648 
00649