Changeset 190 for trunk/src/helpers/tmsgfile.c
- Timestamp:
- Jul 22, 2002, 10:08:02 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/tmsgfile.c
r169 r190 105 105 /* 106 106 *@@ 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. 107 111 * 108 112 *@@added V0.9.16 (2001-10-08) [umoeller] … … 111 115 typedef struct _MSGENTRY 112 116 { 113 TREE Tree; // ulKey points to strID.psz114 XSTRING strID; // message ID115 ULONG ulOfsText; // offset of start of text (in C-format buffer)116 ULONG cbText; // length of text in msg file117 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 117 121 } MSGENTRY, *PMSGENTRY; 118 122 … … 143 147 * 144 148 *@@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 152 APIRET LoadAndCompile(PTMFMSGFILE pTmf, // in: TMF struct to set up 153 PXFILE pFile) // in: opened XFILE for msg file 148 154 { 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 155 163 { 156 164 // file loaded: … … 162 170 ulEndMarkerLength = strlen(G_pcszEndMarker); 163 171 172 XSTRING strContents; 173 164 174 // 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 168 182 169 183 // convert to plain C format 170 xstrConvertLineFormat(&pFile->strContent,184 /* xstrConvertLineFormat(&pTmf->strContent, 171 185 CRLF2LF); 186 */ 172 187 173 188 // kick out all the comments 174 while (pStartOfMarker = strstr(pFile->strContent.psz, "\n;")) 189 /* 190 while (pStartOfMarker = strstr(pTmf->strContent.psz, "\n;")) 175 191 { 176 192 // copy the next line over this 177 193 PCSZ pEOL = strhFindEOL(pStartOfMarker + 2, NULL); 178 xstrrpl(&p File->strContent,194 xstrrpl(&pTmf->strContent, 179 195 // ofs of first char to replace: "\n;" 180 pStartOfMarker - p File->strContent.psz,196 pStartOfMarker - pTmf->strContent.psz, 181 197 // no. of chars to replace: 182 198 pEOL - pStartOfMarker, … … 186 202 0); 187 203 } 204 */ 188 205 189 206 // free excessive memory 190 xstrShrink(&pFile->strContent);191 192 pStartOfFile = pFile->strContent.psz;207 // xstrShrink(&pTmf->strContent); 208 209 pStartOfFile = strContents.psz; 193 210 194 211 // go build a tree of all message IDs... … … 205 222 // search next start marker 206 223 PCSZ pStartOfNextMarker = strstr(pStartOfMsgID + 1, 207 G_pcszStartMarker); 224 G_pcszStartMarker); // "\n<--" 208 225 // and the end-marker 209 226 PCSZ pEndOfMarker = strstr(pStartOfMsgID + 1, 210 G_pcszEndMarker); 227 G_pcszEndMarker); // "-->:" 211 228 212 229 PMSGENTRY pNew; … … 236 253 { 237 254 // 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; 240 258 241 259 ZERO(pNew); … … 253 271 pStartOfText++; 254 272 255 // store start of text273 // store offset of start of text 256 274 pNew->ulOfsText = pStartOfText - pStartOfFile; 257 275 258 276 // check if there's a comment before the 259 277 // next item 260 /*if (pNextComment = strstr(pStartOfText, "\n;"))278 if (pNextComment = strstr(pStartOfText, "\n;")) 261 279 { 262 280 if ( (!pStartOfNextMarker) 263 281 || (pNextComment < pStartOfNextMarker) 264 282 ) 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 } 273 293 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); 276 302 277 303 // remove trailing newlines 278 304 while ( (pNew->cbText) 279 && (pStartOfText[pNew->cbText-1] == '\n') 305 && ( (pStartOfText[pNew->cbText - 1] == '\n') 306 || (pStartOfText[pNew->cbText - 1] == '\r') 307 ) 280 308 ) 281 309 (pNew->cbText)--; 282 310 283 311 // store this thing 284 if (!treeInsert(&p File->IDsTreeRoot,312 if (!treeInsert(&pTmf->IDsTreeRoot, 285 313 NULL, 286 314 (TREE*)pNew, 287 315 treeCompareStrings)) 288 316 // successfully inserted: 289 (p File->cIDs)++;317 (pTmf->cIDs)++; 290 318 } 291 319 … … 293 321 pStartOfMarker = pStartOfNextMarker; 294 322 } // end while ( (pStartOfMarker) ... 295 } // end else if (!(pFile = NEW(TMFMSGFILE))) 323 324 free(pszContent); 325 326 } // end else if (!(pTmf = NEW(TMFMSGFILE))) 296 327 297 328 return arc; … … 318 349 * 319 350 *@@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 320 352 */ 321 353 … … 325 357 APIRET arc; 326 358 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))) 332 365 { 333 366 // create a TMFMSGFILE entry 334 PTMFMSGFILE p File;335 if (!(p File= NEW(TMFMSGFILE)))367 PTMFMSGFILE pTmf; 368 if (!(pTmf = NEW(TMFMSGFILE))) 336 369 arc = ERROR_NOT_ENOUGH_MEMORY; 337 370 else 338 371 { 339 ZERO(p File);340 p File->pszFilename = strdup(pcszMessageFile);372 ZERO(pTmf); 373 pTmf->pszFilename = strdup(pcszMessageFile); 341 374 342 375 // TMFMSGFILE created: 343 if (!(arc = LoadAndCompile(p File)))376 if (!(arc = LoadAndCompile(pTmf, pFile))) 344 377 { 345 378 // 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 } 352 392 } 353 else 393 394 if (arc) 354 395 // error: 355 tmfCloseMessageFile(&p File);396 tmfCloseMessageFile(&pTmf); 356 397 } 398 399 doshClose(&pFile); 357 400 } 358 401 … … 370 413 */ 371 414 372 static VOID FreeInternalMem(PTMFMSGFILE p File)415 static VOID FreeInternalMem(PTMFMSGFILE pTmf) 373 416 { 374 417 LONG cItems; 375 418 TREE** papNodes; 376 419 377 xstrClear(&pFile->strContent); 378 379 if (cItems = pFile->cIDs) 420 if (cItems = pTmf->cIDs) 380 421 { 381 if (papNodes = treeBuildArray(p File->IDsTreeRoot,422 if (papNodes = treeBuildArray(pTmf->IDsTreeRoot, 382 423 &cItems)) 383 424 { … … 410 451 if (ppMsgFile && *ppMsgFile) 411 452 { 412 PTMFMSGFILE p File= *ppMsgFile;413 414 if (p File->pszFilename)415 free(p File->pszFilename);416 417 FreeInternalMem(p File);418 419 free(p File);453 PTMFMSGFILE pTmf = *ppMsgFile; 454 455 if (pTmf->pszFilename) 456 free(pTmf->pszFilename); 457 458 FreeInternalMem(pTmf); 459 460 free(pTmf); 420 461 *ppMsgFile = NULL; 421 462 … … 453 494 *@@added V0.9.16 (2001-10-08) [umoeller] 454 495 *@@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 455 497 */ 456 498 … … 463 505 APIRET arc = NO_ERROR; 464 506 465 if (!pMsgFile) 507 if ( (!pMsgFile) 508 || (!pMsgFile->pszFilename) 509 ) 466 510 arc = ERROR_INVALID_PARAMETER; 467 511 else 468 512 { 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))) 477 520 { 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)))) 483 529 { 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 } 489 543 } 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) 499 546 { 500 // copy the raw string to the output buffer501 xstrcpy(pstr,502 pMsgFile->strContent.psz + pEntry->ulOfsText,503 pEntry->cbText);504 505 // now replace strings from the table506 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 507 554 { 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))) 513 565 { 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 } 522 595 } 523 596 } 524 597 } 525 else 526 arc = ERROR_MR_MID_NOT_FOUND;598 599 doshClose(&pFile); 527 600 } 528 601 }
Note:
See TracChangeset
for help on using the changeset viewer.