forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakey
authored and
Jakey
committed
Jul 9, 2015
1 parent
fee18d6
commit 1713d6a
Showing
9 changed files
with
239 additions
and
2 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
Categories/Foundation/NSFileHandle/NSFileHandle+readLine.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
93
Categories/Foundation/NSFileHandle/NSFileHandle+readLine.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Demos/Foundation/NSFileHandle/NSFileHandleDemoViewController.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
Demos/Foundation/NSFileHandle/NSFileHandleDemoViewController.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
Demos/Foundation/NSFileHandle/NSFileHandleDemoViewController.xib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters