home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Graphics / ImageBrowser-1.0 / FileInfo.m < prev    next >
Encoding:
Text File  |  1998-01-11  |  5.3 KB  |  164 lines

  1. /*
  2.     File:       FileInfo.m
  3.  
  4.     Contains:   Implementation for FileInfo, an object that gathers directory
  5.                 information from the user's filesystem
  6.  
  7.     Written by: Andy Wildenberg
  8.  
  9.     Created:    9 July 1997
  10.  
  11.     Copyright:  (c)1997 by Apple Computer, Inc., all rights reserved.
  12.  
  13.     Change History:
  14.        version 1.0: first public version
  15.  
  16.     You may incorporate this sample code into your applications without
  17.     restriction, though the sample code has been provided "AS IS" and the
  18.     responsibility for its operation is 100% yours.  However, what you are
  19.     not permitted to do is to redistribute the source as "DTS Sample Code"
  20.     after having made changes. If you're going to re-distribute the source,
  21.     we require that you make it clear in the source that the code was
  22.     descended from Apple Sample Code, but that you've made changes.
  23. */
  24.  
  25.  
  26. #import "FileInfo.h"
  27.  
  28. @implementation FileInfo
  29.  
  30. -(id) initWithPath:(NSString*) path
  31. {
  32.     NSFileManager* fileManager;
  33.     NSDictionary* fileInformation;
  34.  
  35.     // this takes the current path and gets all the relevant information from the file system.
  36.     self = [super init]; // must init myself properly first
  37.  
  38.     fileManager = [NSFileManager defaultManager];
  39.  
  40.     // first get the information about me
  41.     fileInformation = [fileManager fileAttributesAtPath: path traverseLink:NO];
  42.  
  43.     // set the fullPathName to be the complete specification of where this file is
  44.     fullPathName = [path copy];  // keep it around!
  45.     filetype = [[fileInformation objectForKey: NSFileType] copy]; // copy all these, since we don't want them to change
  46.     lastModified= [[fileInformation objectForKey: NSFileModificationDate] copy];
  47.  
  48.     // setup my subfolders appropriately
  49.     containedFiles = nil;
  50.  
  51.     // note that we don't find anything out about the subfiles until we're queried about it
  52.  
  53.     // fix our filename to be just the tail end of the fullPathName
  54.     filename = [[fullPathName lastPathComponent] copy];
  55.     return self;
  56. }
  57.  
  58.  
  59. /* findContainedFiles does the hard work in the FileInfo class.  If self is a directory,  it gets a list of all the files contained in that directory, creates a FileInfo for  each of them and keeps an array with those FileInfo objects.  It is called internally,  and is called lazily -- if information about the children was gathered immediately, this would cause huge startup times since the entire file tree would have to be travesed to gather the appropriate information.
  60. */
  61. - (void)findContainedFiles
  62. {
  63.     NSFileManager    *fileManager = [NSFileManager defaultManager];
  64.     NSEnumerator    *enumerator;
  65.     NSString        *currentFile;
  66.     FileInfo        *fileInfo;
  67.     NSArray        *filenames;
  68.     NSMutableArray    *tempArray; // to build it up
  69.  
  70.     // get a list of all files contained in me
  71.     filenames = [fileManager directoryContentsAtPath:fullPathName];
  72.  
  73.     // create the arrays to cache the data
  74.     tempArray = [[NSMutableArray arrayWithCapacity: [filenames count]] retain];
  75.  
  76.     // okay, now iterate through the file list, adding info as we get to it
  77.     enumerator = [filenames objectEnumerator];
  78.  
  79.     while ((currentFile = [enumerator nextObject]))
  80.     {
  81.         fileInfo = [[[self class] alloc] initWithPath: [fullPathName stringByAppendingPathComponent: currentFile]];
  82.         if ( fileInfo )
  83.             [tempArray addObject: fileInfo];
  84.     }
  85.  
  86.     [self setContainedFiles:tempArray];
  87. }
  88.  
  89. - (void)setContainedFiles:(NSArray *)value;
  90. {
  91.     [containedFiles autorelease];
  92.     containedFiles = [[value sortedArrayUsingSelector:@selector(compare:)] retain];
  93. }
  94.  
  95. /* containedFiles returns the NSArray of files contained in self.  If self is a  normal file, the answer is nil.  If self is a directory, the answer may be a 0 length array.  If self has never checked for the existence of children, it calls findContainedFiles to create the FileInfo objects for all its children.
  96. */
  97. -(NSArray *) containedFiles;
  98. {
  99.     if (filetype == NSFileTypeDirectory)
  100.     {
  101.         if (containedFiles == nil)
  102.         {
  103.             //cache them up
  104.             [self findContainedFiles];
  105.         }
  106.         return containedFiles;
  107.     }
  108.     else
  109.         return nil;
  110. }
  111.  
  112. // accessor functions for the FileInfo.
  113. -(NSString*) filename
  114. {
  115.     return filename;
  116. }
  117.  
  118. -(NSString*) fullPathName
  119. {
  120.     return fullPathName;
  121. }
  122.  
  123. -(NSString*) filetype
  124. {
  125.     return filetype;
  126. }
  127.  
  128. -(NSString*) lastModified
  129. {
  130.     return lastModified;
  131. }
  132.  
  133. // cleanup functions
  134. - (void)dealloc
  135. {
  136.     // this should never be called directly, but the system will call it when the
  137.     // last copy of a particular object is released.  At this point it is necessary
  138.     // to free all the objects that have been copied or retained by this object
  139.     // NOTE: it is not necessary to check that the objects have already been allocated --
  140.     // if they haven't they will be nil, and sending release to nil is guaranteed not
  141.     // to cause an error or do anything stupid.  (actually sending nil to any object has
  142.     // the same guarantee).
  143.     [filename release];
  144.     [filetype release];
  145.     [lastModified release];
  146.     [fullPathName release];
  147.     [containedFiles release]; 
  148. }
  149.  
  150. - (NSComparisonResult)compare:(FileInfo *)obj
  151. {
  152.     NSComparisonResult result = NSOrderedDescending;
  153.     if ( obj )
  154.     {
  155.         result = [[self filetype] compare:[obj filetype]];
  156.         if ( result == NSOrderedSame )
  157.             result = [[self filename] compare:[obj filename]];
  158.     }
  159.  
  160.     return result;
  161. }
  162.  
  163. @end
  164.