-
Notifications
You must be signed in to change notification settings - Fork 1
/
FFData.m
executable file
·1294 lines (1059 loc) · 37.4 KB
/
FFData.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
RCS_ID("$Id: FFData.m 264 2004-08-20 14:22:11Z ravemax $")
#import "FFData.h"
#import <id3tag.h>
#import "id3tag_filestructs.h"
#import <string.h>
//#import <id3.h>
//#import "id3lib_enc_wrapper.h"
#import "FFPlaceholders.h"
#import "FFFilenameToTag.h"
#import "FFTagToFilename.h"
#import "FFController.h"
#import "FFV1Genres.h"
//#import "FFFreeDB.h"
#import "FFMP3Len.h"
@implementation FFData
// Consts
static const unsigned PaddingAssumedBlockSize = 4096;
static const unsigned PaddingMinPadSize = 512;
// The special idents
NSString* FilePathIdent = @"Filepath"; // Internal use
static NSString* FilenameIdent = @"Filename";
static NSString* CompilationIdent = @"Compilation"; // Internal use
static NSString* CommentIdent = @"Comment"; // Internal use
static NSString* DataOfsIdent = @"data_ofs"; // Internal use
static NSString* DataSizeIdent = @"data_size"; // Internal use
static NSString* HasV1TagIdent = @"has_v1_tag"; // Internal use
static NSString* V1TagOfsIdent = @"v1_tag_ofs"; // Internal use
static NSString* ArrayIndexIdent = @"array_index"; // Internal use
static NSString* RemovedWhileSortingIdent = @"removed_sort"; // Internal use
static NSString* CommentTextEncIdent = @"cmt_text_enc"; // Internal use
static NSString* CommentLanguageIdent = @"cmt_lang"; // Internal use
static NSString* CommentShortTextIdent = @"cmt_short_txt"; // Internal use
static NSString* CommentFullTextIdent = @"cmt_full_txt"; // Internal use
static NSString* FileExtension = @"mp3";
static NSString* Unknown = @"_Unknown";
static NSString* Compilations = @"_Compilations";
// No way to convert a defined string into a static NSString ?
static NSString* FrameTitleIdent = @"TIT2"; // ID3_FRAME_TITLE
static NSString* FrameArtistIdent = @"TPE1"; // ID3_FRAME_ARTIST
static NSString* FrameAlbumIdent = @"TALB"; // ID3_FRAME_ALBUM
static NSString* FrameTrackIdent = @"TRCK"; // ID3_FRAME_TRACK
static NSString* FrameYearIdent = @"TDRC"; // ID3_FRAME_YEAR
static NSString* FrameGenreIdent = @"TCON"; // ID3_FRAME_GENRE
static NSString* FrameCommentIdent = @"COMM"; // ID3_FRAME_COMMENT
// Vars
static NSColor* m_filenameColor;
static NSFileManager* m_fileManager;
static NSDictionary* m_emptyIdentRow;
static NSDictionary* m_identToFrame;
/*
* Init & destroy
*/
+ (void)initialize
{
m_filenameColor = [[NSColor secondarySelectedControlColor] retain];
m_fileManager = [[NSFileManager defaultManager] retain];
m_identToFrame = [[NSDictionary dictionaryWithObjectsAndKeys:
FrameTitleIdent, TrackTitleIdent,
FrameArtistIdent, ArtistIdent,
FrameAlbumIdent, AlbumIdent,
FrameTrackIdent, TrackNumberIdent,
FrameYearIdent, YearIdent,
FrameGenreIdent, GenreIdent,
FrameCommentIdent, CommentIdent,
nil] retain];
m_emptyIdentRow = [[NSDictionary dictionaryWithObjectsAndKeys:
@"", ArtistIdent,
@"", AlbumIdent,
@"", TrackNumberIdent,
@"", TrackTitleIdent,
@"", YearIdent,
@"", GenreIdent,
// nil, CommentIdent, // Not necessary and also not possible because obj=nil
nil] retain];
}
- (id)init
{
if (self = [super init]) {
m_tableArray = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[m_tableArray release];
[super dealloc];
}
/*
* Delegate
*/
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)col row:(int)rowIndex
{
if ([[col identifier] isEqualTo:FilenameIdent]) {
[cell setBackgroundColor:m_filenameColor];
[cell setDrawsBackground:TRUE];
}
}
/*
* Data source
*/
- (int)numberOfRowsInTableView:(NSTableView*)tv
{
return [m_tableArray count];
}
- (id)tableView:(NSTableView*)tv objectValueForTableColumn:(NSTableColumn*)col row:(int)rowIndex
{
return [[m_tableArray objectAtIndex:rowIndex] objectForKey:[col identifier]];
}
- (void)tableView:(NSTableView*)tv setObjectValue:(id)value forTableColumn:col row:(int)rowIndex {
[[m_tableArray objectAtIndex:rowIndex] setObject:value forKey:[col identifier]];
}
/*
* Adding and removing files (= paths)
*/
- (BOOL)_isfilePathAlreadyRegistered:(NSString*)fpath
{
NSEnumerator* en;
NSDictionary* dict;
en = [m_tableArray objectEnumerator];
while (dict = [en nextObject]) {
if ([[dict objectForKey:FilePathIdent] isEqualTo:fpath])
return TRUE;
}
return FALSE;
}
- (void)_readComment:(NSMutableDictionary*)dict field:(union id3_field*)afield {
id3_ucs4_t* ustr;
NSMutableDictionary* cd;
// Create comment dict
cd = [dict objectForKey:CommentIdent];
if (cd == nil) {
cd = [[NSMutableDictionary alloc] initWithCapacity:1];
[dict setObject:cd forKey:CommentIdent];
}
#define UCS4_TO_NSTRING(METHOD, IDENT) \
ustr = (id3_ucs4_t*)id3_ucs4_utf16duplicate(METHOD); \
[cd setObject:[NSString stringWithFormat:@"%S", ustr] forKey:IDENT]; \
free(ustr);
switch (id3_field_type(afield)) {
case ID3_FIELD_TYPE_TEXTENCODING :
[cd setObject:[NSNumber numberWithLong:afield->number.value] forKey:CommentTextEncIdent]; // no method ?
break;
case ID3_FIELD_TYPE_LANGUAGE :
[cd setObject:[NSString stringWithFormat:@"%c%c%c",
afield->immediate.value[0], afield->immediate.value[1], afield->immediate.value[2]]
forKey:CommentLanguageIdent]; // No method ?
break;
case ID3_FIELD_TYPE_STRING :
UCS4_TO_NSTRING(id3_field_getstring(afield), CommentShortTextIdent)
break;
case ID3_FIELD_TYPE_STRINGFULL :
UCS4_TO_NSTRING(id3_field_getfullstring(afield), CommentFullTextIdent)
break;
default :
// Unsupported type
return;
}
}
- (NSDictionary*)_createFramesDictForTag:(struct id3_tag*)itag
{
NSMutableDictionary* dict;
unsigned i, j;
struct id3_frame* aframe;
union id3_field* afield;
unsigned nstr;
id3_utf16_t* ustr;
NSString* ftxt;
BOOL isComment;
dict = [[NSMutableDictionary dictionary] retain];
for (i = 0; i < itag->nframes; i++) {
aframe = itag->frames[i];
isComment = (BOOL)!strncmp(aframe->id, ID3_FRAME_COMMENT, 4);
for (j = 0; j < aframe->nfields; j++) {
afield = id3_frame_field(aframe, j);
// All fields of a comment are stored
if (isComment)
[self _readComment:dict field:afield];
// Only string for the rest of the frames
else {
nstr = id3_field_getnstrings(afield);
if (nstr > 0) {
ustr = id3_ucs4_utf16duplicate(id3_field_getstrings(afield, 0));
ftxt = [NSString stringWithFormat:@"%S", ustr];
// Special handling for genres
if (!strcmp(aframe->id, ID3_FRAME_GENRE)) {
if (([ftxt length] == 1) || ([ftxt length] == 2))
ftxt = v1GenreToString([ftxt intValue]);
}
// Store it
if (ftxt != nil)
[dict setObject:ftxt forKey:[NSString stringWithCString:aframe->id]];
free(ustr);
}
}
}
}
////
// NSLog(@"%@", dict);
return dict;
}
- (NSString*)_emptyIfNilString:(NSString*)str
{
if (str == nil)
return @"";
return str;
}
- (BOOL)_addFile:(NSString*)fpath
{
NSFileHandle* fh;
NSString* fname;
struct id3_file* ifile;
struct id3_tag* itag;
NSDictionary* tagDict;
unsigned i, firstOfs, lastOfs, fileSize;
BOOL hasV1;
fname = [fpath lastPathComponent];
if (![[[fname pathExtension] lowercaseString] isEqualToString:FileExtension])
return TRUE;
// Read tag
fh = [[NSFileHandle fileHandleForReadingAtPath:fpath] retain];
if (fh == nil) {
if (NSRunAlertPanel(@"Open error", @"Failed to open\n'%@'",
@"Continue", @"Stop adding files", nil, fpath) == NSAlertDefaultReturn)
return TRUE;
return FALSE;
}
ifile = id3_file_fdopen([fh fileDescriptor], ID3_FILE_MODE_READONLY);
if (ifile == NULL) {
NSLog(@"id3_file_open failed");
return TRUE;
}
itag = id3_file_tag(ifile);
if (itag == NULL) {
NSLog(@"id3_file_tag failed");
return TRUE;
}
tagDict = [self _createFramesDictForTag:itag];
// Determine the data size & ofs
firstOfs = 0;
fileSize = (unsigned)[fh seekToEndOfFile];
lastOfs = fileSize;
// NSLog(@"lastOfs = %lu", lastOfs);
for (i = 0; i < ifile->ntags; i++) {
if (ifile->tags[i].location == 0)
firstOfs = ifile->tags[i].length;
else if (ifile->tags[i].location < lastOfs)
lastOfs = ifile->tags[i].location;
}
hasV1 = (BOOL)(lastOfs < fileSize);
// NSLog(@"datasize: ofs=%lu, size=%lu, lastOfs=%lu", firstOfs, lastOfs-firstOfs, lastOfs);
// Free the tag memory
id3_tag_delete(itag);
id3_file_close(ifile);
// Add
NSMutableDictionary* fdict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
fpath, FilePathIdent,
fname, FilenameIdent,
[self _emptyIfNilString:[tagDict objectForKey:FrameArtistIdent]], ArtistIdent,
[self _emptyIfNilString:[tagDict objectForKey:FrameAlbumIdent]], AlbumIdent,
[self _emptyIfNilString:[tagDict objectForKey:FrameTrackIdent]], TrackNumberIdent,
[self _emptyIfNilString:[tagDict objectForKey:FrameTitleIdent]], TrackTitleIdent,
[self _emptyIfNilString:[tagDict objectForKey:FrameYearIdent]], YearIdent,
[self _emptyIfNilString:[tagDict objectForKey:FrameGenreIdent]], GenreIdent,
[NSNumber numberWithBool:FALSE], CompilationIdent,
[NSNumber numberWithUnsignedInt:firstOfs], DataOfsIdent,
[NSNumber numberWithUnsignedInt:(lastOfs - firstOfs)], DataSizeIdent,
[NSNumber numberWithBool:hasV1], HasV1TagIdent,
[NSNumber numberWithUnsignedInt:lastOfs], V1TagOfsIdent,
nil];
if ([tagDict objectForKey:CommentIdent] != nil)
[fdict setObject:[tagDict objectForKey:CommentIdent] forKey:CommentIdent];
[m_tableArray addObject:fdict];
// NSLog(@"last = %@", [m_tableArray lastObject]);
return TRUE;
}
- (NSArray*)_filepathsForDirectory:(NSString*)dir
{
NSArray* nfWOPath;
NSMutableArray* nfWithPath;
NSEnumerator* en;
NSString* name;
nfWOPath = [m_fileManager directoryContentsAtPath:dir];
nfWithPath = [NSMutableArray arrayWithCapacity:[nfWOPath count]];
en = [nfWOPath objectEnumerator];
while (name = [en nextObject]) {
if ([name characterAtIndex:0] != '.')
[nfWithPath addObject:[dir stringByAppendingFormat:@"/%@", name]];
}
return nfWithPath;
}
- (void)addFiles:(NSArray*)files
{
NSEnumerator* en;
NSString* fpath;
BOOL isDir;
en = [[files sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectEnumerator];
while (fpath = [en nextObject]) {
if ([m_fileManager fileExistsAtPath:fpath isDirectory:&isDir] && isDir) {// dir
[self addFiles:[self _filepathsForDirectory:fpath]];
} else if (![self _isfilePathAlreadyRegistered:fpath]) // file
if (![self _addFile:fpath])
break;
}
}
- (void)removeAllFiles
{
[m_tableArray removeAllObjects];
}
- (unsigned)numOfRows
{
return [m_tableArray count];
}
- (NSString*)stringForRow:(unsigned)row andIdent:(NSString*)ident;
{
return [[m_tableArray objectAtIndex:row] objectForKey:ident];
}
- (void)setStringForRow:(unsigned)row andIdent:(NSString*)ident toString:(NSString*)tostr {
[[m_tableArray objectAtIndex:row] setObject:tostr forKey:ident];
}
- (BOOL)rowIsInCompilation:(unsigned)row
{
return [[[m_tableArray objectAtIndex:row] objectForKey:CompilationIdent] boolValue];
}
- (void)setIsCompilationForRow:(unsigned)row value:(BOOL)iscomp
{
[[m_tableArray objectAtIndex:row] setObject:[NSNumber numberWithBool:iscomp] forKey:CompilationIdent];
}
/*
* Update table data or change tags / filenames
*/
- (BOOL)_createDirectoryRecursive:(NSString*)dir
{
if (![m_fileManager fileExistsAtPath:dir]) {
if (![self _createDirectoryRecursive:[dir stringByDeletingLastPathComponent]])
return FALSE;
return [m_fileManager createDirectoryAtPath:dir attributes:nil];
}
return TRUE;
}
- (BOOL)_moveFileFrom:(NSString*)oldPath to:(NSString*)newPath
{
unsigned idx = 1;
if ([oldPath isEqualTo:newPath])
return TRUE;
while ([m_fileManager fileExistsAtPath:newPath]) {
newPath = [NSString stringWithFormat:@"%@-%u.%@", [newPath stringByDeletingPathExtension], idx, FileExtension];
idx++;
}
if (![m_fileManager movePath:oldPath toPath:newPath handler:nil]) {
NSBeginAlertSheet(@"File problems", @"Skip file", nil, nil,
[NSApp mainWindow], self, nil, nil, nil,
@"Failed to move '%@' to '%@'", oldPath, newPath);
return FALSE;
}
// NSLog(@"mv %@ -> %@", oldPath, newPath);
return TRUE;
}
- (NSString*)_outputDirPanel
{
NSOpenPanel* dirPanel;
dirPanel = [NSOpenPanel openPanel];
[dirPanel setCanChooseFiles:FALSE];
[dirPanel setCanChooseDirectories:TRUE];
[dirPanel setTitle:@"Select the target directory"];
[dirPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil];
return [[dirPanel filenames] objectAtIndex:0];
}
- (void)_InsertDefaultValuesIntoDictionary:(NSMutableDictionary*)dict withOpts:(id)optObj
{
if ([[optObj defaultArtist] length] > 0)
if ([optObj forceDefaultValues] || ([(NSString*)[dict objectForKey:ArtistIdent] length] == 0))
[dict setObject:[optObj defaultArtist] forKey:ArtistIdent];
if ([[optObj defaultAlbum] length] > 0)
if ([optObj forceDefaultValues] || ([(NSString*)[dict objectForKey:AlbumIdent] length] == 0))
[dict setObject:[optObj defaultAlbum] forKey:AlbumIdent];
if ([[optObj defaultYear] length] > 0)
if ([optObj forceDefaultValues] || ([(NSString*)[dict objectForKey:YearIdent] length] == 0))
[dict setObject:[optObj defaultYear] forKey:YearIdent];
if ([[optObj defaultGenre] length] > 0)
if ([optObj forceDefaultValues] || ([(NSString*)[dict objectForKey:GenreIdent] length] == 0))
[dict setObject:[optObj defaultGenre] forKey:GenreIdent];
}
- (NSMutableDictionary*)_createAlbumDictWithOpts:(id)optObj
{
NSMutableDictionary* row, *albumDict;
unsigned i;
NSString* albumTitle;
NSMutableArray* sa;
albumDict = [NSMutableDictionary dictionary];
for (i = 0; i < [m_tableArray count]; i++) {
// Join with default values
row = [NSMutableDictionary dictionaryWithDictionary:[m_tableArray objectAtIndex:i]];
[self _InsertDefaultValuesIntoDictionary:row withOpts:optObj];
// Store the index
[row setObject:[NSNumber numberWithUnsignedInt:i] forKey:ArrayIndexIdent];
// Get the album title
albumTitle = [row objectForKey:AlbumIdent];
if ([albumTitle length] == 0)
albumTitle = Unknown;
// Create the key=album, value="row" dict
sa = [albumDict objectForKey:albumTitle];
if (sa == nil) {
sa = [NSMutableArray array];
[albumDict setObject:sa forKey:albumTitle];
}
[sa addObject:row];
}
return [albumDict retain];
}
- (BOOL)_isCompilation:(NSArray*)tracks
{
if ([tracks count] >= 2) {
unsigned i;
for (i = 1; i < [tracks count]; i++) {
if (![[[tracks objectAtIndex:i] objectForKey:ArtistIdent] isEqualTo:[[tracks objectAtIndex:i-1] objectForKey:ArtistIdent]])
return TRUE;
}
}
return FALSE;
}
- (void)allTagsToFolderSortedFilenamesForPattern:(NSString*)patStr withOpts:(id)optObj
{
FFTagToFilename* ttf;
NSMutableDictionary* albumDict, *tarow;
NSEnumerator* en;
NSDictionary* row;
NSString* albumTitle;
NSMutableArray* sa;
NSString* outDir;
// Get target dir from the user
outDir = [optObj outDirectory];
if (outDir == nil)
outDir = [self _outputDirPanel];
// Create album dictionary (key = album title)
albumDict = [self _createAlbumDictWithOpts:optObj];
// Handle each album
ttf = [[FFTagToFilename alloc] initWithPattern:patStr andOpts:optObj];
en = [albumDict keyEnumerator];
while (albumTitle = [en nextObject]) {
NSString* newODir, *prevODir = nil, *newFname, *oldFPath, *newFPath;
BOOL noAlbumTitle;
unsigned i;
sa = [albumDict objectForKey:albumTitle];
// Outpath
if (![albumTitle isEqualTo:Unknown]) {
noAlbumTitle = FALSE;
if ([self _isCompilation:sa])
newODir = [outDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@", [FFTagToFilename replaceInvalidCharacters:Compilations], [FFTagToFilename replaceInvalidCharacters:albumTitle]]];
else {
NSString* artist = [[sa objectAtIndex:0] objectForKey:ArtistIdent];
newODir = [outDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@", [FFTagToFilename replaceInvalidCharacters:artist], [FFTagToFilename replaceInvalidCharacters:albumTitle]]];
}
if (![self _createDirectoryRecursive:newODir]) {
NSBeginAlertSheet(@"Access permissions", @"Skip this album", nil, nil,
[NSApp mainWindow], self, nil, nil, nil,
@"Failed to create the directory '%@'", newODir);
continue;
}
// NSLog(@"directory: %@", newODir);
} else
noAlbumTitle = TRUE;
// All files
for (i = 0; i < [sa count]; i++) {
row = [sa objectAtIndex:i];
newFname = [ttf filenameFromTag:row];
if (newFname != nil) {
oldFPath = [row objectForKey:FilePathIdent];
if (noAlbumTitle) {
NSString* artist = [row objectForKey:ArtistIdent];
if ([artist length] == 0)
newODir = [outDir stringByAppendingPathComponent:Unknown];
else
newODir = [outDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@", [FFTagToFilename replaceInvalidCharacters:[row objectForKey:ArtistIdent]], Unknown]];
if ((prevODir == nil) || ![newODir isEqualTo:prevODir]) {
if (![self _createDirectoryRecursive:newODir]) {
continue;
}
}
prevODir = newODir; // retain ?
}
newFPath = [newODir stringByAppendingPathComponent:[newFname stringByAppendingPathExtension:FileExtension]];
tarow = [m_tableArray objectAtIndex:[(NSNumber*)[row objectForKey:ArrayIndexIdent] intValue]];
if ([self _moveFileFrom:oldFPath to:newFPath])
// [m_tableArray removeObjectAtIndex:[(NSNumber*)[row objectForKey:ArrayIndexIdent] unsignedIntValue]];
[tarow setObject:[NSNumber numberWithBool:TRUE] forKey:RemovedWhileSortingIdent];
else
[tarow setObject:[NSNumber numberWithBool:FALSE] forKey:RemovedWhileSortingIdent];
}
}
}
[ttf release];
// Remove successfully moved files from filetable
int idx = [m_tableArray count]-1;
while (idx >= 0) {
if ([[[m_tableArray objectAtIndex:idx] objectForKey:RemovedWhileSortingIdent] boolValue])
[m_tableArray removeObjectAtIndex:idx];
idx--;
}
}
- (BOOL)_renameFileAtIndex:(unsigned)index toFilename:(NSString*)fname withOpt:(id)optObj
{
NSString* oldPath, *newPath;
oldPath = [[m_tableArray objectAtIndex:index] objectForKey:FilePathIdent];
newPath = [optObj outDirectory];
if (newPath == nil)
newPath = [oldPath stringByDeletingLastPathComponent];
else
[self _createDirectoryRecursive:newPath];
newPath = [newPath stringByAppendingPathComponent:fname];
return [self _moveFileFrom:oldPath to:newPath];
}
- (void)allTagsToFilenamesForPattern:(NSString*)patStr withOpts:(id)optObj shouldApply:(BOOL)apply
{
FFTagToFilename* ttf;
unsigned i;
NSMutableDictionary* ctag;
NSString* newFname;
BOOL entryWasRemoved;
ttf = [[FFTagToFilename alloc] initWithPattern:patStr andOpts:optObj];
if (ttf == nil)
return;
i = 0;
while (i < [m_tableArray count]) {
entryWasRemoved = FALSE;
// Merge with defaults
ctag = [NSMutableDictionary dictionaryWithDictionary:[m_tableArray objectAtIndex:i]];
[self _InsertDefaultValuesIntoDictionary:ctag withOpts:optObj];
newFname = [ttf filenameFromTag:ctag];
// Show or apply
if (newFname != nil) {
newFname = [newFname stringByAppendingPathExtension:FileExtension];
if (apply) {
// NSLog(@"newfilename : %@", newFname);
if ([self _renameFileAtIndex:i toFilename:newFname withOpt:optObj]) {
[m_tableArray removeObjectAtIndex:i];
entryWasRemoved = TRUE;
}
} else
[[m_tableArray objectAtIndex:i] setObject:newFname forKey:FilenameIdent];
}
if (!entryWasRemoved)
i++;
}
[ttf release];
}
/*
Unused code !
The ID3 lib from id3lib.org ... has problems with unicode
*/
/*
- (void)addFrameToTag:(ID3Tag*)tag withID:(ID3_FrameID)frameID usingText:(NSString*)str
{
ID3Frame* frame;
ID3Field* field;
unsigned int clen;
if (str == NULL)
return;
clen = [str length];
if (clen == 0)
return;
// Create the frame
frame = ID3Frame_NewID(frameID);
if (frame == NULL) {
free(ubuf);
return;
}
// Encoding
field = ID3Frame_GetField(frame, ID3FN_TEXTENC);
ID3Field_SetINT(field, ID3TE_UNICODE);
// Text
field = ID3Frame_GetField(frame, ID3FN_TEXT);
ID3Field_SetEncoding(field, ID3TE_UNICODE);
// ID3Field_SetUNICODE(field, (unicode_t*)ubuf);
ID3Tag_AddFrame(tag, frame);
free(ubuf);
}
- (BOOL)changeTagInFile:(NSString*)fpath toTags:(NSDictionary*)newTag
{
ID3Tag* tag;
ID3_Err errCode;
tag = ID3Tag_New();
if (tag == NULL) {
NSLog(@"New failed");
return FALSE;
}
ID3Tag_Link(tag, [fpath fileSystemRepresentation]);
ID3Tag_Clear(tag);
[self addFrameToTag:tag withID:ID3FID_LEADARTIST usingText:[newTag objectForKey:ArtistIdent]];
[self addFrameToTag:tag withID:ID3FID_ALBUM usingText:[newTag objectForKey:AlbumIdent]];
[self addFrameToTag:tag withID:ID3FID_TRACKNUM usingText:[newTag objectForKey:TrackNumberIdent]];
[self addFrameToTag:tag withID:ID3FID_TITLE usingText:[newTag objectForKey:TrackTitleIdent]];
[self addFrameToTag:tag withID:ID3FID_YEAR usingText:[newTag objectForKey:YearIdent]];
ID3Tag_SetPadding(tag, FALSE);
ID3Tag_SetUnsync(tag, FALSE);
errCode = ID3Tag_UpdateByTagType(tag, ID3TT_ID3V2);
if (errCode != ID3E_NoError)
NSLog(@"Update failed! %d", errCode);
ID3Tag_Delete(tag);
return TRUE;
}
*/
- (void)hexdump:(NSData*)data ofs:(unsigned)ofs length:(unsigned)len desc:(NSString*)desc
{
unsigned i;
unsigned char* ucptr;
NSLog(desc);
ucptr = (unsigned char*)[data bytes];
for (i = 0; i < len; i++) {
fprintf(stderr, "%02X(%c) ", ucptr[ofs+i], ucptr[ofs+i]);
if (i % 16 == 0)
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
- (void)addID3v1EntryToData:(NSMutableData*)data usingTag:(NSDictionary*)newTag
usingIdent:(NSString*)ident maxLen:(unsigned)maxLen andPos:(unsigned)pos
{
NSString* id3frameText = [newTag objectForKey:ident];
if ([id3frameText length] > 0) {
NSData* td = [id3frameText dataUsingEncoding:NSISOLatin1StringEncoding];
unsigned tl = [td length];
if (tl == 0) // Nothing left after data encoding
return;
if (tl > maxLen-1) // 30 - '\0'
tl = maxLen-1;
[data replaceBytesInRange:NSMakeRange(pos, tl) withBytes:[td bytes]];
}
}
- (BOOL)changeTagInFile:(NSString*)fpath withOpts:(id)optObj
toTags:(NSDictionary*)newTag withOldTag:(NSDictionary*)oldTag
{
// V2
#define ID3V2_HEADER_SIZE 10
static const unsigned char ID3v2Header[ID3V2_HEADER_SIZE] = {
'I', 'D', '3', // magic
0x03, 0x00, // version
0x00, // flags (no unsyc, no ext.header, no exp. indicator
0x00, 0x00, 0x00, 0x00 // tag size (calculated later)
};
#define ENC_TAG_SIZE(UL) \
(UL & 0x0000007F) | \
((UL << 1) & 0x00007F00) | \
((UL << 2) & 0x007F0000) | \
((UL << 3) & 0x7F000000)
#define SET_TAG_SIZE(BUF, TS) \
*((unsigned long*)(((void*)BUF)+6)) = htonl(ENC_TAG_SIZE(TS));
#define ID3V2_FRAME_BEGIN_SIZE 11
static unsigned char ID3v2FrameBegin[ID3V2_FRAME_BEGIN_SIZE] = {
0x00, 0x00, 0x00, 0x00, // frame ID (z.b. TIT2)
0x00, 0x00, 0x00, 0x00, // frame size
0x00, 0x00, // flags (preserve if altered)
0x00, // encoding
};
#define SET_FRAME_ID(ID) \
*((unsigned long*)ID3v2FrameBegin) = *((unsigned long*)ID);
#define SET_FRAME_SIZE(FS) \
*((unsigned long*)&ID3v2FrameBegin[4]) = htonl(FS);
#define ID3V2_ENCODING_ISO_8859_1 0x00
#define ID3V2_ENCODING_UTF16 0x01
#define SET_FRAME_ENCODING(ENC) \
ID3v2FrameBegin[10] = ENC;
#define ID3V2_NUM_COMMENT_FIELDS 4
static unsigned char ID3v2NoLanguage[3] = {
0x00, 0x00, 0x00
};
// V1
#define ID3V1_TAG_SIZE 128
static const char ID3v1Ident[3] = { 'T', 'A', 'G' };
#define ID3V1_TAG_BYTE(VAR, POS, VALUE) \
*((unsigned char*)([VAR mutableBytes]+POS)) = (unsigned char)VALUE;
//
NSMutableData* tag;
NSEnumerator* en;
NSString* key, *id3frameID, *id3frameText;
char frameID[4+1];
unsigned long frameSize, tagSize;
NSStringEncoding useEnc;
NSString* workPath;
NSFileHandle* inFH, *outFH;
int genreIndex = GENRE_CUSTOM;
BOOL isComment;
NSDictionary* cmtDict;
unsigned dataOfs;
BOOL createNewFile, moveOtherDir;
tag = [NSMutableData dataWithBytes:ID3v2Header length:ID3V2_HEADER_SIZE];
// Create all frames
en = [newTag keyEnumerator];
while (key = [en nextObject]) {
id3frameID = [m_identToFrame objectForKey:key];
if (id3frameID == nil)
continue;
if ([id3frameID isEqualToString:FrameCommentIdent]) {
cmtDict = [newTag objectForKey:key];
if ((cmtDict == nil) || ([cmtDict count] != ID3V2_NUM_COMMENT_FIELDS))
continue;
isComment = TRUE;
} else {
id3frameText = [newTag objectForKey:key];
if ([id3frameText length] == 0)
continue;
isComment = FALSE;
}
// Frame header
[id3frameID getCString:frameID maxLength:5];
SET_FRAME_ID(frameID);
// Comment frame contains more than a simple string...
if (isComment) {
NSString* st = [cmtDict objectForKey:CommentShortTextIdent],
*ft = [cmtDict objectForKey:CommentFullTextIdent],
*lang = [cmtDict objectForKey:CommentLanguageIdent];
char tenc = [[cmtDict objectForKey:CommentTextEncIdent] charValue];
frameSize = [st length] + [ft length] + 5; // 5 = enc, lang(3), '\0'
SET_FRAME_SIZE(frameSize);
[tag appendBytes:ID3v2FrameBegin length:ID3V2_FRAME_BEGIN_SIZE];
SET_FRAME_ENCODING(tenc);
if ([lang length] == 0)
[tag appendBytes:ID3v2NoLanguage length:3];
else
[tag appendBytes:[lang cString] length:3];
[tag appendData:[st dataUsingEncoding:NSISOLatin1StringEncoding]];
[tag appendBytes:ID3v2NoLanguage length:1];
[tag appendData:[ft dataUsingEncoding:NSISOLatin1StringEncoding]];
// Not a comment frame
} else {
// Genre
if (id3frameID == FrameGenreIdent) {
genreIndex = v1GenreFromString(id3frameText);
if (genreIndex != GENRE_CUSTOM)
id3frameText = [NSString stringWithFormat:@"(%d)", genreIndex]; // itunes only ?
}
/* // Latin1 encoding
if ([id3frameText canBeConvertedToEncoding:NSISOLatin1StringEncoding]) {
frameSize = [id3frameText length]+2; // +2 = latin1-ID, '\0'
useEnc = NSISOLatin1StringEncoding;
SET_FRAME_ENCODING(ID3V2_ENCODING_ISO_8859_1);
NSLog(@"can be encoded with latin 1");
// UTF16 encoding
} else {
*/
frameSize = (([id3frameText length]+1) << 1)+3; // +3 = UTF16-ID, UTF-endianess (2 byte)
useEnc = NSUnicodeStringEncoding;
SET_FRAME_ENCODING(ID3V2_ENCODING_UTF16);
// }
SET_FRAME_SIZE(frameSize);
[tag appendBytes:ID3v2FrameBegin length:ID3V2_FRAME_BEGIN_SIZE];
// Now the text
[tag appendData:[id3frameText dataUsingEncoding:useEnc]];
if (useEnc == NSUnicodeStringEncoding)
[tag increaseLengthBy:2]; // 0-termination
else
[tag increaseLengthBy:1];
}
}
dataOfs = [[oldTag objectForKey:DataOfsIdent] unsignedIntValue]; // = Size of prev. v1 tag
// Padding
if (dataOfs >= [tag length]) {
createNewFile = FALSE;
[tag increaseLengthBy:(dataOfs - [tag length])];
} else {
createNewFile = TRUE;
if ([optObj padTag]) {
unsigned padsize, fsize = [[oldTag objectForKey:DataSizeIdent] unsignedIntValue]; // datasize
fsize += [tag length];
if ([optObj generateV1Tag])
fsize += ID3V1_TAG_SIZE;
padsize = fsize % PaddingAssumedBlockSize;
if (padsize < PaddingMinPadSize)
padsize = PaddingAssumedBlockSize;
[tag increaseLengthBy:padsize];
}
}
// NSLog(@"creating new file... = %d", createNewFile);
// Fix the tag size
tagSize = [tag length] - ID3V2_HEADER_SIZE;
SET_TAG_SIZE([tag mutableBytes], tagSize);
// Create a new file w/ the v2 tag and the data
if (createNewFile) {
// Write the tag
workPath = [fpath stringByAppendingString:@"-tritag"];
if (![m_fileManager createFileAtPath:workPath contents:tag attributes:nil])
return FALSE;
// Copy the mp3 data itself
inFH = [NSFileHandle fileHandleForReadingAtPath:fpath];
if (inFH == NULL) {
NSBeginAlertSheet(@"File permissions", @"Skip file", nil, nil,
[NSApp mainWindow], self, nil, nil, nil,
@"Failed to reopen '%@'", fpath);
[m_fileManager removeFileAtPath:workPath handler:nil];
return FALSE;
}
[inFH seekToFileOffset:[[oldTag objectForKey:DataOfsIdent] unsignedLongLongValue]];
outFH = [NSFileHandle fileHandleForUpdatingAtPath:workPath];
[outFH seekToEndOfFile];
[outFH writeData:
[inFH readDataOfLength:[[oldTag objectForKey:DataSizeIdent] unsignedIntValue]]];
[inFH closeFile];
// Just overwrite the old tag
} else {
outFH = [NSFileHandle fileHandleForUpdatingAtPath:fpath]; // seeks to 0
[outFH writeData:tag];
}
// Also generate v1 tag if wanted
if ([optObj generateV1Tag]) {
int trackNo;
tag = [NSMutableData dataWithLength:ID3V1_TAG_SIZE];
[tag replaceBytesInRange:NSMakeRange(0, 3) withBytes:ID3v1Ident];
[self addID3v1EntryToData:tag usingTag:newTag usingIdent:TrackTitleIdent maxLen:30 andPos:3];
[self addID3v1EntryToData:tag usingTag:newTag usingIdent:ArtistIdent maxLen:30 andPos:33];
[self addID3v1EntryToData:tag usingTag:newTag usingIdent:AlbumIdent maxLen:30 andPos:63];
[self addID3v1EntryToData:tag usingTag:newTag usingIdent:YearIdent maxLen:5 andPos:93];
// IDv1.1 track no
id3frameText = [newTag objectForKey:TrackNumberIdent];
if ([id3frameText length] > 0) {
trackNo = [id3frameText intValue];
if ((trackNo >= 0) && (trackNo <= 255)) // {
ID3V1_TAG_BYTE(tag, 126, trackNo);
// *((unsigned char*)([tag mutableBytes]+126)) = (unsigned char)trackNo;
// }
}
// Genre - only when v1 compatible
if (genreIndex != GENRE_CUSTOM)