Skip to content

Commit

Permalink
NSFileHandle+readLine
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakey authored and Jakey committed Jul 9, 2015
1 parent fee18d6 commit 1713d6a
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Categories/Foundation/NSFileHandle/NSFileHandle+readLine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSFileHandle+readLine.h
//
// Created by Ethan Horger on 11/27/12.
// Copyright (c) 2012 Ethan Horger. All rights reserved.
//
// A Cocoa / Objective-C NSFileHandle category that adds the ability to read a file line by line.
//https://github.com/arbalest/NSFileHandle-readLine
#import <Foundation/Foundation.h>

@interface NSFileHandle (readLine)

- (NSData *)readLineWithDelimiter:(NSString *)theDelimier;

@end
93 changes: 93 additions & 0 deletions Categories/Foundation/NSFileHandle/NSFileHandle+readLine.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// NSFileHandle+readLine.m
//
// Created by Ethan Horger on 11/27/12.
// Copyright (c) 2012 Ethan Horger. All rights reserved.
//

#import "NSFileHandle+readLine.h"

@implementation NSFileHandle (readLine)

- (NSData *)readLineWithDelimiter:(NSString *)theDelimiter
{
NSUInteger bufferSize = 1024; // Set our buffer size

// Read the delimiter string into a C string
NSData *delimiterData = [theDelimiter dataUsingEncoding:NSASCIIStringEncoding];
const char *delimiter = [delimiterData bytes];

NSUInteger delimiterIndex = 0;

NSData *lineData; // Our buffer of data

unsigned long long currentPosition = [self offsetInFile];
NSUInteger positionOffset = 0;

BOOL hasData = YES;
BOOL lineBreakFound = NO;

while (lineBreakFound == NO && hasData == YES)
{
// Fill our buffer with data
lineData = [self readDataOfLength:bufferSize];

// If our buffer gets some data, proceed
if ([lineData length] > 0)
{
// Get a pointer to our buffer's raw data
const char *buffer = [lineData bytes];

// Loop over the raw data, byte-by-byte
for (int i = 0; i < [lineData length]; i++)
{
// If the current character matches a character in the delimiter sequence...
if (buffer[i] == delimiter[delimiterIndex])
{
delimiterIndex++; // Move to the next char of the delimiter sequence

if (delimiterIndex >= [delimiterData length])
{
// If we've found all of the delimiter characters, break out of the loop
lineBreakFound = YES;
positionOffset += i + 1;
break;
}
}
else
{
// Otherwise, reset the current delimiter character offset
delimiterIndex = 0;
}
}

if (lineBreakFound == NO)
{
positionOffset += [lineData length];
}
}
else
{
hasData = NO;
break;
}
}

// Use positionOffset to determine the string to return...

// Return to the start of this line
[self seekToFileOffset:currentPosition];

NSData *returnData = [self readDataOfLength:positionOffset];

if ([returnData length] > 0)
{
return returnData;
}
else
{
return nil;
}
}

@end
13 changes: 13 additions & 0 deletions Demos/Foundation/NSFileHandle/NSFileHandleDemoViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// NSFileHandleDemoViewController.h
// IOS-Categories
//
// Created by Jakey on 15/7/9.
// Copyright © 2015年 www.skyfox.org. All rights reserved.
//

#import "BaseViewController.h"

@interface NSFileHandleDemoViewController : BaseViewController

@end
55 changes: 55 additions & 0 deletions Demos/Foundation/NSFileHandle/NSFileHandleDemoViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// NSFileHandleDemoViewController.m
// IOS-Categories
//
// Created by Jakey on 15/7/9.
// Copyright © 2015年 www.skyfox.org. All rights reserved.
//

#import "NSFileHandleDemoViewController.h"
#import "NSFileHandle+readLine.h"
@interface NSFileHandleDemoViewController ()

@end

@implementation NSFileHandleDemoViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSMutableArray *linesFound = [[NSMutableArray alloc] init];

// Open our demo file

NSString *demoFilePath = [[NSBundle mainBundle] pathForResource:@"NSFileHandleSampleFile" ofType:@"txt"];
NSFileHandle *demoFileHandle = [NSFileHandle fileHandleForReadingAtPath:demoFilePath];

// Use readLineWithDelimiter to fill our NSTableView with each line found

NSData *lineData;

