File: programming/cocoa/Filie.zip/Filie/UliKit/UKDirectoryEnumerator.m


/* =============================================================================
	FILE:		UKDirectoryEnumerator.m
	PROJECT:	Filie
 
    COPYRIGHT:  (c) 2004 M. Uli Kusterer, all rights reserved.
    
	AUTHORS:	M. Uli Kusterer - UK
    
    LICENSES:   MIT License
 
	REVISIONS:
		2006-03-13	UK	Clarified license, miscellaneous additions.
		2004-04-15	UK	Created.
   ========================================================================== */
 
// -----------------------------------------------------------------------------
//  Headers:
// -----------------------------------------------------------------------------
 
#import "UKDirectoryEnumerator.h"
#import "NSString+CarbonUtilities.h"
 
 
// -----------------------------------------------------------------------------
//  Prototypes:
// -----------------------------------------------------------------------------
#if UKDE_ALLOW_ENUMERATORWITHPATH
#endif
// -----------------------------------------------------------------------------
//  initWithPath:
//      Convenience initializer. Uses a default cache size.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  initWithFSRef:cacheSize:
//      Designated initializer. Opens our FSIterator and initializes our
//      cache.
//
//  REVISIONS:
//      2006-08-22  UK  Extracted from initWithPath:cacheSize:.
// -----------------------------------------------------------------------------
"UKDirectoryEnumerator::initWithFSRef: - MacOS Error ID= %d"// In case it's a link or an alias that got resolved.
'/'// -----------------------------------------------------------------------------
//  initWithPath:cacheSize:
//      Opens our FSIterator and initializes our cache.
//
//  REVISIONS:
//      2006-08-22  UK  Moved code to init from FSRef into a separate method.
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
"UKDirectoryEnumerator::initWithPath: Can't get an FSRef for the specified path."// -----------------------------------------------------------------------------
//  dealloc:
//      Close FSIterator that we wrap with this object.
//
//  REVISIONS:
//		2005-10-15	UK	Made this release infoCache. Thanks Nicholas Jitkoff!
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  iterator:
//      Let those Carbon fans manually mess with the FSIterator if they have
//      a need to.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  nextObjectFSRef:
//      Fetch the next file FSRef from the cache. If the cache is empty,
//      this will get the next batch of FSRefs into the cache using the Carbon
//      FSGetCatalogInfoBulk call, and also cache their file info.
//
//  REVISIONS:
//      2006-08-22  UK  Extracted this code from nextObjectFullPath:.
// -----------------------------------------------------------------------------
// TODO: We could use this param to get the names for a faster -nextObject implementation.
"UKDirectoryEnumerator::nextObjectFSRef - MacOS Error ID= %d"// No need to show error if we're just at end of list.
"UKDirectoryEnumerator::nextObjectFSRef - FSCatalogInfoBulk returned 0 items, but not errFSNoMoreItems."// -----------------------------------------------------------------------------
//  nextObjectFullPath:
//      Fetch the next file path from the cache. The Cache contains FSRefs, so
//      this will convert the FSRef into an NSString. If the cache is empty,
//      -nextObjectFSRef will transparently recache the next batch.
//
//  REVISIONS:
//		2006-08-22	UK	Extracted most of the code in this method into
//						-nextObjectFSRef.
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// stringWithFSRef returns NIL when it gets a NIL FSRef.
}
 
 
// -----------------------------------------------------------------------------
//  nextObject:
//      Fetch the next file name from the cache. This is made to work the same
//      way as NSDirectoryEnumerator, and thus only returns the filenames. If
//      you want the absolute pathname, use nextObjectFullPath.
//
//  REVISIONS:
//		2006-08-22	UK	Changed this to call nextObjectFSRef directly instead
//						of going through -nextObjectFullPath.
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// stringWithFSRef returns NIL when it gets a NIL FSRef.
// Remove the prefix (parent folder) from this path to make it relative.
}
 
 
// -----------------------------------------------------------------------------
//  cacheExhausted:
//      Tells you whether the next call to nextObject will cause a reload of the
//      cache. You usually don't need this, but sometimes it's useful for
//      deciding when to update progress information. If there will be a short
//      pause while the File Manager caches some more FSRefs, you might as well
//      accept the overhead of drawing new status info to the screen.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  fileAttributes:
//      This is basically the same as NSDirectoryEnumerator's fileAttributes
//      method. However, what you get here depends on what flags you specified
//      to setDesiredInfo:, which defaults to kFSCatInfoNone, which means you
//      get an empty dictionary here if you don't explicitly ask for info.
//
//      Depending on what you pass to setDesiredInfo:, you can even get
//      additional info that you wouldn't get from an NSDirectoryEnumerator.
//      In particular, since we're using Carbon under the hood, we get all the
//      nice info the Finder knows, but other Cocoa apps don't.
//
//      This is an expensive call. If you can, use others like isInvisible or
//      isDirectory.
//
//  REVISIONS:
//      2005-07-03  UK  Extracted CatInfo -> Dictionary code into separate
//                      function UKDictionaryFromFSCatInfo().
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
 
