-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathLineReaderAppDelegate.m
178 lines (140 loc) · 4.49 KB
/
LineReaderAppDelegate.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
//
// LineReaderAppDelegate.m
// LineReader
//
// Created by Tobias Preuss on 05.10.10.
// Copyright 2010 Tobias Preuss. All rights reserved.
//
#import "LineReaderAppDelegate.h"
#import "DirectoryReader.h"
#import "FileReader.h"
/**
An application delegate.
*/
@implementation LineReaderAppDelegate
/**
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_sourcePath = [NSString stringWithFormat:@"/tmp/"];
m_maxNumLines = [NSNumber numberWithInt:3];
m_selectedReadMode = [NSNumber numberWithInt:BACKWARDS];
m_printLines = [NSNumber numberWithBool:NO];
m_status = [NSString stringWithFormat:@"Application started."];
m_directoryListing = nil;
}
return self;
}
// -----------------------------------------------------------------------------
// Properties.
// -----------------------------------------------------------------------------
@synthesize window = m_window;
@synthesize maxNumLines = m_maxNumLines;
@synthesize sourcePath = m_sourcePath;
@synthesize selectedReadMode = m_selectedReadMode;
@synthesize printLines = m_printLines;
@synthesize status = m_status;
// -----------------------------------------------------------------------------
// Event functions.
// -----------------------------------------------------------------------------
/**
Sent by the default notification center after the application
has been launched and initialized but before it has received
its first event.
@param aNotification A notification named NSApplicationDidFinishLaunchingNotification.
*/
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
}
/**
The function is called whenever lines should be read.
@param sender The object calling this method.
*/
- (IBAction)readLinesRequested:(id)sender {
[self processSource];
}
// -----------------------------------------------------------------------------
// Private functions.
// -----------------------------------------------------------------------------
/**
Reads a various number of lines from multiple files as
found in the source path. The lines can be read forwards
or backwards from the file.
*/
- (void)processSource {
int lineCount;
NSTimeInterval processingStarted = [NSDate timeIntervalSinceReferenceDate];
DirectoryReader* directoryReader = [[DirectoryReader alloc] initWithPath:m_sourcePath];
if (!directoryReader) {
return;
}
if ([directoryReader readDirectory:&m_directoryListing]) {
for (NSString* path in m_directoryListing) {
NSLog(@"File: %@", path); /* DEBUG LOG */
lineCount = 0;
FileReader* fileReader = [[FileReader alloc] initWithFilePath:path];
if (!fileReader) {
return;
}
NSString* line = nil;
if ([m_printLines boolValue]) {
// Print lines to console.
switch ([m_selectedReadMode intValue]) {
case FORWARDS:
while ((line = [fileReader readLine])) {
lineCount++;
NSLog(@"%3.d: %@", lineCount, line);
if (lineCount >= [m_maxNumLines intValue]) {
break;
}
}
break;
case BACKWARDS:
while ((line = [fileReader readLineBackwards])) {
lineCount++;
NSLog(@"%3.d: %@", lineCount, line);
if (lineCount >= [m_maxNumLines intValue]) {
break;
}
}
break;
default:
NSLog(@"Warning: Read mode not set correctly."); /* DEBUG LOG */
break;
}
}
else {
// Do not print lines to console.
switch ([m_selectedReadMode intValue]) {
case FORWARDS:
while ((line = [fileReader readLine])) {
lineCount++;
if (lineCount >= [m_maxNumLines intValue]) {
break;
}
}
break;
case BACKWARDS:
while ((line = [fileReader readLineBackwards])) {
lineCount++;
if (lineCount >= [m_maxNumLines intValue]) {
break;
}
}
break;
default:
NSLog(@"Warning: Read mode not set correctly."); /* DEBUG LOG */
break;
}
}
}
}
NSTimeInterval processingEnded = [NSDate timeIntervalSinceReferenceDate];
if ([m_selectedReadMode intValue] == FORWARDS)
self.status = [NSString stringWithFormat:@"Processing %d lines forwards took %f seconds.", lineCount, (processingEnded - processingStarted)];
else
self.status = [NSString stringWithFormat:@"Processing %d lines backwards took %f seconds.", lineCount, (processingEnded - processingStarted)];
}
@end