-
Notifications
You must be signed in to change notification settings - Fork 2
/
SYPBXProjEncoder.m
71 lines (68 loc) · 2.09 KB
/
SYPBXProjEncoder.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
//
// SYPBXProjEncoder.m
// XcodeProjectEditor
//
// Created by Daniel DeCovnick on 4/2/10.
// Copyright 2010 Softyards Software. All rights reserved.
//
#import "SYPBXProjEncoder.h"
@implementation SYPBXProjEncoder
+(NSString *)encode:(NSDictionary *)project
{
return [NSString stringWithFormat:@"// !$*UTF8*$!\n%@\n", [self encodeHash:project withIndentation:0]];
}
+(NSString *)encodeHash:(NSDictionary *)hash withIndentation:(int)indent
{
NSMutableString *ret = [NSMutableString stringWithString:@"{\n"];
NSArray *map = [[hash allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (id key in map)
{
[ret appendFormat:@"%@%@ = %@;\n", [self encodeIndentation:indent+1], [self encodeValue:key withIndentation:indent+1], [self encodeObject:[hash objectForKey:key] withIndentation:indent+1]];
[ret appendFormat:@"%@}", [self encodeIndentation:indent]];
}
return ret;
}
+(NSString *)encodeObject:(id)object withIndentation:(int)indent
{
if ([object isKindOfClass:[NSDictionary class]])
{
return [self encodeHash:object withIndentation:indent];
}
else if ([object isKindOfClass:[NSArray class]])
{
return [self encodeArray:object withIndentation:indent];
}
else if ([object isKindOfClass:[NSString class]])
{
return [self encodeValue:object withIndentation:indent];
}
else
{
NSLog(@"something went wrong trying to encode object: %@", object);
return nil;
}
}
+(NSString *)encodeArray:(NSArray *)myArray withIndentation:(int)indent
{
NSMutableString *ret = [NSMutableString stringWithString:@"(\n"];
for (id value in myArray)
{
[ret appendFormat:@"%@%@,\n", [self encodeIndentation:indent+1], [self encodeObject:value withIndentation:indent+1]];
}
[ret appendFormat:@"%@)", [self encodeIndentation:indent]];
return ret;
}
+(NSString *)encodeValue:(id)value withIndentation:(int)indent
{
return [value description]; //NSString doesn't have ruby's inspect. TODO: implement this or find a library that does
}
+(NSString *)encodeIndentation:(int)indent
{
NSMutableString *ret = [NSMutableString string];
for (int i = 0; i < indent; i++)
{
[ret appendString:@"\t"];
}
return ret;
}
@end