FreeRDP
Loading...
Searching...
No Matches
TSXAdditions.m
1/*
2 Additions to Cocoa touch classes
3
4 Copyright 2013 Thincast Technologies GmbH, Authors: Dorian Johnson, Martin Fleisz
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9 */
10
11#import "TSXAdditions.h"
12
13@implementation NSObject (TSXAdditions)
14
15- (void)setValuesForKeyPathsWithDictionary:(NSDictionary *)keyedValues
16{
17 for (id keyPath in keyedValues)
18 [self setValue:[keyedValues objectForKey:keyPath] forKeyPath:keyPath];
19}
20
21- mutableDeepCopy
22{
23 if ([self respondsToSelector:@selector(mutableCopyWithZone:)])
24 return [self mutableCopy];
25 else if ([self respondsToSelector:@selector(copyWithZone:)])
26 return [self copy];
27 else
28 return [self retain];
29}
30
31@end
32
33#pragma mark -
34
35@implementation NSString (TSXAdditions)
36
37#pragma mark Creation routines
38+ (NSString *)stringWithUUID
39{
40 CFUUIDRef uuidObj = CFUUIDCreate(nil);
41 NSString *uuidString = (NSString *)CFUUIDCreateString(nil, uuidObj);
42 CFRelease(uuidObj);
43 return [uuidString autorelease];
44}
45
46/* Code from
47 * http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMNSData%2BHex.m?r=344
48 */
49- (NSData *)dataFromHexString
50{
51 NSData *hexData = [self dataUsingEncoding:NSASCIIStringEncoding];
52 const char *hexBuf = [hexData bytes];
53 NSUInteger hexLen = [hexData length];
54
55 // This indicates an error converting to ASCII.
56 if (!hexData)
57 return nil;
58
59 if ((hexLen % 2) != 0)
60 {
61 return nil;
62 }
63
64 NSMutableData *binaryData = [NSMutableData dataWithLength:(hexLen / 2)];
65 unsigned char *binaryPtr = [binaryData mutableBytes];
66 unsigned char value = 0;
67 for (NSUInteger i = 0; i < hexLen; i++)
68 {
69 char c = hexBuf[i];
70
71 if (!isxdigit(c))
72 {
73 return nil;
74 }
75
76 if (isdigit(c))
77 {
78 value += c - '0';
79 }
80 else if (islower(c))
81 {
82 value += 10 + c - 'a';
83 }
84 else
85 {
86 value += 10 + c - 'A';
87 }
88
89 if (i & 1)
90 {
91 *binaryPtr++ = value;
92 value = 0;
93 }
94 else
95 {
96 value <<= 4;
97 }
98 }
99
100 return [NSData dataWithData:binaryData];
101}
102
103+ (NSString *)hexStringFromData:(const unsigned char *)data
104 ofSize:(unsigned int)size
105 withSeparator:(NSString *)sep
106 afterNthChar:(int)sepnth
107{
108 NSMutableString *result;
109 NSString *immutableResult;
110
111 result = [[NSMutableString alloc] init];
112 for (int i = 0; i < size; i++)
113 {
114 if (i && sep && sepnth && i % sepnth == 0)
115 [result appendString:sep];
116 [result appendFormat:@"%02X", data[i]];
117 }
118
119 immutableResult = [NSString stringWithString:result];
120 [result release];
121 return immutableResult;
122}
123
124@end
125
126#pragma mark Mutable deep copy for dictionary, array and set
127
128@implementation NSDictionary (TSXAdditions)
129
130- mutableDeepCopy
131{
132 NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] init];
133 NSEnumerator *enumerator = [self keyEnumerator];
134 id key;
135 while ((key = [enumerator nextObject]))
136 {
137 id obj = [[self objectForKey:key] mutableDeepCopy];
138 [newDictionary setObject:obj forKey:key];
139 [obj release];
140 }
141 return newDictionary;
142}
143
144@end
145
146@implementation NSArray (TSXAdditions)
147
148- mutableDeepCopy
149{
150 NSMutableArray *newArray = [[NSMutableArray alloc] init];
151 NSEnumerator *enumerator = [self objectEnumerator];
152 id obj;
153 while ((obj = [enumerator nextObject]))
154 {
155 obj = [obj mutableDeepCopy];
156 [newArray addObject:obj];
157 [obj release];
158 }
159 return newArray;
160}
161
162@end
163
164@implementation NSSet (TSXAdditions)
165
166- mutableDeepCopy
167{
168 NSMutableSet *newSet = [[NSMutableSet alloc] init];
169 NSEnumerator *enumerator = [self objectEnumerator];
170 id obj;
171 while ((obj = [enumerator nextObject]))
172 {
173 obj = [obj mutableDeepCopy];
174 [newSet addObject:obj];
175 [obj release];
176 }
177 return newSet;
178}
179
180@end
181
182#pragma mark -
183
184/* Code from
185 * http://stackoverflow.com/questions/1305225/best-way-to-serialize-a-nsdata-into-an-hexadeximal-string
186 */
187@implementation NSData (TSXAdditions)
188
189#pragma mark - String Conversion
190- (NSString *)hexadecimalString
191{
192 /* Returns hexadecimal string of NSData. Empty string if data is empty. */
193
194 const unsigned char *dataBuffer = (const unsigned char *)[self bytes];
195
196 if (!dataBuffer)
197 return [NSString string];
198
199 NSUInteger dataLength = [self length];
200 NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
201
202 for (int i = 0; i < dataLength; ++i)
203 [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
204
205 return [NSString stringWithString:hexString];
206}
207
208/* Code from http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html */
209- (NSString *)base64EncodedString
210{
211 const char *data = (const char *)[self bytes];
212
213 NSData *nsData = [data dataUsingEncoding:NSUTF8StringEncoding];
214 NSString *sEnc = [nsData base64EncodedStringWithOptions:0];
215
216 return sEnc;
217}
218
219@end