while ((lineData = [demoFileHandle readLineWithDelimiter:@"\n"]))
{
NSString *lineString = [[NSString alloc] initWithData:lineData encoding:NSASCIIStringEncoding];
NSLog(@"lineString:%@",lineString);

[linesFound addObject:lineString];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
19 changes: 19 additions & 0 deletions Demos/Foundation/NSFileHandle/NSFileHandleDemoViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="NSFileHandleDemoViewController">
<connections>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</objects>
</document>
4 changes: 4 additions & 0 deletions Demos/Foundation/NSFileHandle/NSFileHandleSampleFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The quick yellow Pikachu
jumped over the lazy Snorlax
while the thirsty Lotad
carried water on his back!
36 changes: 36 additions & 0 deletions IOS-Categories.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
A202D9E31B10400100EAB199 /* BaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A202D9DF1B10400100EAB199 /* BaseViewController.m */; };
A202D9E41B10400100EAB199 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A202D9E11B10400100EAB199 /* RootViewController.m */; };
A202D9E51B10400100EAB199 /* RootViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A202D9E21B10400100EAB199 /* RootViewController.xib */; };
A20A5A671B4E27EA004E2474 /* NSFileHandle+readLine.m in Sources */ = {isa = PBXBuildFile; fileRef = A20A5A661B4E27EA004E2474 /* NSFileHandle+readLine.m */; };
A20A5A6C1B4E2886004E2474 /* NSFileHandleDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A20A5A6A1B4E2886004E2474 /* NSFileHandleDemoViewController.m */; };
A20A5A6D1B4E2886004E2474 /* NSFileHandleDemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A20A5A6B1B4E2886004E2474 /* NSFileHandleDemoViewController.xib */; };
A20A5A6F1B4E28CE004E2474 /* NSFileHandleSampleFile.txt in Resources */ = {isa = PBXBuildFile; fileRef = A20A5A6E1B4E28CE004E2474 /* NSFileHandleSampleFile.txt */; };
A218D8D71A479E9C00AB83CA /* UIColor+Web.m in Sources */ = {isa = PBXBuildFile; fileRef = A218D8D61A479E9C00AB83CA /* UIColor+Web.m */; };
A218D8DF1A47A25100AB83CA /* UIWebView+Canvas.m in Sources */ = {isa = PBXBuildFile; fileRef = A218D8DC1A47A25100AB83CA /* UIWebView+Canvas.m */; };
A218D8E01A47A25100AB83CA /* UIWebView+JS.m in Sources */ = {isa = PBXBuildFile; fileRef = A218D8DE1A47A25100AB83CA /* UIWebView+JS.m */; };
Expand Down Expand Up @@ -402,6 +406,12 @@
A202D9E01B10400100EAB199 /* RootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = "<group>"; };
A202D9E11B10400100EAB199 /* RootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
A202D9E21B10400100EAB199 /* RootViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RootViewController.xib; sourceTree = "<group>"; };
A20A5A651B4E27EA004E2474 /* NSFileHandle+readLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFileHandle+readLine.h"; sourceTree = "<group>"; };
A20A5A661B4E27EA004E2474 /* NSFileHandle+readLine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileHandle+readLine.m"; sourceTree = "<group>"; };
A20A5A691B4E2886004E2474 /* NSFileHandleDemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSFileHandleDemoViewController.h; sourceTree = "<group>"; };
A20A5A6A1B4E2886004E2474 /* NSFileHandleDemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSFileHandleDemoViewController.m; sourceTree = "<group>"; };
A20A5A6B1B4E2886004E2474 /* NSFileHandleDemoViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NSFileHandleDemoViewController.xib; sourceTree = "<group>"; };
A20A5A6E1B4E28CE004E2474 /* NSFileHandleSampleFile.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = NSFileHandleSampleFile.txt; sourceTree = "<group>"; };
A218D8D51A479E9C00AB83CA /* UIColor+Web.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIColor+Web.h"; sourceTree = "<group>"; };
A218D8D61A479E9C00AB83CA /* UIColor+Web.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+Web.m"; sourceTree = "<group>"; };
A218D8DB1A47A25100AB83CA /* UIWebView+Canvas.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+Canvas.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1149,6 +1159,26 @@
path = Controller;
sourceTree = "<group>";
};
A20A5A641B4E27C6004E2474 /* NSFileHandle */ = {
isa = PBXGroup;
children = (
A20A5A651B4E27EA004E2474 /* NSFileHandle+readLine.h */,
A20A5A661B4E27EA004E2474 /* NSFileHandle+readLine.m */,
);
path = NSFileHandle;
sourceTree = "<group>";
};
A20A5A681B4E287B004E2474 /* NSFileHandle */ = {
isa = PBXGroup;
children = (
A20A5A6E1B4E28CE004E2474 /* NSFileHandleSampleFile.txt */,
A20A5A691B4E2886004E2474 /* NSFileHandleDemoViewController.h */,
A20A5A6A1B4E2886004E2474 /* NSFileHandleDemoViewController.m */,
A20A5A6B1B4E2886004E2474 /* NSFileHandleDemoViewController.xib */,
);
path = NSFileHandle;
sourceTree = "<group>";
};
A21CF17B1B38F610006415E0 /* NSHTTPCookieStorage */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1255,6 +1285,7 @@
A22725F11A3E736D0061605B /* Foundation */ = {
isa = PBXGroup;
children = (
A20A5A641B4E27C6004E2474 /* NSFileHandle */,
A21CF17B1B38F610006415E0 /* NSHTTPCookieStorage */,
A2EF56EB1B21D0800005F730 /* NSNotificationCenter */,
A202D99F1B102A3B00EAB199 /* NSNumber */,
Expand Down Expand Up @@ -1423,6 +1454,7 @@
A281FB4D1AC8101F009040DA /* Foundation */ = {
isa = PBXGroup;
children = (
A20A5A681B4E287B004E2474 /* NSFileHandle */,
A21CF17C1B38F61F006415E0 /* NSHTTPCookieStorage */,
A2EF56EF1B21D1170005F730 /* NSNotificationCenter */,
A202D9A31B102D6600EAB199 /* NSNumber */,
Expand Down Expand Up @@ -2684,6 +2716,7 @@
A202D9A81B102D7800EAB199 /* NSNumberDemoViewController.xib in Resources */,
A21F34CE1B0F248D00D73A91 /* UINavigationItemDemoViewController.xib in Resources */,
A284C14A1AEBCFB000D90ED5 /* NSIndexPathDemoViewController.xib in Resources */,
A20A5A6F1B4E28CE004E2474 /* NSFileHandleSampleFile.txt in Resources */,
A281FC0E1AC813EA009040DA /* UIWindowDemoViewController.xib in Resources */,
A202D9811B0F7CD100EAB199 /* CLLocationDemoViewController.xib in Resources */,
A202D9D91B103F9400EAB199 /* LaunchScreen.xib in Resources */,
Expand All @@ -2697,6 +2730,7 @@
A281FBA91AC81299009040DA /* NSSetDemoViewController.xib in Resources */,
A202D99B1B0F916900EAB199 /* UIControlDemoViewController.xib in Resources */,
A21F34C91B0F247800D73A91 /* UIScreenDemoViewController.xib in Resources */,
A20A5A6D1B4E2886004E2474 /* NSFileHandleDemoViewController.xib in Resources */,
A284C1261AEBC19C00D90ED5 /* UINavigationBarDemoViewController.xib in Resources */,
A284C1111AEBBCFC00D90ED5 /* NSDateTimeAgo.bundle in Resources */,
A284C1081AEB944B00D90ED5 /* UITableViewDemoViewController.xib in Resources */,
Expand Down Expand Up @@ -2866,6 +2900,7 @@
A284C0FE1AEB908100D90ED5 /* UIBezierPath+BasicShapes.m in Sources */,
A21CF1801B38F631006415E0 /* NSHTTPCookieStorageDemoViewController.m in Sources */,
A28BE38E1A3ED5A4005C4AC6 /* UITextField+Blocks.m in Sources */,
A20A5A671B4E27EA004E2474 /* NSFileHandle+readLine.m in Sources */,
A2EF56CC1B21C41C0005F730 /* NSManagedObject+Extensions.m in Sources */,
A281FC171AC81427009040DA /* NSUserDefaultsDemoViewController.m in Sources */,
A28BE3591A3EAA52005C4AC6 /* NSSet+Block.m in Sources */,
Expand Down Expand Up @@ -2927,6 +2962,7 @@
A2958CBA1B357AEF00D7AA0F /* UIViewController+StoreKit.m in Sources */,
A238E0581A4555F400383A97 /* NSObject+Property.m in Sources */,
A284C12A1AEBCB7F00D90ED5 /* NSIndexPath+Offset.m in Sources */,
A20A5A6C1B4E2886004E2474 /* NSFileHandleDemoViewController.m in Sources */,
A284C1251AEBC19C00D90ED5 /* UINavigationBarDemoViewController.m in Sources */,
A219B8B71A615CA100135327 /* UIView+Screenshot.m in Sources */,
A284C13F1AEBCF9600D90ED5 /* NSDateFormatterDemoViewController.m in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions IOS-Categories/Controller/RootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ - (void)viewDidLoad {
@"NSDateFormatter",
@"NSNumber",
@"NSNotificationCenter",
@"NSHTTPCookieStorage"
@"NSHTTPCookieStorage",
@"NSFileHandle"
],

@"CoreLocation":@[@"CALayer",
Expand All @@ -87,6 +88,7 @@ - (void)viewDidLoad {
@"MapKit":@[@"MKMapView"
]
};
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.tableView reloadData];
}

Expand All @@ -103,7 +105,6 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

// Configure the cell...
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ IOS中的各种Objective-C Category, a collection of useful Objective-C Categori
* NSURL
* NSUserDefaults
* NSHTTPCookieStorage
* NSFileHandle

## UIKit
* UIAlertView
Expand Down

0 comments on commit 1713d6a

Please sign in to comment.