-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTFileReader.m
64 lines (53 loc) · 1.26 KB
/
BTFileReader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// BTFileReader.m
// BTToolkit
//
// Created by Thaddeus on 3/7/14.
// Copyright (c) 2014 Bluetoo. All rights reserved.
//
#import "BTFileReader.h"
#import "BTARCHelpers.h"
@interface BTFileReader ()
@property (nonatomic, retain) NSFileHandle *fileHandle;
@property (nonatomic, assign) BOOL isEndOfFile;
@end
@implementation BTFileReader
- (id)initWithFilePath:(NSString *)filePath
{
self = [super init];
if(self)
{
self.isEndOfFile = NO;
self.fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
}
return self;
}
- (void)dealloc
{
self.fileHandle = nil;
BT_SUPER_DEALLOC;
}
- (NSString *)readLine
{
if(!self.isEndOfFile)
{
NSMutableData *working = [NSMutableData data];
for(;;)
{
NSData *byte = [self.fileHandle readDataOfLength:1];
if(byte.length == 0)
{
self.isEndOfFile = YES;
break;
}
const char *bit = (const char *)[byte bytes];
if(bit[0] =='\n')
break;
else
[working appendData:byte];
}
return [[NSString alloc] initWithData:working encoding:NSUTF8StringEncoding];
}
return nil;
}
@end