home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 1992 August / info-mac-1992.iso / Source / C / Dragonsmith / Dragonsmith 1.0b2 / Utilities / FileUtils.c next >
Encoding:
C/C++ Source or Header  |  1992-05-19  |  2.9 KB  |  103 lines  |  [TEXT/KAHL]

  1. /*
  2.     FileUtils.c
  3.     
  4.     Created    05 May 1992    Extracted from Dragon.c
  5.     Modified    
  6.     
  7. */
  8.  
  9. #include    "FileUtils.h"
  10.  
  11. OSType **FREFTypes (short *numTypesReturned)
  12. {
  13.     short    numFREFs, frefNum, numTypes = 0;
  14.     OSType    type, **typesHndl, **fref;
  15.     
  16.     /* NOTE:    This function will only work on the most recently opened resource file
  17.             — we have to call Count1Resources and Get1IndResource because
  18.             the System file (at least in System 7) has 'FREF' resources of its own,
  19.             but we want to ignore them — CountResources and GetIndResource
  20.             would screw everything up! */
  21.             
  22.     *numTypesReturned = 0;
  23.     numFREFs = Count1Resources ('FREF');
  24.     if (numFREFs != 0) {
  25.         typesHndl = (OSType **) NewHandle (numFREFs * sizeof (OSType));
  26.         if (typesHndl != NULL) {
  27.             for (numTypes = 0, frefNum = 1; frefNum <= numFREFs; frefNum++) {
  28.                 fref = (OSType **) Get1IndResource ('FREF', frefNum);
  29.                 if (fref != NULL) {
  30.                     type = **fref;
  31.                     DisposHandle ((Handle) fref);
  32.                     if (type != 'APPL')                        // Don't count the 'APPL' type — every application
  33.                         (*typesHndl) [numTypes++] = type;    //    has an 'FREF' for this
  34.                 }
  35.             }
  36.             if (numTypes < numFREFs)
  37.                 SetHandleSize ((Handle) typesHndl, numTypes * sizeof (OSType));
  38.         }
  39.     }    
  40.     *numTypesReturned = numTypes;
  41.     return typesHndl;
  42. }
  43.  
  44. Boolean OpenableType (OSType fileType, short numOKTypes, OSType **OKTypesHndl)
  45. {
  46.     short    i;
  47.     OSType    t, *tp = *OKTypesHndl;
  48.     
  49.     for (i = numOKTypes; i > 0; i--, tp++) {
  50.         t = *tp;
  51.         if (fileType == 'fold' || fileType == 'disk') {
  52.             if (t == fileType)
  53.                 return TRUE;
  54.         } else if (t == '****' || t == fileType)
  55.             return TRUE;
  56.     }
  57.     return FALSE;        // If we got here, there's no match
  58. }
  59.  
  60. OSType FSpType (FSSpec *fss, OSErr *err)
  61. {
  62.     CInfoPBRec        pb;
  63.     long                dirID;
  64.     
  65.     // If the parent ID of an FSSpec object (file, folder, volume) == 1, it's a volume —
  66.     //    I don't know if this is ALWAYS the case, but it seems to work for me.  It has
  67.     //    NOT, however, been tested in a networked environment — what would the
  68.     //    parID be for an AppleShare volume, for example?  The rules might be totally
  69.     //    different.  On the other hand, Apple might have said in some documentation
  70.     //    (that I haven't read) that a directory ID of 1 means exactly this.  Wish I knew
  71.     //    for sure…
  72.     dirID = fss->parID;
  73.     if (dirID == 1) {
  74.         *err = noErr;
  75.         return 'disk';
  76.     }
  77.     pb.hFileInfo.ioNamePtr = fss->name;
  78.     pb.hFileInfo.ioVRefNum = fss->vRefNum;
  79.     pb.hFileInfo.ioFDirIndex = 0;
  80.     pb.hFileInfo.ioDirID = dirID;
  81.     *err = PBGetCatInfoSync (&pb);
  82.     
  83.     // Bit 4 (where 0 is the least significant, i.e. rightmost, bit) of the ioFlAttrib field will
  84.     //    be set after the call to PBHGetInfo if this is in fact a folder and not a file
  85.     if (pb.hFileInfo.ioFlAttrib & 16)
  86.         return 'fold';
  87.     else
  88.         return pb.hFileInfo.ioFlFndrInfo.fdType;
  89. }
  90.  
  91. Boolean FSpOpenableType (FSSpec *fss, short numOKTypes, OSType **OKTypesHndl)
  92. {
  93.     OSType            fileType;
  94.     OSErr            err;
  95.     
  96.     fileType = FSpType (fss, &err);
  97.     if (err)
  98.         return FALSE;
  99.     else if (OKTypesHndl != NULL)
  100.         return OpenableType (fileType, numOKTypes, OKTypesHndl);
  101. }
  102.  
  103.