forked from NSGod/ichm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHMDocumentFile.m
903 lines (717 loc) · 26.9 KB
/
CHMDocumentFile.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
//
// CHMDocumentFile.m
// ichm
//
// Created by Mark Douma on 4/15/2016.
//
// Copyright © 2016 Mark Douma.
#import "CHMDocumentFile.h"
#import <CHM/CHM.h>
#import "lcid.h"
#import "CHMLinkItem.h"
#import "CHMTableOfContents.h"
#import "CHMSearchResult.h"
#import "CHMITSSURLProtocol.h"
#import "CHMKitPrivateInterfaces.h"
#import "CHMArchiveItem.h"
#define MD_DEBUG 0
#if MD_DEBUG
#define MDLog(...) NSLog(__VA_ARGS__)
#else
#define MDLog(...)
#endif
static BOOL automaticallyPreparesSearchIndex = YES;
@implementation CHMDocumentFile
@synthesize filePath;
@synthesize title;
@synthesize homePath;
@synthesize tableOfContentsPath;
@synthesize indexPath;
@synthesize tableOfContents;
@synthesize index;
@synthesize searchResults;
@synthesize encoding;
@synthesize encodingName;
@synthesize customEncoding;
@synthesize customEncodingName;
@dynamic currentEncodingName;
@synthesize searchDelegate;
@synthesize hasPreparedSearchIndex;
@synthesize isPreparingSearchIndex;
@synthesize archiveItems;
@synthesize allArchiveItems;
+ (BOOL)automaticallyPreparesSearchIndex {
return automaticallyPreparesSearchIndex;
}
+ (void)setAutomaticallyPreparesSearchIndex:(BOOL)shouldPrepare {
automaticallyPreparesSearchIndex = shouldPrepare;
}
+ (id)documentFileWithContentsOfFile:(NSString *)path error:(NSError **)outError {
return [[(CHMDocumentFile *)[[self class] alloc] initWithContentsOfFile:path error:outError] autorelease];
}
- (id)initWithContentsOfFile:(NSString *)path error:(NSError **)outError {
if ((self = [super init])) {
// error reporting isn't yet implemented, so set to nil to be safe if they try to use it.
if (outError) *outError = nil;
filePath = [path retain];
chmFileHandle = chm_open([filePath fileSystemRepresentation]);
if (chmFileHandle == NULL) {
[self release];
return nil;
}
hasPreparedSearchIndex = NO;
isPreparingSearchIndex = NO;
searchIndexCondition = [[NSCondition alloc] init];
searchResults = [[NSMutableArray alloc] init];
self.archiveItems = [CHMArchiveItem rootArchiveItemWithDocumentFile:self chmFileHandle:chmFileHandle];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedStandardCompare:)] autorelease];
[archiveItems sortWithSortDescriptors:[NSArray arrayWithObject:sortDescriptor] recursively:YES];
// MDLog(@"[%@ %@] archiveItems == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), archiveItems);
[self loadMetadata];
[self setupTableOfContentsAndIndex];
if ([[self class] automaticallyPreparesSearchIndex]) [self prepareSearchIndex];
}
return self;
}
- (void)dealloc {
searchDelegate = nil;
[filePath release];
if (chmFileHandle) {
chm_close(chmFileHandle);
}
[title release];
[homePath release];
[tableOfContentsPath release];
[indexPath release];
[tableOfContents release];
[index release];
[searchResults release];
if (skIndex) SKIndexClose(skIndex);
[searchIndexData release];
[searchIndexCondition release];
[encodingName release];
[customEncodingName release];
[archiveItems release];
[allArchiveItems release];
[super dealloc];
}
#pragma mark - Basic CHM reading operations
static inline unsigned short readShort(NSData *data, NSUInteger offset) {
unsigned short value;
[data getBytes:(void *)&value range:NSMakeRange(offset, 2)];
return NSSwapLittleShortToHost(value);
}
static inline uint32_t readInt(NSData *data, NSUInteger offset) {
uint32_t value = 0;
[data getBytes:&value range:NSMakeRange(offset, 4)];
return NSSwapLittleIntToHost(value);
}
static inline NSString *readString(NSData *data, NSUInteger offset, NSStringEncoding anEncoding) {
const char *stringData = (char *)[data bytes] + offset;
return [[[NSString alloc] initWithCString:stringData encoding:anEncoding] autorelease];
}
static inline NSString *readTrimmedString(NSData *data, NSUInteger offset, NSStringEncoding anEncoding) {
NSString *str = readString(data, offset, anEncoding);
return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
static inline NSString *LCIDtoEncodingName(unsigned int lcid) {
NSString *name = nil;
switch (lcid) {
case LCID_CS: //1250
case LCID_HR: //1250
case LCID_HU: //1250
case LCID_PL: //1250
case LCID_RO: //1250
case LCID_SK: //1250
case LCID_SL: //1250
case LCID_SQ: //1250
case LCID_SR_SP: //1250
name = @"windows-1250";
break;
case LCID_AZ_CY: //1251
case LCID_BE: //1251
case LCID_BG: //1251
case LCID_MS_MY: //1251
case LCID_RU: //1251
case LCID_SB: //1251
case LCID_SR_SP2: //1251
case LCID_TT: //1251
case LCID_UK: //1251
case LCID_UZ_UZ2: //1251
case LCID_YI: //1251
name = @"windows-1251";
break;
case LCID_AF: //1252
case LCID_CA: //1252
case LCID_DA: //1252
case LCID_DE_AT: //1252
case LCID_DE_CH: //1252
case LCID_DE_DE: //1252
case LCID_DE_LI: //1252
case LCID_DE_LU: //1252
case LCID_EN_AU: //1252
case LCID_EN_BZ: //1252
case LCID_EN_CA: //1252
case LCID_EN_CB: //1252
case LCID_EN_GB: //1252
case LCID_EN_IE: //1252
case LCID_EN_JM: //1252
case LCID_EN_NZ: //1252
case LCID_EN_PH: //1252
case LCID_EN_TT: //1252
case LCID_EN_US: //1252
case LCID_EN_ZA: //1252
case LCID_ES_AR: //1252
case LCID_ES_BO: //1252
case LCID_ES_CL: //1252
case LCID_ES_CO: //1252
case LCID_ES_CR: //1252
case LCID_ES_DO: //1252
case LCID_ES_EC: //1252
case LCID_ES_ES: //1252
case LCID_ES_GT: //1252
case LCID_ES_HN: //1252
case LCID_ES_MX: //1252
case LCID_ES_NI: //1252
case LCID_ES_PA: //1252
case LCID_ES_PE: //1252
case LCID_ES_PR: //1252
case LCID_ES_PY: //1252
case LCID_ES_SV: //1252
case LCID_ES_UY: //1252
case LCID_ES_VE: //1252
case LCID_EU: //1252
case LCID_FI: //1252
case LCID_FO: //1252
case LCID_FR_BE: //1252
case LCID_FR_CA: //1252
case LCID_FR_CH: //1252
case LCID_FR_FR: //1252
case LCID_FR_LU: //1252
case LCID_GD: //1252
case LCID_HI: //1252
case LCID_ID: //1252
case LCID_IS: //1252
case LCID_IT_CH: //1252
case LCID_IT_IT: //1252
case LCID_MS_BN: //1252
case LCID_NL_BE: //1252
case LCID_NL_NL: //1252
case LCID_NO_NO: //1252
case LCID_NO_NO2: //1252
case LCID_PT_BR: //1252
case LCID_PT_PT: //1252
case LCID_SV_FI: //1252
case LCID_SV_SE: //1252
case LCID_SW: //1252
name = @"windows-1252";
break;
case LCID_EL: //1253
name = @"windows-1253";
break;
case LCID_AZ_LA: //1254
case LCID_TR: //1254
case LCID_UZ_UZ: //1254
name = @"windows-1254";
break;
case LCID_HE: //1255
name = @"windows-1255";
break;
case LCID_AR_AE: //1256
case LCID_AR_BH: //1256
case LCID_AR_DZ: //1256
case LCID_AR_EG: //1256
case LCID_AR_IQ: //1256
case LCID_AR_JO: //1256
case LCID_AR_KW: //1256
case LCID_AR_LB: //1256
case LCID_AR_LY: //1256
case LCID_AR_MA: //1256
case LCID_AR_OM: //1256
case LCID_AR_QA: //1256
case LCID_AR_SA: //1256
case LCID_AR_SY: //1256
case LCID_AR_TN: //1256
case LCID_AR_YE: //1256
case LCID_FA: //1256
case LCID_UR: //1256
name = @"windows-1256";
break;
case LCID_ET: //1257
case LCID_LT: //1257
case LCID_LV: //1257
name = @"windows-1257";
break;
case LCID_VI: //1258
name = @"windows-1258";
break;
case LCID_TH: //874
name = @"cp874";
break;
case LCID_JA: //932
name = @"cp932";
break;
case LCID_ZH_CN: //936
case LCID_ZH_SG: //936
name = @"cp936";
break;
case LCID_KO: //949
name = @"cp949";
break;
case LCID_ZH_HK: //950
case LCID_ZH_MO: //950
case LCID_ZH_TW: //950
name = @"cp950";
break;
case LCID_GD_IE: //??
case LCID_MK: //??
case LCID_RM: //??
case LCID_RO_MO: //??
case LCID_RU_MO: //??
case LCID_ST: //??
case LCID_TN: //??
case LCID_TS: //??
case LCID_XH: //??
case LCID_ZU: //??
case LCID_HY: //0
case LCID_MR: //0
case LCID_MT: //0
case LCID_SA: //0
case LCID_TA: //0
default:
break;
}
return name;
}
#pragma mark - chmlib
- (BOOL)hasObjectAtPath:(NSString *)absolutePath {
struct chmUnitInfo info;
if (chmFileHandle) {
return chm_resolve_object(chmFileHandle, [absolutePath UTF8String], &info) == CHM_RESOLVE_SUCCESS;
}
return NO;
}
- (NSData *)dataForObjectAtPath:(NSString *)absolutePath {
// if ([NSThread isMainThread]) MDLog(@"[%@ %@] absolutePath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), absolutePath);
if (absolutePath == nil) return nil;
if ([absolutePath hasPrefix:@"/"]) {
// Quick fix
if ([absolutePath hasPrefix:@"///"]) {
absolutePath = [absolutePath substringFromIndex:2];
}
} else {
absolutePath = [absolutePath chm__stringByAssuringAbsolutePath];
}
struct chmUnitInfo info;
void *buffer = NULL;
if (chm_resolve_object(chmFileHandle, [absolutePath UTF8String], &info) == CHM_RESOLVE_SUCCESS) {
buffer = malloc((size_t)info.length);
if (buffer) {
if (!chm_retrieve_object(chmFileHandle, &info, buffer, 0, info.length)) {
NSLog(@"[%@ %@] failed to load %lu bytes for item at path \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), (unsigned long)info.length, absolutePath);
free(buffer);
buffer = NULL;
}
}
}
if (buffer)
return [NSData dataWithBytesNoCopy:buffer length:(NSUInteger)info.length];
return nil;
}
- (CHMArchiveItem *)archiveItemAtPath:(NSString *)absolutePath {
return [archiveItems descendantAtPath:absolutePath];
}
- (CHMArchiveItem *)archiveItemAtPath:(NSString *)relativePath relativeToArchiveItem:(CHMArchiveItem *)anItem {
NSString *fullPath = [[anItem.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:relativePath];
return [archiveItems descendantAtPath:fullPath];
}
/* NOTE: I've seen some CHM files who, in the Table of Contents, record the path to a page item in a
case-insensitive manner, for example, "html/01lc01a.htm", yet whose actual item would exist at "HTML/01lc01a.htm".
While chmlib seems able to deal with this case-insensitiveness, it was causing CHMKit problems: we were
assuming case-sensitive paths, which caused errors when trying to locate those items. This method, then, is intended to be
used by CHMTableOfContents to sanitize the paths of CHMLinkItems to make sure they reflect the actual paths as exists
in the CHM file's archived file hierarchy. */
- (NSString *)actualAbsolutePathForRelativeCaseInsensitivePath:(NSString *)aPath {
// MDLog(@"[%@ %@] aPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), aPath);
aPath = [aPath chm__stringByAssuringAbsolutePath];
NSString *sanitizedPath = [archiveItems actualPathForItemWithCaseInsensitivePath:aPath];
#if MD_DEBUG
if (![sanitizedPath isEqualToString:aPath]) {
NSLog(@"[%@ %@] *** NOTICE: correcting \"%@\" to \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), aPath, sanitizedPath);
}
#endif
return sanitizedPath;
}
- (BOOL)loadMetadata {
/* before anything else, get the encoding */
NSData *systemData = [self dataForObjectAtPath:@"/#SYSTEM"];
if (systemData == nil) {
return NO;
}
NSUInteger maxOffset = [systemData length];
NSUInteger offset = 4;
for (; offset < maxOffset; ) {
uint16_t data = readShort(systemData, offset);
if (data == 4) {
uint32_t lcid = readInt(systemData, offset + 4);
MDLog(@"[%@ %@] (SYSTEM) LCID == %u", NSStringFromClass([self class]), NSStringFromSelector(_cmd), (unsigned)lcid);
encodingName = LCIDtoEncodingName(lcid);
MDLog(@"[%@ %@] (SYSTEM) encodingName == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), encodingName);
CFStringEncoding cfStringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)encodingName);
// MDLog(@"cfStringEncoding == %lu", (unsigned long)cfStringEncoding);
// MDLog(@"cfStringEncoding == \"%@\"", CFStringGetNameOfEncoding(cfStringEncoding));
NSStringEncoding nsStringEncoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding);
// MDLog(@"nsStringEncoding == %lu", (unsigned long)nsStringEncoding);
#if MD_DEBUG
NSString *locDescrp = [NSString localizedNameOfStringEncoding:nsStringEncoding];
#endif
MDLog(@"[%@ %@] (SYSTEM) encoding == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), locDescrp);
encoding = nsStringEncoding;
break;
}
offset += readShort(systemData, offset + 2) + 4;
}
//--- Start with WINDOWS object ---
NSData *windowsData = [self dataForObjectAtPath:@"/#WINDOWS"];
NSData *stringsData = [self dataForObjectAtPath:@"/#STRINGS"];
if (windowsData && stringsData) {
const uint32_t entryCount = readInt(windowsData, 0);
const uint32_t entrySize = readInt(windowsData, 4);
for (NSUInteger entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
NSUInteger entryOffset = 8 + (entryIndex * entrySize);
if (title == nil || title.length == 0) {
title = readTrimmedString(stringsData, readInt(windowsData, entryOffset + 0x14), encoding);
MDLog(@"[%@ %@] (STRINGS) title == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), title);
}
if (tableOfContentsPath == nil || tableOfContentsPath.length == 0) {
tableOfContentsPath = readString(stringsData, readInt(windowsData, entryOffset + 0x60), encoding);
if (tableOfContentsPath.length) tableOfContentsPath = [tableOfContentsPath chm__stringByAssuringAbsolutePath];
MDLog(@"[%@ %@] (STRINGS) tableOfContentsPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), tableOfContentsPath);
}
if (indexPath == nil || indexPath.length == 0) {
indexPath = readString(stringsData, readInt(windowsData, entryOffset + 0x64), encoding);
if (indexPath.length) indexPath = [indexPath chm__stringByAssuringAbsolutePath];
MDLog(@"[%@ %@] (STRINGS) indexPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), indexPath);
}
if (homePath == nil || homePath.length == 0) {
homePath = readString(stringsData, readInt(windowsData, entryOffset + 0x68), encoding);
if (homePath.length) homePath = [homePath chm__stringByAssuringAbsolutePath];
MDLog(@"[%@ %@] (STRINGS) homePath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), homePath);
}
}
}
//--- Use SYSTEM object ---
offset = 4;
for (; offset < maxOffset; ) {
switch (readShort(systemData, offset)) {
case 0: {
if (tableOfContentsPath == nil || tableOfContentsPath.length == 0) {
tableOfContentsPath = readString(systemData, offset + 4, encoding);
if (tableOfContentsPath.length) tableOfContentsPath = [tableOfContentsPath chm__stringByAssuringAbsolutePath];
MDLog(@"[%@ %@] (SYSTEM) tableOfContentsPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), tableOfContentsPath);
}
break;
}
case 1: {
if (indexPath == nil || indexPath.length == 0) {
indexPath = readString(systemData, offset + 4, encoding);
if (indexPath.length) indexPath = [indexPath chm__stringByAssuringAbsolutePath];
MDLog(@"[%@ %@] (SYSTEM) indexPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), indexPath);
}
break;
}
case 2: {
if (homePath == nil || homePath.length == 0) {
homePath = readString(systemData, offset + 4, encoding);
if (homePath.length) homePath = [homePath chm__stringByAssuringAbsolutePath];
MDLog(@"[%@ %@] (SYSTEM) homePath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), homePath);
}
break;
}
case 3: {
if (title == nil || title.length == 0) {
title = readTrimmedString(systemData, offset + 4, encoding);
MDLog(@"[%@ %@] (SYSTEM) title == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), title);
}
break;
}
case 6: {
const char *data = (const char *)([systemData bytes] + offset + 4);
NSString *prefix = [[NSString alloc] initWithCString:data encoding:encoding];
if (tableOfContentsPath == nil || tableOfContentsPath.length == 0) {
NSString *path = [NSString stringWithFormat:@"/%@.hhc", prefix];
if ([self hasObjectAtPath:path]) {
tableOfContentsPath = path;
}
MDLog(@"[%@ %@] (SYSTEM) (case 6) tableOfContentsPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), tableOfContentsPath);
}
if (indexPath == nil || indexPath.length == 0) {
NSString *path = [NSString stringWithFormat:@"/%@.hhk", prefix];
if ([self hasObjectAtPath:path]) {
indexPath = path;
}
MDLog(@"[%@ %@] (SYSTEM) (case 6) indexPath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), indexPath);
}
[prefix release];
break;
}
case 9:
break;
case 16:
break;
default:
MDLog(@"[%@ %@] (SYSTEM) unhandled value == %d", NSStringFromClass([self class]), NSStringFromSelector(_cmd), readShort(systemData, offset));
break;
}
offset += readShort(systemData, offset + 2) + 4;
}
// Check for empty string titles
if (title.length == 0) {
title = nil;
} else {
[title retain];
}
// Check for lack of index page
if (homePath == nil) {
homePath = [self findHomePath];
MDLog(@"[%@ %@] (IMPLICIT) implicit homePath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), homePath);
}
[homePath retain];
[tableOfContentsPath retain];
[indexPath retain];
return YES;
}
- (NSString *)findHomePath {
NSString *testPath = @"/index.htm";
if ([self hasObjectAtPath:testPath]) {
return testPath;
}
testPath = @"/default.html";
if ([self hasObjectAtPath:testPath]) {
return testPath;
}
testPath = @"/default.htm";
if ([self hasObjectAtPath:testPath]) {
return testPath;
}
return @"/index.html";
}
- (void)setupTableOfContentsAndIndex {
if (tableOfContentsPath && tableOfContentsPath.length) {
NSData *tocData = [self dataForObjectAtPath:tableOfContentsPath];
CHMTableOfContents *newTOC = [[CHMTableOfContents alloc] initWithData:tocData encodingName:[self currentEncodingName] documentFile:self];
CHMTableOfContents *oldTOC = tableOfContents;
tableOfContents = newTOC;
if (oldTOC) {
[oldTOC release];
}
}
if (indexPath && indexPath.length) {
NSData *tocData = [self dataForObjectAtPath:indexPath];
CHMTableOfContents *newTOC = [[CHMTableOfContents alloc] initWithData:tocData encodingName:[self currentEncodingName] documentFile:self];
CHMTableOfContents *oldTOC = index;
index = newTOC;
[index sort];
if (oldTOC) {
[oldTOC release];
}
}
// MDLog(@"[%@ %@] tableOfContents == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), tableOfContents);
}
- (CHMLinkItem *)linkItemAtPath:(NSString *)absolutePath {
// MDLog(@"[%@ %@] absolutePath == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), absolutePath);
CHMLinkItem *item = nil;
if (tableOfContents) item = [tableOfContents linkItemAtPath:absolutePath];
if (item == nil && index) item = [index linkItemAtPath:absolutePath];
return item;
}
- (NSArray *)allArchiveItems {
if (allArchiveItems == nil) {
allArchiveItems = [[NSMutableArray alloc] init];
[allArchiveItems addObject:archiveItems];
NSArray *childNodes = archiveItems.childNodes;
for (CHMArchiveItem *childNode in childNodes) {
if (!childNode.isLeaf) {
[allArchiveItems addObjectsFromArray:[childNode descendants]];
}
}
}
return [[allArchiveItems copy] autorelease];
}
#pragma mark - search
- (void)prepareSearchIndex {
MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
self.isPreparingSearchIndex = YES;
[searchIndexData release];
searchIndexData = [[NSMutableData dataWithCapacity: 2^22] retain];
if (skIndex) {
SKIndexClose(skIndex);
skIndex = NULL;
}
skIndex = SKIndexCreateWithMutableData((CFMutableDataRef)searchIndexData, NULL, kSKIndexInverted, (CFDictionaryRef)NULL);
[NSThread detachNewThreadSelector:@selector(buildSearchIndexInBackgroundThread) toTarget:self withObject:nil];
}
static int forEachFile(struct chmFile *h, struct chmUnitInfo *ui, void *context) {
if (ui->path[0] != '/' || strstr(ui->path, "/../") != NULL || ui->path[strlen(ui->path)-1] == '/')
return CHM_ENUMERATOR_CONTINUE;
CHMDocumentFile *docFile = (CHMDocumentFile *)context;
[docFile addToSearchIndex:ui->path];
return CHM_ENUMERATOR_CONTINUE;
}
- (void)buildSearchIndexInBackgroundThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
[searchIndexCondition lock];
chm_enumerate(chmFileHandle, CHM_ENUMERATE_FILES || CHM_ENUMERATE_NORMAL, forEachFile, (void *)self);
hasPreparedSearchIndex = YES;
[searchIndexCondition signal];
[searchIndexCondition unlock];
[self performSelectorOnMainThread:@selector(notifyDelegateSearchIndexIsPrepared:) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)addToSearchIndex:(const char *)path {
NSString *filepath = [NSString stringWithCString:path encoding:encoding];
NSData *data = [self dataForObjectAtPath:filepath];
NSURL *url = [NSURL chm__ITSSURLWithPath:filepath];
if (!url) {
return;
}
SKDocumentRef doc = SKDocumentCreateWithURL((CFURLRef)url);
[(id)doc autorelease];
NSString *contents = [[NSString alloc] initWithData:data encoding:encoding];
// if the encoding being set is invalid, try following encoding.
if (contents == nil && data.length) {
contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
if (contents == nil && data.length) {
contents = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}
SKIndexAddDocumentWithText(skIndex, doc, (CFStringRef)contents, (Boolean)true);
[contents release];
}
- (void)notifyDelegateSearchIndexIsPrepared:(id)sender {
if (searchDelegate && [searchDelegate respondsToSelector:@selector(documentFileDidPrepareSearchIndex:)]) {
[searchDelegate documentFileDidPrepareSearchIndex:self];
}
}
#if MD_DEBUG
static NSString * const CHMDocumentFileSearchModeDescriptions[] = {
@"<invalid value>",
@"CHMDocumentFileSearchInFile",
@"CHMDocumentFileSearchInIndex",
};
#endif
- (void)searchForString:(NSString *)searchString usingMode:(CHMDocumentFileSearchMode)searchMode {
NSParameterAssert(searchString != nil && searchMode >= CHMDocumentFileSearchInFile && searchMode <= CHMDocumentFileSearchInIndex);
MDLog(@"[%@ %@] searchMode == %@, searchString == \"%@\", ", NSStringFromClass([self class]), NSStringFromSelector(_cmd), CHMDocumentFileSearchModeDescriptions[searchMode], searchString);
if (self.hasPreparedSearchIndex == NO && self.isPreparingSearchIndex == NO) {
[self prepareSearchIndex];
}
if (searchMode == CHMDocumentFileSearchInFile) {
// wait for the building of index
[searchIndexCondition lock];
while (!hasPreparedSearchIndex) [searchIndexCondition wait];
[searchIndexCondition unlock];
}
[searchResults removeAllObjects];
if (searchMode == CHMDocumentFileSearchInIndex) {
if (index == nil) {
if (searchDelegate && [searchDelegate respondsToSelector:@selector(documentFile:didUpdateSearchResults:)]) {
[searchDelegate documentFile:self didUpdateSearchResults:[self searchResults]];
}
return;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith[c] %@ ", searchString];
NSArray *indexChildren = index.linkItems.children;
NSArray *filteredResults = [indexChildren filteredArrayUsingPredicate:predicate];
NSMutableArray *mSearchResults = [NSMutableArray array];
for (CHMLinkItem *item in filteredResults) {
CHMSearchResult *searchResult = [[CHMSearchResult alloc] initWithLinkItem:item score:0];
if (searchResult) [mSearchResults addObject:searchResult];
[searchResult release];
}
[searchResults setArray:mSearchResults];
if (searchDelegate && [searchDelegate respondsToSelector:@selector(documentFile:didUpdateSearchResults:)]) {
[searchDelegate documentFile:self didUpdateSearchResults:[self searchResults]];
}
return;
}
// search in file
if (index == nil && tableOfContents == nil) {
if (searchDelegate && [searchDelegate respondsToSelector:@selector(documentFile:didUpdateSearchResults:)]) {
[searchDelegate documentFile:self didUpdateSearchResults:[self searchResults]];
}
return;
}
SKSearchOptions options = kSKSearchOptionDefault;
SKIndexFlush(skIndex);
SKSearchRef search = SKSearchCreate(skIndex, (CFStringRef)searchString, options);
[(id)search autorelease];
Boolean more = true;
uint32_t totalCount = 0;
uint32_t kSearchMax = 10;
NSMutableArray *mSearchResults = [NSMutableArray array];
while (more) {
SKDocumentID foundDocIDs[kSearchMax];
float foundScores[kSearchMax];
SKDocumentRef foundDocRefs[kSearchMax];
float *scores;
scores = foundScores;
CFIndex foundCount = 0;
CFIndex pos;
more = SKSearchFindMatches(search,
kSearchMax,
foundDocIDs,
scores,
1, // maximum time before func returns, in seconds
&foundCount
);
totalCount += foundCount;
//..........................................................................
// get document locations for matches and display results.
// alternatively, you can collect results over iterations of this loop
// for display later.
SKIndexCopyDocumentRefsForDocumentIDs((SKIndexRef)skIndex,
(CFIndex)foundCount,
(SKDocumentID *)foundDocIDs,
(SKDocumentRef *)foundDocRefs);
for (pos = 0; pos < foundCount; pos++) {
SKDocumentRef doc = (SKDocumentRef)[(id)foundDocRefs[pos] autorelease];
NSURL *url = [(id)SKDocumentCopyURL(doc) autorelease];
CHMLinkItem *item = [self linkItemAtPath:url.path];
if (item) {
CHMSearchResult *searchResult = [[CHMSearchResult alloc] initWithLinkItem:item score:foundScores[pos]];
if (searchResult) [mSearchResults addObject:searchResult];
[searchResult release];
}
}
}
static NSArray *searchInFileSortDescriptors = nil;
if (searchInFileSortDescriptors == nil) {
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"score" ascending:NO] autorelease];
searchInFileSortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
}
[mSearchResults sortUsingDescriptors:searchInFileSortDescriptors];
[searchResults setArray:mSearchResults];
if (searchDelegate && [searchDelegate respondsToSelector:@selector(documentFile:didUpdateSearchResults:)]) {
[searchDelegate documentFile:self didUpdateSearchResults:[self searchResults]];
}
}
- (NSArray *)searchResults {
return [[searchResults copy] autorelease];
}
#pragma mark - encodings
- (void)setCustomEncoding:(NSStringEncoding)aCustomEncoding customEncodingName:(NSString *)aCustomEncodingName {
NSParameterAssert((aCustomEncoding && aCustomEncodingName) ||
(!aCustomEncoding && !aCustomEncodingName));
self.customEncoding = aCustomEncoding;
self.customEncodingName = aCustomEncodingName;
[self setupTableOfContentsAndIndex];
}
- (NSString *)currentEncodingName {
if (customEncoding) {
return customEncodingName;
}
return encodingName;
}
@end