forked from johnjohndoe/LineReader
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileReader.m
223 lines (178 loc) · 5.96 KB
/
FileReader.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// FileReader.m
// LineReader
//
// Created by Tobias Preuss on 05.10.10.
// Copyright 2010 Tobias Preuss. All rights reserved.
//
// Originally written by Dave DeLong,
// Source: http://stackoverflow.com/questions/3707427#3711079
#import "FileReader.h"
#import "NSDataExtensions.h"
/**
A file reader.
Files can be read forwards or backwards by calling the
corresponding function multiple times.
*/
@implementation FileReader
//- (void)dealloc{
// [m_fileHandle release], m_fileHandle = nil;
// [super dealloc];
//}
/**
Initialized a file reader object.
@param filePath A file path.
@returns An initialized FileReader object or nil if the object could not be created.
*/
- (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];
m_currentInset = m_totalFileLength;
m_prevDelimiterRange = NSMakeRange(m_currentInset, 1);
NSLog(@"%qu characters in %@", m_totalFileLength, [filePath lastPathComponent]); /* DEBUG LOG */
}
return self;
}
/**
Reads the file forwards.
Empty lines are not returned.
@returns Another single line on each call or nil if the file end has been reached.
*/
- (NSString*)readLine {
if (m_totalFileLength == 0 || m_currentOffset >= m_totalFileLength) {
return nil;
}
NSData* newLineData = [m_lineDelimiter dataUsingEncoding:NSUTF8StringEncoding];
[m_fileHandle seekToFileOffset:m_currentOffset];
NSMutableData* currentData = [[NSMutableData alloc] init];
BOOL shouldReadMore = YES;
while (shouldReadMore) {
if (m_currentOffset >= m_totalFileLength) {
break;
}
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];
if (newLineRange.location != NSNotFound) {
// Include the length so we can include the delimiter in the string.
NSRange subDataRange = NSMakeRange(0, newLineRange.location + [newLineData length]);
chunk = [chunk subdataWithRange:subDataRange];
shouldReadMore = NO;
}
[currentData appendData:chunk];
m_currentOffset += [chunk length];
}
NSString* line = [currentData stringValueWithEncoding:NSUTF8StringEncoding];
// finished with data
// [currentData release], currentData = nil;
return line;
}
/**
Reads the file backwards.
Empty lines are returned as well.
@returns Another single line on each call or nil if the file end has been reached.
*/
- (NSString*)readLineBackwards {
if (m_totalFileLength == 0 || (m_currentInset == 0 && m_chunkSize == 0)) {
return nil;
}
NSData* newLineData = [m_lineDelimiter dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger currentChunkSize = m_chunkSize;
// Process block smaller than standard chunk size.
// Shrink current chunk size.
// Shift inset to zero.
if (m_currentInset < m_chunkSize) {
currentChunkSize = m_currentInset;
m_currentInset -= currentChunkSize;
}
// Process blocks of chunk size.
// Shift inset by standard chunk size.
if (m_currentInset >= m_chunkSize && m_currentInset <= m_totalFileLength) {
m_currentInset -= m_chunkSize;
}
[m_fileHandle seekToFileOffset:m_currentInset];
NSMutableData* currentData = [[NSMutableData alloc] init];
BOOL shouldReadMore = YES;
while (shouldReadMore) {
if (m_currentInset == NSNotFound) {
break;
}
NSData* chunk = [m_fileHandle readDataOfLength:currentChunkSize];
NSRange newLineRange = [chunk rangeOfDataBackwardsSearch:newLineData];
m_prevDelimiterRange = NSMakeRange(m_currentInset + newLineRange.location, newLineRange.length);
if (newLineRange.location == NSNotFound) {
m_prevDelimiterRange = NSMakeRange(NSNotFound, 0);
}
if (newLineRange.location != NSNotFound) {
NSUInteger subDataLoc = newLineRange.location + [newLineData length];
NSUInteger subDataLen = currentChunkSize - subDataLoc;
chunk = [chunk subdataWithRange:NSMakeRange(subDataLoc, subDataLen)];
shouldReadMore = NO;
}
[currentData prepend:chunk];
if (m_prevDelimiterRange.location == NSNotFound) {
// Process block smaller than standard chunk size.
// Shrink current chunk size.
// Shift inset to zero.
// Break while loop if front has been reached.
if (m_currentInset < currentChunkSize) {
currentChunkSize = m_currentInset;
m_currentInset -= currentChunkSize;
if (currentChunkSize == 0 && m_currentInset == 0) {
m_chunkSize = 0;
break;
}
}
// Process blocks of chunk size.
// Shift inset by standard chunk size.
else {
currentChunkSize = m_chunkSize;
m_currentInset -= currentChunkSize;
}
[m_fileHandle seekToFileOffset:m_currentInset];
}
// Shift inset to last found position.
else {
m_currentInset = m_prevDelimiterRange.location;
}
} // End if while.
NSString* line = [[NSString alloc] initWithData:currentData encoding:NSUTF8StringEncoding];
// finished with data
// [currentData release], currentData = nil;
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.
@param block
*/
- (void)enumerateLinesUsingBlock:(void(^)(NSString*, BOOL*))block {
NSString* line = nil;
BOOL stop = NO;
while (stop == NO && (line = [self readLine])) {
block(line, &stop);
}
}
#endif
@end