-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCFBundle_Resources.c
2979 lines (2720 loc) · 160 KB
/
CFBundle_Resources.c
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
/*
* Copyright (c) 2008-2012 Brent Fulgham <[email protected]>. All rights reserved.
* Copyright (c) 2009 David M. Cotter <[email protected]>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFBundle_Resources.c
Copyright (c) 1999-2011, Apple Inc. All rights reserved.
Responsibility: David Smith
*/
#if DEPLOYMENT_TARGET_MACOSX
#define READ_DIRECTORIES 1
#elif DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX
#define READ_DIRECTORIES 1
#elif DEPLOYMENT_TARGET_WINDOWS
#define READ_DIRECTORIES 0
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#define READ_DIRECTORIES_CACHE_CAPACITY 128
#include "CFBundle_Internal.h"
#include <CoreFoundation/CFURLAccess.h>
#include <CoreFoundation/CFPropertyList.h>
#include <CoreFoundation/CFByteOrder.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFPreferences.h>
#include <string.h>
#include "ForFoundationOnly.h"
#include <CoreFoundation/CoreFoundation_Prefix.h>
#include "CFInternal.h"
#include <CoreFoundation/CFPriv.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
#include <sys/sysctl.h>
#endif
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_LINUX
#include <unistd.h>
#elif DEPLOYMENT_TARGET_EMBEDDED
#include <unistd.h>
#elif DEPLOYMENT_TARGET_WINDOWS
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#if READ_DIRECTORIES
#include <dirent.h>
#endif /* READ_DIRECTORIES */
CF_EXPORT bool CFDictionaryGetKeyIfPresent(CFDictionaryRef dict, const void *key, const void **actualkey);
static inline Boolean _CFBundleSortedArrayContains(CFArrayRef arr, CFStringRef target) {
CFRange arrRange = CFRangeMake(0, CFArrayGetCount(arr));
CFIndex itemIdx = CFArrayBSearchValues(arr, arrRange, target, (CFComparatorFunction)CFStringCompare, NULL);
return itemIdx < arrRange.length && CFEqual(CFArrayGetValueAtIndex(arr, itemIdx), target);
}
// The following strings are initialized 'later' (i.e., not at static initialization time) because static init time is too early for CFSTR to work, on Windows
// This is here to make sure it gets updated when _CFGetPlatformName does
#define _CFBundleNumberOfPlatforms 7
static CFStringRef _CFBundleSupportedPlatforms[_CFBundleNumberOfPlatforms] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
static const char *_CFBundleSupportedPlatformStrings[_CFBundleNumberOfPlatforms] = { "iphoneos", "macos", "windows", "linux", "freebsd", "solaris", "hpux" };
// This is here to make sure it gets updated when _CFGetProductName does
#define _CFBundleNumberOfProducts 3
static CFStringRef _CFBundleSupportedProducts[_CFBundleNumberOfProducts] = { NULL, NULL, NULL };
static const char *_CFBundleSupportedProductStrings[_CFBundleNumberOfProducts] = { "iphone", "ipod", "ipad" };
#define _CFBundleNumberOfiPhoneOSPlatformProducts 3
static CFStringRef _CFBundleSupportediPhoneOSPlatformProducts[_CFBundleNumberOfiPhoneOSPlatformProducts] = { NULL, NULL, NULL };
static const char *_CFBundleSupportediPhoneOSPlatformProductStrings[_CFBundleNumberOfiPhoneOSPlatformProducts] = { "iphone", "ipod", "ipad" };
void _CFBundleResourcesInitialize() {
for (unsigned int i = 0; i < _CFBundleNumberOfPlatforms; i++) _CFBundleSupportedPlatforms[i] = CFStringCreateWithCString(kCFAllocatorSystemDefault, _CFBundleSupportedPlatformStrings[i], kCFStringEncodingUTF8);
for (unsigned int i = 0; i < _CFBundleNumberOfProducts; i++) _CFBundleSupportedProducts[i] = CFStringCreateWithCString(kCFAllocatorSystemDefault, _CFBundleSupportedProductStrings[i], kCFStringEncodingUTF8);
for (unsigned int i = 0; i < _CFBundleNumberOfiPhoneOSPlatformProducts; i++) _CFBundleSupportediPhoneOSPlatformProducts[i] = CFStringCreateWithCString(kCFAllocatorSystemDefault, _CFBundleSupportediPhoneOSPlatformProductStrings[i], kCFStringEncodingUTF8);
}
static CFStringRef platform = NULL;
void _CFSetProductName(CFStringRef str) {
if (str) CFRetain(str);
platform = str;
// Note that the previous value is leaked, which is fine normally
// because the initial values would tend to be the constant strings
// below. That is required for thread-safety value due to the Get
// function [not being Copy]. It is also fine because people
// shouldn't be screwing around with this value casually.
}
CFStringRef _CFGetProductName(void) {
#if DEPLOYMENT_TARGET_EMBEDDED
if (!platform) {
char buffer[256];
memset(buffer, 0, sizeof(buffer));
size_t buflen = sizeof(buffer);
int ret = sysctlbyname("hw.machine", buffer, &buflen, NULL, 0);
if (0 == ret || (-1 == ret && ENOMEM == errno)) {
if (6 <= buflen && 0 == memcmp(buffer, "iPhone", 6)) {
platform = CFSTR("iphone");
} else if (4 <= buflen && 0 == memcmp(buffer, "iPod", 4)) {
platform = CFSTR("ipod");
} else if (4 <= buflen && 0 == memcmp(buffer, "iPad", 4)) {
platform = CFSTR("ipad");
} else {
const char *env = __CFgetenv("IPHONE_SIMULATOR_DEVICE");
if (env) {
if (0 == strcmp(env, "iPhone")) {
platform = CFSTR("iphone");
} else if (0 == strcmp(env, "iPad")) {
platform = CFSTR("ipad");
} else {
// fallback, unrecognized IPHONE_SIMULATOR_DEVICE
}
} else {
// fallback, unrecognized hw.machine and no IPHONE_SIMULATOR_DEVICE
}
}
}
if (!platform) platform = CFSTR("iphone"); // fallback
}
return platform;
#endif
return CFSTR("");
}
// All new-style bundles will have these extensions.
__private_extern__ CFStringRef _CFGetPlatformName(void) {
#if DEPLOYMENT_TARGET_MACOSX
return _CFBundleMacOSXPlatformName;
#elif DEPLOYMENT_TARGET_EMBEDDED
return _CFBundleiPhoneOSPlatformName;
#elif DEPLOYMENT_TARGET_WINDOWS
return _CFBundleWindowsPlatformName;
#elif DEPLOYMENT_TARGET_SOLARIS
return _CFBundleSolarisPlatformName;
#elif DEPLOYMENT_TARGET_HPUX
return _CFBundleHPUXPlatformName;
#elif DEPLOYMENT_TARGET_LINUX
return _CFBundleLinuxPlatformName;
#elif DEPLOYMENT_TARGET_FREEBSD
return _CFBundleFreeBSDPlatformName;
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
}
__private_extern__ CFStringRef _CFGetAlternatePlatformName(void) {
#if DEPLOYMENT_TARGET_MACOSX
return _CFBundleAlternateMacOSXPlatformName;
#elif DEPLOYMENT_TARGET_EMBEDDED
return _CFBundleMacOSXPlatformName;
#elif DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
return CFSTR("");
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
}
static CFSpinLock_t CFBundleResourceGlobalDataLock = CFSpinLockInit;
static UniChar *_AppSupportUniChars1 = NULL;
static CFIndex _AppSupportLen1 = 0;
static UniChar *_AppSupportUniChars2 = NULL;
static CFIndex _AppSupportLen2 = 0;
static UniChar *_ResourcesUniChars = NULL;
static CFIndex _ResourcesLen = 0;
static UniChar *_PlatformUniChars = NULL;
static CFIndex _PlatformLen = 0;
static UniChar *_AlternatePlatformUniChars = NULL;
static CFIndex _AlternatePlatformLen = 0;
static UniChar *_LprojUniChars = NULL;
static CFIndex _LprojLen = 0;
static UniChar *_GlobalResourcesUniChars = NULL;
static CFIndex _GlobalResourcesLen = 0;
static UniChar *_InfoExtensionUniChars = NULL;
static CFIndex _InfoExtensionLen = 0;
static UniChar _ResourceSuffix3[32];
static CFIndex _ResourceSuffix3Len = 0;
static UniChar _ResourceSuffix2[16];
static CFIndex _ResourceSuffix2Len = 0;
static UniChar _ResourceSuffix1[16];
static CFIndex _ResourceSuffix1Len = 0;
static void _CFBundleInitStaticUniCharBuffers(void) {
CFStringRef appSupportStr1 = _CFBundleSupportFilesDirectoryName1;
CFStringRef appSupportStr2 = _CFBundleSupportFilesDirectoryName2;
CFStringRef resourcesStr = _CFBundleResourcesDirectoryName;
CFStringRef platformStr = _CFGetPlatformName();
CFStringRef alternatePlatformStr = _CFGetAlternatePlatformName();
CFStringRef lprojStr = _CFBundleLprojExtension;
CFStringRef globalResourcesStr = _CFBundleNonLocalizedResourcesDirectoryName;
CFStringRef infoExtensionStr = _CFBundleInfoExtension;
_AppSupportLen1 = CFStringGetLength(appSupportStr1);
_AppSupportLen2 = CFStringGetLength(appSupportStr2);
_ResourcesLen = CFStringGetLength(resourcesStr);
_PlatformLen = CFStringGetLength(platformStr);
_AlternatePlatformLen = CFStringGetLength(alternatePlatformStr);
_LprojLen = CFStringGetLength(lprojStr);
_GlobalResourcesLen = CFStringGetLength(globalResourcesStr);
_InfoExtensionLen = CFStringGetLength(infoExtensionStr);
_AppSupportUniChars1 = (UniChar *)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(UniChar) * (_AppSupportLen1 + _AppSupportLen2 + _ResourcesLen + _PlatformLen + _AlternatePlatformLen + _LprojLen + _GlobalResourcesLen + _InfoExtensionLen), 0);
_AppSupportUniChars2 = _AppSupportUniChars1 + _AppSupportLen1;
_ResourcesUniChars = _AppSupportUniChars2 + _AppSupportLen2;
_PlatformUniChars = _ResourcesUniChars + _ResourcesLen;
_AlternatePlatformUniChars = _PlatformUniChars + _PlatformLen;
_LprojUniChars = _AlternatePlatformUniChars + _AlternatePlatformLen;
_GlobalResourcesUniChars = _LprojUniChars + _LprojLen;
_InfoExtensionUniChars = _GlobalResourcesUniChars + _GlobalResourcesLen;
if (_AppSupportLen1 > 0) CFStringGetCharacters(appSupportStr1, CFRangeMake(0, _AppSupportLen1), _AppSupportUniChars1);
if (_AppSupportLen2 > 0) CFStringGetCharacters(appSupportStr2, CFRangeMake(0, _AppSupportLen2), _AppSupportUniChars2);
if (_ResourcesLen > 0) CFStringGetCharacters(resourcesStr, CFRangeMake(0, _ResourcesLen), _ResourcesUniChars);
if (_PlatformLen > 0) CFStringGetCharacters(platformStr, CFRangeMake(0, _PlatformLen), _PlatformUniChars);
if (_AlternatePlatformLen > 0) CFStringGetCharacters(alternatePlatformStr, CFRangeMake(0, _AlternatePlatformLen), _AlternatePlatformUniChars);
if (_LprojLen > 0) CFStringGetCharacters(lprojStr, CFRangeMake(0, _LprojLen), _LprojUniChars);
if (_GlobalResourcesLen > 0) CFStringGetCharacters(globalResourcesStr, CFRangeMake(0, _GlobalResourcesLen), _GlobalResourcesUniChars);
if (_InfoExtensionLen > 0) CFStringGetCharacters(infoExtensionStr, CFRangeMake(0, _InfoExtensionLen), _InfoExtensionUniChars);
_ResourceSuffix1Len = CFStringGetLength(platformStr);
if (_ResourceSuffix1Len > 0) _ResourceSuffix1[0] = '-';
if (_ResourceSuffix1Len > 0) CFStringGetCharacters(platformStr, CFRangeMake(0, _ResourceSuffix1Len), _ResourceSuffix1 + 1);
if (_ResourceSuffix1Len > 0) _ResourceSuffix1Len++;
CFStringRef productStr = _CFGetProductName();
if (CFEqual(productStr, CFSTR("ipod"))) { // For now, for resource lookups, hide ipod distinction and make it look for iphone resources
productStr = CFSTR("iphone");
}
_ResourceSuffix2Len = CFStringGetLength(productStr);
if (_ResourceSuffix2Len > 0) _ResourceSuffix2[0] = '~';
if (_ResourceSuffix2Len > 0) CFStringGetCharacters(productStr, CFRangeMake(0, _ResourceSuffix2Len), _ResourceSuffix2 + 1);
if (_ResourceSuffix2Len > 0) _ResourceSuffix2Len++;
if (_ResourceSuffix1Len > 1 && _ResourceSuffix2Len > 1) {
_ResourceSuffix3Len = _ResourceSuffix1Len + _ResourceSuffix2Len;
memmove(_ResourceSuffix3, _ResourceSuffix1, sizeof(UniChar) * _ResourceSuffix1Len);
memmove(_ResourceSuffix3 + _ResourceSuffix1Len, _ResourceSuffix2, sizeof(UniChar) * _ResourceSuffix2Len);
}
}
CF_INLINE void _CFEnsureStaticBuffersInited(void) {
__CFSpinLock(&CFBundleResourceGlobalDataLock);
if (!_AppSupportUniChars1) _CFBundleInitStaticUniCharBuffers();
__CFSpinUnlock(&CFBundleResourceGlobalDataLock);
}
#if READ_DIRECTORIES
static CFMutableDictionaryRef contentsCache = NULL;
static CFMutableDictionaryRef directoryContentsCache = NULL;
static CFMutableDictionaryRef unknownContentsCache = NULL;
typedef enum {
_CFBundleAllContents = 0,
_CFBundleDirectoryContents = 1,
_CFBundleUnknownContents = 2
} _CFBundleDirectoryContentsType;
extern void _CFArraySortValues(CFMutableArrayRef array, CFComparatorFunction comparator, void *context);
static CFArrayRef _CFBundleCopySortedDirectoryContentsAtPath(CFStringRef path, _CFBundleDirectoryContentsType contentsType) {
CFArrayRef result = NULL;
__CFSpinLock(&CFBundleResourceGlobalDataLock);
if (contentsType == _CFBundleUnknownContents) {
if (unknownContentsCache) result = (CFMutableArrayRef)CFDictionaryGetValue(unknownContentsCache, path);
} else if (contentsType == _CFBundleDirectoryContents) {
if (directoryContentsCache) result = (CFMutableArrayRef)CFDictionaryGetValue(directoryContentsCache, path);
} else {
if (contentsCache) result = (CFMutableArrayRef)CFDictionaryGetValue(contentsCache, path);
}
if (result) CFRetain(result);
__CFSpinUnlock(&CFBundleResourceGlobalDataLock);
if (!result) {
Boolean tryToOpen = false, allDots = true;
char cpathBuff[CFMaxPathSize];
CFIndex cpathLen = 0, idx, lastSlashIdx = 0;
DIR *dirp = NULL;
struct dirent *dent;
CFMutableArrayRef contents = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks), directoryContents = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks), unknownContents = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
CFStringRef dirName, name;
cpathBuff[0] = '\0';
if (CFStringGetFileSystemRepresentation(path, cpathBuff, CFMaxPathSize)) {
tryToOpen = true;
cpathLen = (CFIndex)strlen(cpathBuff);
// First see whether we already know that the directory doesn't exist
for (idx = cpathLen; lastSlashIdx == 0 && idx-- > 0;) {
if (cpathBuff[idx] == '/') lastSlashIdx = idx;
else if (cpathBuff[idx] != '.') allDots = false;
}
if (lastSlashIdx > 0 && lastSlashIdx + 1 < cpathLen && !allDots) {
cpathBuff[lastSlashIdx] = '\0';
dirName = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, cpathBuff);
if (dirName) {
name = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, cpathBuff + lastSlashIdx + 1);
if (name) {
// ??? we might like to use directoryContentsCache rather than contentsCache here, but we cannot unless we resolve DT_LNKs below
CFArrayRef dirDirContents = NULL;
__CFSpinLock(&CFBundleResourceGlobalDataLock);
if (contentsCache) dirDirContents = (CFArrayRef)CFDictionaryGetValue(contentsCache, dirName);
if (dirDirContents) {
Boolean foundIt = false;
CFIndex dirDirIdx, dirDirLength = CFArrayGetCount(dirDirContents);
for (dirDirIdx = 0; !foundIt && dirDirIdx < dirDirLength; dirDirIdx++) if (kCFCompareEqualTo == CFStringCompare(name, (CFStringRef)CFArrayGetValueAtIndex(dirDirContents, dirDirIdx), kCFCompareCaseInsensitive)) foundIt = true;
if (!foundIt) tryToOpen = false;
}
__CFSpinUnlock(&CFBundleResourceGlobalDataLock);
CFRelease(name);
}
CFRelease(dirName);
}
cpathBuff[lastSlashIdx] = '/';
}
}
if (tryToOpen && (dirp = opendir(cpathBuff))) {
while ((dent = readdir(dirp))) {
#if DEPLOYMENT_TARGET_LINUX
CFIndex nameLen = strlen(dent->d_name);
#else
CFIndex nameLen = dent->d_namlen;
#endif
if (0 == nameLen || 0 == dent->d_fileno || ('.' == dent->d_name[0] && (1 == nameLen || (2 == nameLen && '.' == dent->d_name[1]) || '_' == dent->d_name[1]))) continue;
name = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, dent->d_name);
if (name) {
// ??? should we follow links for DT_LNK? unless we do, results are approximate, but for performance reasons we do not
// ??? likewise for DT_UNKNOWN
// ??? the utility of distinguishing directories from other contents is somewhat doubtful anyway
CFArrayAppendValue(contents, name);
if (dent->d_type == DT_DIR) {
CFArrayAppendValue(directoryContents, name);
} else if (dent->d_type == DT_UNKNOWN) {
CFArrayAppendValue(unknownContents, name);
}
CFRelease(name);
}
}
(void)closedir(dirp);
}
_CFArraySortValues(contents, (CFComparatorFunction)CFStringCompare, NULL);
_CFArraySortValues(directoryContents, (CFComparatorFunction)CFStringCompare, NULL);
_CFArraySortValues(unknownContents, (CFComparatorFunction)CFStringCompare, NULL);
__CFSpinLock(&CFBundleResourceGlobalDataLock);
if (!contentsCache) contentsCache = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, READ_DIRECTORIES_CACHE_CAPACITY, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (READ_DIRECTORIES_CACHE_CAPACITY <= CFDictionaryGetCount(contentsCache)) CFDictionaryRemoveAllValues(contentsCache);
CFDictionaryAddValue(contentsCache, path, contents);
if (!directoryContentsCache) directoryContentsCache = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, READ_DIRECTORIES_CACHE_CAPACITY, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (READ_DIRECTORIES_CACHE_CAPACITY <= CFDictionaryGetCount(directoryContentsCache)) CFDictionaryRemoveAllValues(directoryContentsCache);
CFDictionaryAddValue(directoryContentsCache, path, directoryContents);
if (!unknownContentsCache) unknownContentsCache = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, READ_DIRECTORIES_CACHE_CAPACITY, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (READ_DIRECTORIES_CACHE_CAPACITY <= CFDictionaryGetCount(unknownContentsCache)) CFDictionaryRemoveAllValues(unknownContentsCache);
CFDictionaryAddValue(unknownContentsCache, path, unknownContents);
if (contentsType == _CFBundleUnknownContents) {
result = (CFArrayRef)CFRetain(unknownContents);
} else if (contentsType == _CFBundleDirectoryContents) {
result = (CFArrayRef)CFRetain(directoryContents);
} else {
result = (CFArrayRef)CFRetain(contents);
}
CFRelease(contents);
CFRelease(directoryContents);
CFRelease(unknownContents);
__CFSpinUnlock(&CFBundleResourceGlobalDataLock);
}
return result;
}
static void _CFBundleFlushContentsCaches(void) {
__CFSpinLock(&CFBundleResourceGlobalDataLock);
if (contentsCache) CFDictionaryRemoveAllValues(contentsCache);
if (directoryContentsCache) CFDictionaryRemoveAllValues(directoryContentsCache);
if (unknownContentsCache) CFDictionaryRemoveAllValues(unknownContentsCache);
__CFSpinUnlock(&CFBundleResourceGlobalDataLock);
}
static void _CFBundleFlushContentsCacheForPath(CFMutableDictionaryRef cache, CFStringRef path) {
CFStringRef keys[READ_DIRECTORIES_CACHE_CAPACITY];
unsigned i, count = CFDictionaryGetCount(cache);
if (count <= READ_DIRECTORIES_CACHE_CAPACITY) {
CFDictionaryGetKeysAndValues(cache, (const void **)keys, NULL);
for (i = 0; i < count; i++) {
if (CFStringFindWithOptions(keys[i], path, CFRangeMake(0, CFStringGetLength(keys[i])), kCFCompareAnchored|kCFCompareCaseInsensitive, NULL)) CFDictionaryRemoveValue(cache, keys[i]);
}
}
}
static void _CFBundleFlushContentsCachesForPath(CFStringRef path) {
__CFSpinLock(&CFBundleResourceGlobalDataLock);
if (contentsCache) _CFBundleFlushContentsCacheForPath(contentsCache, path);
if (directoryContentsCache) _CFBundleFlushContentsCacheForPath(directoryContentsCache, path);
if (unknownContentsCache) _CFBundleFlushContentsCacheForPath(unknownContentsCache, path);
__CFSpinUnlock(&CFBundleResourceGlobalDataLock);
}
#endif /* READ_DIRECTORIES */
CF_EXPORT void _CFBundleFlushCachesForURL(CFURLRef url) {
#if READ_DIRECTORIES
CFURLRef absoluteURL = CFURLCopyAbsoluteURL(url);
CFStringRef path = CFURLCopyFileSystemPath(absoluteURL, PLATFORM_PATH_STYLE);
_CFBundleFlushContentsCachesForPath(path);
CFRelease(path);
CFRelease(absoluteURL);
#endif /* READ_DIRECTORIES */
}
CF_EXPORT void _CFBundleFlushCaches(void) {
#if READ_DIRECTORIES
_CFBundleFlushContentsCaches();
#endif /* READ_DIRECTORIES */
}
static inline Boolean _CFIsResourceCommon(char *path, Boolean *isDir) {
Boolean exists;
SInt32 mode;
if (_CFGetPathProperties(kCFAllocatorSystemDefault, path, &exists, &mode, NULL, NULL, NULL, NULL) == 0) {
if (isDir) *isDir = ((exists && ((mode & S_IFMT) == S_IFDIR)) ? true : false);
return (exists && (mode & 0444));
}
return false;
}
__private_extern__ Boolean _CFIsResourceAtURL(CFURLRef url, Boolean *isDir) {
char path[CFMaxPathSize];
if (!CFURLGetFileSystemRepresentation(url, true, (uint8_t *)path, CFMaxPathLength)) return false;
return _CFIsResourceCommon(path, isDir);
}
__private_extern__ Boolean _CFIsResourceAtPath(CFStringRef path, Boolean *isDir) {
char pathBuf[CFMaxPathSize];
if (!CFStringGetFileSystemRepresentation(path, pathBuf, CFMaxPathSize)) return false;
return _CFIsResourceCommon(pathBuf, isDir);
}
#if READ_DIRECTORIES
static CFArrayRef _CFCopyTypesForSearchBundleDirectory(CFAllocatorRef alloc, UniChar *pathUniChars, CFIndex pathLen, UniChar *nameUniChars, CFIndex nameLen, CFArrayRef resTypes, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, uint8_t version) {
CFMutableArrayRef result = CFArrayCreateMutable(alloc, 0, &kCFTypeArrayCallBacks);
CFArrayRef contents;
CFRange contentsRange, resultRange = CFRangeMake(0, 0);
CFIndex dirPathLen = pathLen, numResTypes = CFArrayGetCount(resTypes), i, j;
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, dirPathLen, dirPathLen);
CFStringReplaceAll(cheapStr, tmpString);
//fprintf(stderr, "looking in ");CFShow(cheapStr);
contents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleAllContents);
contentsRange = CFRangeMake(0, CFArrayGetCount(contents));
CFStringSetExternalCharactersNoCopy(tmpString, nameUniChars, nameLen, nameLen);
CFStringReplaceAll(cheapStr, tmpString);
for (i = 0; i < contentsRange.length; i++) {
CFStringRef content = (CFStringRef)CFArrayGetValueAtIndex(contents, i);
if (CFStringHasPrefix(content, cheapStr)) {
//fprintf(stderr, "found ");CFShow(content);
for (j = 0; j < numResTypes; j++) {
CFStringRef resType = (CFStringRef)CFArrayGetValueAtIndex(resTypes, j);
if (!CFArrayContainsValue(result, resultRange, resType) && CFStringHasSuffix(content, resType)) {
CFArrayAppendValue(result, resType);
resultRange.length = CFArrayGetCount(result);
}
}
}
}
//fprintf(stderr, "result ");CFShow(result);
CFRelease(contents);
return result;
}
#endif /* READ_DIRECTORIES */
#if DEPLOYMENT_TARGET_EMBEDDED
static void _CFSearchBundleDirectory2(CFAllocatorRef alloc, CFMutableArrayRef result, UniChar *pathUniChars, CFIndex pathLen, UniChar *nameUniChars, CFIndex nameLen, UniChar *typeUniChars, CFIndex typeLen, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, uint8_t version) {
// pathUniChars is the full path to the directory we are searching.
// nameUniChars is what we are looking for.
// typeUniChars is the type we are looking for.
// platformUniChars is the platform name.
// cheapStr is available for our use for whatever we want.
// URLs for found resources get added to result.
Boolean appendSucceeded = true;
if (nameLen > 0) appendSucceeded = _CFAppendPathComponent(pathUniChars, &pathLen, CFMaxPathSize, nameUniChars, nameLen);
if (! appendSucceeded) return;
CFIndex savedPathLen = pathLen;
// Try in order:
// NAME-PLATFORM~PRODUCT.TYPE (disabled for now)
// NAME~PRODUCT.TYPE
// NAME-PLATFORM.TYPE (disabled for now)
// NAME.TYPE
#if 0
appendSucceeded = (pathLen + _ResourceSuffix3Len < CFMaxPathSize);
if (appendSucceeded) {
memmove(pathUniChars + pathLen, _ResourceSuffix3, _ResourceSuffix3Len * sizeof(UniChar));
pathLen += _ResourceSuffix3Len;
}
if (appendSucceeded && typeLen > 0) appendSucceeded = _CFAppendPathExtension(pathUniChars, &pathLen, CFMaxPathSize, typeUniChars, typeLen);
if (appendSucceeded) {
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
Boolean Found = false, IsDir = false;
Found = _CFIsResourceAtPath(cheapStr, &IsDir);
if (Found) {
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, cheapStr, PLATFORM_PATH_STYLE, IsDir);
CFArrayAppendValue(result, url);
CFRelease(url);
return;
}
}
#endif
pathLen = savedPathLen;
appendSucceeded = (pathLen + _ResourceSuffix2Len < CFMaxPathSize);
if (appendSucceeded) {
memmove(pathUniChars + pathLen, _ResourceSuffix2, _ResourceSuffix2Len * sizeof(UniChar));
pathLen += _ResourceSuffix2Len;
}
if (appendSucceeded && typeLen > 0) appendSucceeded = _CFAppendPathExtension(pathUniChars, &pathLen, CFMaxPathSize, typeUniChars, typeLen);
if (appendSucceeded) {
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
Boolean Found = false, IsDir = false;
Found = _CFIsResourceAtPath(cheapStr, &IsDir);
if (Found) {
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, cheapStr, PLATFORM_PATH_STYLE, IsDir);
CFArrayAppendValue(result, url);
CFRelease(url);
return;
}
}
#if 0
pathLen = savedPathLen;
appendSucceeded = (pathLen + _ResourceSuffix1Len < CFMaxPathSize);
if (appendSucceeded) {
memmove(pathUniChars + pathLen, _ResourceSuffix1, _ResourceSuffix1Len * sizeof(UniChar));
pathLen += _ResourceSuffix1Len;
}
if (appendSucceeded && typeLen > 0) appendSucceeded = _CFAppendPathExtension(pathUniChars, &pathLen, CFMaxPathSize, typeUniChars, typeLen);
if (appendSucceeded) {
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
Boolean Found = false, IsDir = false;
Found = _CFIsResourceAtPath(cheapStr, &IsDir);
if (Found) {
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, cheapStr, PLATFORM_PATH_STYLE, IsDir);
CFArrayAppendValue(result, url);
CFRelease(url);
return;
}
}
#endif
pathLen = savedPathLen;
appendSucceeded = true;
if (appendSucceeded && typeLen > 0) appendSucceeded = _CFAppendPathExtension(pathUniChars, &pathLen, CFMaxPathSize, typeUniChars, typeLen);
if (appendSucceeded) {
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
Boolean Found = false, IsDir = false;
Found = _CFIsResourceAtPath(cheapStr, &IsDir);
if (Found) {
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, cheapStr, PLATFORM_PATH_STYLE, IsDir);
CFArrayAppendValue(result, url);
CFRelease(url);
return;
}
}
}
#endif
static void _CFSearchBundleDirectory(CFAllocatorRef alloc, CFMutableArrayRef result, UniChar *pathUniChars, CFIndex pathLen, UniChar *nameUniChars, CFIndex nameLen, UniChar *typeUniChars, CFIndex typeLen, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, uint8_t version) {
#if DEPLOYMENT_TARGET_EMBEDDED
_CFSearchBundleDirectory2(alloc, result, pathUniChars, pathLen, nameUniChars, nameLen, typeUniChars, typeLen, cheapStr, tmpString, version);
#else
// pathUniChars is the full path to the directory we are searching.
// nameUniChars is what we are looking for.
// typeUniChars is the type we are looking for.
// platformUniChars is the platform name.
// cheapStr is available for our use for whatever we want.
// URLs for found resources get added to result.
CFIndex savedPathLen;
Boolean appendSucceeded = true, platformGenericFound = false, platformSpecificFound = false, platformGenericIsDir = false, platformSpecificIsDir = false;
#if READ_DIRECTORIES
Boolean platformGenericIsUnknown = false, platformSpecificIsUnknown = false;
#endif
CFStringRef platformGenericStr = NULL;
#if READ_DIRECTORIES
CFIndex dirPathLen = pathLen;
CFArrayRef contents, directoryContents, unknownContents;
CFRange contentsRange, directoryContentsRange, unknownContentsRange;
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, dirPathLen, dirPathLen);
CFStringReplaceAll(cheapStr, tmpString);
//fprintf(stderr, "looking in ");CFShow(cheapStr);
contents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleAllContents);
contentsRange = CFRangeMake(0, CFArrayGetCount(contents));
directoryContents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleDirectoryContents);
directoryContentsRange = CFRangeMake(0, CFArrayGetCount(directoryContents));
unknownContents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleUnknownContents);
unknownContentsRange = CFRangeMake(0, CFArrayGetCount(unknownContents));
#endif /* READ_DIRECTORIES */
if (nameLen > 0) appendSucceeded = _CFAppendPathComponent(pathUniChars, &pathLen, CFMaxPathSize, nameUniChars, nameLen);
savedPathLen = pathLen;
if (appendSucceeded && typeLen > 0) appendSucceeded = _CFAppendPathExtension(pathUniChars, &pathLen, CFMaxPathSize, typeUniChars, typeLen);
if (appendSucceeded) {
#if READ_DIRECTORIES
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars + dirPathLen + 1, pathLen - dirPathLen - 1, pathLen - dirPathLen - 1);
CFStringReplaceAll(cheapStr, tmpString);
platformGenericFound = _CFBundleSortedArrayContains(contents, cheapStr);
platformGenericIsDir = _CFBundleSortedArrayContains(directoryContents, cheapStr);
platformGenericIsUnknown = _CFBundleSortedArrayContains(unknownContents, cheapStr);
//fprintf(stderr, "looking for ");CFShow(cheapStr);if (platformGenericFound) fprintf(stderr, "found it\n"); if (platformGenericIsDir) fprintf(stderr, "a directory\n");
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
if (platformGenericFound && platformGenericIsUnknown) {
(void)_CFIsResourceAtPath(cheapStr, &platformGenericIsDir);
//if (platformGenericIsDir) fprintf(stderr, "a directory after all\n"); else fprintf(stderr, "not a directory after all\n");
}
#else /* READ_DIRECTORIES */
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
platformGenericFound = _CFIsResourceAtPath(cheapStr, &platformGenericIsDir);
#endif /* READ_DIRECTORIES */
}
// Check for platform specific.
if (platformGenericFound) {
platformGenericStr = (CFStringRef)CFStringCreateCopy(kCFAllocatorSystemDefault, cheapStr);
if (!platformSpecificFound && (_PlatformLen > 0)) {
pathLen = savedPathLen;
pathUniChars[pathLen++] = (UniChar)'-';
memmove(pathUniChars + pathLen, _PlatformUniChars, _PlatformLen * sizeof(UniChar));
pathLen += _PlatformLen;
if (appendSucceeded && typeLen > 0) appendSucceeded = _CFAppendPathExtension(pathUniChars, &pathLen, CFMaxPathSize, typeUniChars, typeLen);
if (appendSucceeded) {
#if READ_DIRECTORIES
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars + dirPathLen + 1, pathLen - dirPathLen - 1, pathLen - dirPathLen - 1);
CFStringReplaceAll(cheapStr, tmpString);
platformSpecificFound = _CFBundleSortedArrayContains(contents, cheapStr);
platformSpecificIsDir = _CFBundleSortedArrayContains(directoryContents, cheapStr);
platformSpecificIsUnknown = _CFBundleSortedArrayContains(unknownContents, cheapStr);
//fprintf(stderr, "looking for ");CFShow(cheapStr);if (platformSpecificFound) fprintf(stderr, "found it\n"); if (platformSpecificIsDir) fprintf(stderr, "a directory\n");
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
if (platformSpecificFound && platformSpecificIsUnknown) {
(void)_CFIsResourceAtPath(cheapStr, &platformSpecificIsDir);
//if (platformSpecificIsDir) fprintf(stderr, "a directory after all\n"); else fprintf(stderr, "not a directory after all\n");
}
#else /* READ_DIRECTORIES */
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, pathLen, pathLen);
CFStringReplaceAll(cheapStr, tmpString);
platformSpecificFound = _CFIsResourceAtPath(cheapStr, &platformSpecificIsDir);
#endif /* READ_DIRECTORIES */
}
}
}
if (platformSpecificFound) {
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, cheapStr, PLATFORM_PATH_STYLE, platformSpecificIsDir);
CFArrayAppendValue(result, url);
CFRelease(url);
} else if (platformGenericFound) {
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, platformGenericStr ? platformGenericStr : cheapStr, PLATFORM_PATH_STYLE, platformGenericIsDir);
CFArrayAppendValue(result, url);
CFRelease(url);
}
if (platformGenericStr) CFRelease(platformGenericStr);
#if READ_DIRECTORIES
CFRelease(contents);
CFRelease(directoryContents);
CFRelease(unknownContents);
#endif /* READ_DIRECTORIES */
#endif
}
#if READ_DIRECTORIES
#if __BLOCKS__
static void _CFSearchBundleDirectoryWithPredicate(CFAllocatorRef alloc, CFMutableArrayRef result, UniChar *pathUniChars, CFIndex dirPathLen, Boolean (^predicate)(CFStringRef filename, Boolean *stop), CFMutableStringRef cheapStr, CFMutableStringRef tmpString, Boolean *stopLooking, uint8_t version) {
// pathUniChars is the full path to the directory we are searching.
// platformUniChars is the platform name.
// predicate is a block that evaluates a given filename to see if it's a match.
// cheapStr is available for our use for whatever we want.
// URLs for found resources get added to result.
// get the contents of the directory
CFArrayRef contents, directoryContents, unknownContents;
CFRange contentsRange;
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, dirPathLen, dirPathLen);
CFStringReplaceAll(cheapStr, tmpString);
if (!_CFAppendTrailingPathSlash(pathUniChars, &dirPathLen, CFMaxPathSize)) {
return;
}
//fprintf(stderr, "looking in ");CFShow(cheapStr);
contents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleAllContents);
contentsRange = CFRangeMake(0, CFArrayGetCount(contents));
directoryContents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleDirectoryContents);
unknownContents = _CFBundleCopySortedDirectoryContentsAtPath(cheapStr, _CFBundleUnknownContents);
// scan directory contents for matches against predicate
for (int i = 0; i < contentsRange.length; i++) {
CFStringRef candidateFilename = CFArrayGetValueAtIndex(contents, i);
if (predicate(candidateFilename, stopLooking)) {
// we want this resource, though possibly a platform specific version of it
// unpack candidateFilename string into pathUniChars after verifying that we have enough space in the buffer
CFIndex candidateFilenameLength = CFStringGetLength(candidateFilename);
if ((dirPathLen + candidateFilenameLength < CFMaxPathSize)) {
CFStringGetCharacters(candidateFilename, CFRangeMake(0, candidateFilenameLength), pathUniChars + dirPathLen);
// is there a platform specific version available? if so update pathUniChars to contain it and candidateFilenameLength to describe its length.
static const int platformSeparatorLen = 1; // the length of '-', as appears in foo-macos.tiff. sugar to make the following easier to read.
if (_PlatformLen && (dirPathLen + candidateFilenameLength + platformSeparatorLen + _PlatformLen < CFMaxPathSize)) {
CFIndex candidateFilenameWithoutExtensionLen = _CFLengthAfterDeletingPathExtension(pathUniChars + dirPathLen, candidateFilenameLength);
CFIndex extensionLen = candidateFilenameLength - candidateFilenameWithoutExtensionLen;
// shift the extension over to make space for the platform
memmove(pathUniChars + dirPathLen + candidateFilenameWithoutExtensionLen + platformSeparatorLen + _PlatformLen, pathUniChars + dirPathLen + candidateFilenameWithoutExtensionLen, extensionLen * sizeof(UniChar));
// write the platform into the middle of the string
pathUniChars[dirPathLen + candidateFilenameWithoutExtensionLen] = (UniChar)'-';
memcpy(pathUniChars + dirPathLen + candidateFilenameWithoutExtensionLen + platformSeparatorLen, _PlatformUniChars, _PlatformLen * sizeof(UniChar));
// pack it up as a CFStringRef
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars + dirPathLen, candidateFilenameLength + platformSeparatorLen + _PlatformLen, candidateFilenameLength + _PlatformLen);
CFStringReplaceAll(cheapStr, tmpString);
// is the platform specialized version there?
if (_CFBundleSortedArrayContains(contents, cheapStr)) {
// woo. update the candidateFilenameLength. we'll update the candidateFilename too for consistency, but we don't actually use it again.
// the pathUniChars now contains the full path to the file
candidateFilename = cheapStr;
candidateFilenameLength = candidateFilenameLength + _PlatformLen + platformSeparatorLen;
} else {
// nope, no platform specific resource. Put the pathUniChars back how they were before, without the platform.
memmove(pathUniChars + dirPathLen + candidateFilenameWithoutExtensionLen, pathUniChars + dirPathLen + candidateFilenameWithoutExtensionLen + platformSeparatorLen + _PlatformLen, extensionLen * sizeof(UniChar));
}
}
// get the full path into cheapStr
CFStringSetExternalCharactersNoCopy(tmpString, pathUniChars, dirPathLen + candidateFilenameLength, dirPathLen + candidateFilenameLength);
CFStringReplaceAll(cheapStr, tmpString);
// is the resource a directory? we need to know so that we can avoid file access when making a URL.
Boolean isDir = 0;
if (_CFBundleSortedArrayContains(directoryContents, cheapStr)) {
isDir = 1;
} else if (_CFBundleSortedArrayContains(unknownContents, cheapStr)) {
_CFIsResourceAtPath(cheapStr, &isDir);
}
CFURLRef url = CFURLCreateWithFileSystemPath(alloc, cheapStr, PLATFORM_PATH_STYLE, isDir);
CFArrayAppendValue(result, url);
CFRelease(url);
}
}
if (*stopLooking) break;
}
CFRelease(contents);
CFRelease(directoryContents);
CFRelease(unknownContents);
}
#endif
#endif
#if _BLOCKS
static void _CFFindBundleResourcesInRawDir(CFAllocatorRef alloc, UniChar *workingUniChars, CFIndex workingLen, UniChar *nameUniChars, CFIndex nameLen, CFArrayRef resTypes, CFIndex limit, Boolean *stopLooking, Boolean (^predicate)(CFStringRef filename, Boolean *stop), uint8_t version, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, CFMutableArrayRef result) {
if (predicate) {
#if READ_DIRECTORIES
_CFSearchBundleDirectoryWithPredicate(alloc, result, workingUniChars, workingLen, predicate, cheapStr, tmpString, stopLooking, version);
return;
#else
CFLog(kCFLogLevelCritical, CFSTR("_CFFindBundleResourcesInRawDir: predicate blocks are not supported on this platform"));
HALT;
#endif
}
#else
static void _CFFindBundleResourcesInRawDir(CFAllocatorRef alloc, UniChar *workingUniChars, CFIndex workingLen, UniChar *nameUniChars, CFIndex nameLen, CFArrayRef resTypes, CFIndex limit, Boolean *stopLooking, void* predicate, uint8_t version, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, CFMutableArrayRef result) {
#endif
if (nameLen > 0) {
// If we have a resName, just call the search API. We may have to loop over the resTypes.
if (!resTypes) {
_CFSearchBundleDirectory(alloc, result, workingUniChars, workingLen, nameUniChars, nameLen, NULL, 0, cheapStr, tmpString, version);
} else {
CFArrayRef subResTypes = resTypes;
Boolean releaseSubResTypes = false;
CFIndex i, c = CFArrayGetCount(resTypes);
#if READ_DIRECTORIES
if (c > 2) {
// this is an optimization we employ when searching for large numbers of types, if the directory contents are available
// we scan the directory contents and restrict the list of resTypes to the types that might actually occur with the specified name
subResTypes = _CFCopyTypesForSearchBundleDirectory(alloc, workingUniChars, workingLen, nameUniChars, nameLen, resTypes, cheapStr, tmpString, version);
c = CFArrayGetCount(subResTypes);
releaseSubResTypes = true;
}
#endif /* READ_DIRECTORIES */
for (i = 0; i < c; i++) {
CFStringRef curType = (CFStringRef)CFArrayGetValueAtIndex(subResTypes, i);
CFIndex typeLen = CFStringGetLength(curType);
STACK_BUFFER_DECL(UniChar, typeChars, typeLen);
CFStringGetCharacters(curType, CFRangeMake(0, typeLen), typeChars);
_CFSearchBundleDirectory(alloc, result, workingUniChars, workingLen, nameUniChars, nameLen, typeChars, typeLen, cheapStr, tmpString, version);
if (limit <= CFArrayGetCount(result)) break;
}
if (releaseSubResTypes) CFRelease(subResTypes);
}
} else {
// If we have no resName, do it by hand. We may have to loop over the resTypes.
char cpathBuff[CFMaxPathSize];
CFIndex cpathLen;
CFMutableArrayRef children;
CFStringSetExternalCharactersNoCopy(tmpString, workingUniChars, workingLen, workingLen);
if (!CFStringGetFileSystemRepresentation(tmpString, cpathBuff, CFMaxPathSize)) return;
cpathLen = (CFIndex)strlen(cpathBuff);
if (!resTypes) {
// ??? should this use _CFBundleCopyDirectoryContentsAtPath?
children = _CFContentsOfDirectory(alloc, cpathBuff, NULL, NULL, NULL);
if (children) {
CFIndex childIndex, childCount = CFArrayGetCount(children);
for (childIndex = 0; childIndex < childCount; childIndex++) CFArrayAppendValue(result, CFArrayGetValueAtIndex(children, childIndex));
CFRelease(children);
}
} else {
CFIndex i, c = CFArrayGetCount(resTypes);
for (i = 0; i < c; i++) {
CFStringRef curType = (CFStringRef)CFArrayGetValueAtIndex(resTypes, i);
// ??? should this use _CFBundleCopyDirectoryContentsAtPath?
children = _CFContentsOfDirectory(alloc, cpathBuff, NULL, NULL, curType);
if (children) {
CFIndex childIndex, childCount = CFArrayGetCount(children);
for (childIndex = 0; childIndex < childCount; childIndex++) CFArrayAppendValue(result, CFArrayGetValueAtIndex(children, childIndex));
CFRelease(children);
}
if (limit <= CFArrayGetCount(result)) break;
}
}
}
}
#ifdef _BLOCKS
static void _CFFindBundleResourcesInResourcesDir(CFAllocatorRef alloc, UniChar *workingUniChars, CFIndex workingLen, UniChar *subDirUniChars, CFIndex subDirLen, CFArrayRef searchLanguages, UniChar *nameUniChars, CFIndex nameLen, CFArrayRef resTypes, CFIndex limit, Boolean (^predicate)(CFStringRef filename, Boolean *stop), uint8_t version, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, CFMutableArrayRef result) {
#else
static void _CFFindBundleResourcesInResourcesDir(CFAllocatorRef alloc, UniChar *workingUniChars, CFIndex workingLen, UniChar *subDirUniChars, CFIndex subDirLen, CFArrayRef searchLanguages, UniChar *nameUniChars, CFIndex nameLen, CFArrayRef resTypes, CFIndex limit, void* predicate, uint8_t version, CFMutableStringRef cheapStr, CFMutableStringRef tmpString, CFMutableArrayRef result) {
#endif
CFIndex savedWorkingLen = workingLen;
Boolean stopLooking = false; // for predicate based-queries, we set stopLooking instead of using a limit
// Look directly in the directory specified in workingUniChars. as if it is a Resources directory.
if (1 == version) {
// Add the non-localized resource directory.
Boolean appendSucceeded = _CFAppendPathComponent(workingUniChars, &workingLen, CFMaxPathSize, _GlobalResourcesUniChars, _GlobalResourcesLen);
if (appendSucceeded && subDirLen > 0) appendSucceeded = _CFAppendPathComponent(workingUniChars, &workingLen, CFMaxPathSize, subDirUniChars, subDirLen);
if (appendSucceeded) _CFFindBundleResourcesInRawDir(alloc, workingUniChars, workingLen, nameUniChars, nameLen, resTypes, limit, &stopLooking, predicate, version, cheapStr, tmpString, result);
// Strip the non-localized resource directory.
workingLen = savedWorkingLen;
}
if (CFArrayGetCount(result) < limit && !stopLooking) {
Boolean appendSucceeded = true;
if (subDirLen > 0) appendSucceeded = _CFAppendPathComponent(workingUniChars, &workingLen, CFMaxPathSize, subDirUniChars, subDirLen);
if (appendSucceeded) _CFFindBundleResourcesInRawDir(alloc, workingUniChars, workingLen, nameUniChars, nameLen, resTypes, limit, &stopLooking, predicate, version, cheapStr, tmpString, result);
}
// Now search the local resources.
workingLen = savedWorkingLen;
if (CFArrayGetCount(result) < limit && !stopLooking) {
CFIndex langCount = (searchLanguages ? CFArrayGetCount(searchLanguages) : 0);
// MF:??? OK to hard-wire this length?
UniChar curLangUniChars[255];
CFIndex numResults = CFArrayGetCount(result);
for (CFIndex langIndex = 0; langIndex < langCount; langIndex++) {
CFStringRef curLangStr = (CFStringRef)CFArrayGetValueAtIndex(searchLanguages, langIndex);
CFIndex curLangLen = CFStringGetLength(curLangStr);
if (curLangLen > 255) curLangLen = 255;
CFStringGetCharacters(curLangStr, CFRangeMake(0, curLangLen), curLangUniChars);
savedWorkingLen = workingLen;
if (!_CFAppendPathComponent(workingUniChars, &workingLen, CFMaxPathSize, curLangUniChars, curLangLen)) {
workingLen = savedWorkingLen;
continue;
}
if (!_CFAppendPathExtension(workingUniChars, &workingLen, CFMaxPathSize, _LprojUniChars, _LprojLen)) {
workingLen = savedWorkingLen;
continue;
}
if (subDirLen > 0) {
if (!_CFAppendPathComponent(workingUniChars, &workingLen, CFMaxPathSize, subDirUniChars, subDirLen)) {
workingLen = savedWorkingLen;
continue;
}
}
_CFFindBundleResourcesInRawDir(alloc, workingUniChars, workingLen, nameUniChars, nameLen, resTypes, limit, &stopLooking, predicate, version, cheapStr, tmpString, result);
// Back off this lproj component
workingLen = savedWorkingLen;
if (CFArrayGetCount(result) != numResults) {
// We found resources in a language we already searched. Don't look any farther.
// We also don't need to check the limit, since if the count changed at all, we are bailing.
break;
}
}
}
}
extern void _CFStrSetDesiredCapacity(CFMutableStringRef str, CFIndex len);
#ifdef _BLOCKS
CFArrayRef _CFFindBundleResources(CFBundleRef bundle, CFURLRef bundleURL, CFStringRef subDirName, CFArrayRef searchLanguages, CFStringRef resName, CFArrayRef resTypes, CFIndex limit, Boolean (^predicate)(CFStringRef filename, Boolean *stop), uint8_t version) {
#else
CFArrayRef _CFFindBundleResources(CFBundleRef bundle, CFURLRef bundleURL, CFStringRef subDirName, CFArrayRef searchLanguages, CFStringRef resName, CFArrayRef resTypes, CFIndex limit, void* predicate, uint8_t version) {
#endif
CFMutableArrayRef result = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
// Build an absolute path to the base directory.
// If no URL was passed, we get it from the bundle.
CFURLRef baseURL = bundleURL ? (CFURLRef)CFRetain(bundleURL) : (bundle ? CFBundleCopyBundleURL(bundle) : NULL);
CFURLRef absoluteURL = baseURL ? CFURLCopyAbsoluteURL(baseURL) : NULL;
CFStringRef basePath = absoluteURL ? CFURLCopyFileSystemPath(absoluteURL, PLATFORM_PATH_STYLE) : NULL;
if (absoluteURL) CFRelease(absoluteURL);
if (baseURL) CFRelease(baseURL);
baseURL = absoluteURL = bundleURL = NULL;
bundle = NULL;
// bundle and bundleURL arguments are not used any further
if (!basePath) return result;
UniChar *workingUniChars, *nameUniChars, *subDirUniChars;
CFIndex nameLen = 0;
CFIndex workingLen, savedWorkingLen;
CFMutableStringRef cheapStr, tmpString;
if (resName) {
char buff[CFMaxPathSize];
CFStringRef newResName = NULL;
if (CFStringGetFileSystemRepresentation(resName, buff, CFMaxPathSize)) newResName = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, buff);
resName = newResName ? newResName : (CFStringRef)CFRetain(resName);
nameLen = CFStringGetLength(resName);
}
// Init the one-time-only unichar buffers.
_CFEnsureStaticBuffersInited();
// Build UniChar buffers for some of the string pieces we need.
CFIndex subDirLen = (subDirName ? CFStringGetLength(subDirName) : 0);
nameUniChars = (UniChar *)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(UniChar) * (nameLen + subDirLen + CFMaxPathSize), 0);
if (nameUniChars) {