Skip to content
Browse files

Implemented NSData backwards search.

+ Added backwards search to FileReader (still experimental).
+ Added backwards search to NSData (approved).
+ Added NSDataExtensions target for testing.
+ Added stepper to user interface to control number of lines.
+ Bugfix: DirectoryReader handles empty path now.
  • Loading branch information...
1 parent 94dbfa8 commit 47b6ee2ff4373c61d7e5692efa0b7b075d05e0ca @johnjohndoe committed Oct 6, 2010
View
7 DirectoryReader.m
@@ -24,6 +24,9 @@ - (id)initWithPath:(NSString*)path {
self = [super init];
if (self != nil) {
+ if (!path || [path length] <= 0) {
+ return nil;
+ }
m_path = path;
}
return self;
@@ -36,11 +39,7 @@ - (id)initWithPath:(NSString*)path {
*/
- (BOOL)readDirectory:(NSArray**)files {
-
BOOL success = false;
- if (!m_path) {
- return success;
- }
NSArray* fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:m_path error:nil];
View
193 English.lproj/MainMenu.xib
@@ -12,7 +12,7 @@
</object>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
- <integer value="372"/>
+ <integer value="371"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -1317,7 +1317,7 @@
<object class="NSWindowTemplate" id="972006081">
<int key="NSWindowStyleMask">15</int>
<int key="NSWindowBacking">2</int>
- <string key="NSWindowRect">{{335, 74}, {499, 676}}</string>
+ <string key="NSWindowRect">{{781, 326}, {499, 676}}</string>
<int key="NSWTFlags">1954021376</int>
<string key="NSWindowTitle">LineReader</string>
<string key="NSWindowClass">NSWindow</string>
@@ -1331,13 +1331,13 @@
<object class="NSTextField" id="231717339">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{130, 634}, {349, 22}}</string>
+ <string key="NSFrame">{{20, 606}, {459, 22}}</string>
<reference key="NSSuperview" ref="439893737"/>
<bool key="NSEnabled">YES</bool>
<object class="NSTextFieldCell" key="NSCell" id="355415411">
<int key="NSCellFlags">-1804468671</int>
<int key="NSCellFlags2">272630784</int>
- <string key="NSContents">/tmp/</string>
+ <string key="NSContents"/>
<object class="NSFont" key="NSSupport" id="953681415">
<string key="NSName">LucidaGrande</string>
<double key="NSSize">13</double>
@@ -1377,7 +1377,7 @@
<string key="NSContents">Directory path:</string>
<reference key="NSSupport" ref="953681415"/>
<reference key="NSControlView" ref="1045706751"/>
- <object class="NSColor" key="NSBackgroundColor">
+ <object class="NSColor" key="NSBackgroundColor" id="685989597">
<int key="NSColorSpace">6</int>
<string key="NSCatalogName">System</string>
<string key="NSColorName">controlColor</string>
@@ -1386,19 +1386,67 @@
<bytes key="NSWhite">MC42NjY2NjY2NjY3AA</bytes>
</object>
</object>
- <object class="NSColor" key="NSTextColor">
+ <object class="NSColor" key="NSTextColor" id="249716613">
<int key="NSColorSpace">6</int>
<string key="NSCatalogName">System</string>
<string key="NSColorName">controlTextColor</string>
<reference key="NSColor" ref="181844672"/>
</object>
</object>
</object>
+ <object class="NSStepper" id="1013929455">
+ <reference key="NSNextResponder" ref="439893737"/>
+ <int key="NSvFlags">268</int>
+ <string key="NSFrame">{{463, 548}, {19, 27}}</string>
+ <reference key="NSSuperview" ref="439893737"/>
+ <bool key="NSEnabled">YES</bool>
+ <object class="NSStepperCell" key="NSCell" id="372307018">
+ <int key="NSCellFlags">917024</int>
+ <int key="NSCellFlags2">0</int>
+ <reference key="NSControlView" ref="1013929455"/>
+ <double key="NSMaxValue">100</double>
+ <double key="NSIncrement">1</double>
+ <bool key="NSAutorepeat">YES</bool>
+ </object>
+ </object>
+ <object class="NSTextField" id="293172749">
+ <reference key="NSNextResponder" ref="439893737"/>
+ <int key="NSvFlags">268</int>
+ <string key="NSFrame">{{17, 553}, {444, 17}}</string>
+ <reference key="NSSuperview" ref="439893737"/>
+ <bool key="NSEnabled">YES</bool>
+ <object class="NSTextFieldCell" key="NSCell" id="844018831">
+ <int key="NSCellFlags">68288064</int>
+ <int key="NSCellFlags2">71304192</int>
+ <string key="NSContents">Count</string>
+ <reference key="NSSupport" ref="953681415"/>
+ <reference key="NSControlView" ref="293172749"/>
+ <bool key="NSDrawsBackground">YES</bool>
+ <reference key="NSBackgroundColor" ref="685989597"/>
+ <reference key="NSTextColor" ref="249716613"/>
+ </object>
+ </object>
+ <object class="NSTextField" id="449809172">
+ <reference key="NSNextResponder" ref="439893737"/>
+ <int key="NSvFlags">268</int>
+ <string key="NSFrame">{{17, 581}, {465, 17}}</string>
+ <reference key="NSSuperview" ref="439893737"/>
+ <bool key="NSEnabled">YES</bool>
+ <object class="NSTextFieldCell" key="NSCell" id="630023291">
+ <int key="NSCellFlags">68288064</int>
+ <int key="NSCellFlags2">272630784</int>
+ <string key="NSContents">Numbers of lines per file:</string>
+ <reference key="NSSupport" ref="953681415"/>
+ <reference key="NSControlView" ref="449809172"/>
+ <reference key="NSBackgroundColor" ref="685989597"/>
+ <reference key="NSTextColor" ref="249716613"/>
+ </object>
+ </object>
</object>
<string key="NSFrameSize">{499, 676}</string>
<reference key="NSSuperview"/>
</object>
- <string key="NSScreenRect">{{0, 0}, {1920, 1178}}</string>
+ <string key="NSScreenRect">{{0, 0}, {1280, 1002}}</string>
<string key="NSMaxSize">{1.79769e+308, 1.79769e+308}</string>
</object>
<object class="NSCustomObject" id="976324537">
@@ -2100,12 +2148,52 @@
<int key="connectionID">546</int>
</object>
<object class="IBConnectionRecord">
- <object class="IBOutletConnection" key="connection">
- <string key="label">m_sourcePath</string>
- <reference key="source" ref="976324537"/>
- <reference key="destination" ref="231717339"/>
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: maxNumLines</string>
+ <reference key="source" ref="1013929455"/>
+ <reference key="destination" ref="976324537"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="1013929455"/>
+ <reference key="NSDestination" ref="976324537"/>
+ <string key="NSLabel">value: maxNumLines</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">maxNumLines</string>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
</object>
- <int key="connectionID">547</int>
+ <int key="connectionID">557</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: maxNumLines</string>
+ <reference key="source" ref="844018831"/>
+ <reference key="destination" ref="976324537"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="844018831"/>
+ <reference key="NSDestination" ref="976324537"/>
+ <string key="NSLabel">value: maxNumLines</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">maxNumLines</string>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
+ </object>
+ <int key="connectionID">558</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: sourcePath</string>
+ <reference key="source" ref="355415411"/>
+ <reference key="destination" ref="976324537"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="355415411"/>
+ <reference key="NSDestination" ref="976324537"/>
+ <string key="NSLabel">value: sourcePath</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">sourcePath</string>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
+ </object>
+ <int key="connectionID">559</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
@@ -2664,6 +2752,9 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<reference ref="1045706751"/>
<reference ref="231717339"/>
+ <reference ref="449809172"/>
+ <reference ref="1013929455"/>
+ <reference ref="293172749"/>
</object>
<reference key="parent" ref="972006081"/>
</object>
@@ -3181,6 +3272,48 @@
<reference key="object" ref="183607361"/>
<reference key="parent" ref="1045706751"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">549</int>
+ <reference key="object" ref="1013929455"/>
+ <object class="NSMutableArray" key="children">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <reference ref="372307018"/>
+ </object>
+ <reference key="parent" ref="439893737"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">550</int>
+ <reference key="object" ref="372307018"/>
+ <reference key="parent" ref="1013929455"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">551</int>
+ <reference key="object" ref="293172749"/>
+ <object class="NSMutableArray" key="children">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <reference ref="844018831"/>
+ </object>
+ <reference key="parent" ref="439893737"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">552</int>
+ <reference key="object" ref="844018831"/>
+ <reference key="parent" ref="293172749"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">553</int>
+ <reference key="object" ref="449809172"/>
+ <object class="NSMutableArray" key="children">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <reference ref="630023291"/>
+ </object>
+ <reference key="parent" ref="439893737"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">554</int>
+ <reference key="object" ref="630023291"/>
+ <reference key="parent" ref="449809172"/>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -3408,6 +3541,12 @@
<string>534.IBPluginDependency</string>
<string>535.IBPluginDependency</string>
<string>536.IBPluginDependency</string>
+ <string>549.IBPluginDependency</string>
+ <string>550.IBPluginDependency</string>
+ <string>551.IBPluginDependency</string>
+ <string>552.IBPluginDependency</string>
+ <string>553.IBPluginDependency</string>
+ <string>554.IBPluginDependency</string>
<string>56.IBPluginDependency</string>
<string>56.ImportedFromIB2</string>
<string>57.IBEditorWindowLastContentRect</string>
@@ -3574,9 +3713,9 @@
<integer value="1"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1"/>
- <string>{{382, 248}, {499, 676}}</string>
+ <string>{{403, 304}, {499, 676}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string>{{382, 248}, {499, 676}}</string>
+ <string>{{403, 304}, {499, 676}}</string>
<integer value="1"/>
<string>{{33, 99}, {480, 360}}</string>
<string>{3.40282e+38, 3.40282e+38}</string>
@@ -3667,6 +3806,12 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1"/>
<string>{{286, 129}, {275, 183}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -3718,7 +3863,7 @@
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">548</int>
+ <int key="maxID">559</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -3735,13 +3880,11 @@
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>m_sourcePath</string>
- <string>m_tableView</string>
<string>window</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>NSTextField</string>
- <string>NSTableView</string>
<string>NSWindow</string>
</object>
</object>
@@ -4202,6 +4345,22 @@
</object>
</object>
<object class="IBPartialClassDescription">
+ <string key="className">NSStepper</string>
+ <string key="superclassName">NSControl</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBFrameworkSource</string>
+ <string key="minorKey">AppKit.framework/Headers/NSStepper.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
+ <string key="className">NSStepperCell</string>
+ <string key="superclassName">NSActionCell</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBFrameworkSource</string>
+ <string key="minorKey">AppKit.framework/Headers/NSStepperCell.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
<string key="className">NSTableView</string>
<string key="superclassName">NSControl</string>
<reference key="sourceIdentifier" ref="809545482"/>
View
5 FileReader.h
@@ -5,8 +5,8 @@
// Created by Tobias Preuss on 05.10.10.
// Copyright 2010 Tobias Preuss. All rights reserved.
//
-// Source: Dave DeLong, http://stackoverflow.com/questions/3707427#3711079
-
+// Originally written by Dave DeLong,
+// Source: http://stackoverflow.com/questions/3707427#3711079
#import <Cocoa/Cocoa.h>
@@ -26,6 +26,7 @@
- (id)initWithFilePath:(NSString*)filePath;
- (NSString*)readLine;
+- (NSString*)readLineBackwards;
- (NSString*)readTrimmedLine;
#if NS_BLOCKS_AVAILABLE
View
147 FileReader.m
@@ -5,41 +5,48 @@
// Created by Tobias Preuss on 05.10.10.
// Copyright 2010 Tobias Preuss. All rights reserved.
//
-// Source: Dave DeLong, http://stackoverflow.com/questions/3707427#3711079
+// Originally written by Dave DeLong,
+// Source: http://stackoverflow.com/questions/3707427#3711079
#import "FileReader.h"
// -----------------------------------------------------------------------------
-// NSData extension.
+// NSData extensions declaration.
// -----------------------------------------------------------------------------
@interface NSData (Additions)
- (NSRange)rangeOfData:(NSData*)dataToFind;
+- (NSRange)rangeOfDataBackwardsSearch:(NSData*)dataToFind;
+- (NSString*)stringValueWithEncoding:(NSStringEncoding)encoding;
@end
+// -----------------------------------------------------------------------------
+// NSData extensions implementation.
+// -----------------------------------------------------------------------------
+
+
/**
- Extension of the NSData class.
- @param Additions Additions.
- @returns An initialized NSData object or nil if the object could not be created.
+ Extension of the NSData class.
+ @param Additions Additions.
+ @returns An initialized NSData object or nil if the object could not be created.
*/
@implementation NSData (Additions)
/**
- Returns a range of data.
- @param dataToFind Data object specifying the delimiter and encoding.
- @returns A range.
+ Returns a range of data.
+ @param dataToFind Data object specifying the delimiter and encoding.
+ @returns A range.
*/
- (NSRange)rangeOfData:(NSData*)dataToFind {
-
const void* bytes = [self bytes];
NSUInteger length = [self length];
@@ -49,31 +56,82 @@ - (NSRange)rangeOfData:(NSData*)dataToFind {
NSRange foundRange = {NSNotFound, searchLength};
for (NSUInteger index = 0; index < length; index++) {
+ // The current character matches.
+ if (((char*)bytes)[index] == ((char*)searchBytes)[searchIndex]) {
+ // Store found location if not done earlier.
+ if (foundRange.location == NSNotFound) {
+ foundRange.location = index;
+ }
+ // Increment search character index to check for match.
+ searchIndex++;
+ // All search character match.
+ // Break search routine and return found position.
+ if (searchIndex >= searchLength) {
+ return foundRange;
+ }
+ }
+ // Match does not continue.
+ // Return to the first search character.
+ // Discard former found location.
+ else {
+ searchIndex = 0;
+ foundRange.location = NSNotFound;
+ }
+ }
+ return foundRange;
+}
+
+
+- (NSRange)rangeOfDataBackwardsSearch:(NSData*)dataToFind {
+
+ const void* bytes = [self bytes];
+ NSUInteger length = [self length];
+ const void* searchBytes = [dataToFind bytes];
+ NSUInteger searchLength = [dataToFind length];
+ NSUInteger searchIndex = 0;
+
+ NSRange foundRange = {NSNotFound, searchLength};
+ for (NSUInteger index = length - searchLength; index >= 0;) {
if (((char*)bytes)[index] == ((char*)searchBytes)[searchIndex]) {
// The current character matches.
if (foundRange.location == NSNotFound) {
foundRange.location = index;
}
+ index++;
searchIndex++;
if (searchIndex >= searchLength) {
return foundRange;
}
}
else {
+ // Decrement to search backwards.
+ if (foundRange.location == NSNotFound) {
+ index--;
+ }
+ // Jump over the former found location
+ // to avoid endless loop.
+ else {
+ index = index - 2;
+ }
searchIndex = 0;
foundRange.location = NSNotFound;
}
}
return foundRange;
}
+- (NSString*)stringValueWithEncoding:(NSStringEncoding)encoding {
+ return [[NSString alloc] initWithData:self encoding:encoding];
+}
+
@end
+
// -----------------------------------------------------------------------------
-// FileReader class.
+// FileReader implementation.
// -----------------------------------------------------------------------------
@@ -96,31 +154,36 @@ @implementation FileReader
*/
- (id)initWithFilePath:(NSString*)filePath {
-
self = [super init];
if (self != nil) {
+ if (!filePath || [filePath length] <= 0) {
+ return nil;
+ }
m_fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
if (m_fileHandle == nil) {
return nil;
}
+ // TODO: How can I use NSLineSeparatorCharacter instead of \n here?
m_lineDelimiter = [[NSString alloc] initWithString:@"\n"];
m_filePath = filePath;
m_currentOffset = 0ULL;
m_chunkSize = 10;
[m_fileHandle seekToEndOfFile];
m_totalFileLength = [m_fileHandle offsetInFile];
+ NSLog(@"%qu characters in %@", m_totalFileLength, [filePath lastPathComponent]); /* DEBUG LOG */
// We do not need to seek back since readLine will do that.
}
return self;
}
+
+
/**
Reads the file forwards.
@returns Another single line on each call or nil if the file end has been reached.
*/
- (NSString*)readLine {
-
if (m_currentOffset >= m_totalFileLength) {
return nil;
}
@@ -134,32 +197,83 @@ - (NSString*)readLine {
if (m_currentOffset >= m_totalFileLength) {
break;
}
- NSData* chunk = [m_fileHandle readDataOfLength:m_chunkSize];
+ NSLog(@"\t m_currentOffset: %qu", m_currentOffset); /* DEBUG LOG */
+ NSData* chunk = [m_fileHandle readDataOfLength:m_chunkSize]; // always length = 10
+ // Find the location and length of the next line delimiter.
NSRange newLineRange = [chunk rangeOfData:newLineData];
+ NSLog(@"\t\t newLineRange: %d (%d)", newLineRange.location, newLineRange.length); /* DEBUG LOG */
if (newLineRange.location != NSNotFound) {
// Include the length so we can include the delimiter in the string.
- chunk = [chunk subdataWithRange:NSMakeRange(0, newLineRange.location + [newLineData length])];
+ NSRange subDataRange = NSMakeRange(0, newLineRange.location + [newLineData length]);
+ NSLog(@"\t\t subDataRange: %d (%d)", subDataRange.location, subDataRange.length); /* DEBUG LOG */
+ chunk = [chunk subdataWithRange:subDataRange];
+ NSLog(@"\t\t\t\t chunk: %@", [chunk stringValueWithEncoding:NSUTF8StringEncoding]); /* DEBUG LOG */
shouldReadMore = NO;
}
[currentData appendData:chunk];
+ NSLog(@"\t\t currentData: %@", [currentData stringValueWithEncoding:NSUTF8StringEncoding]); /* DEBUG LOG */
m_currentOffset += [chunk length];
}
- NSString* line = [[NSString alloc] initWithData:currentData encoding:NSUTF8StringEncoding];
+ NSString* line = [currentData stringValueWithEncoding:NSUTF8StringEncoding];
return line;
}
+
+
+
+- (NSString*)readLineBackwards {
+
+ if (m_currentOffset >= m_totalFileLength) {
+ return nil;
+ }
+
+ NSData* newLineData = [m_lineDelimiter dataUsingEncoding:NSUTF8StringEncoding];
+ m_currentOffset = m_totalFileLength - m_chunkSize;
+ [m_fileHandle seekToFileOffset:m_currentOffset];
+ NSMutableData* currentData = [[NSMutableData alloc] init];
+ BOOL shouldReadMore = YES;
+
+ while (shouldReadMore) {
+// if (m_currentOffset >= m_totalFileLength) {
+// break;
+// }
+ NSLog(@"\t m_currentOffset: %qu", m_currentOffset); /* DEBUG LOG */
+ if (m_currentOffset <= 0ULL) {
+ break;
+ }
+ NSData* chunk = [m_fileHandle readDataOfLength:m_chunkSize];
+ NSLog(@"chunk: %@", [chunk stringValueWithEncoding:NSUTF8StringEncoding]); /* DEBUG LOG */
+ // Find the location and length of the next line delimiter.
+ NSRange newLineRange = [chunk rangeOfDataBackwardsSearch:newLineData];
+ NSLog(@"\t\t newLineRange: %d (%d)", newLineRange.location, newLineRange.length); /* DEBUG LOG */
+ if (newLineRange.location != NSNotFound) {
+ // Include the length so we can include the delimiter in the string.
+ NSUInteger chunkLength = newLineRange.location - [newLineData length];
+ chunk = [chunk subdataWithRange:NSMakeRange(m_currentOffset - chunkLength, chunkLength)];
+ shouldReadMore = NO;
+ }
+ [currentData appendData:chunk];
+ m_currentOffset -= [chunk length];
+ }
+
+ NSString* line = [[NSString alloc] initWithData:currentData encoding:NSUTF8StringEncoding];
+ return line;
+}
+
+
+
/**
Reads the file forwards while trimming white spaces.
@returns Another single line on each call or nil if the file end has been reached.
*/
- (NSString*)readTrimmedLine {
-
return [[self readLine] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
+
#if NS_BLOCKS_AVAILABLE
/**
Reads the file forwards using a block object.
@@ -177,3 +291,4 @@ - (void)enumerateLinesUsingBlock:(void(^)(NSString*, BOOL*))block {
@end
+
View
143 LineReader.xcodeproj/project.pbxproj
@@ -9,8 +9,11 @@
/* Begin PBXBuildFile section */
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
256AC3DA0F4B6AC300CF3369 /* LineReaderAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 256AC3D90F4B6AC300CF3369 /* LineReaderAppDelegate.m */; };
- 5548D857125B69B6001C6932 /* FileReader.m in Sources */ = {isa = PBXBuildFile; fileRef = 5548D856125B69B6001C6932 /* FileReader.m */; };
- 558DC637125B369600AF819E /* DirectoryReader.m in Sources */ = {isa = PBXBuildFile; fileRef = 558DC636125B369600AF819E /* DirectoryReader.m */; };
+ 557E504F125CD4350055E512 /* DirectoryReader.m in Sources */ = {isa = PBXBuildFile; fileRef = 557E504C125CD4350055E512 /* DirectoryReader.m */; };
+ 557E5050125CD4350055E512 /* FileReader.m in Sources */ = {isa = PBXBuildFile; fileRef = 557E504E125CD4350055E512 /* FileReader.m */; };
+ 557E508E125CD5B80055E512 /* NSDataExtensionsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 557E508C125CD5890055E512 /* NSDataExtensionsTest.m */; };
+ 55A52E1D125CDC7A00A9C721 /* FileReader.h in Sources */ = {isa = PBXBuildFile; fileRef = 557E504D125CD4350055E512 /* FileReader.h */; };
+ 55A52E1E125CDC7A00A9C721 /* FileReader.m in Sources */ = {isa = PBXBuildFile; fileRef = 557E504E125CD4350055E512 /* FileReader.m */; };
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
@@ -27,15 +30,25 @@
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
- 5548D855125B69B6001C6932 /* FileReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileReader.h; sourceTree = "<group>"; };
- 5548D856125B69B6001C6932 /* FileReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileReader.m; sourceTree = "<group>"; };
- 558DC635125B369600AF819E /* DirectoryReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DirectoryReader.h; sourceTree = "<group>"; };
- 558DC636125B369600AF819E /* DirectoryReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DirectoryReader.m; sourceTree = "<group>"; };
+ 557E504B125CD4350055E512 /* DirectoryReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DirectoryReader.h; sourceTree = "<group>"; };
+ 557E504C125CD4350055E512 /* DirectoryReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DirectoryReader.m; sourceTree = "<group>"; };
+ 557E504D125CD4350055E512 /* FileReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileReader.h; sourceTree = "<group>"; };
+ 557E504E125CD4350055E512 /* FileReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileReader.m; sourceTree = "<group>"; };
+ 557E5077125CD51D0055E512 /* NSDataExtensions.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NSDataExtensions.app; sourceTree = BUILT_PRODUCTS_DIR; };
+ 557E5079125CD51E0055E512 /* NSDataExtensions-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "NSDataExtensions-Info.plist"; sourceTree = "<group>"; };
+ 557E508C125CD5890055E512 /* NSDataExtensionsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSDataExtensionsTest.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* LineReader-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "LineReader-Info.plist"; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* LineReader.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LineReader.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
+ 557E5075125CD51D0055E512 /* Frameworks */ = {
+ isa = PBXFrameworksBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
8D11072E0486CEB800E47090 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
@@ -52,10 +65,11 @@
children = (
256AC3D80F4B6AC300CF3369 /* LineReaderAppDelegate.h */,
256AC3D90F4B6AC300CF3369 /* LineReaderAppDelegate.m */,
- 558DC635125B369600AF819E /* DirectoryReader.h */,
- 558DC636125B369600AF819E /* DirectoryReader.m */,
- 5548D855125B69B6001C6932 /* FileReader.h */,
- 5548D856125B69B6001C6932 /* FileReader.m */,
+ 557E504B125CD4350055E512 /* DirectoryReader.h */,
+ 557E504C125CD4350055E512 /* DirectoryReader.m */,
+ 557E504D125CD4350055E512 /* FileReader.h */,
+ 557E504E125CD4350055E512 /* FileReader.m */,
+ 557E508A125CD56B0055E512 /* Testing */,
);
name = Classes;
sourceTree = "<group>";
@@ -82,6 +96,7 @@
isa = PBXGroup;
children = (
8D1107320486CEB800E47090 /* LineReader.app */,
+ 557E5077125CD51D0055E512 /* NSDataExtensions.app */,
);
name = Products;
sourceTree = "<group>";
@@ -111,6 +126,7 @@
isa = PBXGroup;
children = (
8D1107310486CEB800E47090 /* LineReader-Info.plist */,
+ 557E5079125CD51E0055E512 /* NSDataExtensions-Info.plist */,
089C165CFE840E0CC02AAC07 /* InfoPlist.strings */,
1DDD58140DA1D0A300B32029 /* MainMenu.xib */,
);
@@ -126,9 +142,35 @@
name = Frameworks;
sourceTree = "<group>";
};
+ 557E508A125CD56B0055E512 /* Testing */ = {
+ isa = PBXGroup;
+ children = (
+ 557E508C125CD5890055E512 /* NSDataExtensionsTest.m */,
+ );
+ name = Testing;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
+ 557E5076125CD51D0055E512 /* NSDataExtensions */ = {
+ isa = PBXNativeTarget;
+ buildConfigurationList = 557E507C125CD51E0055E512 /* Build configuration list for PBXNativeTarget "NSDataExtensions" */;
+ buildPhases = (
+ 557E5073125CD51D0055E512 /* Resources */,
+ 557E5074125CD51D0055E512 /* Sources */,
+ 557E5075125CD51D0055E512 /* Frameworks */,
+ );
+ buildRules = (
+ );
+ comments = "Test for NSDataExtensions Backwards search.";
+ dependencies = (
+ );
+ name = NSDataExtensions;
+ productName = NSDataExtensions;
+ productReference = 557E5077125CD51D0055E512 /* NSDataExtensions.app */;
+ productType = "com.apple.product-type.application";
+ };
8D1107260486CEB800E47090 /* LineReader */ = {
isa = PBXNativeTarget;
buildConfigurationList = C01FCF4A08A954540054247B /* Build configuration list for PBXNativeTarget "LineReader" */;
@@ -163,11 +205,19 @@
projectRoot = "";
targets = (
8D1107260486CEB800E47090 /* LineReader */,
+ 557E5076125CD51D0055E512 /* NSDataExtensions */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
+ 557E5073125CD51D0055E512 /* Resources */ = {
+ isa = PBXResourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
8D1107290486CEB800E47090 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
@@ -180,14 +230,24 @@
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
+ 557E5074125CD51D0055E512 /* Sources */ = {
+ isa = PBXSourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 55A52E1D125CDC7A00A9C721 /* FileReader.h in Sources */,
+ 55A52E1E125CDC7A00A9C721 /* FileReader.m in Sources */,
+ 557E508E125CD5B80055E512 /* NSDataExtensionsTest.m in Sources */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
8D11072C0486CEB800E47090 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8D11072D0486CEB800E47090 /* main.m in Sources */,
256AC3DA0F4B6AC300CF3369 /* LineReaderAppDelegate.m in Sources */,
- 558DC637125B369600AF819E /* DirectoryReader.m in Sources */,
- 5548D857125B69B6001C6932 /* FileReader.m in Sources */,
+ 557E504F125CD4350055E512 /* DirectoryReader.m in Sources */,
+ 557E5050125CD4350055E512 /* FileReader.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -213,6 +273,56 @@
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
+ 557E507A125CD51E0055E512 /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ALWAYS_SEARCH_USER_PATHS = NO;
+ COPY_PHASE_STRIP = NO;
+ GCC_DYNAMIC_NO_PIC = NO;
+ GCC_ENABLE_FIX_AND_CONTINUE = YES;
+ GCC_ENABLE_OBJC_GC = required;
+ GCC_MODEL_TUNING = G5;
+ GCC_OPTIMIZATION_LEVEL = 0;
+ GCC_PRECOMPILE_PREFIX_HEADER = YES;
+ GCC_PREFIX_HEADER = "$(SYSTEM_LIBRARY_DIR)/Frameworks/AppKit.framework/Headers/AppKit.h";
+ INFOPLIST_FILE = "NSDataExtensions-Info.plist";
+ INSTALL_PATH = "$(HOME)/Applications";
+ OTHER_LDFLAGS = (
+ "-framework",
+ Foundation,
+ "-framework",
+ AppKit,
+ );
+ PREBINDING = NO;
+ PRODUCT_NAME = NSDataExtensions;
+ };
+ name = Debug;
+ };
+ 557E507B125CD51E0055E512 /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ALWAYS_SEARCH_USER_PATHS = NO;
+ COPY_PHASE_STRIP = YES;
+ DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+ GCC_ENABLE_FIX_AND_CONTINUE = NO;
+ GCC_ENABLE_OBJC_GC = required;
+ GCC_MODEL_TUNING = G5;
+ GCC_PRECOMPILE_PREFIX_HEADER = YES;
+ GCC_PREFIX_HEADER = "$(SYSTEM_LIBRARY_DIR)/Frameworks/AppKit.framework/Headers/AppKit.h";
+ INFOPLIST_FILE = "NSDataExtensions-Info.plist";
+ INSTALL_PATH = "$(HOME)/Applications";
+ OTHER_LDFLAGS = (
+ "-framework",
+ Foundation,
+ "-framework",
+ AppKit,
+ );
+ PREBINDING = NO;
+ PRODUCT_NAME = NSDataExtensions;
+ ZERO_LINK = NO;
+ };
+ name = Release;
+ };
C01FCF4B08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
@@ -273,6 +383,15 @@
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
+ 557E507C125CD51E0055E512 /* Build configuration list for PBXNativeTarget "NSDataExtensions" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 557E507A125CD51E0055E512 /* Debug */,
+ 557E507B125CD51E0055E512 /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
C01FCF4A08A954540054247B /* Build configuration list for PBXNativeTarget "LineReader" */ = {
isa = XCConfigurationList;
buildConfigurations = (
View
8 LineReaderAppDelegate.h
@@ -9,13 +9,15 @@
#import <Cocoa/Cocoa.h>
@interface LineReaderAppDelegate : NSObject <NSApplicationDelegate> {
- NSWindow* window;
+ NSWindow* m_window;
IBOutlet NSTextField* m_sourcePath;
- IBOutlet NSTableView* m_tableView;
+ NSNumber* m_maxNumLines;
NSArray* m_directoryListing;
}
-@property (assign) IBOutlet NSWindow *window;
+@property (assign) IBOutlet NSWindow* window;
+@property (readwrite, assign) NSString* sourcePath;
+@property (readwrite, assign) NSNumber* maxNumLines;
- (IBAction)sourcePathChanged:(id)sender;
View
62 LineReaderAppDelegate.m
@@ -11,16 +11,43 @@
#import "FileReader.h"
-
/**
Application delegate.
*/
@implementation LineReaderAppDelegate
-@synthesize window;
+/**
+ Initializes an application delegate object.
+ @returns An initialized LineReaderAppDelegate object or nil if the object could not be created.
+ */
+- (id)init {
+
+ self = [super init];
+ if (self != nil) {
+ m_maxNumLines = [NSNumber numberWithInt:3];
+ [self setSourcePath:[NSString stringWithFormat:@"/tmp/"]];
+ }
+ return self;
+}
+
+
+@synthesize window = m_window;
+@synthesize maxNumLines = m_maxNumLines;
+@dynamic sourcePath;
+- (NSString*)sourcePath {
+ return [m_sourcePath stringValue];
+}
+- (void)setSourcePath:(NSString*)sourcePath {
+ m_sourcePath = [[NSTextField alloc] init];
+ if (!sourcePath || [sourcePath length] <= 0) {
+ [m_sourcePath setStringValue:@""];
+ return;
+ }
+ [m_sourcePath setStringValue:sourcePath];
+}
/**
Sent by the default notification center after the application
@@ -38,22 +65,31 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
@param sender The object calling this method.
*/
- (IBAction)sourcePathChanged:(id)sender {
+
+ DirectoryReader* directoryReader = [[DirectoryReader alloc] initWithPath:[m_sourcePath stringValue]];
+ if (!directoryReader) {
+ return;
+ }
+
+ if ([directoryReader readDirectory:&m_directoryListing]) {
- DirectoryReader* dr = [[DirectoryReader alloc] initWithPath:[m_sourcePath stringValue]];
- if ([dr readDirectory:&m_directoryListing]) {
- NSLog(@"%@",m_directoryListing); /* DEBUG LOG */
-
for (NSString* path in m_directoryListing) {
+ NSLog(@"File: %@", path); /* DEBUG LOG */
+ int numLine = 0;
+ FileReader* fileReader = [[FileReader alloc] initWithFilePath:path];
+ if (!fileReader) {
+ return;
+ }
- int numLines = 0;
-
- FileReader* fr = [[FileReader alloc] initWithFilePath:path];
NSString* line = nil;
- while (numLines <= 5 && (line = [fr readTrimmedLine])) {
- NSLog(@"%@",line); /* DEBUG LOG */
- numLines++;
+ while (line = [fileReader readLine]) {
+ numLine++;
+ NSLog(@"------------------ %2.d: %@", numLine, line); /* DEBUG LOG */
+ if (numLine >= [m_maxNumLines intValue]) {
+ break;
+ }
}
- }
+ }
}
}
View
28 NSDataExtensions-Info.plist
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1</string>
+ <key>LSMinimumSystemVersion</key>
+ <string>${MACOSX_DEPLOYMENT_TARGET}</string>
+ <key>NSMainNibFile</key>
+ <string>MainMenu</string>
+ <key>NSPrincipalClass</key>
+ <string>NSApplication</string>
+</dict>
+</plist>
View
41 NSDataExtensionsTest.m
@@ -0,0 +1,41 @@
+//
+// NSDataExtensionsTest.m
+// LineReader
+//
+// Created by Tobias Preuss on 06.10.10.
+// Copyright 2010 Tobias Preuss. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+#import "FileReader.h"
+
+
+int main()
+
+{
+ NSString* contents = @"one\ntwo\nthree\none\ntwo\nthree";
+ NSString* delim = @"o\n";
+
+ // -----------------------------------------------------------------------------
+ // Test using NSString objects.
+ // -----------------------------------------------------------------------------
+
+ NSRange r1 = [contents rangeOfString:delim];
+ NSLog(@"NSString forwards: \t%3.d (%d)", r1.location, r1.length); /* DEBUG LOG */
+ NSRange r2 = [contents rangeOfString:delim options:NSBackwardsSearch];
+ NSLog(@"NSString backwards: \t%3.d (%d)", r2.location, r2.length); /* DEBUG LOG */
+
+
+ // -----------------------------------------------------------------------------
+ // Test using NSData objects.
+ // -----------------------------------------------------------------------------
+
+ NSData* delimData = [delim dataUsingEncoding:NSUTF8StringEncoding];
+ NSData* contentsData = [contents dataUsingEncoding:NSUTF8StringEncoding];
+
+ NSRange r3 = [contentsData rangeOfData:delimData];
+ NSLog(@"NSData forwards: \t%3.d (%d)", r3.location, r3.length); /* DEBUG LOG */
+
+ NSRange r4 = [contentsData rangeOfDataBackwardsSearch:delimData];
+ NSLog(@"NSData backwards: \t%3.d (%d)", r4.location, r4.length); /* DEBUG LOG */
+}

0 comments on commit 47b6ee2

Please sign in to comment.
Something went wrong with that request. Please try again.