-
Notifications
You must be signed in to change notification settings - Fork 2
/
TaskObject.m
349 lines (259 loc) · 9.44 KB
/
TaskObject.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//
// TaskObject.m
// CronniX
//
// Created by Sven A. Schmidt on Mon Dec 31 2001.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//
#import "TaskObject.h"
#import "Crontab.h"
#import "SasString.h"
#import "CrInfoCommentLine.h"
@implementation TaskObject
- (id)init {
return [ self initWithString: nil ];
}
- (id)initWithString:(NSString *)string forSystem: (BOOL)isSystemCrontab {
[super init];
task = [[ NSMutableDictionary alloc ] init ];
isSystemCrontabTask = isSystemCrontab;
NS_DURING
[ self parseString: string ];
NS_HANDLER
NSLog( @"Error parsing line: %@", string );
NS_ENDHANDLER
return self;
}
- (id)initWithString:(NSString *)string {
return [ self initWithString: string forSystem: NO ];
}
- (id)initWithTask:(id)aTask {
return [ aTask copy ];
}
+ (id)taskWithString:(NSString *)string {
return [[[ TaskObject alloc ] initWithString: string forSystem: NO ] autorelease ];
}
+ (id)taskWithTask:(id)value {
return [[ value copy ] autorelease ];
}
- (void)dealloc {
[ task release ];
[ super dealloc ];
}
- (id)copy {
id newTask = [[ TaskObject alloc ] init ];
[ newTask setDictionary: task ];
[ newTask setIsSystemCrontabTask: [ self isSystemCrontabTask ]];
return newTask;
}
// ----------
+ (BOOL)isContainedInString: (NSString *)line {
if ( [ line startsWithStringIgnoringWhitespace: @"#" ] &&
! [ line startsWithStringIgnoringWhitespace: DisableTag ] ) return NO;
// ignore '@' commands (for now)
if ( [ line startsWithStringIgnoringWhitespace: @"@" ] ) return NO;
{
NSCharacterSet *ws = [ NSCharacterSet whitespaceCharacterSet ];
int fieldCount = [ line fieldCountSeperatedBy: ws ];
if ( fieldCount < 6 ) return NO;
}
return YES;
}
- (void)parseString: (NSString *)string {
NSScanner *scanner;
NSCharacterSet *whitespace = [ NSCharacterSet whitespaceCharacterSet ];
NSString *min;
NSString *hour;
NSString *mday;
NSString *month;
NSString *wday;
NSString *user;
NSString *command = nil;
unsigned cind;
if ( ! string ) return;
// NSLog( @"Parsing string: %@", string );
if ( [ string hasPrefix: DisableTag ] ) {
string = [ string substringFromIndex: [ DisableTag length ]];
[ self setActive: NO ];
} else {
[ self setActive: YES ];
}
scanner = [ NSScanner scannerWithString: string ];
[ scanner scanUpToCharactersFromSet: whitespace intoString: &min ];
[ scanner scanUpToCharactersFromSet: whitespace intoString: &hour ];
[ scanner scanUpToCharactersFromSet: whitespace intoString: &mday ];
[ scanner scanUpToCharactersFromSet: whitespace intoString: &month ];
[ scanner scanUpToCharactersFromSet: whitespace intoString: &wday ];
if ( [ self isSystemCrontabTask ] )
[ scanner scanUpToCharactersFromSet: whitespace intoString: &user ];
cind = [ scanner scanLocation ] + 1;
if ( cind <= [ string length ] ) {
command = [ string substringFromIndex: cind ];
command = [ command stringByTrimmingCharactersInSet: whitespace ];
}
[ self setMinute: min ];
[ self setHour: hour ];
[ self setMday: mday ];
[ self setMonth: month ];
[ self setWday: wday ];
if ( [ self isSystemCrontabTask ] )
[ self setUser: user ];
[ self setCommand: command ];
}
- (void)setObject:(id)anObject forKey:(id)aKey {
[ task setObject: anObject forKey: aKey ];
}
- (id)objectForKey:(id)aKey {
return [ task objectForKey: aKey ];
}
- (void)setActive: (BOOL)value {
[ self setObject: [ NSString stringWithFormat: @"%u", value ] forKey: @"Active" ];
}
- (BOOL)isActive {
return [[ self objectForKey: @"Active" ] intValue ] == 1;
}
- (void)setMinute: (NSString *)value {
[ self setObject: value forKey: @"Min" ];
}
- (NSString *)minute {
return [ self objectForKey: @"Min" ];
}
- (void)setHour: (NSString *)value {
[ self setObject: value forKey: @"Hour" ];
}
- (NSString *)hour {
return [ self objectForKey: @"Hour" ];
}
- (void)setMday: (NSString *)value {
[ self setObject: value forKey: @"Mday" ];
}
- (NSString *)mday {
return [ self objectForKey: @"Mday" ];
}
- (void)setMonth: (NSString *)value {
[ self setObject: value forKey: @"Month" ];
}
- (NSString *)month {
return [ self objectForKey: @"Month" ];
}
- (void)setWday: (NSString *)value {
[ self setObject: value forKey: @"Wday" ];
}
- (NSString *)wday {
return [ self objectForKey: @"Wday" ];
}
- (void)setCommand: (NSString *)value {
[ self setObject: value forKey: @"Command" ];
}
- (NSString *)command {
return [ self objectForKey: @"Command" ];
}
- (void)setUser: (NSString *)value {
[ self setObject: value forKey: @"User" ];
}
- (NSString *)user {
return [ self objectForKey: @"User" ];
}
- (NSString *)info {
return [ self objectForKey: @"Info" ];
}
- (void)setInfo: (NSString *)aValue {
id string = [ NSMutableString stringWithString: [ aValue stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ]]];
[ string replaceOccurrencesOfString: [ NSString stringWithFormat: @"%@ ", CrInfoComment ]
withString: @""
options: NSLiteralSearch
range: NSMakeRange( 0, [ string length ] ) ];
[ string replaceOccurrencesOfString: [ NSString stringWithFormat: @"%@\t", CrInfoComment ]
withString: @""
options: NSLiteralSearch
range: NSMakeRange( 0, [ string length ] ) ];
/*
if ( [ string startsWithString: CrInfoComment ] ) {
string = [ string substringFromIndex: [ CrInfoComment length ] -1 ];
string = [ string stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ]];
}
*/
[ self setObject: string forKey: @"Info" ];
}
- (NSData *)data {
NSMutableData *data = [ NSMutableData data ];
// add info line
if ( [ self info ] ) {
id string = [ NSMutableString stringWithFormat: @"%@ %@", CrInfoComment, [ self info ]];
[ string replaceOccurrencesOfString: @"\n"
withString: [ NSString stringWithFormat: @"\n%@ ", CrInfoComment ]
options: NSLiteralSearch
range: NSMakeRange( 0, [ string length ] ) ];
[ string appendString: @"\n" ];
[ data appendData: [ string dataUsingEncoding: [ NSString defaultCStringEncoding ]]];
}
// prepare the task line
id line;
// prepare the active/inactive string
id activeString = [ self isActive ] ? @"" : [ NSString stringWithFormat: @"%@ ", DisableTag ];
id asterisk = @"*";
if ( [ self isSystemCrontabTask ] ) {
line = [ NSString stringWithFormat: @"%@%@\t%@\t%@\t%@\t%@\t%@\t%@",
activeString,
[[ self objectForKey: @"Min" ] length ] != 0 ? [ self objectForKey: @"Min" ] : asterisk,
[[ self objectForKey: @"Hour" ] length ] != 0 ? [ self objectForKey: @"Hour" ] : asterisk,
[[ self objectForKey: @"Mday" ] length ] != 0 ? [ self objectForKey: @"Mday" ] : asterisk,
[[ self objectForKey: @"Month" ] length ] != 0 ? [ self objectForKey: @"Month" ] : asterisk,
[[ self objectForKey: @"Wday" ] length ] != 0 ? [ self objectForKey: @"Wday" ] : asterisk,
[[ self objectForKey: @"User" ] length ] != 0 ? [ self objectForKey: @"User" ] : @"root",
[[ self objectForKey: @"Command" ] length ] != 0 ? [ self objectForKey: @"Command" ] : asterisk ];
} else {
line = [ NSString stringWithFormat: @"%@%@\t%@\t%@\t%@\t%@\t%@",
activeString,
[[ self objectForKey: @"Min" ] length ] != 0 ? [ self objectForKey: @"Min" ] : asterisk,
[[ self objectForKey: @"Hour" ] length ] != 0 ? [ self objectForKey: @"Hour" ] : asterisk,
[[ self objectForKey: @"Mday" ] length ] != 0 ? [ self objectForKey: @"Mday" ] : asterisk,
[[ self objectForKey: @"Month" ] length ] != 0 ? [ self objectForKey: @"Month" ] : asterisk,
[[ self objectForKey: @"Wday" ] length ] != 0 ? [ self objectForKey: @"Wday" ] : asterisk,
[[ self objectForKey: @"Command" ] length ] != 0 ? [ self objectForKey: @"Command" ] : asterisk ];
}
[ data appendData: [ line dataUsingEncoding: [ NSString defaultCStringEncoding ]]];
return data;
}
- (NSString *)description {
id string = [[ NSString alloc ] initWithData: [ self data ] encoding: [ NSString defaultCStringEncoding ]];
return [ string autorelease ];
}
// accessors
- (void)setDictionary:(NSDictionary *)dict {
task = [[ NSMutableDictionary alloc ] initWithDictionary: dict ];
}
- (BOOL)isSystemCrontabTask {
return isSystemCrontabTask;
}
- (void)setIsSystemCrontabTask: (BOOL)aValue {
isSystemCrontabTask = aValue;
}
// transparent forwarding
/* essential forwarding methods
- (void)forwardInvocation: (NSInvocation *)invocation {
NSLog( @"forward" );
if ( [[ self task ] respondsToSelector: [invocation selector] ] ) {
[ invocation invokeWithTarget: [ self task ] ];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
if ( [ self respondsToSelector: aSelector ] ) {
return [ self methodSignatureForSelector: aSelector ];
} else {
return [[ self task ] methodSignatureForSelector: aSelector ];
}
} */
/* not really needed
- (BOOL)respondsToSelector: (SEL)aSelector {
NSLog( @"responds" );
return [ super respondsToSelector: aSelector ] || [[ self task ] respondsToSelector: aSelector ];
}
- (BOOL)isKindOfClass:(Class)aClass {
return [[ self task ] isKindOfClass: aClass ];
}
+ (BOOL)instancesRespondToSelector:(SEL)aSelector {
NSLog( @"instance responds" );
return [ self instancesRespondToSelector: aSelector ] || [[ self task ] instancesRespondToSelector: aSelector ];
}*/
@end