/*
print with threads :
print every file's first n char contents under the path that pass to this program,
format like : pwt [-h n] /some/path
*/
#import <Foundation/Foundation.h>
#define DEFAULT_N_SIZE 32
//#define MIN(x,y) (x<=y?x:y)
@class Main;
@interface OperationReadFile:NSOperation{
NSString *path;
}
@property(copy) NSString *path;
-(id)initWithPath:(NSString*)path;
@end
@interface Main:NSObject{
NSString *path;
NSOperationQueue *queue;
}
@property (copy) NSString *path;
+(id)shared;
-(void)start;
-(id)initWithPath:(NSString *)path;
-(void)print:(NSString *)str;
@end
@implementation OperationReadFile
@synthesize path;
-(id)initWithPath:(NSString*)path_v{
self = [super init];
if(self){
self.path = path_v;
}
return self;
}
NSString *head_n(NSString *str,NSUInteger n){
return [str substringToIndex:MIN(n,[str length])];
}
-(void)main{
//NSLog(@"path is %@",path);
NSString *content = [NSString stringWithContentsOfFile:path
encoding:NSASCIIStringEncoding error:NULL];
content = head_n(content,DEFAULT_N_SIZE);
NSLog(@"content:%@",content);
[[Main shared] performSelectorOnMainThread:@selector(print:)
withObject:content waitUntilDone:NO];
}
@end
@implementation Main{
NSMutableArray *files;
}
static Main *shared;
@synthesize path;
+(id)shared{
if(!shared){
NSLog(@"ERR:shared is not inited!");
}
return shared;
}
-(void)start{
for(NSString *file in files){
OperationReadFile *orf = [[OperationReadFile alloc] initWithPath:file];
[queue addOperation:orf];
}
}
-(void)print:(NSString *)str{
NSLog(@"file contents : %@",str);
}
-(id)initWithPath:(NSString *)path_v{
if(shared) return shared;
self = [super init];
if(self){
self.path = [path_v stringByExpandingTildeInPath];
NSLog(@"full path is %@",path);
NSFileManager *fm = [NSFileManager defaultManager];
files = [NSMutableArray array];
NSDirectoryEnumerator *enum_dir = [fm enumeratorAtPath:path];
NSString *file;
BOOL flag = false;
while(file = [enum_dir nextObject]){
//NSLog(@"file is %@",file);
[fm fileExistsAtPath:[path stringByAppendingPathComponent:file] isDirectory:&flag];
if(flag)
[enum_dir skipDescendents];
else
[files addObject:[path stringByAppendingPathComponent:file]];
}
NSLog(@"files is : %@",files);
queue = [[NSOperationQueue alloc] init];
shared = self;
}
return self;
}
-(id)init{
return [self initWithPath:@"."];
}
@end
int main(void){
@autoreleasepool{
NSProcessInfo *pi = [NSProcessInfo processInfo];
NSArray *args = [pi arguments];
NSLog(@"args : %@",args);
//NSString *path = @".";
Main *main = [[Main alloc] init];
//NSLog(@"%@",[@"~/src" stringByAppendingPathComponent:@"asm_src"]);
//Main *main = [[Main alloc]initWithPath:@"~/src/asm_src/nasm_src/linux"];
[main start];
//NSString *content = [[NSString alloc] initWithContentsOfFile:@"./9.m"
//encoding:NSUTF8StringEncoding error:NULL];
//NSLog(@"%@",[NSString stringWithContentsOfFile:@"./9.m"
//encoding:NSASCIIStringEncoding error:NULL]);
//NSLog(@"%@",content);
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[loop run];
}
return 0;
}