Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 150 lines (120 sloc) 4.057 kb
dc5be1c Initial check-in.
uli authored
1 //
2 // UKFileFinder.m
3 // Shovel
4 //
5 // Created by Uli Kusterer on Wed Mar 24 2004.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright (c) 2004 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 #import "UKFileFinder.h"
29
30
31 @implementation UKFileFinder
32
33 +(id) fileFinderForFolder: (NSString*)folder withPattern: (NSString*)pattern
34 {
35 return [[[self alloc] initForFolder: folder withPattern: pattern] autorelease];
36 }
37
38 -(id) initForFolder: (NSString*)folder withPattern: (NSString*)pattern
39 {
40 self = [super init];
41 if( !self )
42 return nil;
43
44 tempBuffer = [[NSMutableString alloc] init];
45
46 findPipe = [[NSPipe alloc] init];
47 NSFileHandle* fHand = [findPipe fileHandleForReading];
48
49 findTask = [[NSTask alloc] init];
50 [findTask setLaunchPath: @"/usr/bin/find"];
51 [findTask setArguments: [NSArray arrayWithObjects: folder, @"-name", pattern, nil]];
52 [findTask setStandardOutput: findPipe];
53 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(captureOutput:)
54 name: NSFileHandleReadCompletionNotification object: fHand];
55 [fHand readInBackgroundAndNotify];
56
57 [findTask launch];
58
59 return self;
60 }
61
62
63 -(void) dealloc
64 {
65 [[NSNotificationCenter defaultCenter] removeObserver:self
66 name:NSFileHandleReadCompletionNotification object: [findPipe fileHandleForReading]];
67
68 [findTask release];
69 findTask = nil;
70 [tempBuffer release];
71 tempBuffer = nil;
72 [findPipe release];
73 findPipe = nil;
74
75 [super dealloc];
76 }
77
78
79 -(NSString*) nextObject
80 {
81 NSCharacterSet* charset = [NSCharacterSet characterSetWithCharactersInString: @"\n\r"];
82 [lastPath release];
83 lastPath = nil;
84
85 while( (findTask && [findTask isRunning]) || [tempBuffer length] > 0 )
86 {
87 [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
88
89 NSRange breakRange = [tempBuffer rangeOfCharacterFromSet: charset];
90 if( breakRange.location != NSNotFound || breakRange.length > 0 )
91 {
92 lastPath = [[tempBuffer substringToIndex: breakRange.location] retain];
93 breakRange.length += breakRange.location;
94 breakRange.location = 0;
95 [tempBuffer deleteCharactersInRange: breakRange];
96 NSLog(@"Size: %d.",[tempBuffer length]);
97 return lastPath;
98 }
99 else if( !findTask )
100 {
101 NSLog(@"Finished.");
102 lastPath = tempBuffer;
103 tempBuffer = [[NSMutableString alloc] init];
104 return lastPath;
105 }
106 }
107
108 return nil;
109 }
110
111
112 -(NSDictionary*) fileAttributes
113 {
114 return [[NSFileManager defaultManager] fileAttributesAtPath: lastPath traverseLink: NO];
115 }
116
117
118 -(void) captureOutput: (NSNotification*)notif
119 {
120 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
121 NSFileHandle* theHand = [notif object];
122 NSData* theData = [[notif userInfo] objectForKey: NSFileHandleNotificationDataItem];
123
124 if( theData && [theData length] > 0 )
125 {
126 NSString* newData = [[[NSString alloc] initWithData: theData encoding: NSUTF8StringEncoding] autorelease];
127
128 [tempBuffer appendString: newData];
129
130 [theHand readInBackgroundAndNotify];
131 }
132 /*else if( [findTask isRunning] )
133 [theHand readInBackgroundAndNotify];*/
134 else
135 {
136 NSTask* oldTask = findTask;
137 findTask = nil;
138 [oldTask terminate];
139 [oldTask release];
140 //[findPipe release];
141 //findPipe = nil;
142 }
143
144 NSLog(@"ReadNotification");
145
146 [pool release];
147 }
148
149 @end
Something went wrong with that request. Please try again.