1 /* Copyright (c) 2007 Dirk Theisen
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSConcreteDirectoryEnumerator.h>
10 #import <Foundation/NSFileManager.h>
11 #import <Foundation/NSPathUtilities.h>
12 #import <Foundation/NSArray.h>
14 @implementation NSConcreteDirectoryEnumerator
16 - (void) skipDescendents
18 skipDescendents = YES;
25 [lastFileAttributes release];
30 - (NSString*) description
32 return [NSString stringWithFormat: @"%@, to enumerate: %@", [super description], list];
36 - (id)initWithPath: (NSString*)aPath
38 if ((self = [super init])) {
39 startPath = [aPath copy];
40 fm = [[NSFileManager defaultManager] retain];
41 list = [[fm directoryContentsAtPath: aPath] mutableCopy];
48 - (void) setLastFilePath: (NSString*) aPath
49 /*" Sets the lastFilePath (relative) to aPath and its file attributes. "*/
51 NSString* fullPath = [startPath stringByAppendingPathComponent: aPath];
52 NSDictionary* attrs = [fm fileAttributesAtPath: fullPath
55 //NSLog(@"Found '%@' to have attributes %@", fullPath, attrs);
56 [lastFilePath release];
57 lastFilePath = [aPath retain];
59 [lastFileAttributes release];
60 lastFileAttributes = [attrs retain];
65 NSString* result = nil;
66 if ([[lastFileAttributes fileType] isEqualToString: NSFileTypeDirectory]) {
67 // last enumerated file was a directory
68 if (! skipDescendents) {
69 // Add all files in the directory to the list,
70 // after making them relative to the lastFilePath:
71 NSString* lastFilePathAbs = [startPath stringByAppendingPathComponent: lastFilePath];
72 NSArray* dirContent = [fm directoryContentsAtPath: lastFilePathAbs];
74 if ([dirContent count]) {
75 NSEnumerator* dirContentEnumerator = [dirContent reverseObjectEnumerator];
78 //NSLog(@"Found dir content of '%@' to be %@", lastFilePathAbs, dirContent);
80 while ((filename = [dirContentEnumerator nextObject])) {
81 NSString* filePath = [lastFilePath stringByAppendingPathComponent: filename];
82 [list insertObject: filePath atIndex: 0];
88 if ([list count] > 0) {
89 result = [[[list objectAtIndex: 0] retain] autorelease];
90 [list removeObjectAtIndex: 0];
93 if (result) [self setLastFilePath: result];
96 //NSLog(@"Enumerating %@", result);
104 - (NSDictionary*) directoryAttributes
106 return [fm fileAttributesAtPath: startPath traverseLink: NO];
109 - (NSDictionary*) fileAttributes
111 return lastFileAttributes;
115 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
118 state->itemsPtr = stackbuf;
119 state->mutationsPtr = (unsigned long *)self;
121 id next = [self nextObject];
122 if (nil == next) return 0;