Ignore:
Timestamp:
Jul 22, 2002, 10:08:02 PM (23 years ago)
Author:
umoeller
Message:

DBCS fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/tmsgfile.c

    r169 r190  
    105105/*
    106106 *@@ MSGENTRY:
     107 *      private representation of a message in a text
     108 *      message file. Each tree entry in
     109 *      TMFMSGFILE.IDsTreeRoot points to one of these
     110 *      structures.
    107111 *
    108112 *@@added V0.9.16 (2001-10-08) [umoeller]
     
    111115typedef struct _MSGENTRY
    112116{
    113     TREE    Tree;               // ulKey points to strID.psz
    114     XSTRING strID;              // message ID
    115     ULONG   ulOfsText;          // offset of start of text (in C-format buffer)
    116     ULONG   cbText;             // length of text in msg file
     117    TREE        Tree;               // ulKey points to strID.psz
     118    XSTRING     strID;              // message ID
     119    ULONG       ulOfsText;          // offset of start of message text in file
     120    ULONG       cbText;             // length of text in msg file
    117121} MSGENTRY, *PMSGENTRY;
    118122
     
    143147 *
    144148 *@@added V0.9.18 (2002-03-24) [umoeller]
    145  */
    146 
    147 APIRET LoadAndCompile(PTMFMSGFILE pFile)
     149 *@@changed V0.9.20 (2002-07-19) [umoeller]: optimized, no longer holding all msgs im mem
     150 */
     151
     152APIRET LoadAndCompile(PTMFMSGFILE pTmf,     // in: TMF struct to set up
     153                      PXFILE pFile)         // in: opened XFILE for msg file
    148154{
    149     APIRET arc;
    150 
    151     PSZ pszContent = NULL;
    152     if (!(arc = doshLoadTextFile(pFile->pszFilename,
    153                                  &pszContent,
    154                                  NULL)))
     155    APIRET  arc;
     156
     157    PSZ     pszContent = NULL;
     158    ULONG   cbRead;
     159
     160    if (!(arc = doshReadText(pFile,
     161                             &pszContent,
     162                             &cbRead)))    // bytes read including null char
    155163    {
    156164        // file loaded:
     
    162170                ulEndMarkerLength = strlen(G_pcszEndMarker);
    163171
     172        XSTRING strContents;
     173
    164174        // initialize TMFMSGFILE struct
    165         treeInit(&pFile->IDsTreeRoot, NULL);
    166 
    167         xstrInitSet(&pFile->strContent, pszContent);
     175        treeInit(&pTmf->IDsTreeRoot, NULL);
     176
     177        xstrInit(&strContents,
     178                 cbRead);       // including null byte
     179        xstrcpy(&strContents,
     180                pszContent,
     181                cbRead - 1);    // not including null byte
    168182
    169183        // convert to plain C format
    170         xstrConvertLineFormat(&pFile->strContent,
     184        /* xstrConvertLineFormat(&pTmf->strContent,
    171185                              CRLF2LF);
     186        */
    172187
    173188        // kick out all the comments
    174         while (pStartOfMarker = strstr(pFile->strContent.psz, "\n;"))
     189        /*
     190        while (pStartOfMarker = strstr(pTmf->strContent.psz, "\n;"))
    175191        {
    176192            // copy the next line over this
    177193            PCSZ pEOL = strhFindEOL(pStartOfMarker + 2, NULL);
    178             xstrrpl(&pFile->strContent,
     194            xstrrpl(&pTmf->strContent,
    179195                    // ofs of first char to replace: "\n;"
    180                     pStartOfMarker - pFile->strContent.psz,
     196                    pStartOfMarker - pTmf->strContent.psz,
    181197                    // no. of chars to replace:
    182198                    pEOL - pStartOfMarker,
     
    186202                    0);
    187203        }
     204        */
    188205
    189206        // free excessive memory
    190         xstrShrink(&pFile->strContent);
    191 
    192         pStartOfFile = pFile->strContent.psz;
     207        // xstrShrink(&pTmf->strContent);
     208
     209        pStartOfFile = strContents.psz;
    193210
    194211        // go build a tree of all message IDs...
     
    205222            // search next start marker
    206223            PCSZ pStartOfNextMarker = strstr(pStartOfMsgID + 1,
    207                                              G_pcszStartMarker);
     224                                             G_pcszStartMarker);    // "\n<--"
    208225            // and the end-marker
    209226            PCSZ pEndOfMarker = strstr(pStartOfMsgID + 1,
    210                                        G_pcszEndMarker);
     227                                       G_pcszEndMarker);              // "-->:"
    211228
    212229            PMSGENTRY pNew;
     
    236253            {
    237254                // length of the ID
    238                 ULONG ulIDLength = pEndOfMarker - pStartOfMsgID;
    239                 PCSZ pStartOfText = pEndOfMarker + ulEndMarkerLength;
     255                ULONG   ulIDLength = pEndOfMarker - pStartOfMsgID;
     256                PCSZ    pStartOfText = pEndOfMarker + ulEndMarkerLength,
     257                        pNextComment;
    240258
    241259                ZERO(pNew);
     
    253271                    pStartOfText++;
    254272
    255                 // store start of text
     273                // store offset of start of text
    256274                pNew->ulOfsText = pStartOfText - pStartOfFile;
    257275
    258276                // check if there's a comment before the
    259277                // next item
    260                 /* if (pNextComment = strstr(pStartOfText, "\n;"))
     278                if (pNextComment = strstr(pStartOfText, "\n;"))
    261279                {
    262280                    if (    (!pStartOfNextMarker)
    263281                         || (pNextComment < pStartOfNextMarker)
    264282                       )
    265                         pEndOfText = pNextComment;
    266                 } */
    267 
    268                 if (pStartOfNextMarker)
    269                     // other markers left:
    270                     pNew->cbText =    // offset of next marker
    271                                      (pStartOfNextMarker - pStartOfFile)
    272                                    - pNew->ulOfsText;
     283                        pNew->cbText =    // offset of comment marker
     284                                          (pNextComment - pStartOfFile)
     285                                        - pNew->ulOfsText;
     286                    else
     287                        // we have a next marker AND
     288                        // the comment comes after it
     289                        pNew->cbText =    // offset of next marker
     290                                         (pStartOfNextMarker - pStartOfFile)
     291                                       - pNew->ulOfsText;
     292                }
    273293                else
    274                     // this was the last message:
    275                     pNew->cbText = strlen(pStartOfText);
     294                    if (pStartOfNextMarker)
     295                        // other markers left:
     296                        pNew->cbText =    // offset of next marker
     297                                         (pStartOfNextMarker - pStartOfFile)
     298                                       - pNew->ulOfsText;
     299                    else
     300                        // this was the last message:
     301                        pNew->cbText = strlen(pStartOfText);
    276302
    277303                // remove trailing newlines
    278304                while (    (pNew->cbText)
    279                         && (pStartOfText[pNew->cbText-1] == '\n')
     305                        && (    (pStartOfText[pNew->cbText - 1] == '\n')
     306                             || (pStartOfText[pNew->cbText - 1] == '\r')
     307                           )
    280308                      )
    281309                    (pNew->cbText)--;
    282310
    283311                // store this thing
    284                 if (!treeInsert(&pFile->IDsTreeRoot,
     312                if (!treeInsert(&pTmf->IDsTreeRoot,
    285313                                NULL,
    286314                                (TREE*)pNew,
    287315                                treeCompareStrings))
    288316                    // successfully inserted:
    289                     (pFile->cIDs)++;
     317                    (pTmf->cIDs)++;
    290318            }
    291319
     
    293321            pStartOfMarker = pStartOfNextMarker;
    294322        } // end while (    (pStartOfMarker) ...
    295     } // end else if (!(pFile = NEW(TMFMSGFILE)))
     323
     324        free(pszContent);
     325
     326    } // end else if (!(pTmf = NEW(TMFMSGFILE)))
    296327
    297328    return arc;
     
    318349 *
    319350 *@@added V0.9.16 (2001-10-08) [umoeller]
     351 *@@changed V0.9.20 (2002-07-19) [umoeller]: optimized, no longer holding all msgs im mem
    320352 */
    321353
     
    325357    APIRET arc;
    326358
    327     FILESTATUS3 fs3;
    328     if (!(arc = DosQueryPathInfo((PSZ)pcszMessageFile,
    329                                  FIL_STANDARD,
    330                                  &fs3,
    331                                  sizeof(fs3))))
     359    ULONG   cbFile;
     360    PXFILE  pFile;
     361    if (!(arc = doshOpen(pcszMessageFile,
     362                         XOPEN_READ_EXISTING,
     363                         &cbFile,
     364                         &pFile)))
    332365    {
    333366        // create a TMFMSGFILE entry
    334         PTMFMSGFILE pFile;
    335         if (!(pFile = NEW(TMFMSGFILE)))
     367        PTMFMSGFILE pTmf;
     368        if (!(pTmf = NEW(TMFMSGFILE)))
    336369            arc = ERROR_NOT_ENOUGH_MEMORY;
    337370        else
    338371        {
    339             ZERO(pFile);
    340             pFile->pszFilename = strdup(pcszMessageFile);
     372            ZERO(pTmf);
     373            pTmf->pszFilename = strdup(pcszMessageFile);
    341374
    342375            // TMFMSGFILE created:
    343             if (!(arc = LoadAndCompile(pFile)))
     376            if (!(arc = LoadAndCompile(pTmf, pFile)))
    344377            {
    345378                // set timestamp to that of the file
    346                 dtCreateFileTimeStamp(pFile->szTimestamp,
    347                                       &fs3.fdateLastWrite,
    348                                       &fs3.ftimeLastWrite);
    349 
    350                 // output
    351                 *ppMsgFile = pFile;
     379                FILESTATUS3 fs3;
     380                if (!(arc = DosQueryFileInfo(pFile->hf,
     381                                             FIL_STANDARD,
     382                                             &fs3,
     383                                             sizeof(fs3))))
     384                {
     385                    dtCreateFileTimeStamp(pTmf->szTimestamp,
     386                                          &fs3.fdateLastWrite,
     387                                          &fs3.ftimeLastWrite);
     388
     389                    // output
     390                    *ppMsgFile = pTmf;
     391                }
    352392            }
    353             else
     393
     394            if (arc)
    354395                // error:
    355                 tmfCloseMessageFile(&pFile);
     396                tmfCloseMessageFile(&pTmf);
    356397        }
     398
     399        doshClose(&pFile);
    357400    }
    358401
     
    370413 */
    371414
    372 static VOID FreeInternalMem(PTMFMSGFILE pFile)
     415static VOID FreeInternalMem(PTMFMSGFILE pTmf)
    373416{
    374417    LONG   cItems;
    375418    TREE**  papNodes;
    376419
    377     xstrClear(&pFile->strContent);
    378 
    379     if (cItems = pFile->cIDs)
     420    if (cItems = pTmf->cIDs)
    380421    {
    381         if (papNodes = treeBuildArray(pFile->IDsTreeRoot,
     422        if (papNodes = treeBuildArray(pTmf->IDsTreeRoot,
    382423                                      &cItems))
    383424        {
     
    410451    if (ppMsgFile && *ppMsgFile)
    411452    {
    412         PTMFMSGFILE pFile = *ppMsgFile;
    413 
    414         if (pFile->pszFilename)
    415             free(pFile->pszFilename);
    416 
    417         FreeInternalMem(pFile);
    418 
    419         free(pFile);
     453        PTMFMSGFILE pTmf = *ppMsgFile;
     454
     455        if (pTmf->pszFilename)
     456            free(pTmf->pszFilename);
     457
     458        FreeInternalMem(pTmf);
     459
     460        free(pTmf);
    420461        *ppMsgFile = NULL;
    421462
     
    453494 *@@added V0.9.16 (2001-10-08) [umoeller]
    454495 *@@changed V0.9.18 (2002-03-24) [umoeller]: now recompiling if last write date changed
     496 *@@changed V0.9.20 (2002-07-19) [umoeller]: optimized, no longer holding all msgs im mem
    455497 */
    456498
     
    463505    APIRET arc = NO_ERROR;
    464506
    465     if (!pMsgFile)
     507    if (    (!pMsgFile)
     508         || (!pMsgFile->pszFilename)
     509       )
    466510        arc = ERROR_INVALID_PARAMETER;
    467511    else
    468512    {
    469         // check if last-write date/time changed compared
    470         // to the last time we opened the thing...
    471         // V0.9.18 (2002-03-24) [umoeller]
    472         FILESTATUS3 fs3;
    473         if (!(arc = DosQueryPathInfo(pMsgFile->pszFilename,
    474                                      FIL_STANDARD,
    475                                      &fs3,
    476                                      sizeof(fs3))))
     513        // open the file again V0.9.20 (2002-07-19) [umoeller]
     514        ULONG   cbFile;
     515        PXFILE  pFile;
     516        if (!(arc = doshOpen(pMsgFile->pszFilename,
     517                             XOPEN_READ_EXISTING,
     518                             &cbFile,
     519                             &pFile)))
    477520        {
    478             CHAR szTemp[30];
    479             dtCreateFileTimeStamp(szTemp,
    480                                   &fs3.fdateLastWrite,
    481                                   &fs3.ftimeLastWrite);
    482             if (strcmp(szTemp, pMsgFile->szTimestamp))
     521            // check if last-write date/time changed compared
     522            // to the last time we opened the thing...
     523            // V0.9.18 (2002-03-24) [umoeller]
     524            FILESTATUS3 fs3;
     525            if (!(arc = DosQueryFileInfo(pFile->hf,
     526                                         FIL_STANDARD,
     527                                         &fs3,
     528                                         sizeof(fs3))))
    483529            {
    484                 // last write date changed:
    485                 _Pmpf((__FUNCTION__ ": timestamp changed, recompiling"));
    486                 FreeInternalMem(pMsgFile);
    487                 if (!(arc = LoadAndCompile(pMsgFile)))
    488                     strcpy(pMsgFile->szTimestamp, szTemp);
     530                CHAR szTemp[30];
     531                dtCreateFileTimeStamp(szTemp,
     532                                      &fs3.fdateLastWrite,
     533                                      &fs3.ftimeLastWrite);
     534                if (strcmp(szTemp, pMsgFile->szTimestamp))
     535                {
     536                    // last write date changed:
     537                    _Pmpf((__FUNCTION__ ": timestamp changed, recompiling"));
     538                    FreeInternalMem(pMsgFile);
     539
     540                    if (!(arc = LoadAndCompile(pMsgFile, pFile)))
     541                        strcpy(pMsgFile->szTimestamp, szTemp);
     542                }
    489543            }
    490         }
    491 
    492         if (!arc)
    493         {
    494             // go find the message in the tree
    495             PMSGENTRY pEntry;
    496             if (pEntry = (PMSGENTRY)treeFind(pMsgFile->IDsTreeRoot,
    497                                              (ULONG)pcszMessageName,
    498                                              treeCompareStrings))
     544
     545            if (!arc)
    499546            {
    500                 // copy the raw string to the output buffer
    501                 xstrcpy(pstr,
    502                         pMsgFile->strContent.psz + pEntry->ulOfsText,
    503                         pEntry->cbText);
    504 
    505                 // now replace strings from the table
    506                 if (cTableEntries && pTable)
     547                // go find the message in the tree
     548                PMSGENTRY pEntry;
     549                if (!(pEntry = (PMSGENTRY)treeFind(pMsgFile->IDsTreeRoot,
     550                                                   (ULONG)pcszMessageName,
     551                                                   treeCompareStrings)))
     552                    arc = ERROR_MR_MID_NOT_FOUND;
     553                else
    507554                {
    508                     CHAR szFind[10] = "%0";
    509                     ULONG ul;
    510                     for (ul = 0;
    511                          ul < cTableEntries;
    512                          ul++)
     555                    PSZ     pszMsg;
     556                    ULONG   cbRead = pEntry->cbText;
     557
     558                    if (!(pszMsg = (PSZ)malloc(cbRead + 1)))
     559                        arc = ERROR_NOT_ENOUGH_MEMORY;
     560                    else if (!(arc = doshReadAt(pFile,
     561                                                pEntry->ulOfsText,
     562                                                &cbRead,
     563                                                pszMsg,
     564                                                DRFL_NOCACHE | DRFL_FAILIFLESS)))
    513565                    {
    514                         ULONG ulOfs = 0;
    515 
    516                         _ultoa(ul + 1, szFind + 1, 10);
    517                         while (xstrFindReplaceC(pstr,
    518                                                 &ulOfs,
    519                                                 szFind,
    520                                                 pTable[ul]))
    521                             ;
     566                        // null-terminate
     567                        pszMsg[cbRead] = '\0';
     568                        xstrset2(pstr,
     569                                 pszMsg,
     570                                 cbRead);
     571
     572                        // kick out \r\n
     573                        xstrConvertLineFormat(pstr,
     574                                              CRLF2LF);
     575
     576                        // now replace strings from the table
     577                        if (cTableEntries && pTable)
     578                        {
     579                            CHAR szFind[10] = "%0";
     580                            ULONG ul;
     581                            for (ul = 0;
     582                                 ul < cTableEntries;
     583                                 ul++)
     584                            {
     585                                ULONG ulOfs = 0;
     586
     587                                _ultoa(ul + 1, szFind + 1, 10);
     588                                while (xstrFindReplaceC(pstr,
     589                                                        &ulOfs,
     590                                                        szFind,
     591                                                        pTable[ul]))
     592                                    ;
     593                            }
     594                        }
    522595                    }
    523596                }
    524597            }
    525             else
    526                 arc = ERROR_MR_MID_NOT_FOUND;
     598
     599            doshClose(&pFile);
    527600        }
    528601    }
Note: See TracChangeset for help on using the changeset viewer.