forked from karelia/ConnectionKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCKFTPCommand.m
117 lines (88 loc) · 2.82 KB
/
CKFTPCommand.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
//
// CKFTPRequest.m
// Connection
//
// Created by Mike on 24/03/2009.
// Copyright 2009 Karelia Software. All rights reserved.
//
#import "CKFTPCommand.h"
@implementation CKFTPCommand
+ (NSCharacterSet *)FTPCommandCodeCharacters
{
static NSCharacterSet *result = nil;
if (!result)
{
result = [NSCharacterSet characterSetWithCharactersInString:
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
[result retain];
}
return result;
}
#pragma mark -
#pragma mark Init
+ (CKFTPCommand *)commandWithCode:(NSString *)code argumentField:(NSString *)argumentField;
{
return [[[self alloc] initWithCommandCode:code argumentField:argumentField] autorelease];
}
+ (CKFTPCommand *)commandWithCode:(NSString *)code
{
return [[[self alloc] initWithCommandCode:code] autorelease];
}
- (id)initWithCommandCode:(NSString *)verb argumentField:(NSString *)argumentField
{
NSParameterAssert(verb);
NSParameterAssert([verb length] > 0 || [verb length] <= 4);
NSCharacterSet *legalVerbCharacters = [[self class] FTPCommandCodeCharacters];
NSCharacterSet *illegalVerbCharacters = [legalVerbCharacters invertedSet];
NSParameterAssert([verb rangeOfCharacterFromSet:illegalVerbCharacters].location == NSNotFound);
if (argumentField) NSParameterAssert([argumentField length] > 0);
[super init];
_commandCode = [verb copy];
_argumentField = [argumentField copy];
return self;
}
- (id)initWithCommandCode:(NSString *)code
{
return [self initWithCommandCode:code argumentField:nil];
}
- (void)dealloc
{
[_commandCode release];
[_argumentField release];
[super dealloc];
}
#pragma mark -
#pragma mark Accessor Methods
- (NSString *)commandCode { return _commandCode; }
- (NSString *)argumentField { return _argumentField; }
#pragma mark -
#pragma mark Copy
- (id)copyWithZone:(NSZone *)zone
{
return [self retain]; // immutable object
}
#pragma mark -
#pragma mark Description and Serialization
- (NSData *)serializedCommand
{
NSMutableString *string = [[self description] mutableCopy];
/*[string replaceOccurrencesOfString:@"\r" // escape carriage returns for Telnet. RFC 854.
withString:@"\r[NULL]" // not functional yet
options:0
range:NSMakeRange(0, [string length])];*/
[string appendString:@"\r\n"];
NSData *result = [string dataUsingEncoding:NSASCIIStringEncoding];
[string release];
return result;
}
- (NSString *)description
{
NSString *result = [[self commandCode] uppercaseString];
NSString *parameter = [self argumentField];
if (parameter)
{
result = [result stringByAppendingFormat:@" %@", parameter];
}
return result;
}
@end