#define UK_BTST(f,m)	(((f) & (m)) == (m))    // Shorthand bit-test macro.
// -----------------------------------------------------------------------------
//  isInvisible:
//      If you passed the kFSCatInfoFinderInfo flag to setDesiredInfo:, this
//      will return the value of the Finder's kIsInvisible file flag. Otherwise
//      this will ruthlessly claim the file was visible.
//
//      This will *not* do any other checks, like whether the file name starts
//      with a period.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
'.'));
}
 
 
// -----------------------------------------------------------------------------
//  isDirectory:
//      If you passed the kFSCatInfoNodeFlags flag to setDesiredInfo:, this
//      will tell you whether an item is a directory (aka folder) or not.
//      Otherwise this will ruthlessly claim it was a file.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  setDesiredInfo:
//      Takes a bit field of or-ed together FSCatalogInfoBitmap flags that
//      control what information will be collected about files. You can then
//      query this information using the fileAttributes, isInvisible and
//      isDirectory methods.
//
//		This only works *before* making your first call to nextObject or the
//		likes.
//
//      FSCatalogInfoBitmap and the associated flags are defined in
//      <Carbon/Files.h>.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  desiredInfo:
//      Returns the flags set using setDesiredInfo:. If you didn't call that,
//      you'll probably get the default kFSCatalogInfoNone.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
#if UKDE_ALLOW_SETWHICHINFO
// -----------------------------------------------------------------------------
//  setWhichInfo: and whichInfo:    *DEPRECATED*
// -----------------------------------------------------------------------------
#endif
 
 
// -----------------------------------------------------------------------------
//  setCacheSize:
//      Controls the size (in number of files) of the cache used when getting
//      the files. The file list is retrieved in batches of that many files,
//      and -nextObject will automatically fetch the next item from the cache
//      and load the next batch into the cache as needed.
//
//      Note that this destroys any currently cached items. So only call this
//      before your first call to -nextObject or when -cacheExhausted is YES.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// Get rid of the old FSRef and FSCatalogInfo caches:
// Allocate new caches of the requested size:
// -----------------------------------------------------------------------------
//  cacheSize:
//      Returns the size (in number of files) of the cache used when getting
//      the files. The file list is retrieved in batches of that many files,
//      and -nextObject will automatically fetch the next item from the cache
//      and load the next batch into the cache as needed.
//
//  REVISIONS:
//      2004-11-11  UK  Documented.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//  visibleDirectoryContentsAtPath:
//      Lists the contents of a particular directory (aka folder), removing
//      any items that are invisible according to MacOS X conventions. Note
//      that this will not consider "latent" invisibility. I.e. if you list the
//      contents of an invisible folder, only the files that are themselves
//      invisible inside it will be removed.
//
//      This tries to apply the same criteria as the Finder when it comes to
//      invisibility.
//
//  REVISIONS:
//      2004-11-11  UK  Created.
// -----------------------------------------------------------------------------
// Everything created now will be autoreleased if it isn't in arr:
// Loop through the directory:
'.' )    // Unix-style invisibility?
// MacOS-style invisibility?
// File is visible and should be listed.
        }
        
        // Now, if we're at the file system root, consult .hidden on what other files we should hide:
"/"] )  // At the root level, we have some specially hidden Unix folders:
"/.hidden""\n"// End of autoreleased area.
"changeCarbonFileAttributes:atPath: FSSetCatalogInfo: MacOS Error ID=%d"// Clear all fields.
    
    // Node Flags:
// Finder Flags:
// Extended Finder Flags:
// Permissions:
// Dates:
    // TO DO: Write code to set dates.
    /*CFAbsoluteTime      absTime = 0;
    if( UK_BTST(whichInfo, kFSCatInfoCreateDate) )
    {
        UCConvertUTCDateTimeToCFAbsoluteTime( &currInfo->createDate, &absTime );
		[dict setObject: [NSDate dateWithTimeIntervalSinceReferenceDate: absTime] forKey: NSFileCreationDate];
    }
    if( UK_BTST(whichInfo, kFSCatInfoAttrMod) )
    {
        UCConvertUTCDateTimeToCFAbsoluteTime( &currInfo->attributeModDate, &absTime );
		[dict setObject: [NSDate dateWithTimeIntervalSinceReferenceDate: absTime] forKey: UKFileAttrModificationDate];
    }
    if( UK_BTST(whichInfo, kFSCatInfoContentMod) )
    {
        UCConvertUTCDateTimeToCFAbsoluteTime( &currInfo->contentModDate, &absTime );
		[dict setObject: [NSDate dateWithTimeIntervalSinceReferenceDate: absTime] forKey: NSFileModificationDate];
    }
    if( UK_BTST(whichInfo, kFSCatInfoAccessDate) )
    {
        UCConvertUTCDateTimeToCFAbsoluteTime( &currInfo->accessDate, &absTime );
		[dict setObject: [NSDate dateWithTimeIntervalSinceReferenceDate: absTime] forKey: UKFileAccessDate];
    }
    if( UK_BTST(whichInfo, kFSCatInfoBackupDate) )
    {
        UCConvertUTCDateTimeToCFAbsoluteTime( &currInfo->backupDate, &absTime );
		[dict setObject: [NSDate dateWithTimeIntervalSinceReferenceDate: absTime] forKey: UKFileBackupDate];
    }*/
}
 
 

This code uses the PclZip Zip File reading code, which is subject to the GNU LGPL. It also uses the GeSHi syntax highlighter, subject to the GPL. Ask if you want this for your own web site, it's free.