forked from korseby/TriTag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFPlaceholders.m
executable file
·146 lines (119 loc) · 3.03 KB
/
FFPlaceholders.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
RCS_ID("$Id: FFPlaceholders.m 234 2004-07-31 13:55:31Z ravemax $")
#import "FFPlaceholders.h"
NSString* ArtistIdent = @"Artist";
NSString* AlbumIdent = @"Album";
NSString* TrackNumberIdent = @"TrackNumber";
NSString* TrackTitleIdent = @"TrackTitle";
NSString* YearIdent = @"Year";
NSString* GenreIdent = @"Genre";
@implementation FFPlaceholders
static struct {
char ichr;
NSString* ident;
} IdentCharacterMap[PH_NUM_IDENTS] = {
{ 'a', nil },
{ 't', nil },
{ 'n', nil },
{ 's', nil },
{ 'y', nil },
{ 'g', nil }
};
+ (char)placeholderNo:(unsigned)no
{
if (no >= PH_NUM_IDENTS)
return '*';
return IdentCharacterMap[no].ichr;
}
+ (void)initialize
{
IdentCharacterMap[PH_ARTIST].ident = ArtistIdent;
IdentCharacterMap[PH_ALBUM].ident = AlbumIdent;
IdentCharacterMap[PH_TRACK_NUMBER].ident = TrackNumberIdent;
IdentCharacterMap[PH_TRACK_TITLE].ident = TrackTitleIdent;
IdentCharacterMap[PH_YEAR].ident = YearIdent;
IdentCharacterMap[PH_GENRE].ident = GenreIdent;
}
- (NSString*)_indentForChar:(char)chr
{
int i;
for (i = 0; i < PH_NUM_IDENTS; i++) {
if (IdentCharacterMap[i].ichr == chr)
return IdentCharacterMap[i].ident;
}
NSBeginAlertSheet(@"Pattern error", @"Stop processing", nil, nil,
[NSApp mainWindow], self, nil, nil, nil,
@"The placeholder '%c' is unknown", chr);
return nil;
}
- (BOOL)_identOrderFromString:(NSString*)str
{
unsigned i = 0;
NSString* ident;
m_num = 0;
m_str = [NSMutableString stringWithString:str];
while (i < [m_str length]) {
if ([m_str characterAtIndex:i] == '%') {
// No characters after %
if ((i+1 == [m_str length]) || ([m_str characterAtIndex:i+1] == '.')) {
NSBeginAlertSheet(@"Pattern error", @"Stop processing", nil, nil,
[NSApp mainWindow], self, nil, nil, nil,
@"Expected another character after the last '%%'.");
return FALSE;
}
// A real %
if ([m_str characterAtIndex:i+1] == '%') {
[m_str deleteCharactersInRange:NSMakeRange(i, 1)];
i++;
// Placeholder
} else {
// Already PH_NUM idents ?
if (m_num == PH_NUM_IDENTS) {
NSBeginAlertSheet(@"Pattern error", @"Stop processing", nil, nil,
[NSApp mainWindow], self, nil, nil, nil,
@"The pattern is invalid - to many placeholders");
return FALSE;
}
// char -> ident
ident = [self _indentForChar:(char)[m_str characterAtIndex:i+1]];
if (ident == nil)
return FALSE;
m_phs[m_num].ident = ident;
m_phs[m_num].pos = i;
m_num++;
i += 2;
}
// Just a normal character
} else
i++;
}
return TRUE;
}
- (id)initWithString:(NSString*)str
{
if (self = [super init]) {
if (![self _identOrderFromString:str])
return nil;
}
return self;
}
- (NSMutableString*)getString
{
return m_str;
}
- (unsigned)numberOfUsedIdents
{
return m_num;
}
- (NSString*)identAtIndex:(unsigned)idx
{
if (idx >= m_num)
return nil;
return m_phs[idx].ident;
}
- (unsigned)positionAtIndex:(unsigned)idx
{
if (idx >= m_num)
return (unsigned)-1;
return m_phs[idx].pos;
}
@end