forked from gdbinit/MachOView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataController.mm
1396 lines (1205 loc) · 42.1 KB
/
DataController.mm
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
/*
* DataController.mm
* MachOView
*
* Created by psaghelyi on 15/06/2010.
*
*/
#import "Common.h"
#import "DataController.h"
#import "MachOLayout.h"
#import "FatLayout.h"
#import "ArchiveLayout.h"
#import <mach-o/loader.h>
#import <mach-o/fat.h>
#import <mach-o/swap.h>
enum {
MVUnderlineAttributeOrdinal = 1,
MVCellColorAttributeOrdinal,
MVTextColorAttributeOrdinal,
MVMetaDataAttributeOrdinal
};
enum {
MVBlackColorOrdinal = 1,
MVDarkGrayColorOrdinal,
MVLightGrayColorOrdinal,
MVWhiteColorOrdinal,
MVGrayColorOrdinal,
MVRedColorOrdinal,
MVGreenColorOrdinal,
MVBlueColorOrdinal,
MVCyanColorOrdinal,
MVYellowColorOrdinal,
MVMagentaColorOrdinal,
MVOrangeColorOrdinal,
MVPurpleColorOrdinal,
MVBrownColorOrdinal
};
NSString * const MVUnderlineAttributeName = @"MVUnderlineAttribute";
NSString * const MVCellColorAttributeName = @"MVCellColorAttribute";
NSString * const MVTextColorAttributeName = @"MVTextColorAttribute";
NSString * const MVMetaDataAttributeName = @"MVMetaDataAttribute";
NSString * const MVLayoutUserInfoKey = @"MVLayoutUserInfoKey";
NSString * const MVNodeUserInfoKey = @"MVNodeUserInfoKey";
NSString * const MVStatusUserInfoKey = @"MVStatusUserInfoKey";
NSString * const MVDataTreeWillChangeNotification = @"MVDataTreeWillChangeNotification";
NSString * const MVDataTreeDidChangeNotification = @"MVDataTreeDidChangeNotification";
NSString * const MVDataTreeChangedNotification = @"MVDataTreeChanged";
NSString * const MVDataTableChangedNotification = @"MVDataTableChanged";
NSString * const MVThreadStateChangedNotification = @"MVThreadStateChanged";
NSString * const MVStatusTaskStarted = @"MVStatusTaskStarted";
NSString * const MVStatusTaskTerminated = @"MVStatusTaskTerminated";
//============================================================================
@implementation MVColumns
@synthesize offsetStr, dataStr, descriptionStr, valueStr;
//-----------------------------------------------------------------------------
- (instancetype)init
{
self = [super init];
if (self)
{
#ifdef MV_STATISTICS
OSAtomicIncrement64(&nrow_loaded);
#endif
}
return self;
}
//-----------------------------------------------------------------------------
-(id)initWithData:(NSString *)col0 :(NSString *)col1 :(NSString *)col2 :(NSString *)col3
{
if (self = [super init])
{
offsetStr = col0;
dataStr = col1;
descriptionStr = col2;
valueStr = col3;
#ifdef MV_STATISTICS
OSAtomicIncrement64(&nrow_loaded);
#endif
}
return self;
}
//-----------------------------------------------------------------------------
+(MVColumns *) columnsWithData:(NSString *)col0 :(NSString *)col1 :(NSString *)col2 :(NSString *)col3
{
return [[MVColumns alloc] initWithData:col0:col1:col2:col3];
}
//-----------------------------------------------------------------------------
-(void)dealloc
{
#ifdef MV_STATISTICS
OSAtomicDecrement64(&nrow_loaded);
#endif
}
@end
//============================================================================
@implementation MVRow
@synthesize columns, attributes, offset, deleted, dirty;
//-----------------------------------------------------------------------------
- (instancetype)init
{
self = [super init];
if (self)
{
#ifdef MV_STATISTICS
OSAtomicIncrement64(&nrow_total);
#endif
}
return self;
}
//-----------------------------------------------------------------------------
-(void)dealloc
{
#ifdef MV_STATISTICS
OSAtomicDecrement64(&nrow_total);
#endif
}
//-----------------------------------------------------------------------------
-(NSString *)columnAtIndex:(NSUInteger)index
{
switch (index)
{
case OFFSET_COLUMN: return columns.offsetStr;
case DATA_COLUMN: return columns.dataStr;
case DESCRIPTION_COLUMN: return columns.descriptionStr;
case VALUE_COLUMN: return columns.valueStr;
}
return nil;
}
//-----------------------------------------------------------------------------
-(void)replaceColumnAtIndex:(NSUInteger)index withString:(NSString *)str
{
columnsOffset = 0;
switch (index)
{
case OFFSET_COLUMN: columns.offsetStr = str; break;
case DATA_COLUMN: columns.dataStr = str; break;
case DESCRIPTION_COLUMN: columns.descriptionStr = str; break;
case VALUE_COLUMN: columns.valueStr = str; break;
}
}
//-----------------------------------------------------------------------------
- (void)writeString:(NSString *)str toFile:(FILE *)pFile
{
if (str) {
fwrite(CSTRING(str), [str length] + 1, 1, pFile);
} else {
fputc('\0', pFile);
}
}
//-----------------------------------------------------------------------------
- (NSString *)readStringFromFile:(FILE *)pFile
{
std::string s;
for(;;)
{
char c = fgetc(pFile);
if (!feof(pFile) && c)
s += c;
else
break;
}
return NSSTRING(s.c_str());
}
//-----------------------------------------------------------------------------
- (void)writeColor:(NSColor *)color toFile:(FILE *)pFile
{
int colorOrdinal = [color isEqualTo:[NSColor blackColor]] ? MVBlackColorOrdinal
: [color isEqualTo:[NSColor darkGrayColor]] ? MVDarkGrayColorOrdinal
: [color isEqualTo:[NSColor lightGrayColor]] ? MVLightGrayColorOrdinal
: [color isEqualTo:[NSColor whiteColor]] ? MVWhiteColorOrdinal
: [color isEqualTo:[NSColor grayColor]] ? MVGrayColorOrdinal
: [color isEqualTo:[NSColor redColor]] ? MVRedColorOrdinal
: [color isEqualTo:[NSColor greenColor]] ? MVGreenColorOrdinal
: [color isEqualTo:[NSColor blueColor]] ? MVBlueColorOrdinal
: [color isEqualTo:[NSColor cyanColor]] ? MVCyanColorOrdinal
: [color isEqualTo:[NSColor yellowColor]] ? MVYellowColorOrdinal
: [color isEqualTo:[NSColor magentaColor]] ? MVMagentaColorOrdinal
: [color isEqualTo:[NSColor orangeColor]] ? MVOrangeColorOrdinal
: [color isEqualTo:[NSColor purpleColor]] ? MVPurpleColorOrdinal
: [color isEqualTo:[NSColor brownColor]] ? MVBrownColorOrdinal
: 0;
putc(colorOrdinal, pFile);
if (colorOrdinal == 0) {
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
float fred = red, fgreen = green, fblue = blue, falpha = alpha;
fwrite(&fred, sizeof(float), 1, pFile);
fwrite(&fgreen, sizeof(float), 1, pFile);
fwrite(&fblue, sizeof(float), 1, pFile);
fwrite(&falpha, sizeof(float), 1, pFile);
}
}
//-----------------------------------------------------------------------------
- (NSColor *)readColorFromFile:(FILE *)pFile
{
int colorOrdinal = getc(pFile);
switch (colorOrdinal)
{
case MVBlackColorOrdinal: return [NSColor blackColor];
case MVDarkGrayColorOrdinal: return [NSColor darkGrayColor];
case MVLightGrayColorOrdinal: return [NSColor lightGrayColor];
case MVWhiteColorOrdinal: return [NSColor whiteColor];
case MVGrayColorOrdinal: return [NSColor grayColor];
case MVRedColorOrdinal: return [NSColor redColor];
case MVGreenColorOrdinal: return [NSColor greenColor];
case MVBlueColorOrdinal: return [NSColor blueColor];
case MVCyanColorOrdinal: return [NSColor cyanColor];
case MVYellowColorOrdinal: return [NSColor yellowColor];
case MVMagentaColorOrdinal: return [NSColor magentaColor];
case MVOrangeColorOrdinal: return [NSColor orangeColor];
case MVPurpleColorOrdinal: return [NSColor purpleColor];
case MVBrownColorOrdinal: return [NSColor brownColor];
}
float fred, fgreen, fblue, falpha;
fread(&fred, sizeof(float), 1, pFile);
fread(&fgreen, sizeof(float), 1, pFile);
fread(&fblue, sizeof(float), 1, pFile);
fread(&falpha, sizeof(float), 1, pFile);
return [NSColor colorWithDeviceRed:fred green:fgreen blue:fblue alpha:falpha];
}
//----------------------------------------------------------------------------
- (void)saveAttributestoFile:(FILE *)pFile
{
uint64_t numAttributes = [attributes count];
if (fwrite (&numAttributes, sizeof(uint64_t), 1, pFile) < 1) {
NSLog(@"fwrite failed in saveAttributestoFile:");
return;
}
for (NSString * key in [attributes allKeys]) {
id value = [attributes objectForKey:key];
if (value == nil) {
continue;
}
int keyOrdinal = [key isEqualToString:MVUnderlineAttributeName] ? MVUnderlineAttributeOrdinal
: [key isEqualToString:MVCellColorAttributeName] ? MVCellColorAttributeOrdinal
: [key isEqualToString:MVTextColorAttributeName] ? MVTextColorAttributeOrdinal
: [key isEqualToString:MVMetaDataAttributeName] ? MVMetaDataAttributeOrdinal
: 0;
putc(keyOrdinal, pFile);
switch (keyOrdinal)
{
case MVUnderlineAttributeOrdinal: [self writeString:value toFile:pFile]; break;
case MVCellColorAttributeOrdinal: [self writeColor:value toFile:pFile]; break;
case MVTextColorAttributeOrdinal: [self writeColor:value toFile:pFile]; break;
case MVMetaDataAttributeOrdinal: [self writeString:value toFile:pFile]; break;
default: NSLog(@"warning: unknown attribute key");
}
}
}
//----------------------------------------------------------------------------
- (void)loadAttributesFromFile:(FILE *)pFile
{
uint64_t numAttributes;
fread(&numAttributes, sizeof(uint64_t), 1, pFile);
NSMutableDictionary * _attributes = [[NSMutableDictionary alloc] initWithCapacity:numAttributes];
while (numAttributes-- > 0)
{
int keyOrdinal = getc(pFile);
switch (keyOrdinal)
{
case MVUnderlineAttributeOrdinal: [_attributes setObject:[self readStringFromFile:pFile] forKey:MVUnderlineAttributeName]; break;
case MVCellColorAttributeOrdinal: [_attributes setObject:[self readColorFromFile:pFile] forKey:MVCellColorAttributeName]; break;
case MVTextColorAttributeOrdinal: [_attributes setObject:[self readColorFromFile:pFile] forKey:MVTextColorAttributeName]; break;
case MVMetaDataAttributeOrdinal: [_attributes setObject:[self readStringFromFile:pFile] forKey:MVMetaDataAttributeName]; break;
default: NSLog(@"warning: unknown attribute key");
}
}
attributes = _attributes;
}
//----------------------------------------------------------------------------
- (void)saveToFile:(FILE *)pFile
{
// dont need to seek, we always append new items
if (columnsOffset == 0) { // isSaved == NO
off_t filePos = ftello(pFile);
if (filePos == -1) {
NSLog(@"MVRow saveToFile: ftello failed: %s", strerror(errno));
}
[self writeString:columns.offsetStr toFile:(FILE *)pFile];
[self writeString:columns.dataStr toFile:(FILE *)pFile];
[self writeString:columns.descriptionStr toFile:(FILE *)pFile];
[self writeString:columns.valueStr toFile:(FILE *)pFile];
columnsOffset = filePos;
}
if (dirty) {
// reload the attributes if they are out of cache
if (attributesOffset > 0) {
// import new items
NSMutableDictionary * _attributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
// load old attributes
if (fseeko(pFile, attributesOffset, SEEK_SET) == -1) {
NSLog(@"MVRow saveToFile: fseeko SEEK_SET failed: %s", strerror(errno));
}
[self loadAttributesFromFile:pFile];
if (fseeko(pFile, 0, SEEK_END) == -1) {
NSLog(@"MVRow saveToFile: fseeko SEEK_END failed: %s", strerror(errno));
}
// extend stored attributes with loaded items
[_attributes addEntriesFromDictionary:attributes];
// store extended attributes
attributes = _attributes;
}
off_t filePos = ftello(pFile);
if (filePos == -1) {
NSLog(@"MVRow saveToFile: ftello failed: %s", strerror(errno));
}
[self saveAttributestoFile:(FILE *)pFile];
dirty = NO;
attributesOffset = filePos;
}
}
//----------------------------------------------------------------------------
- (void)loadFromFile:(FILE *)pFile
{
if (columns == nil) {
NSParameterAssert(columnsOffset != 0);
if (fseeko(pFile, columnsOffset, SEEK_SET) == 0) {
columns = [[MVColumns alloc] init];
columns.offsetStr = [self readStringFromFile:pFile];
columns.dataStr = [self readStringFromFile:pFile];
columns.descriptionStr = [self readStringFromFile:pFile];
columns.valueStr = [self readStringFromFile:pFile];
} else {
NSLog(@"*** reading error (columns) '%s'",strerror(errno));
NSParameterAssert(0);
return;
}
}
if (attributes == nil && attributesOffset > 0) {
if (fseeko(pFile, attributesOffset, SEEK_SET) == 0) {
[self loadAttributesFromFile:pFile];
} else {
NSLog(@"*** reading error (attributes) '%s'",strerror(errno));
NSParameterAssert(0);
}
}
}
//----------------------------------------------------------------------------
- (void)saveIndexToFile:(FILE *)pFile
{
fwrite(&offset, sizeof(uint32_t), 1, pFile);
fwrite(&columnsOffset, sizeof(uint32_t), 1, pFile);
fwrite(&attributesOffset, sizeof(uint32_t), 1, pFile);
fwrite(&deleted, sizeof(BOOL), 1, pFile);
}
//----------------------------------------------------------------------------
- (void)loadIndexFromFile:(FILE *)pFile
{
fread(&offset, sizeof(uint32_t), 1, pFile);
fread(&columnsOffset, sizeof(uint32_t), 1, pFile);
fread(&attributesOffset, sizeof(uint32_t), 1, pFile);
fread(&deleted, sizeof(BOOL), 1, pFile);
}
//----------------------------------------------------------------------------
-(BOOL) isSaved
{
return (columnsOffset > 0);
}
//----------------------------------------------------------------------------
-(void) clear
{
if (columnsOffset > 0) // isSaved == YES
{
columns = nil;
if (dirty == NO)
{
attributes = nil;
}
}
}
@end
//============================================================================
@implementation MVTable
@synthesize swapFile;
//-----------------------------------------------------------------------------
- (instancetype)init
{
NSAssert(NO, @"plain init is not allowed");
return nil;
}
//-----------------------------------------------------------------------------
- (instancetype)initWithArchiver:(MVArchiver *)_archiver
{
if (self = [super init])
{
rows = [[NSMutableArray alloc] init];
archiver = _archiver;
tableLock = [[NSLock alloc] init];
}
return self;
}
//----------------------------------------------------------------------------
+(MVTable *) tableWithArchiver:(MVArchiver *)_archiver
{
return [[MVTable alloc] initWithArchiver:_archiver];
}
//----------------------------------------------------------------------------
- (NSUInteger)rowCountToDisplay
{
return [displayRows count];
}
//----------------------------------------------------------------------------
- (MVRow *)getRowToDisplay: (NSUInteger)rowIndex
{
MVRow * row = nil;
if (rowIndex < [displayRows count])
{
row = [displayRows objectAtIndex:rowIndex];
}
if (row != nil)
{
if (row.deleted)
{
row = nil;
}
else if (row.columns == nil)
{
[row loadFromFile:swapFile];
}
}
return row;
}
//----------------------------------------------------------------------------
- (void)insertRowWithOffset:(uint64_t)offset :(id)col0 :(id)col1 :(id)col2 :(id)col3
{
MVRow * row = [[MVRow alloc] init];
row.columns = [MVColumns columnsWithData:col0:col1:col2:col3];
row.offset = offset;
[tableLock lock];
[rows addObject:row];
[tableLock unlock];
[archiver addObjectToSave:row];
}
//----------------------------------------------------------------------------
- (void)appendRow:(id)col0 :(id)col1 :(id)col2 :(id)col3
{
[self insertRowWithOffset:0 :col0:col1:col2:col3];
}
//----------------------------------------------------------------------------
- (void)updateCellContentTo:(id)object atRow:(NSUInteger)rowIndex andCol:(NSUInteger)colIndex
{
MVRow * row = [rows objectAtIndex:rowIndex];
[row replaceColumnAtIndex:colIndex withString:object];
[rows replaceObjectAtIndex:rowIndex withObject:row];
[archiver addObjectToSave:row];
}
//----------------------------------------------------------------------------
- (void)popRow
{
MVRow * row = [rows lastObject];
row.deleted = YES;
}
//----------------------------------------------------------------------------
- (NSUInteger)rowCount
{
return [rows count];
}
//----------------------------------------------------------------------------
// input are name-value pairs
//----------------------------------------------------------------------------
-(NSMutableDictionary *)attributesWithPairs:(id)firstArg :(va_list)args
{
NSMutableDictionary * attributes = [[NSMutableDictionary alloc] init];
NSString * name = nil;
for (id arg = firstArg; arg != nil; arg = va_arg(args, id))
{
if (name == nil)
{
name = arg;
continue;
}
[attributes setObject:arg forKey:name];
name = nil;
}
return attributes;
}
//----------------------------------------------------------------------------
- (void)setAttributes:(NSMutableDictionary *)attributes forRow:(MVRow *)row
{
NSParameterAssert(row != nil);
if (row.dirty)
{
[attributes addEntriesFromDictionary:row.attributes];
}
row.attributes = attributes;
row.dirty = YES;
}
//----------------------------------------------------------------------------
- (void)setAttributes:(id)firstArg, ...
{
va_list args;
va_start(args, firstArg);
NSMutableDictionary * attributes = [self attributesWithPairs:firstArg:args];
va_end(args);
MVRow * row = [rows lastObject];
[self setAttributes:attributes forRow:row];
// update saved
[archiver addObjectToSave:row];
}
//----------------------------------------------------------------------------
- (void)setAttributesForRowIndex:(NSUInteger)index :(id)firstArg, ...
{
va_list args;
va_start(args, firstArg);
NSMutableDictionary * attributes = [self attributesWithPairs:firstArg:args];
va_end(args);
MVRow * row = [rows objectAtIndex:index];
[self setAttributes:attributes forRow:row];
// update saved
[archiver addObjectToSave:row];
}
//----------------------------------------------------------------------------
- (void)setAttributesFromRowIndex:(NSUInteger)index :(id)firstArg, ...
{
va_list args;
va_start(args, firstArg);
NSDictionary * attributes = [self attributesWithPairs:firstArg:args];
va_end(args);
for (NSUInteger numRows = [rows count]; index < numRows; ++index)
{
MVRow * row = [rows objectAtIndex:index];
[self setAttributes:[NSMutableDictionary dictionaryWithDictionary:attributes] forRow:row];
// update saved
[archiver addObjectToSave:row];
}
}
//----------------------------------------------------------------------------
- (void) applyFilter: (NSString *)filter
{
[tableLock lock];
if (filter == nil || [filter length] == 0)
{
// copy everything (copy by elems because want to exclude later added rows)
displayRows = [NSMutableArray arrayWithArray:rows];
/*
displayRows = [[NSMutableArray alloc] init];
for (MVRow * row in rows)
{
if (row.isSaved)
{
[displayRows addObject:row];
}
}
*/
}
else
{
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"self contains[cd] %@", filter];
displayRows = [[NSMutableArray alloc] init];
for (MVRow * row in rows)
{
if (row.columns == nil)
{
[row loadFromFile:swapFile];
}
NSString * metadata = [row.attributes objectForKey:MVMetaDataAttributeName];
if (metadata == nil || [predicate evaluateWithObject:metadata] == YES)
{
[displayRows addObject:row];
}
}
}
[tableLock unlock];
}
//----------------------------------------------------------------------------
- (void)sortByOffset
{
[tableLock lock];
[rows sortWithOptions:NSSortStable usingComparator:^(id obj1, id obj2)
{
MVRow * row1 = obj1;
MVRow * row2 = obj2;
if (row1.offset < row2.offset) return (NSComparisonResult)NSOrderedAscending;
if (row1.offset > row2.offset) return (NSComparisonResult)NSOrderedDescending;
return (NSComparisonResult)NSOrderedSame;
}];
[tableLock unlock];
}
//----------------------------------------------------------------------------
- (void)saveIndexes
{
uint64_t rowCount = [rows count];
if (fwrite(&rowCount, sizeof(uint64_t), 1, swapFile) < 1) {
NSLog(@"saveIndexes write error");
return;
}
for (MVRow * row in rows) {
[row saveIndexToFile:swapFile];
}
}
//----------------------------------------------------------------------------
- (void)loadIndexes
{
uint64_t rowCount;
fread(&rowCount, sizeof(uint64_t), 1, swapFile);
while (rowCount-- > 0) {
MVRow * row = [[MVRow alloc] init];
[row loadIndexFromFile:swapFile];
[rows addObject:row];
}
}
@end
//============================================================================
@implementation MVNode
@synthesize caption, parent, dataRange, details, userInfo, detailsOffset;
//-----------------------------------------------------------------------------
- (instancetype)init
{
if (self = [super init])
{
children = [[NSMutableArray alloc] init];
userInfo = [[NSMutableDictionary alloc] init];
}
return self;
}
//----------------------------------------------------------------------------
-(NSString *)description
{
return [[super description] stringByAppendingFormat:@" [%@]", caption];
}
//----------------------------------------------------------------------------
- (MVNode *)childAtIndex:(NSUInteger)n
{
return [children objectAtIndex:n];
}
//----------------------------------------------------------------------------
- (NSUInteger)numberOfChildren
{
return [children count];
}
//----------------------------------------------------------------------------
- (void)insertNode:(MVNode *)node
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
[layout.dataController.treeLock lock];
NSUInteger index = [children indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop)
{
if (node.dataRange.location < [obj dataRange].location)
{
*stop = YES;
return YES;
}
return NO;
}];
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:MVDataTreeWillChangeNotification
object:layout.dataController];
if (index == NSNotFound)
{
[children addObject:node];
}
else
{
[children insertObject:node atIndex:index];
}
[nc postNotificationName:MVDataTreeDidChangeNotification
object:layout.dataController];
[layout.dataController updateTreeView:self];
[layout.dataController.treeLock unlock];
}
//----------------------------------------------------------------------------
- (MVNode *)insertChild:(NSString *)_caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNode * node = [[MVNode alloc] init];
node.caption = _caption;
node.dataRange = NSMakeRange(location,length);
node.parent = self;
[node.userInfo addEntriesFromDictionary:userInfo];
[self insertNode:node];
return node;
}
//----------------------------------------------------------------------------
- (MVNode *)insertChildWithDetails:(NSString *)_caption
location:(uint64_t)location
length:(uint64_t)length
saver:(MVNodeSaver &)saver
{
MVNode * node = [self insertChild:_caption location:location length:length];
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
node.details = [MVTable tableWithArchiver:layout.archiver];
saver.setNode(node);
return node;
}
//----------------------------------------------------------------------------
- (MVNode *)findNodeByUserInfo:(NSDictionary *)uinfo
{
// act node
if ([userInfo isEqualToDictionary:uinfo] == YES)
{
return self;
}
// recursively on childrens
for (MVNode * node in children)
{
MVNode * found = [node findNodeByUserInfo:uinfo];
if (found != nil)
{
return found;
}
}
// give up
return nil;
}
//-----------------------------------------------------------------------------
- (void)openDetails
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
FILE * pFile = fopen(CSTRING(layout.archiver.swapPath), "r");
if (pFile != NULL)
{
if (details != nil) // saving in progress
{
details.swapFile = pFile;
}
else if (detailsOffset != 0) // saved and has content
{
[self loadFromFile:pFile];
}
}
}
//-----------------------------------------------------------------------------
- (void)closeDetails
{
if (details.swapFile != NULL)
{
fclose(details.swapFile);
details.swapFile = NULL;
}
}
//-----------------------------------------------------------------------------
- (void)sortDetails
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
[layout.dataController updateStatus:MVStatusTaskStarted];
[details sortByOffset];
[layout.dataController updateStatus:MVStatusTaskTerminated];
}
//----------------------------------------------------------------------------
- (void)filterDetails: (NSString *)filter
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
[layout.dataController updateStatus:MVStatusTaskStarted];
[layout.archiver suspend];
[details applyFilter:filter];
[layout.archiver resume];
[layout.dataController updateStatus:MVStatusTaskTerminated];
}
//-----------------------------------------------------------------------------
- (void)saveToFile:(FILE *)pFile
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
[layout.dataController updateStatus:MVStatusTaskStarted];
off_t filePos = ftello(pFile);
// XXX: error check
if (filePos == -1) {
NSLog(@"MVNode saveToFile: ftello failed: %s", strerror(errno));
}
details.swapFile = pFile;
[details saveIndexes];
detailsOffset = filePos;
// clear the * prefix
[layout.dataController updateTreeView:self];
// update the details table
if (self == layout.dataController.selectedNode) {
[self openDetails];
[details applyFilter:nil];
}
[layout.dataController updateStatus:MVStatusTaskTerminated];
}
//-----------------------------------------------------------------------------
- (void)loadFromFile:(FILE *)pFile
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
[layout.dataController updateStatus:MVStatusTaskStarted];
details = [MVTable tableWithArchiver:layout.archiver];
details.swapFile = pFile;
NSParameterAssert(detailsOffset != 0);
fseek (pFile, detailsOffset, SEEK_SET);
[details loadIndexes];
[layout.dataController updateStatus:MVStatusTaskTerminated];
}
//-----------------------------------------------------------------------------
-(void)clear
{
MVLayout * layout = [userInfo objectForKey:MVLayoutUserInfoKey];
if (layout.dataController.selectedNode != self)
{
details = nil;
}
}
@end
//============================================================================
@implementation MVDataController
@synthesize fileName, fileData, realData, layouts, rootNode, selectedNode, treeLock;
//-----------------------------------------------------------------------------
/*
- (void)dealloc
{
NSLog(@"********MVDataController deallocated: %@", self);
for (MVLayout * layout in layouts)
{
NSLog(@"%@ Retain count is %ld", layout, CFGetRetainCount((__bridge CFTypeRef)layout));
}
}
*/
//-----------------------------------------------------------------------------
- (instancetype)init
{
if (self = [super init])
{
layouts = [[NSMutableArray alloc] init];
rootNode = [[MVNode alloc] init];
treeLock = [[NSLock alloc] init];
}
return self;
}
//----------------------------------------------------------------------------
-(NSString *)getMachine:(cpu_type_t)cputype
{
switch (cputype)
{
default: return @"???";
case CPU_TYPE_I386: return @"X86";
case CPU_TYPE_POWERPC: return @"PPC";
case CPU_TYPE_X86_64: return @"X86_64";
case CPU_TYPE_POWERPC64: return @"PPC64";
case CPU_TYPE_ARM: return @"ARM";
case CPU_TYPE_ARM64: return @"ARM64";
case CPU_TYPE_ARM64_32: return @"ARM64_32";
}
}
//----------------------------------------------------------------------------
-(NSString *)getARMCpu:(cpu_subtype_t)cpusubtype
{
switch (cpusubtype & ~CPU_SUBTYPE_MASK)
{
default: return @"???";
case CPU_SUBTYPE_ARM_ALL: return @"ARM_ALL";
case CPU_SUBTYPE_ARM_V4T: return @"ARM_V4T";
case CPU_SUBTYPE_ARM_V6: return @"ARM_V6";
case CPU_SUBTYPE_ARM_V5TEJ: return @"ARM_V5TEJ";
case CPU_SUBTYPE_ARM_XSCALE: return @"ARM_XSCALE";
case CPU_SUBTYPE_ARM_V7: return @"ARM_V7";
case CPU_SUBTYPE_ARM_V7F: return @"ARM_V7F";
case CPU_SUBTYPE_ARM_V7S: return @"ARM_V7S";
case CPU_SUBTYPE_ARM_V7K: return @"ARM_V7K";
case CPU_SUBTYPE_ARM_V8: return @"ARM_V8";
case CPU_SUBTYPE_ARM_V6M: return @"ARM_V6M";
case CPU_SUBTYPE_ARM_V7M: return @"ARM_V7M";
case CPU_SUBTYPE_ARM_V7EM: return @"ARM_V7EM";
case CPU_SUBTYPE_ARM_V8M: return @"ARM_V8M";
}
}
//----------------------------------------------------------------------------
-(NSString *)getARM64Cpu:(cpu_subtype_t)cpusubtype
{
switch (cpusubtype & ~CPU_SUBTYPE_MASK)
{
default: return @"???";
case CPU_SUBTYPE_ARM64_ALL: return @"ARM64_ALL";
case CPU_SUBTYPE_ARM64_V8: return @"ARM64_V8";
case CPU_SUBTYPE_ARM64E: return @"ARM64E";
}
}
//----------------------------------------------------------------------------