forked from karelia/MockServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMSSendStringCommand.m
More file actions
69 lines (55 loc) · 1.82 KB
/
KMSSendStringCommand.m
File metadata and controls
69 lines (55 loc) · 1.82 KB
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
//
// KMSSendStringCommand.m
// MockServer
//
// Created by Sam Deane on 25/01/2013.
// Copyright (c) 2013 Karelia Software. All rights reserved.
//
#import "KMSSendStringCommand.h"
#import "KMSConnection.h"
#import "KMSServer.h"
@implementation KMSSendStringCommand
+ (KMSSendStringCommand*)sendString:(NSString *)string
{
KMSSendStringCommand* result = [[KMSSendStringCommand alloc] init];
result.string = string;
return [result autorelease];
}
- (void)dealloc
{
[_string release];
[super dealloc];
}
- (NSTimeInterval)performOnConnection:(KMSConnection*)connection server:(KMSServer*)server
{
// log just the first line of the output
NSString* log = self.string;
NSRange range = [log rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
if (range.location != NSNotFound)
{
log = [NSString stringWithFormat:@"%@… (%ld bytes)", [log substringToIndex:range.location], (long) [log length]];
}
KMSLog(@"queued output %@", log);
[connection appendOutput:[self.string dataUsingEncoding:NSUTF8StringEncoding]];
return 0;
}
- (KMSCommand*)substitutedWithValues:(NSDictionary*)substitutions
{
KMSCommand* result;
BOOL containsTokens = [self.string rangeOfString:@"$"].location != NSNotFound;
if (containsTokens)
{
NSMutableString* substituted = [NSMutableString stringWithString:self.string];
[substitutions enumerateKeysAndObjectsUsingBlock:^(id key, id replacement, BOOL *stop) {
[substituted replaceOccurrencesOfString:key withString:replacement options:0 range:NSMakeRange(0, [substituted length])];
}];
KMSLogDetail(@"expanded response %@ as %@", self.string, substituted);
result = [KMSSendStringCommand sendString:substituted];
}
else
{
result = self;
}
return result;
}
@end