Changeset 4402 for trunk/tools/common/kFileDef.cpp
- Timestamp:
- Oct 3, 2000, 7:42:41 AM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/common/kFileDef.cpp
r2759 r4402 18 18 #include <string.h> 19 19 #include <stdlib.h> 20 20 #include <assert.h> 21 22 #include "kFile.h" 21 23 #include "kFileFormatBase.h" 22 24 #include "kFileDef.h" … … 26 28 * Internal Functions * 27 29 *******************************************************************************/ 28 static char *dupeString(const char *psz );30 static char *dupeString(const char *psz, BOOL fSkipFirstWord = FALSE); 29 31 static char *trim(char *psz); 32 static char *ltrim(const char *psz); 30 33 static char *removeFnutts(char *pszStr); 34 inline char upcase(char ch); 35 static char *stristr(const char *pszStr, const char *pszSubStr); 36 31 37 32 38 /** 33 39 * Duplicates a string. 34 * @returns Pointer to stringcopy. Remeber to delete this! 35 * @param psz Pointer to string to duplicate. 36 */ 37 static char *dupeString(const char *psz) 40 * @returns Pointer to stringcopy. Remeber to delete this! 41 * @param psz Pointer to string to duplicate. 42 * @param fSkipFirstWord Skips the first word before duplicating the string. 43 */ 44 static char *dupeString(const char *psz, BOOL fSkipFirstWord/* = FALSE*/) 38 45 { 39 46 char *pszDupe; 40 47 if (psz == NULL) 41 48 return NULL; 49 50 if (fSkipFirstWord) 51 { 52 while (*psz != ' ' && *psz != '\t' && *psz != '\n' && *psz != '\r' && *psz != '\0') 53 psz++; 54 psz = ltrim(psz); 55 } 56 42 57 pszDupe = new char[strlen(psz)+1]; 43 return strcpy(pszDupe, psz); 58 strcpy(pszDupe, psz); 59 if (fSkipFirstWord) 60 return removeFnutts(pszDupe); 61 return pszDupe; 44 62 } 45 63 … … 56 74 if (psz == NULL) 57 75 return NULL; 58 while (*psz == ' ' )76 while (*psz == ' ' || *psz == '\t') 59 77 psz++; 60 78 i = strlen(psz) - 1; 61 while (i >= 0 && psz[i] == ' ')79 while (i >= 0 && (psz[i] == ' ' || psz[i] == '\t')) 62 80 i--; 63 81 psz[i+1] = '\0'; … … 66 84 67 85 68 kFileDef::kFileDef(FILE *phFile) throw(int) 69 :pszType(NULL), pszBase(NULL), pszCode(NULL), pszData(NULL), pszDescription(NULL), 86 /** 87 * Trims a string, that is removing blank spaces at start and end. 88 * @returns Pointer to first non-blank char. 89 * @param psz Pointer to string. 90 * @result Blank at end of string is removed. ('\0' is moved to the left.) 91 */ 92 static char *ltrim(const char *psz) 93 { 94 if (psz == NULL) 95 return NULL; 96 97 while (*psz == ' ' || *psz == '\t') 98 psz++; 99 return (char *)psz; 100 } 101 102 103 104 kFileDef::kFileDef(kFile *pFile) throw(int) 105 :pszType(NULL), pszModName(NULL), pszBase(NULL), pszCode(NULL), pszData(NULL), pszDescription(NULL), 70 106 pszExeType(NULL), pszHeapSize(NULL), pszOld(NULL), pszProtmode(NULL), pszStackSize(NULL), 71 pszStub(NULL), pSegments(NULL), pImports(NULL), pExports(NULL) 107 pszStub(NULL), pSegments(NULL), pImports(NULL), pExports(NULL), 108 fProgram(FALSE), fLibrary(FALSE), fPhysicalDevice(FALSE), fVirtualDevice(FALSE), 109 fInitInstance(FALSE), fTermInstance(FALSE), fInitGlobal(FALSE), fTermGlobal(FALSE), 110 chAppType(kFileDef::unknown) 72 111 { 73 112 /* determin file size */ 74 if (!fseek(phFile, 0, SEEK_SET)) 75 { 76 this->read(phFile); 113 114 if (pFile->start()) 115 { 116 this->read(pFile); 77 117 } 78 118 else … … 126 166 /** 127 167 * Read/parse the Definition file. 128 * @param phFile Handle to file.129 * @remark 130 */ 131 void kFileDef::read( FILE *phFile) throw (int)168 * @param pFile Pointer to fileobject. 169 * @remark throws errorcode on error (TODO: errorhandling) 170 */ 171 void kFileDef::read(kFile *pFile) throw (int) 132 172 { 133 173 char *pszTmp; … … 136 176 137 177 /* readloop */ 138 psz = readln(p hFile, &szBuffer[0], sizeof(szBuffer));178 psz = readln(pFile, &szBuffer[0], sizeof(szBuffer)); 139 179 while (psz != NULL) 140 180 { … … 146 186 if (pszType != NULL) throw (0x101); 147 187 pszType = dupeString(psz); 188 fLibrary = TRUE; 189 if (!setModuleName()) 190 throw (0x107); 191 fInitInstance = stristr(pszType, "INITINSTANCE") != NULL; 192 fInitGlobal = stristr(pszType, "INITGLOBAL") != NULL || !fInitInstance; 193 fTermInstance = stristr(pszType, "TERMINSTANCE") != NULL; 194 fTermGlobal = stristr(pszType, "TERMGLOBAL") != NULL || !fTermInstance; 148 195 } 149 196 else if (StringCase(psz, "NAME")) … … 151 198 if (pszType != NULL) throw (0x101); 152 199 pszType = dupeString(psz); 200 fProgram = TRUE; 201 setModuleName(); 202 if (stristr(pszType, "WINDOWAPI")) 203 chAppType = kFileDef::pm; 204 else if (stristr(pszType, "NOTWINDOWCOMPAT")) 205 chAppType = kFileDef::fullscreen; 206 else if (stristr(pszType, "WINDOWCOMPAT")) 207 chAppType = kFileDef::pmvio; 208 else 209 chAppType = kFileDef::unknown; 153 210 } 154 211 else if (StringCase(psz, "PHYSICAL DEVICE")) //gap is fixed to one space, this may be fixed in readln. … … 156 213 if (pszType != NULL) throw (0x101); 157 214 pszType = dupeString(psz); 215 fPhysicalDevice = TRUE; 216 setModuleName(); 158 217 } 159 218 else if (StringCase(psz, "VIRTUAL DEVICE")) //gap is fixed to one space, this may be fixed in readln. … … 161 220 if (pszType != NULL) throw (0x101); 162 221 pszType = dupeString(psz); 222 fVirtualDevice = TRUE; 223 setModuleName(); 163 224 } 164 225 else if (StringCase(psz, "BASE")) 165 pszBase = dupeString(psz );226 pszBase = dupeString(psz, TRUE); 166 227 else if (StringCase(psz, "CODE")) 167 pszCode = dupeString(psz );228 pszCode = dupeString(psz, TRUE); 168 229 else if (StringCase(psz, "DATA")) 169 pszData = dupeString(psz );230 pszData = dupeString(psz, TRUE); 170 231 else if (StringCase(psz, "DESCRIPTION")) 171 pszDescription = dupeString(psz );232 pszDescription = dupeString(psz, TRUE); 172 233 else if (StringCase(psz, "EXETYPE")) 173 pszExeType = dupeString(psz );234 pszExeType = dupeString(psz, TRUE); 174 235 else if (StringCase(psz, "HEAPSIZE")) 175 pszHeapSize = dupeString(psz );236 pszHeapSize = dupeString(psz, TRUE); 176 237 else if (StringCase(psz, "OLD")) 177 pszOld = dupeString(psz );238 pszOld = dupeString(psz, TRUE); 178 239 else if (StringCase(psz, "PROTMODE")) 179 pszProtmode = dupeString(psz );240 pszProtmode = dupeString(psz, TRUE); 180 241 else if (StringCase(psz, "STACKSIZE")) 181 pszStackSize = dupeString(psz );242 pszStackSize = dupeString(psz, TRUE); 182 243 else if (StringCase(psz, "STUB")) 183 pszStub = dupeString(psz );244 pszStub = dupeString(psz, TRUE); 184 245 else if (StringCase(psz, "SEGMENTS")) 185 246 { 186 247 PDEFSEGMENT *pps = &pSegments; 187 while (!isKeyword(psz = readln(p hFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)248 while (!isKeyword(psz = readln(pFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL) 188 249 { 189 250 *pps = new DEFSEGMENT; memset(*pps, 0, sizeof(**pps)); … … 196 257 { 197 258 PDEFIMPORT *ppi = &pImports; 198 while (!isKeyword(psz = readln(p hFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)259 while (!isKeyword(psz = readln(pFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL) 199 260 { 200 261 //DOSCALL1.154 or DosQueryHeaderInfo = DOSCALL1.154 … … 229 290 { 230 291 PDEFEXPORT *ppe = &pExports; 231 while (!isKeyword(psz = readln(p hFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL)292 while (!isKeyword(psz = readln(pFile, &szBuffer[0], sizeof(szBuffer))) && psz != NULL) 232 293 { 233 294 /* CloseHandle = CloseHandle@4 @1234 RESIDENTNAME 2 */ … … 312 373 /* next ? */ 313 374 if (fNext) 314 psz = readln(p hFile, &szBuffer[0], sizeof(szBuffer));375 psz = readln(pFile, &szBuffer[0], sizeof(szBuffer)); 315 376 } 316 377 317 378 /* sanity check */ 318 379 if (pszType == NULL) 319 throw ( 0x106);380 throw ((int)0x106); 320 381 } 321 382 … … 324 385 * Reads first meaning full line from a file into a buffer. 325 386 * @returns Pointer to buffer on success; NULL on error. 326 * @param p hFile Filea handle.387 * @param pFile Pointer to fileobject to read line from. 327 388 * @param pszBuffer Pointer to buffer. 328 389 * @param cbBuffer Size of buffer. 329 390 * @remark tabs are expanded. string is trimmed. comments removed. 330 391 */ 331 char *kFileDef::readln( FILE *phFile, char *pszBuffer, int cbBuffer) throw (int)392 char *kFileDef::readln(kFile *pFile, char *pszBuffer, int cbBuffer) throw (int) 332 393 { 333 394 int i; … … 337 398 { 338 399 /* read line */ 339 if (!fgets(pszBuffer, cbBuffer, phFile)) 340 { 341 if (feof(phFile)) 342 return FALSE; 343 else 400 if (!pFile->readln(pszBuffer, cbBuffer)) 401 { 402 if (!pFile->isEOF()) 344 403 throw (0x201); 404 return FALSE; 345 405 } 346 406 … … 378 438 memmove(pszBuffer, &pszBuffer[i], cch + 1); 379 439 } 380 } while ((*pszBuffer == ';' || cch == 0) && ! feof(phFile));440 } while ((*pszBuffer == ';' || cch == 0) && !pFile->isEOF()); 381 441 382 442 return !(*pszBuffer == ';' || cch == 0) ? pszBuffer : NULL; … … 416 476 417 477 /** 418 * Queries the module name.478 * Extracts the module name from the pszType string. 419 479 * @returns Success indicator. 420 480 * @param pszBuffer Pointer to string buffer which is to hold the module name upon return. 421 481 * @remark Assumes that pszBuffer is large enough. 422 482 */ 483 BOOL kFileDef::setModuleName(void) 484 { 485 char *pszEnd; 486 char *pszStart; 487 488 assert(pszType); 489 490 /* skip the first word */ 491 pszStart = strpbrk(pszType, " \t"); 492 if (pszStart != NULL) 493 { 494 pszStart = ltrim(pszStart); 495 pszEnd = strpbrk(pszStart, " \t"); 496 if (pszEnd == NULL) 497 pszEnd = pszStart + strlen(pszEnd); 498 pszModName = new char[pszEnd - pszStart + 1]; 499 memcpy(pszModName, pszStart, pszEnd - pszStart); 500 pszModName[pszEnd - pszStart] = '\0'; 501 } 502 else 503 return !StringCase(pszType, "LIBRARY"); 504 return TRUE; 505 } 506 507 508 /** 509 * Query for the module name. 510 * @returns Success indicator. TRUE / FALSE. 511 * @param pszBuffer Pointer to buffer which to put the name into. 512 */ 423 513 BOOL kFileDef::queryModuleName(char *pszBuffer) 424 514 { 425 char *psz; 426 427 if (pszType != NULL && StringCase(pszType, "LIBRARY")) 428 { 429 psz = pszType + sizeof("LIBRARY") - 1; 430 while (*psz == ' ') 431 psz++; 432 while (*psz != '\0' && *psz != ' ') 433 *pszBuffer++ = *psz++; 434 *pszBuffer = '\0'; 435 } 436 else 515 if (pszModName == NULL) 437 516 return FALSE; 517 518 strcpy(pszBuffer, pszModName); 519 438 520 return TRUE; 439 521 } … … 497 579 498 580 /** 581 * Make a Watcom Linker parameter file addtition of this definition file. 582 * @returns Success indicator. 583 * @param pFile File which we're to write to (append). 584 * Appends at current posistion. 585 * @sketch 586 * @status 587 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no) 588 * @remark 589 */ 590 BOOL kFileDef::makeWatcomLinkFileAddtion(kFile *pFile) throw(int) 591 { 592 PDEFSEGMENT pSeg; 593 PDEFIMPORT pImp; 594 PDEFEXPORT pExp; 595 pFile->setThrowOnErrors(); 596 597 /* 598 * Write a little remark first to tell that converted stuff starts here. 599 */ 600 pFile->printf("#\n# Directives generated from .DEF-file.\n#\n"); 601 602 /* Format - Module type */ 603 pFile->printf("FORMAT OS2 LX %s %s %s\n", 604 fLibrary ? "DLL" : 605 (fProgram ? (chAppType == pm ? "PM" 606 : (chAppType == fullscreen ? "FULLSCREEN" 607 : "PMCOMPATIBLE")) 608 : (fVirtualDevice ? "VIRTDEVICE" 609 : "PHYSDEVICE" )), 610 fLibrary ? (fInitGlobal ? "INITGLOBAL" : "INITINSTANCE") : "", 611 fLibrary ? (fTermGlobal ? "TERMGLOBAL" : "TERMINSTANCE") : ""); 612 613 614 /* Module name */ 615 if (pszModName) 616 pFile->printf("OPTION MODNAME=%s\n", pszModName); 617 618 /* Description */ 619 if (pszDescription) 620 pFile->printf("OPTION DESCRIPTION '%s'\n", pszDescription); 621 622 /* Base */ 623 if (pszBase) 624 pFile->printf("OPTION OFFSET=%s\n", pszBase); 625 626 /* Stub */ 627 if (pszStub) 628 pFile->printf("OPTION STUB='%s'\n", pszStub); 629 630 /* Old */ 631 if (pszOld) 632 pFile->printf("OPTION OLDLIBRARY=%s\n", pszOld); 633 634 /* Protected mode */ 635 if (pszProtmode) 636 pFile->printf("OPTION PROTMODE\n", pszProtmode); 637 638 /* Stacksize */ 639 if (pszStackSize) 640 pFile->printf("OPTION STACK=%s\n", pszStackSize); 641 642 /* HeapSize */ 643 if (pszHeapSize) 644 pFile->printf("OPTION HEAPSIZE=%s\n", pszHeapSize); 645 646 /* Code - not supported */ 647 648 /* Data - not supported */ 649 650 /* 651 * Segments. 652 */ 653 pSeg = pSegments; 654 while (pSeg != NULL) 655 { 656 pFile->printf("SEGMENT %s\n", pSeg->psz); 657 pSeg = pSeg->pNext; 658 } 659 660 /* 661 * Imports. 662 */ 663 pImp = pImports; 664 while (pImp != NULL) 665 { 666 if (pImp->pszName == NULL) 667 pFile->printf("IMPORT '%s' '%s'.%d\n", pImp->pszIntName, pImp->pszDll, pImp->ulOrdinal); 668 else 669 pFile->printf("IMPORT '%s' '%s'.'%s'\n", pImp->pszIntName, pImp->pszDll, pImp->pszName); 670 pImp = pImp->pNext; 671 } 672 673 /* 674 * Exports. 675 */ 676 pExp = pExports; 677 while (pExp != NULL) 678 { 679 pFile->printf("EXPORT '%s'", pExp->pszName); 680 if (pExp->ulOrdinal != ~0UL) 681 pFile->printf(".%d", pExp->ulOrdinal); 682 if (pExp->pszIntName) 683 pFile->printf("='%s'", pExp->pszIntName); 684 if (pExp->fResident) 685 pFile->printf(" RESIDENT"); 686 if (pExp->cParam != ~0UL) 687 pFile->printf(" %d", pExp->cParam * 2); /* .DEFs this is number of words. Watcom should have bytes. */ 688 pFile->printf("\n"); 689 pExp = pExp->pNext; 690 } 691 692 return TRUE; 693 } 694 695 696 697 698 /** 499 699 * Removes '"' and ''' at start and end of the string. 500 700 * @returns pszStr! … … 514 714 return pszStr; 515 715 } 716 717 718 /** 719 * Upcases a char. 720 * @returns Upper case of the char given in ch. 721 * @param ch Char to capitalize. 722 */ 723 inline char upcase(char ch) 724 { 725 return ch >= 'a' && ch <= 'z' ? (char)(ch - ('a' - 'A')) : ch; 726 } 727 728 729 /** 730 * Searches for a substring in a string. 731 * @returns Pointer to start of substring when found, NULL when not found. 732 * @param pszStr String to be searched. 733 * @param pszSubStr String to be searched. 734 * @remark Depends on the upcase function. 735 */ 736 static char *stristr(const char *pszStr, const char *pszSubStr) 737 { 738 int cchSubStr = strlen(pszSubStr); 739 int i = 0; 740 741 while (*pszStr != '\0' && i < cchSubStr) 742 { 743 i = 0; 744 while (i < cchSubStr && pszStr[i] != '\0' && 745 (upcase(pszStr[i]) == upcase(pszSubStr[i]))) 746 i++; 747 pszStr++; 748 } 749 750 return (char*)(*pszStr != '\0' ? pszStr - 1 : NULL); 751 } 752
Note:
See TracChangeset
for help on using the changeset viewer.