-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMDCommand.mm
81 lines (70 loc) · 2.04 KB
/
TMDCommand.mm
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
#import "TMDCommand.h"
#import "Dialog2.h"
#import <vector>
static NSMutableDictionary* Commands = nil;
@implementation TMDCommand
+ (void)registerObject:(id)anObject forCommand:(NSString*)aCommand
{
if(!Commands)
Commands = [NSMutableDictionary new];
[Commands setObject:anObject forKey:aCommand];
}
+ (NSDictionary *)registeredCommands
{
return [[Commands copy] autorelease];
}
+ (id)objectForCommand:(NSString*)aCommand
{
return [Commands objectForKey:aCommand];
}
+ (id)readPropertyList:(NSFileHandle*)aFileHandle error:(NSString**)error;
{
//NSData* data = [aFileHandle readDataToEndOfFile];
NSData* data = [TMDCommand readDataUntilNullTerminator:aFileHandle];
if([data length] == 0)
return nil;
id plist = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListMutableContainersAndLeaves format:nil errorDescription:error];
return plist;
}
+(NSData*)readDataUntilNullTerminator:(NSFileHandle*)aFileHandle
{
NSMutableData* mutableData= [NSMutableData data];
NSData* data;
while((data = [aFileHandle availableData]) && ([data length] > 0))
{
[mutableData appendData:data];
int length = [data length];
if(( (char*) [data bytes] )[length - 1] == 0) {
break;
}
}
return mutableData;
}
+ (void)writePropertyList:(id)aPlist toFileHandle:(NSFileHandle*)aFileHandle
{
NSString* error = nil;
if(NSData* data = [NSPropertyListSerialization dataFromPropertyList:aPlist format:NSPropertyListXMLFormat_v1_0 errorDescription:&error])
{
[aFileHandle writeData:data];
}
else
{
fprintf(stderr, "%s\n", [error UTF8String] ?: "unknown error serializing returned property list");
fprintf(stderr, "%s\n", [[aPlist description] UTF8String]);
}
}
- (NSString *)commandDescription
{
return @"No information available for this command";
}
- (NSString *)usageForInvocation:(NSString *)invocation;
{
return @"No usage information available for this command";
}
@end
@implementation NSFileHandle (WriteString)
- (void)writeString:(NSString *)string;
{
[self writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}
@end