Permalink
Browse files

Added readDirectoryByFileModificationDateDesc to DirectoryReader. Ret…

…ained m_fileHandle so readLine can span event loop cycles, released in added dealloc. Released data at completiong of readLine methods (was leaking). Added standard .gitignore for cocoa projects.
  • Loading branch information...
1 parent ab074ff commit 05f44f789e25ded9bd189fa4fe36b18bd3a05684 @levous levous committed Jun 2, 2011
Showing with 94 additions and 8 deletions.
  1. +40 −0 .gitignore
  2. +6 −1 DirectoryReader.h
  3. +38 −1 DirectoryReader.m
  4. +0 −2 FileReader.h
  5. +9 −2 FileReader.m
  6. +0 −1 NSDataExtensions.h
  7. +1 −1 NSDataExtensions.m
View
@@ -0,0 +1,40 @@
+# Mac OS X Finder and whatnot
+.DS_Store
+
+# XCode (and ancestors) per-user config (very noisy, and not relevant)
+*.mode1
+*.mode1v3
+*.mode2v3
+*.perspective
+*.perspectivev3
+*.pbxuser
+project.xcworkspace/
+xcuserdata/
+*Pbxproj.pyc
+
+# Generated files
+VersionX-revision.h
+
+# build products
+build/
+*.[oa]
+
+
+# Other source repository archive directories (protects when importing)
+.hg
+.svn
+CVS
+
+
+# automatic backup files
+*~.nib
+*.swp
+*~
+*(Autosaved).rtfd/
+Backup[ ]of[ ]*.pages/
+Backup[ ]of[ ]*.key/
+Backup[ ]of[ ]*.numbers/" >> "$FILE"
+*.orig
+
+# Compiled Python files produced by Three20
+*.pyc
View
@@ -6,7 +6,6 @@
// Copyright 2010 Tobias Preuss. All rights reserved.
//
-#import <Cocoa/Cocoa.h>
@interface DirectoryReader : NSObject {
@@ -16,4 +15,10 @@
- (BOOL)readDirectory:(NSArray**)files;
+/**
+ Returns array of string paths for each file in the directory, sorted by file modification date, descending
+ @param files output parameter containing the file paths
+ @returns BOOL indicating whether the directory file list could be read
+ */
+- (BOOL)readDirectoryByFileModificationDateDesc:(NSArray**)files;
@end
View
@@ -14,7 +14,7 @@
*/
@implementation DirectoryReader
-
+#define kFileAttributeFileNameKey @"fileName"
/**
Initializes a directory reader.
@param path A directory path.
@@ -63,5 +63,42 @@ - (BOOL)readDirectory:(NSArray**)files {
return success;
}
+- (BOOL)readDirectoryWithFileAttributes:(NSArray**)fileAttributes{
+ NSArray *fileNames;
+ BOOL success = [self readDirectory:&fileNames];
+
+ if (!success) // couldn't read directory
+ return NO;
+
+ NSError *fileIoError;
+ // create array to gather attributes
+ NSMutableArray *fileAttributesResults = [NSMutableArray arrayWithCapacity:[fileNames count]];
+ for (NSString *fileName in fileNames){
+ NSMutableDictionary *extendedDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:fileName error:&fileIoError]];
+ [extendedDictionary setObject:fileName forKey:kFileAttributeFileNameKey];
+ [fileAttributesResults addObject:extendedDictionary];
+ }
+
+ *fileAttributes = [NSArray arrayWithArray:fileAttributesResults];
+ return YES;
+
+
+}
+
+- (BOOL)readDirectoryByFileModificationDateDesc:(NSArray**)files{
+ NSArray *fileAttributes;
+ BOOL success = [self readDirectoryWithFileAttributes:&fileAttributes];
+ if (!success) // couldn't read directory
+ return NO;
+ NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"fileModificationDate" ascending:NO];
+ NSArray *sortedAttributes = [fileAttributes sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
+ NSMutableArray *fileNames = [NSMutableArray arrayWithCapacity:[fileAttributes count]];
+ for( NSDictionary *fileAttributeDictionary in sortedAttributes){
+ [fileNames addObject:[fileAttributeDictionary objectForKey:kFileAttributeFileNameKey]];
+ }
+
+ *files = [NSArray arrayWithArray:fileNames];
+ return YES;
+}
@end
View
@@ -8,8 +8,6 @@
// Originally written by Dave DeLong,
// Source: http://stackoverflow.com/questions/3707427#3711079
-#import <Cocoa/Cocoa.h>
-
@interface FileReader : NSObject {
View
@@ -20,7 +20,10 @@
*/
@implementation FileReader
-
+- (void)dealloc{
+ [m_fileHandle release], m_fileHandle = nil;
+ [super dealloc];
+}
/**
Initialized a file reader object.
@@ -34,7 +37,7 @@ - (id)initWithFilePath:(NSString*)filePath {
if (!filePath || [filePath length] <= 0) {
return nil;
}
- m_fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
+ m_fileHandle = [[NSFileHandle fileHandleForReadingAtPath:filePath] retain];
if (m_fileHandle == nil) {
return nil;
}
@@ -89,6 +92,8 @@ - (NSString*)readLine {
}
NSString* line = [currentData stringValueWithEncoding:NSUTF8StringEncoding];
+ // finished with data
+ [currentData release], currentData = nil;
return line;
}
@@ -179,6 +184,8 @@ - (NSString*)readLineBackwards {
} // End if while.
NSString* line = [[NSString alloc] initWithData:currentData encoding:NSUTF8StringEncoding];
+ // finished with data
+ [currentData release], currentData = nil;
return line;
}
View
@@ -6,7 +6,6 @@
// Copyright 2010 Tobias Preuss. All rights reserved.
//
-#import <Cocoa/Cocoa.h>
// -----------------------------------------------------------------------------
View
@@ -80,7 +80,7 @@ - (NSRange)rangeOfDataBackwardsSearch:(NSData*)dataToFind {
if (length < searchLength) {
return foundRange;
}
- for (NSUInteger index = length - searchLength; index >= 0;) {
+ for (NSInteger index = length - searchLength; index >= 0;) {
// NSLog(@"%c == %c", ((char*)bytes)[index], ((char*)searchBytes)[searchIndex]); /* DEBUG LOG */
if (((char*)bytes)[index] == ((char*)searchBytes)[searchIndex]) {
// The current character matches.

0 comments on commit 05f44f7

Please sign in to comment.