home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UFile.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  25.8 KB  |  1,046 lines  |  [TEXT/MPS ]

  1. // UFile.cp
  2. // Copyright © 1988-1991 by Apple Computer Inc.  All rights reserved.
  3.  
  4. #ifndef __STDIO__
  5. #include <StdIo.h>
  6. #endif
  7.  
  8. #ifndef __UFILE__
  9. #include <UFile.h>
  10. #endif
  11.  
  12. #ifndef __RESOURCES__
  13. #include <Resources.h>
  14. #endif
  15.  
  16. #ifndef __ERRORS__
  17. #include <Errors.h>
  18. #endif
  19.  
  20. #ifndef __SCRIPT__
  21. #include <Script.h>
  22. #endif
  23.  
  24. #ifndef __UMACAPPUTILITIES__
  25. #include <UMacAppUtilities.h>
  26. #endif
  27.  
  28. //--------------------------------------------------------------------------------------------------
  29. #pragma segment MAFileOpen
  30.  
  31. pascal TFile* NewFile(void)
  32. {
  33.     TFile * theFile;
  34.  
  35.     theFile = new TFile;
  36.     theFile->IFile();
  37.     return theFile;
  38. }
  39.  
  40. //--------------------------------------------------------------------------------------------------
  41. #pragma segment MAFileOpen
  42.  
  43. pascal void TFile::Initialize(void)                // override 
  44. {
  45.     inherited::Initialize();
  46.  
  47.     fFileSpec.vRefNum = 0;
  48.     fFileSpec.parID = 0;
  49.     fFileSpec.name = "";
  50.  
  51.     fModDate = 0;
  52.     fStationery = notStationery;
  53.     fFileType = 'TEXT';
  54.     fCreator = '????';
  55.     fScriptTag = smSystemScript;
  56.     fUsesDataFork = FALSE;
  57.     fUsesRsrcFork = FALSE;
  58.     fDataPermission = fsRdPerm;
  59.     fRsrcPermission = fsRdPerm;
  60.     fKeepDataOpen = FALSE;
  61.     fKeepRsrcOpen = FALSE;
  62.     fDataRefNum = kNoFileRefnum;
  63.     fRsrcRefNum = kNoFileRefnum;
  64. }
  65.  
  66. //--------------------------------------------------------------------------------------------------
  67. #pragma segment MAFileOpen
  68.  
  69. pascal void TFile::IFile(void)
  70. {
  71.     this->IObject();
  72. }
  73.  
  74. //--------------------------------------------------------------------------------------------------
  75. #pragma segment MAFileClose
  76.  
  77. pascal void TFile::Free(void)                    // override 
  78. {
  79.     this->CloseFile(TRUE);
  80.  
  81.     inherited::Free();
  82. }
  83.  
  84. //--------------------------------------------------------------------------------------------------
  85. #pragma segment MAFileClose
  86.  
  87. pascal OSErr TFile::CloseFile(Boolean forceClose)
  88. {
  89.     OSErr dataErr = noErr;
  90.     OSErr rsrcErr = noErr;
  91.  
  92.     if (fUsesDataFork)
  93.         dataErr = this->CloseDataFork(forceClose);
  94.     if (fUsesRsrcFork)
  95.         rsrcErr = this->CloseRsrcFork(forceClose);
  96.  
  97.     if (dataErr != noErr)
  98.         return dataErr;
  99.     else
  100.         return rsrcErr;
  101. }
  102.  
  103. //--------------------------------------------------------------------------------------------------
  104. #pragma segment MAFileClose
  105.  
  106. pascal OSErr TFile::CloseDataFork(Boolean forceClose)
  107. {
  108.     OSErr theErr = noErr;
  109.  
  110.     if ((fDataRefNum != kNoFileRefnum) && (!fKeepDataOpen || forceClose))
  111.     {
  112.         theErr = FSClose(fDataRefNum);
  113.         fDataRefNum = kNoFileRefnum;
  114.     }
  115.     return theErr;
  116. }
  117.  
  118. //--------------------------------------------------------------------------------------------------
  119. #pragma segment MAFileClose
  120.  
  121. pascal OSErr TFile::CloseRsrcFork(Boolean forceClose)
  122. {
  123.     OSErr theErr = noErr;
  124.  
  125.     if ((fRsrcRefNum != kNoFileRefnum) && (!fKeepRsrcOpen || forceClose))
  126.     {
  127.         CloseResFile(fRsrcRefNum);
  128.         theErr = ResError();
  129.         fRsrcRefNum = kNoFileRefnum;
  130.     }
  131.     return theErr;
  132. }
  133.  
  134. //--------------------------------------------------------------------------------------------------
  135. #pragma segment MAFileOpen
  136.  
  137. pascal OSErr TFile::CreateFile(void)
  138. {
  139.     OSErr dataErr = noErr;
  140.     OSErr rsrcErr = noErr;
  141.  
  142.     if (fUsesDataFork)
  143.         dataErr = this->CreateDataFork();
  144.  
  145.     if (fUsesRsrcFork)
  146.         rsrcErr = this->CreateRsrcFork();
  147.  
  148.     if (dataErr != noErr)
  149.         return dataErr;
  150.     else
  151.         return rsrcErr;
  152. }
  153.  
  154. //--------------------------------------------------------------------------------------------------
  155. #pragma segment MAFileOpen
  156.  
  157. pascal OSErr TFile::CreateDataFork(void)
  158. {
  159.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  160.     {
  161.         FSSpec theFile = fFileSpec;
  162.  
  163.         return FSpCreate(theFile, fCreator, fFileType, fScriptTag);
  164.     }
  165.     else
  166.     {
  167.         Str63 fileName = fFileSpec.name;
  168.         
  169.         return HCreate(fFileSpec.vRefNum, fFileSpec.parID, fileName, fCreator, fFileType);
  170.     }
  171. }
  172.  
  173. //--------------------------------------------------------------------------------------------------
  174. #pragma segment MAFileOpen
  175.  
  176. pascal OSErr TFile::CreateRsrcFork(void)
  177. {
  178.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  179.     {
  180.         FSSpec theFile = fFileSpec;
  181.     
  182.         FSpCreateResFile(theFile, fCreator, fFileType, fScriptTag);
  183.     }
  184.     else
  185.     {
  186.         Str63 fileName = fFileSpec.name;
  187.         
  188.         HCreateResFile(fFileSpec.vRefNum, fFileSpec.parID, fileName);
  189.     }
  190.     return ResError();
  191. }
  192.  
  193. //--------------------------------------------------------------------------------------------------
  194. #pragma segment MAFileClose
  195.  
  196. pascal OSErr TFile::DeleteFile(void)
  197. {
  198.     fModDate = 0;
  199.     
  200.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  201.     {
  202.         FSSpec theFileSpec = fFileSpec;
  203.  
  204.         return FSpDelete(theFileSpec);
  205.     }
  206.     else
  207.     {
  208.         Str63 fileName = fFileSpec.name;
  209.         
  210.         return HDelete(fFileSpec.vRefNum, fFileSpec.parID, fileName);
  211.     }
  212. }
  213.  
  214. //--------------------------------------------------------------------------------------------------
  215. #pragma segment MAFileOpen
  216.  
  217. pascal OSErr TFile::OpenFile(void)
  218. {
  219.     OSErr dataErr = noErr;
  220.     OSErr rsrcErr = noErr;
  221.  
  222.     if (fUsesDataFork)
  223.         dataErr = this->OpenDataFork(fDataPermission);
  224.  
  225.     if (fUsesRsrcFork)
  226.         rsrcErr = this->OpenRsrcFork(fRsrcPermission);
  227.  
  228.     if (dataErr != noErr)
  229.         return dataErr;
  230.     else
  231.         return rsrcErr;
  232. }
  233.  
  234. //--------------------------------------------------------------------------------------------------
  235. #pragma segment MAFileOpen
  236.  
  237. pascal OSErr TFile::OpenDataFork(SignedByte permission)
  238. {
  239.     OSErr theErr = noErr;
  240.  
  241.     if (fDataRefNum == kNoFileRefnum)            // Don't open if already open 
  242.     {
  243.         HParamBlockRec pb;
  244.         Str63 fileName = fFileSpec.name;
  245.  
  246.         BlockSet((Ptr) & pb, sizeof(HParamBlockRec), 0x00);
  247.         pb.accessParam.ioNamePtr = &fileName;
  248.         pb.accessParam.ioVRefNum = fFileSpec.vRefNum;
  249.         pb.fileParam.ioDirID = fFileSpec.parID;
  250.         
  251.         switch (permission)
  252.         {
  253.             // read/deny write
  254.             case fsRdPerm:
  255.                 pb.accessParam.ioDenyModes = fsRdPerm | 32;
  256.                 break;
  257.  
  258.             // read/write/deny read/deny write            
  259.             case fsWrPerm:
  260.             case fsRdWrPerm:
  261.             case fsCurPerm:
  262.                 pb.accessParam.ioDenyModes = fsRdWrPerm | 16 | 32;
  263.                 break;
  264.             
  265.             // read/write/deny none
  266.             case fsRdWrShPerm:
  267.                 pb.accessParam.ioDenyModes = fsRdWrPerm;
  268.                 break;
  269.     
  270.             // read/deny write            
  271.             default:
  272.                 pb.accessParam.ioDenyModes = fsRdPerm | 32;
  273.         }
  274.                 
  275.         theErr = PBHOpenDenySync(&pb);            // Try the shared volume open. 
  276.  
  277.         if (theErr == paramErr)                    // Not on a shared volume, try HFS open. 
  278.         {
  279.             pb.ioParam.ioPermssn = permission & 3;
  280.             theErr = PBHOpenSync(&pb);
  281.         }
  282.  
  283.         if (theErr == noErr)
  284.             fDataRefNum = pb.ioParam.ioRefNum;
  285.     }
  286.  
  287.     return theErr;
  288. }
  289.  
  290. //--------------------------------------------------------------------------------------------------
  291. #pragma segment MAFileOpen
  292.  
  293. pascal OSErr TFile::OpenRsrcFork(SignedByte permission)
  294. {
  295.     OSErr theErr = noErr;
  296.  
  297.     if (fRsrcRefNum == kNoFileRefnum)            // Don't open if already open 
  298.     {
  299.         if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  300.         {
  301.             FSSpec theFileSpec = fFileSpec;
  302.             
  303.             fRsrcRefNum = FSpOpenResFile(theFileSpec, (permission & 7));
  304.             theErr = ResError();
  305.         }
  306.         else
  307.         {
  308.             Str63 fileName = fFileSpec.name;
  309.         
  310.             fRsrcRefNum = HOpenResFile(fFileSpec.vRefNum, fFileSpec.parID, fileName, (permission & 7));
  311.             theErr = ResError();
  312.         }
  313.     }
  314.     if (theErr != noErr)
  315.         fRsrcRefNum = kNoFileRefnum;
  316.     return theErr;
  317. }
  318.  
  319. //--------------------------------------------------------------------------------------------------
  320. #pragma segment MAFileOpen
  321.  
  322. pascal void TFile::DefineFile(const OSType itsFileType,
  323.                               const OSType itsCreator,
  324.                               Boolean usesDataFork,
  325.                               Boolean usesRsrcFork,
  326.                               Boolean keepsDataOpen,
  327.                               Boolean keepsRsrcOpen)
  328. {
  329. #if qDebugMsg
  330.     if (keepsDataOpen &&!usesDataFork)
  331.     {
  332.         fprintf(stderr, "In TFile.DefineFile:  keepsDataOpen && !usesDataFork;");
  333.         fprintf(stderr, "In TFile.DefineFile:  usesDataFork = TRUE;");
  334.         usesDataFork = TRUE;
  335.     }
  336.     if (keepsRsrcOpen && (!usesRsrcFork))
  337.     {
  338.         fprintf(stderr, "In TFile.DefineFile:  keepsRsrcOpen && !usesRsrcFork;");
  339.         fprintf(stderr, "In TFile.DefineFile:  usesRsrcFork = TRUE;");
  340.         usesRsrcFork = TRUE;
  341.     }
  342. #endif
  343.  
  344.     fFileType = itsFileType;
  345.     fCreator = itsCreator;
  346.     fUsesDataFork = usesDataFork;
  347.     fUsesRsrcFork = usesRsrcFork;
  348.     fKeepDataOpen = keepsDataOpen;
  349.     fKeepRsrcOpen = keepsRsrcOpen;
  350. }
  351.  
  352. //--------------------------------------------------------------------------------------------------
  353. #pragma segment MAFileRes
  354.  
  355. pascal OSErr TFile::ExchangeFiles(TFile* aFile)
  356. {
  357.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  358.     {
  359.         FSSpec source = fFileSpec;
  360.         FSSpec destination = aFile->fFileSpec;
  361.         
  362.         return FSpExchangeFiles(source, destination);
  363.     }
  364.     else
  365.         return noErr;
  366. }
  367.  
  368. //--------------------------------------------------------------------------------------------------
  369. #pragma segment MAFileRes
  370.  
  371. pascal OSErr TFile::GetAlias(AliasHandle& alias)
  372. {
  373.     FSSpec theFileSpec;
  374.  
  375.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  376.     {
  377.         theFileSpec = fFileSpec;
  378.         return NewAliasMinimal(theFileSpec, alias);
  379.     }
  380.     else
  381.         return noErr;
  382. }
  383.  
  384. //--------------------------------------------------------------------------------------------------
  385. #pragma segment MAFileRes
  386.  
  387. pascal OSErr TFile::GetCatInfo(CInfoPBRec& cInfo)
  388. {
  389.     Str63 itsName;
  390.  
  391.     itsName = fFileSpec.name;
  392.     cInfo.hFileInfo.ioNamePtr = &itsName;
  393.     cInfo.hFileInfo.ioVRefNum = fFileSpec.vRefNum;
  394.     cInfo.hFileInfo.ioFDirIndex = 0;
  395.     cInfo.hFileInfo.ioDirID = fFileSpec.parID;
  396.     return PBGetCatInfoSync(&cInfo);
  397. }
  398.  
  399. //--------------------------------------------------------------------------------------------------
  400. #pragma segment MAFileRes
  401.  
  402. pascal long TFile::GetCreationDate(void)
  403. {
  404.     HParamBlockRec pb;
  405.  
  406.     if (this->GetFileInfo(pb) == noErr)
  407.         return pb.fileParam.ioFlCrDat;
  408.     else
  409.         return 0;
  410. }
  411.  
  412. //--------------------------------------------------------------------------------------------------
  413. #pragma segment MAFileRes
  414.  
  415. pascal OSErr TFile::GetDataLength(long& length)
  416. {
  417.     if (fDataRefNum != kNoFileRefnum)
  418.         return GetEOF(fDataRefNum, length);
  419.     else
  420.         return noErr;
  421. }
  422.  
  423. //--------------------------------------------------------------------------------------------------
  424. #pragma segment MAFileRes
  425.  
  426. pascal OSErr TFile::GetDataMark(long& mark)
  427. {
  428.     if (fDataRefNum != kNoFileRefnum)
  429.         return GetFPos(fDataRefNum, mark);
  430.     else
  431.         return noErr;
  432. }
  433.  
  434. //--------------------------------------------------------------------------------------------------
  435. #pragma segment MAFileRes
  436.  
  437. pascal long TFile::GetDirID(void)
  438. {
  439.     return fFileSpec.parID;
  440. }
  441.  
  442. //--------------------------------------------------------------------------------------------------
  443. #pragma segment MAFileRes
  444.  
  445. pascal OSErr TFile::GetFileCreator(OSType& creator)
  446. {
  447.     FInfo fndrInfo;
  448.     OSErr theErr;
  449.  
  450.     if ((theErr = this->GetFinderInfo(fndrInfo)) == noErr)
  451.         creator = fndrInfo.fdCreator;
  452.     return theErr;
  453. }
  454.  
  455. //--------------------------------------------------------------------------------------------------
  456. #pragma segment MAFileRes
  457.  
  458. pascal OSErr TFile::GetFileInfo(HParamBlockRec& pb)
  459. {
  460.     Str63 itsName;
  461.  
  462.     itsName = fFileSpec.name;
  463.     pb.fileParam.ioNamePtr = &itsName;
  464.     pb.fileParam.ioVRefNum = fFileSpec.vRefNum;
  465.     pb.fileParam.ioDirID = fFileSpec.parID;
  466.     pb.fileParam.ioFVersNum = 0;
  467.     pb.fileParam.ioFDirIndex = 0;
  468.     return PBHGetFInfoSync(&pb);
  469. }
  470.  
  471. //--------------------------------------------------------------------------------------------------
  472. #pragma segment MAFileRes
  473.  
  474. pascal OSErr TFile::GetFileType(OSType& fileType)
  475. {
  476.     FInfo fndrInfo;
  477.     OSErr theErr;
  478.  
  479.     if ((theErr = this->GetFinderInfo(fndrInfo)) == noErr)
  480.         fileType = fndrInfo.fdType;
  481.     return theErr;
  482. }
  483.  
  484. //--------------------------------------------------------------------------------------------------
  485. #pragma segment MAFileRes
  486.  
  487. pascal void TFile::GetFileSpec(FSSpec& theFileSpec)
  488. {
  489.     theFileSpec = fFileSpec;
  490. }
  491.  
  492. //--------------------------------------------------------------------------------------------------
  493. #pragma segment MAFileRes
  494.  
  495. pascal OSErr TFile::GetFinderInfo(FInfo& fndrInfo)
  496. {
  497.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  498.     {
  499.         FSSpec theFileSpec = fFileSpec;
  500.     
  501.         return FSpGetFInfo(theFileSpec, fndrInfo);
  502.     }
  503.     else
  504.     {
  505.         Str63 fileName = fFileSpec.name;
  506.     
  507.         return HGetFInfo(fFileSpec.vRefNum, fFileSpec.parID, fileName, fndrInfo);
  508.     }
  509. }
  510.  
  511. //--------------------------------------------------------------------------------------------------
  512. #pragma segment MAFileRes
  513.  
  514. pascal long TFile::GetModificationDate(void)
  515. {
  516.     HParamBlockRec pb;
  517.  
  518.     if (this->GetFileInfo(pb) == noErr)
  519.         return pb.fileParam.ioFlMdDat;
  520.     else
  521.         return 0;
  522. }
  523.  
  524. //--------------------------------------------------------------------------------------------------
  525. #pragma segment MAFileRes
  526.  
  527. pascal void TFile::GetName(Str63& name)
  528. {
  529.     name = fFileSpec.name;
  530. }
  531.  
  532. //--------------------------------------------------------------------------------------------------
  533. #pragma segment MAFileRes
  534.  
  535. pascal OSErr TFile::GetPhysicalSize(long& dataSize,
  536.                                     long& rsrcSize)
  537. {
  538.     HParamBlockRec pb;
  539.     OSErr theErr;
  540.  
  541.     if ((theErr = this->GetFileInfo(pb)) == noErr)
  542.     {
  543.         dataSize = pb.fileParam.ioFlPyLen;
  544.         rsrcSize = pb.fileParam.ioFlRPyLen;
  545.     }
  546.     return theErr;
  547. }
  548.  
  549. //--------------------------------------------------------------------------------------------------
  550. #pragma segment MAFileRes
  551.  
  552. pascal OSErr TFile::GetScript(ScriptCode& theScript)
  553. {
  554.     OSErr theErr;
  555.     CInfoPBRec cInfo;
  556.  
  557.     BlockSet((Ptr) & cInfo, sizeof(CInfoPBRec), 0x00);// zero-out our storage 
  558.     theErr = this->GetCatInfo(cInfo);
  559.     if (theErr == noErr)
  560.     {
  561.         theScript = cInfo.hFileInfo.ioFlXFndrInfo.fdScript;
  562.         fScriptTag = theScript;
  563.     }
  564.     return theErr;
  565. }
  566.  
  567. //--------------------------------------------------------------------------------------------------
  568. #pragma segment MAFileRes
  569.  
  570. pascal OSErr TFile::FlushVolume(void)
  571. {
  572.     return FlushVol(NULL, fFileSpec.vRefNum);
  573. }
  574.  
  575. //--------------------------------------------------------------------------------------------------
  576. #pragma segment MAFileRes
  577.  
  578. pascal OSErr TFile::GetBlockSize(long& blockSize)
  579. {
  580.     HParamBlockRec pb;
  581.     OSErr theErr;
  582.  
  583.     BlockSet((Ptr) & pb, sizeof(HParamBlockRec), 0x00);// zero-out our storage 
  584.     theErr = this->GetVolumeInfo(pb);
  585.     if (theErr == noErr)
  586.         blockSize = pb.volumeParam.ioVAlBlkSiz;
  587.     else
  588.         blockSize = 0;
  589.     return theErr;
  590. }
  591.  
  592. //--------------------------------------------------------------------------------------------------
  593. #pragma segment MAFileRes
  594.  
  595. pascal OSErr TFile::GetFreeBlocks(long& freeBlocks)
  596. {
  597.     HParamBlockRec pb;
  598.     OSErr theErr;
  599.  
  600.  
  601.     BlockSet((Ptr) & pb, sizeof(HParamBlockRec), 0x00);// zero-out our storage 
  602.     theErr = this->GetVolumeInfo(pb);
  603.     if (theErr == noErr)
  604.         freeBlocks = pb.volumeParam.ioVFrBlk;
  605.     else
  606.         freeBlocks = 0;
  607.     return theErr;
  608. }
  609.  
  610. //--------------------------------------------------------------------------------------------------
  611. #pragma segment MAFileRes
  612.  
  613. pascal OSErr TFile::GetVolumeInfo(HParamBlockRec& pb)
  614. {
  615.     pb.volumeParam.ioCompletion = NULL;
  616.     pb.volumeParam.ioNamePtr = NULL;
  617.     pb.volumeParam.ioVRefNum = fFileSpec.vRefNum;
  618.     pb.volumeParam.ioVolIndex = -1;                // Use the vRefNum since it is all we have 
  619.     return PBHGetVInfoSync(&pb);
  620. }
  621.  
  622. //--------------------------------------------------------------------------------------------------
  623. #pragma segment MAFileRes
  624.  
  625. pascal OSErr TFile::GetVolumeName(Str63& name)
  626. {
  627.     HParamBlockRec pb;
  628.  
  629.     BlockSet((Ptr) & pb, sizeof(HParamBlockRec), 0x00);
  630.     pb.volumeParam.ioNamePtr = &name;            // Fill in the name 
  631.     pb.volumeParam.ioVRefNum = fFileSpec.vRefNum;
  632.     pb.volumeParam.ioVolIndex = 0;                // Use the vRefNum since it is all we have 
  633.     return PBHGetVInfoSync(&pb);
  634. }
  635.  
  636. //--------------------------------------------------------------------------------------------------
  637. #pragma segment MAFileRes
  638.  
  639. pascal short TFile::GetVolRefNum(void)
  640. {
  641.     return fFileSpec.vRefNum;
  642. }
  643.  
  644. //--------------------------------------------------------------------------------------------------
  645. #pragma segment MAFileRes
  646.  
  647. pascal void TFile::Identify(const FSSpec& theFile)
  648. {
  649.     OSErr theErr;
  650.  
  651.     // If we are re-identifying an open file, close the file 
  652.     theErr = this->CloseFile(TRUE);
  653.     fFileSpec = theFile;
  654. }
  655.  
  656. //--------------------------------------------------------------------------------------------------
  657. #pragma segment MAFileRes
  658.  
  659. pascal void TFile::IdentifyWithScript(const FSSpec& theFile,
  660.                                       ScriptCode theScript)
  661. {
  662.     fScriptTag = theScript;
  663.     this->Identify(theFile);
  664. }
  665.  
  666. //--------------------------------------------------------------------------------------------------
  667. #pragma segment MAFileRes
  668.  
  669. pascal OSErr TFile::IdentifyByAlias(AliasHandle alias)
  670. {
  671.     OSErr theErr = noErr;
  672.  
  673.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  674.     {
  675.         FSSpec theFileSpec;
  676.         Boolean dontCare;
  677.  
  678.         theErr = ResolveAlias((FSSpecPtr)NULL, alias, theFileSpec, dontCare);
  679.         if (theErr == noErr)
  680.         {
  681.             FInfo finderInfo;
  682.             
  683.             this->Identify(theFileSpec);
  684.             theErr = this->GetFinderInfo(finderInfo);
  685.             if (theErr == noErr)
  686.             {
  687.                 fFileType = finderInfo.fdType;
  688.                 fCreator = finderInfo.fdCreator;
  689.             }
  690.         }
  691.     }
  692.     return theErr;
  693. }
  694.  
  695. //--------------------------------------------------------------------------------------------------
  696. #pragma segment MAFileRes
  697.  
  698. pascal void TFile::IdentifyByFile(TFile* aFile)
  699. {
  700.     FSSpec theFile = aFile->fFileSpec;
  701.  
  702.     this->IdentifyWithScript(theFile, aFile->fScriptTag);
  703. }
  704.  
  705. //--------------------------------------------------------------------------------------------------
  706. #pragma segment MAFileRes
  707.  
  708. pascal OSErr TFile::IdentifyByTrio(short volRefNum,
  709.                                    long dirID,
  710.                                    const Str63& name)
  711. {
  712.     FSSpec theFileSpec;
  713.     OSErr theErr = noErr;
  714.  
  715.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  716.     {
  717.         if ((theErr = FSMakeFSSpec(volRefNum, dirID, name, &theFileSpec)) == fnfErr)
  718.             theErr = noErr;                        // The file may not yet exist
  719.     }
  720.     else
  721.     {
  722.         if (dirID == 0)                            // Assume that vRefNum is a WDir 
  723.         {
  724.             WDPBRec pb;
  725.             
  726.             BlockSet((Ptr) & pb, sizeof(pb), 0x00);
  727.             pb.ioVRefNum = volRefNum;
  728.             pb.ioWDVRefNum = volRefNum;
  729.             theErr = PBGetWDInfoSync(&pb);
  730.             theFileSpec.vRefNum = pb.ioWDVRefNum;
  731.             theFileSpec.parID = pb.ioWDDirID;
  732.         }
  733.         else
  734.         {
  735.             theFileSpec.vRefNum = volRefNum;
  736.             theFileSpec.parID = dirID;
  737.         }
  738.         theFileSpec.name = name;
  739.     }
  740.     if (theErr == noErr)
  741.         this->Identify(theFileSpec);
  742.     return theErr;
  743. }
  744.  
  745. //--------------------------------------------------------------------------------------------------
  746. #pragma segment MAFileRes
  747.  
  748. pascal Boolean TFile::IsDataForkOpen(void)
  749. {
  750.     return (fDataRefNum != kNoFileRefnum);
  751. }
  752.  
  753. //--------------------------------------------------------------------------------------------------
  754. #pragma segment MAFileRes
  755.  
  756. pascal Boolean TFile::IsRsrcForkOpen(void)
  757. {
  758.     return (fRsrcRefNum != kNoFileRefnum);
  759. }
  760.  
  761. //--------------------------------------------------------------------------------------------------
  762. #pragma segment MAFileRes
  763.  
  764. pascal Boolean TFile::IsSameFile(TFile* aFile)
  765. {
  766.     Str63 name;
  767.  
  768.     if ((fFileSpec.vRefNum == aFile->GetVolRefNum()) && (fFileSpec.parID == aFile->GetDirID()))
  769.     {
  770.         aFile->GetName(name);
  771.         if (EqualString(fFileSpec.name, name, FALSE, TRUE))
  772.             return TRUE;
  773.     }
  774.     return FALSE;
  775. }
  776.  
  777. //--------------------------------------------------------------------------------------------------
  778. #pragma segment MAFileRes
  779.  
  780. pascal Boolean TFile::IsModified(void)
  781. {
  782.     long theDate;
  783.  
  784.     theDate = this->GetModificationDate();
  785.     if (theDate != fModDate)
  786.         return TRUE;
  787.     else
  788.         return FALSE;
  789. }
  790.  
  791. //--------------------------------------------------------------------------------------------------
  792. #pragma segment MAFileRes
  793.  
  794. pascal OSErr TFile::IsVolumeLocked(Boolean& locked)
  795. {
  796.     HParamBlockRec pb;
  797.     OSErr theErr;
  798.  
  799.     if ((theErr = this->GetVolumeInfo(pb)) == noErr)
  800.         locked = ((pb.volumeParam.ioVAtrb & 0x8080) != 0);
  801.     return theErr;
  802. }
  803.  
  804. //--------------------------------------------------------------------------------------------------
  805. #pragma segment MAFileRes
  806.  
  807. pascal Boolean TFile::HasDataFork(void)
  808. {
  809.     return fUsesDataFork;
  810. }
  811.  
  812. //--------------------------------------------------------------------------------------------------
  813. #pragma segment MAFileRes
  814.  
  815. pascal Boolean TFile::HasRsrcFork(void)
  816. {
  817.     return fUsesRsrcFork;
  818. }
  819.  
  820. //--------------------------------------------------------------------------------------------------
  821. #pragma segment MAFileRes
  822.  
  823. pascal OSErr TFile::MakeStationery(void)
  824. {
  825.     return noErr;
  826. }
  827.  
  828. //--------------------------------------------------------------------------------------------------
  829. #pragma segment MAFileRes
  830.  
  831. pascal void TFile::Modified(void)
  832. {
  833.     fModDate = this->GetModificationDate();
  834. }
  835.  
  836. //--------------------------------------------------------------------------------------------------
  837. #pragma segment MAFileRes
  838.  
  839. pascal OSErr TFile::MoveAndRename(const FSSpec& dest)
  840. {
  841.     CInfoPBRec cInfoPB;
  842.     CMovePBRec cMovePB;
  843.     Str63 sourceName;
  844.     Str63 destName;
  845.     long parentDirID;
  846.     OSErr theErr;
  847.  
  848.     cInfoPB.dirInfo.ioVRefNum = dest.vRefNum;
  849.     cInfoPB.dirInfo.ioDrDirID = dest.parID;
  850.     cInfoPB.dirInfo.ioNamePtr = &destName;        // Will be filled in 
  851.     cInfoPB.dirInfo.ioFDirIndex = -1;
  852.     if ((theErr = PBGetCatInfoSync(&cInfoPB)) != noErr)
  853.         return theErr;
  854.     parentDirID = cInfoPB.dirInfo.ioDrParID;
  855.  
  856.     sourceName = fFileSpec.name;
  857.     cMovePB.ioVRefNum = fFileSpec.vRefNum;
  858.     cMovePB.ioDirID = fFileSpec.parID;
  859.     cMovePB.ioNamePtr = &sourceName;
  860.     cMovePB.ioNewDirID = parentDirID;
  861.     cMovePB.ioNewName = &destName;
  862.     if ((theErr = PBCatMoveSync(&cMovePB)) != noErr)
  863.         return theErr;
  864.     return HRename(dest.vRefNum, dest.parID, sourceName, dest.name);
  865. }
  866.  
  867. //--------------------------------------------------------------------------------------------------
  868. #pragma segment MAFileRes
  869.  
  870. pascal OSErr TFile::ReadData(void* buffer,
  871.                              long& count)
  872. {
  873.     if (fDataRefNum != kNoFileRefnum)
  874.         return FSRead(fDataRefNum, count, (Ptr)buffer);
  875.     else
  876.         return noErr;
  877. }
  878.  
  879. //--------------------------------------------------------------------------------------------------
  880. #pragma segment MAFileRes
  881.  
  882. pascal OSErr TFile::RenameFile(Str63& newName)
  883. {
  884.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  885.     {
  886.         FSSpec theFileSpec = fFileSpec;
  887.     
  888.         return FSpRename(theFileSpec, newName);
  889.     }
  890.     else
  891.     {
  892.         Str63 sourceName = fFileSpec.name;
  893.         
  894.         return HRename(fFileSpec.vRefNum, fFileSpec.parID, sourceName, newName);
  895.     }
  896. }
  897.  
  898. //--------------------------------------------------------------------------------------------------
  899. #pragma segment MAFileRes
  900.  
  901. pascal OSErr TFile::SetCatInfo(CInfoPBRec& cInfo)
  902. {
  903.     Str63 fileName = fFileSpec.name;
  904.  
  905.     cInfo.hFileInfo.ioCompletion = NULL;
  906.     cInfo.hFileInfo.ioNamePtr = &fileName;
  907.     cInfo.hFileInfo.ioVRefNum = fFileSpec.vRefNum;
  908.     cInfo.hFileInfo.ioDirID = fFileSpec.parID;
  909.     return PBSetCatInfoSync(&cInfo);
  910. }
  911.  
  912. //--------------------------------------------------------------------------------------------------
  913. #pragma segment MAFileRes
  914.  
  915. pascal OSErr TFile::SetDataLength(long length)
  916. {
  917.     if (fDataRefNum != kNoFileRefnum)
  918.         return SetEOF(fDataRefNum, length);
  919.     else
  920.         return noErr;
  921. }
  922.  
  923. //--------------------------------------------------------------------------------------------------
  924. #pragma segment MAFileRes
  925.  
  926. pascal OSErr TFile::SetDataMark(long mark,
  927.                                 short fromWhere)
  928. {
  929.     if (fDataRefNum != kNoFileRefnum)
  930.         return SetFPos(fDataRefNum, fromWhere, mark);
  931.     else
  932.         return noErr;
  933. }
  934.  
  935. //--------------------------------------------------------------------------------------------------
  936. #pragma segment MAFileRes
  937.  
  938. pascal OSErr TFile::SetFileInfo(HParamBlockRec& pb)
  939. {
  940.     Str63 fileName = fFileSpec.name;
  941.  
  942.     BlockSet((Ptr) & pb, sizeof(HParamBlockRec), 0x00);
  943.     pb.fileParam.ioNamePtr = &fileName;
  944.     pb.fileParam.ioVRefNum = fFileSpec.vRefNum;
  945.     pb.fileParam.ioDirID = fFileSpec.parID;
  946.     return PBHSetFInfoSync(&pb);
  947. }
  948.  
  949. //--------------------------------------------------------------------------------------------------
  950. #pragma segment MAFileRes
  951.  
  952. pascal void TFile::SetName(const Str63& newName)
  953. {
  954.     fFileSpec.name = newName;
  955. }
  956.  
  957. //--------------------------------------------------------------------------------------------------
  958. #pragma segment MAFileRes
  959.  
  960. pascal OSErr TFile::SetScript(ScriptCode theScript)
  961. {
  962.     OSErr theErr;
  963.     CInfoPBRec cInfo;
  964.  
  965.     // Get the current info so we don't have to set up the entire record 
  966.     if ((theErr = this->GetCatInfo(cInfo)) == noErr)
  967.     {
  968.         cInfo.hFileInfo.ioFlXFndrInfo.fdScript = theScript;
  969.         theErr = this->SetCatInfo(cInfo);
  970.     }
  971.     return theErr;
  972. }
  973.  
  974. //--------------------------------------------------------------------------------------------------
  975. #pragma segment MAFileRes
  976.  
  977. pascal void TFile::SetPermissions(SignedByte dataPermission,
  978.                                   SignedByte rsrcPermission)
  979. {
  980.     fDataPermission = dataPermission;
  981.     fRsrcPermission = rsrcPermission;
  982. }
  983.  
  984. //--------------------------------------------------------------------------------------------------
  985. #pragma segment MAFileRes
  986.  
  987. pascal OSErr TFile::UpdateResource(void)
  988. {
  989.     if (fRsrcRefNum != kNoFileRefnum)
  990.     {
  991.         UpdateResFile(fRsrcRefNum);
  992.         return ResError();
  993.     }
  994.     else
  995.         return noErr;
  996. }
  997.  
  998. //--------------------------------------------------------------------------------------------------
  999. #pragma segment MAFileRes
  1000.  
  1001. pascal short TFile::UseResource(void)
  1002. {
  1003.     short curResFileRefNum = CurResFile();            // get the CurResFile ref num on entry
  1004.     if ((curResFileRefNum != fRsrcRefNum) && (fRsrcRefNum != kNoFileRefnum))
  1005.         UseResFile(fRsrcRefNum);                    // set the CurResFile for this file
  1006.     return curResFileRefNum;                        // return the CurResFile ref num on entry
  1007. }
  1008.  
  1009. //--------------------------------------------------------------------------------------------------
  1010. #pragma segment MAFileRes
  1011.  
  1012. pascal OSErr TFile::WriteData(void* buffer,
  1013.                               long& count)
  1014. {
  1015.     if (fDataRefNum != kNoFileRefnum)
  1016.         return FSWrite(fDataRefNum, count, (Ptr)buffer);
  1017.     else
  1018.         return noErr;
  1019. }
  1020.  
  1021. //--------------------------------------------------------------------------------------------------
  1022. #pragma segment MAFields
  1023.  
  1024. pascal void TFile::Fields(TObject* obj)
  1025. {
  1026.     obj->DoToField("TFile", NULL, bClass);
  1027.     obj->DoToField("fFileSpec.name", &fFileSpec.name, bString);
  1028.     obj->DoToField("fFileSpec.vRefNum", &fFileSpec.vRefNum, bInteger);
  1029.     obj->DoToField("fFileSpec.parID", &fFileSpec.parID, bLongInt);
  1030.     obj->DoToField("fModDate", &fModDate, bLongInt);
  1031.     obj->DoToField("fCreator", &fCreator, bOSType);
  1032.     obj->DoToField("fFileType", &fFileType, bOSType);
  1033.     obj->DoToField("fScriptTag", &fScriptTag, bByte);
  1034.     obj->DoToField("fUsesDataFork", &fUsesDataFork, bBoolean);
  1035.     obj->DoToField("fUsesRsrcFork", &fUsesRsrcFork, bBoolean);
  1036.     obj->DoToField("fDataPermission", &fDataPermission, bByte);
  1037.     obj->DoToField("fRsrcPermission", &fRsrcPermission, bByte);
  1038.     obj->DoToField("fKeepDataOpen", &fKeepDataOpen, bBoolean);
  1039.     obj->DoToField("fKeepRsrcOpen", &fKeepRsrcOpen, bBoolean);
  1040.     obj->DoToField("fDataRefNum", &fDataRefNum, bInteger);
  1041.     obj->DoToField("fRsrcRefNum", &fRsrcRefNum, bInteger);
  1042.     inherited::Fields(obj);
  1043. }
  1044.  
  1045.  
  1046.