-
Notifications
You must be signed in to change notification settings - Fork 1
/
IFSFormatter.m
316 lines (259 loc) · 8.98 KB
/
IFSFormatter.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright (c) 2007-2011, Marcus Müller <[email protected]>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Mulle kybernetiK nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#import "common.h"
#import "IFSFormatter.h"
#import "NSObject+FUSEOFS.h"
@interface iTunesFSFormattingResult : NSObject
{
id target;
NSMutableString *buffer;
NSUInteger rewindIndex;
}
- (id)initWithTarget:(id)_obj;
- (void)appendToBuffer:(NSString *)_s;
- (void)appendValueForKeyToBuffer:(NSString *)_key;
- (NSString *)formattedString;
@end
@interface IFSFormatter (Private)
- (void)setupFormattingOps;
- (void)appendStringToFormattingOps:(NSString *)_s;
- (NSString *)_stringValueByFormattingObject:(id)_obj;
@end
@implementation IFSFormatter
static NSCharacterSet *wsSet = nil;
static NSValue *appendToBufferValue = nil;
static NSValue *appendValueForKeyToBufferValue = nil;
+ (void)initialize {
static BOOL didInit = NO;
SEL selector;
if (didInit) return;
didInit = YES;
wsSet = [[NSCharacterSet whitespaceCharacterSet] copy];
selector = @selector(appendToBuffer:);
appendToBufferValue = \
[[NSValue value:&selector withObjCType:@encode(SEL)] copy];
selector = @selector(appendValueForKeyToBuffer:);
appendValueForKeyToBufferValue = \
[[NSValue value:&selector withObjCType:@encode(SEL)] copy];
}
- (id)initWithFormatString:(NSString *)_format {
self = [self init];
if (self) {
self->format = [_format copy];
self->formattingOps = [[NSMutableArray alloc] initWithCapacity:5];
[self setupFormattingOps];
}
return self;
}
- (void)dealloc {
[self->format release];
[self->formattingOps release];
[super dealloc];
}
- (NSString *)formatString {
return self->format;
}
- (void)setupFormattingOps {
NSRange r, sr;
NSUInteger len, lastMark;
[self->formattingOps removeAllObjects];
len = [self->format length];
sr = NSMakeRange(0, len);
lastMark = 0;
while ((r = [self->format rangeOfString:@"%("
options:0
range:sr]).length > 0)
{
NSRange lr, kr;
NSUInteger start;
NSString *key;
/* first copy what's missing */
lr = NSMakeRange(lastMark, r.location - lastMark);
if (lr.length)
[self appendStringToFormattingOps:[self->format substringWithRange:lr]];
/* find end delimiter and construct key */
start = NSMaxRange(r);
sr = NSMakeRange(start, len - start);
r = [self->format rangeOfString:@")" options:0 range:sr];
if (!r.length) {
/* adjust range to end of string */
r.length = 0;
r.location = len;
}
lastMark = NSMaxRange(r);
kr = NSMakeRange(start, r.location - start);
key = [self->format substringWithRange:kr];
key = [key stringByTrimmingCharactersInSet:wsSet];
[self->formattingOps addObject:appendValueForKeyToBufferValue];
[self->formattingOps addObject:key];
}
/* special case if no tokens were found */
if (lastMark == 0) {
[self appendStringToFormattingOps:self->format];
}
else {
NSString *leftover;
leftover = [self->format substringFromIndex:lastMark];
[self appendStringToFormattingOps:leftover];
}
}
- (BOOL)isPathFormat {
return [self->format rangeOfString:@"/"].location != NSNotFound;
}
- (NSString *)stringValueByFormattingObject:(id)_obj {
return [[self _stringValueByFormattingObject:_obj]
properlyEscapedFSRepresentation];
}
- (NSArray *)pathComponentsByFormattingObject:(id)_obj {
NSString *path = [self _stringValueByFormattingObject:_obj];
NSArray *rawComponents = [path pathComponents];
NSUInteger i, count = [rawComponents count];
NSMutableArray *components = [[NSMutableArray alloc] initWithCapacity:count];
for (i = 0; i < count; i++) {
NSString *rc = [rawComponents objectAtIndex:i];
if ([rc isEqualToString:@"/"])
continue;
NSString *c = [[rc stringByTrimmingCharactersInSet:wsSet]
properlyEscapedFSRepresentation];
if ([c length])
[components addObject:c];
}
return [components autorelease];
}
- (NSString *)_stringValueByFormattingObject:(id)_obj {
iTunesFSFormattingResult *result;
NSUInteger i, count;
NSString *formattedString;
result = [[iTunesFSFormattingResult alloc] initWithTarget:_obj];
count = [self->formattingOps count];
for (i = 0; i < count; i+= 2) {
NSValue *selVal;
id arg;
SEL selector;
selVal = [self->formattingOps objectAtIndex:i];
arg = [self->formattingOps objectAtIndex:i + 1];
[selVal getValue:&selector];
[result performSelector:selector withObject:arg];
}
formattedString = [result formattedString];
[result release];
return formattedString;
}
- (void)appendStringToFormattingOps:(NSString *)_s {
[self->formattingOps addObject:appendToBufferValue];
[self->formattingOps addObject:_s];
}
@end /* IFSFormatter */
@implementation iTunesFSFormattingResult
static NSMutableDictionary *fmtCache = nil;
+ (void)initialize {
static BOOL didInit = NO;
if (didInit) return;
fmtCache = [[NSMutableDictionary alloc] initWithCapacity:3];
}
- (id)initWithTarget:(id)_obj {
self = [super init];
if (self) {
self->target = [_obj retain];
self->buffer = [[NSMutableString alloc] initWithCapacity:20];
}
return self;
}
- (void)dealloc {
[self->target release];
[self->buffer release];
[super dealloc];
}
- (NSFormatter *)getFormatterForValue:(id)_value
withFormatString:(NSString *)_fmt
{
NSString *cacheKey;
NSNumberFormatter *formatter;
cacheKey = _fmt;
formatter = [fmtCache objectForKey:cacheKey];
if (!formatter) {
formatter = [[NSNumberFormatter alloc] init];
[formatter setFormat:_fmt];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_0];
[fmtCache setObject:formatter forKey:cacheKey];
[formatter release];
}
return formatter;
}
- (void)appendToBuffer:(NSString *)_s {
if (_s) {
self->rewindIndex = [self->buffer length];
[self->buffer appendString:_s];
}
}
- (void)appendValueForKeyToBuffer:(NSString *)_key {
NSRange r;
NSString *fmt, *value;
r = [_key rangeOfString:@"#"];
if (r.length) {
fmt = [_key substringFromIndex:NSMaxRange(r)];
_key = [_key substringToIndex:r.location];
}
else {
fmt = nil;
}
NS_DURING
value = [self->target valueForKeyPath:_key];
if ([value isKindOfClass:[NSNumber class]] && [value intValue] == 0) {
value = nil;
}
if (fmt && value) {
NSFormatter *formatter;
formatter = [self getFormatterForValue:value withFormatString:fmt];
value = [formatter stringForObjectValue:value];
}
NS_HANDLER
value = [localException reason];
NS_ENDHANDLER
if (value) {
[self->buffer appendString:[value description]];
}
else {
/* Rewind to rewindIndex - this will remove any static "prefixes"
* In most cases this is the right thing to do, but sometimes it
* might lead to undesired side effects. I'm nevertheless convinced
* that this is the best choice for a default operation.
*/
NSRange r;
r = NSMakeRange(self->rewindIndex,
[self->buffer length] - self->rewindIndex);
if (r.length) {
[self->buffer deleteCharactersInRange:r];
self->rewindIndex = [self->buffer length];
}
}
}
- (NSString *)formattedString {
NSString *formattedString = [[self->buffer copy] autorelease];
return [formattedString stringByTrimmingCharactersInSet:wsSet];
}
@end /* iTunesFSFormattingResult */