-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathNSBundle.m
3515 lines (3123 loc) · 95.7 KB
/
NSBundle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Implementation of NSBundle class
Copyright (C) 1993-2002 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: May 1993
Author: Mirko Viviani <mirko.viviani@rccr.cremona.it>
Date: October 2000 Added frameworks support
Author: Nicola Pero <nicola@brainstorm.co.uk>
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
<title>NSBundle class reference</title>
*/
#define EXPOSE_NSBundle_IVARS 1
#import "common.h"
#include "objc-load.h"
#import "Foundation/NSBundle.h"
#import "Foundation/NSException.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSNull.h"
#import "Foundation/NSProcessInfo.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSMapTable.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSData.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSSet.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#import "GSPrivate.h"
/* Store the working directory at startup */
static NSString *_launchDirectory = nil;
static NSFileManager *
manager()
{
static NSFileManager *mgr = nil;
if (nil == mgr)
{
mgr = RETAIN([NSFileManager defaultManager]);
ASSIGN(_launchDirectory, [mgr currentDirectoryPath]);
[[NSObject leakAt: &mgr] release];
}
return mgr;
}
#define NOT_LOCALIZED @""
static NSDictionary *langAliases = nil;
static NSDictionary *langCanonical = nil;
/* Map a language name to any alternative versions. This function should
* return an array of alternative language/localisation directory names in
* the preferred order of precedence (ie resources in the directories named
* earlier in the array are to be preferred to those in directories named
* later).
* We should support regional language specifications (such as en-GB)
* as our first priority, and then fall back to the more general names.
* NB. Also handle the form like en-GB_US (English language, British dialect,
* in the United States region).
*/
static NSArray *
altLang(NSString *full)
{
NSMutableArray *a = nil;
if (nil != full)
{
NSString *alias = nil;
NSString *canon = nil;
NSString *lang = nil;
NSString *dialect = nil;
NSString *region = nil;
NSRange r;
alias = [langAliases objectForKey: full];
if (nil == alias)
{
canon = [langCanonical objectForKey: full];
if (nil != canon)
{
alias = [langAliases objectForKey: canon];
}
if (nil == alias)
{
alias = full;
}
}
canon = [langCanonical objectForKey: alias];
if (nil == canon)
{
canon = [langCanonical objectForKey: full];
if (nil == canon)
{
canon = full;
}
}
if ((r = [canon rangeOfString: @"-"]).length > 1)
{
dialect = [canon substringFromIndex: NSMaxRange(r)];
lang = [canon substringToIndex: r.location];
if ((r = [dialect rangeOfString: @"_"]).length > 1)
{
region = [dialect substringFromIndex: NSMaxRange(r)];
dialect = [dialect substringToIndex: r.location];
}
}
else if ((r = [canon rangeOfString: @"_"]).length > 1)
{
region = [canon substringFromIndex: NSMaxRange(r)];
lang = [canon substringToIndex: r.location];
}
else
{
lang = canon;
}
a = [NSMutableArray arrayWithCapacity: 5];
if (nil != dialect && nil != region)
{
[a addObject: [NSString stringWithFormat: @"%@-%@_%@",
lang, dialect, region]];
}
if (nil != dialect)
{
[a addObject: [NSString stringWithFormat: @"%@-%@",
lang, dialect]];
}
if (nil != region)
{
[a addObject: [NSString stringWithFormat: @"%@_%@",
lang, region]];
}
[a addObject: lang];
if (NO == [a containsObject: alias])
{
[a addObject: alias];
}
}
return a;
}
static NSLock *pathCacheLock = nil;
static NSMutableDictionary *pathCache = nil;
@interface NSObject (PrivateFrameworks)
+ (NSString*) frameworkVersion;
+ (NSString**) frameworkClasses;
@end
typedef enum {
NSBUNDLE_BUNDLE = 1,
NSBUNDLE_APPLICATION,
NSBUNDLE_FRAMEWORK,
NSBUNDLE_LIBRARY
} bundle_t;
/* Class variables - We keep track of all the bundles */
static NSBundle *_mainBundle = nil;
static NSMapTable *_bundles = NULL;
static NSMapTable *_byClass = NULL;
static NSMapTable *_byIdentifier = NULL;
static NSString *_base_version
= OBJC_STRINGIFY(GNUSTEP_BASE_MAJOR_VERSION.GNUSTEP_BASE_MINOR_VERSION);
/*
* An empty strings file table for use when localization files can't be found.
*/
static NSDictionary *_emptyTable = nil;
static NSString *_mainBundlePath = nil;
/* When we are linking in an object file, GSPrivateLoadModule calls our
callback routine for every Class and Category loaded. The following
variable stores the bundle that is currently doing the loading so we know
where to store the class names.
*/
static NSBundle *_loadingBundle = nil;
static NSBundle *_gnustep_bundle = nil;
static NSRecursiveLock *load_lock = nil;
static BOOL _strip_after_loading = NO;
/* List of framework linked in the _loadingBundle */
static NSMutableArray *_loadingFrameworks = nil;
static NSString *_currentFrameworkName = nil;
static NSString *gnustep_target_dir =
#ifdef GNUSTEP_TARGET_DIR
@GNUSTEP_TARGET_DIR;
#else
nil;
#endif
static NSString *gnustep_target_cpu =
#ifdef GNUSTEP_TARGET_CPU
@GNUSTEP_TARGET_CPU;
#else
nil;
#endif
static NSString *gnustep_target_os =
#ifdef GNUSTEP_TARGET_OS
@GNUSTEP_TARGET_OS;
#else
nil;
#endif
static NSString *library_combo =
#ifdef LIBRARY_COMBO
@LIBRARY_COMBO;
#else
nil;
#endif
#ifdef __ANDROID__
static jobject _jassetManager = NULL;
static AAssetManager *_assetManager = NULL;
#endif
/*
* Try to find the absolute path of an executable.
* Search all the directoried in the PATH.
* The atLaunch flag determines whether '.' is considered to be
* the current working directory or the working directory at the
* time when the program was launched (technically the directory
* at the point when NSBundle was first used ... so programs must
* use NSBundle *before* changing their working directories).
*/
static NSString*
AbsolutePathOfExecutable(NSString *path, BOOL atLaunch)
{
NSString *tmp;
if (0 == [path length])
{
fprintf(stderr, "AbsolutePathOfExecutable() empty path.\n");
return nil;
}
if (NO == [path isAbsolutePath])
{
NSFileManager *mgr = manager();
NSDictionary *env;
NSString *pathlist;
NSString *prefix = nil;
id pathArray;
NSEnumerator *enumerator;
NSString *result = nil;
NSUInteger index;
env = [[NSProcessInfo processInfo] environment];
pathlist = [env objectForKey: @"PATH"];
/* Windows 2000 and perhaps others have "Path" not "PATH" */
if (pathlist == nil)
{
pathlist = [env objectForKey: @"Path"];
}
#if defined(_WIN32)
pathArray = [pathlist componentsSeparatedByString: @";"];
#else
pathArray = [pathlist componentsSeparatedByString: @":"];
#endif
pathArray = AUTORELEASE([pathArray mutableCopy]);
/* The directory value '.' can be replaced by either the
* path to the current directory or the launch directory
* if it is known. Duplicates can be removed.
*/
if (atLaunch == YES)
{
prefix = _launchDirectory;
if ([prefix length] == 0)
{
fprintf(stderr, "AbsolutePathOfExecutable() failed to get"
" launch directory for '%s'.\n", [path UTF8String]);
return nil;
}
}
if (nil == prefix)
{
prefix = [mgr currentDirectoryPath];
if ([prefix length] == 0)
{
fprintf(stderr, "AbsolutePathOfExecutable() failed to get"
" current directory for '%s'.\n", [path UTF8String]);
return nil;
}
}
index = [pathArray indexOfObject: @"."];
if (NSNotFound == index)
{
[pathArray addObject: prefix];
}
else
{
[pathArray replaceObjectAtIndex: index withObject: prefix];
}
while ((index = [pathArray indexOfObject: @"."]) != NSNotFound)
{
[pathArray removeObjectAtIndex: index];
}
enumerator = [pathArray objectEnumerator];
while (nil != (prefix = [enumerator nextObject]))
{
prefix = [prefix stringByAppendingPathComponent: path];
if ([mgr isExecutableFileAtPath: prefix])
{
result = [prefix stringByStandardizingPath];
break;
}
#if defined(_WIN32)
else
{
NSString *extension = [path pathExtension];
/* Also try adding any executable extensions on windows
*/
if ([extension length] == 0)
{
static NSSet *executable = nil;
NSString *wpath;
NSEnumerator *e;
NSString *s;
if (nil == executable)
{
executable = [[NSTask executableExtensions] copy];
}
e = [executable objectEnumerator];
while (nil != (s = [e nextObject]))
{
wpath = [prefix stringByAppendingPathExtension: s];
if ([mgr isExecutableFileAtPath: wpath])
{
result = [wpath stringByStandardizingPath];
break;
}
}
}
}
#endif
}
if (nil == result)
{
fprintf(stderr, "AbsolutePathOfExecutable() unable to find '%s'"
" in any of %s.\n", [path UTF8String],
[[pathArray description] UTF8String]);
return nil;
}
path = result;
}
tmp = path;
path = [path stringByResolvingSymlinksInPath];
if ([path length] == 0)
{
fprintf(stderr, "AbsolutePathOfExecutable() resolving symlinks failed"
" for '%s'.\n", [tmp UTF8String]);
return nil;
}
tmp = path;
path = [path stringByStandardizingPath];
if ([path length] == 0)
{
fprintf(stderr, "AbsolutePathOfExecutable() standardizing path failed"
" for '%s'.\n", [tmp UTF8String]);
return nil;
}
return path;
}
/*
* Return the path to this executable.
*/
NSString *
GSPrivateExecutablePath()
{
static NSString *executablePath = nil;
static BOOL beenHere = NO;
if (beenHere == NO)
{
[load_lock lock];
if (beenHere == NO)
{
#if defined(PROCFS_EXE_LINK)
executablePath = [manager()
pathContentOfSymbolicLinkAtPath:
[NSString stringWithUTF8String: PROCFS_EXE_LINK]];
/*
On some systems, the link is of the form "[device]:inode", which
can be used to open the executable, but is useless if you want
the path to it. Thus we check that the path is an actual absolute
path. (Using '/' here is safe; it isn't the path separator
everywhere, but it is on all systems that have PROCFS_EXE_LINK.)
*/
if ([executablePath length] > 0
&& [executablePath characterAtIndex: 0] != '/')
{
executablePath = nil;
}
#endif
if ([executablePath length] == 0)
{
executablePath
= [[[NSProcessInfo processInfo] arguments] objectAtIndex: 0];
if ([executablePath length] == 0)
{
fprintf(stderr,
"Unable to get executable path from NSProcessInfo.\n");
}
}
executablePath = AbsolutePathOfExecutable(executablePath, YES);
IF_NO_ARC([executablePath retain];)
beenHere = YES;
}
[load_lock unlock];
NSCAssert(executablePath != nil, NSInternalInconsistencyException);
}
return executablePath;
}
/* Try to locate resources for tool name (which is this tool) in
* standard places like xxx/Library/Tools/Resources/name */
/* This could be converted into a public +bundleForTool:
* method. At the moment it's only used privately
* to locate the main bundle for this tool.
*/
static inline NSString *
_find_main_bundle_for_tool(NSString *toolName)
{
NSArray *paths;
NSEnumerator *enumerator;
NSString *path;
NSString *tail;
NSFileManager *fm = manager();
/*
* Eliminate any base path or extensions.
*/
toolName = [toolName lastPathComponent];
do
{
toolName = [toolName stringByDeletingPathExtension];
}
while ([[toolName pathExtension] length] > 0);
if ([toolName length] == 0)
{
return nil;
}
tail = [@"Tools" stringByAppendingPathComponent:
[@"Resources" stringByAppendingPathComponent: toolName]];
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSAllDomainsMask, YES);
enumerator = [paths objectEnumerator];
while ((path = [enumerator nextObject]))
{
BOOL isDir;
path = [path stringByAppendingPathComponent: tail];
if ([fm fileExistsAtPath: path isDirectory: &isDir] && isDir)
{
return path;
}
}
return nil;
}
static NSString *
_find_main_bundle_path()
{
if (nil == _mainBundlePath)
{
/* We figure out the main bundle directory by examining the location
of the executable on disk. */
NSString *path, *s;
/* We don't know at the beginning if it's a tool or an application. */
BOOL isApplication = YES;
/* Sometimes we detect that this is a non-installed tool. That is
* special because we want to lookup local resources before installed
* ones. Keep track of this special case in this variable.
*/
BOOL isNonInstalledTool = NO;
/* If it's a tool, we will need the tool name. Since we don't
know yet if it's a tool or an application, we always store
the executable name here - just in case it turns out it's a
tool. */
NSString *toolName = [GSPrivateExecutablePath() lastPathComponent];
#if defined(_WIN32) || defined(__CYGWIN__)
toolName = [toolName stringByDeletingPathExtension];
#endif
/* Strip off the name of the executable */
path = [GSPrivateExecutablePath() stringByDeletingLastPathComponent];
/* We now need to chop off the extra subdirectories, the library
combo and the target directory if they exist. The executable
and this library should match so that is why we can use the
compiled-in settings. */
/* library combo */
s = [path lastPathComponent];
if ([s isEqual: library_combo])
{
path = [path stringByDeletingLastPathComponent];
}
/* target dir */
s = [path lastPathComponent];
if ([s isEqual: gnustep_target_dir])
{
path = [path stringByDeletingLastPathComponent];
}
/* object dir */
s = [path lastPathComponent];
if ([s hasSuffix: @"obj"])
{
path = [path stringByDeletingLastPathComponent];
/* if it has an object dir it can only be a
non-yet-installed tool. */
isApplication = NO;
isNonInstalledTool = YES;
}
#ifndef __ANDROID__ /* don't check suffix on Android's fake executable path */
if (isApplication == YES)
{
s = [path lastPathComponent];
if ([s hasSuffix: @".app"] == NO
&& [s hasSuffix: @".debug"] == NO
&& [s hasSuffix: @".profile"] == NO
&& [s hasSuffix: @".gswa"] == NO // GNUstep Web
&& [s hasSuffix: @".woa"] == NO // GNUstep Web
)
{
NSFileManager *mgr = manager();
BOOL f;
/* Not one of the common extensions, but
* might be an app wrapper with another extension...
* Look for Info-gnustep.plist or Info.plist in a
* Resources subdirectory.
*/
s = [path stringByAppendingPathComponent: @"Resources"];
if ([mgr fileExistsAtPath: s isDirectory: &f] == NO || f == NO)
{
isApplication = NO;
}
else
{
NSString *i;
i = [s stringByAppendingPathComponent: @"Info-gnustep.plist"];
if ([mgr isReadableFileAtPath: i] == NO)
{
i = [s stringByAppendingPathComponent: @"Info.plist"];
if ([mgr isReadableFileAtPath: i] == NO)
{
isApplication = NO;
}
}
}
}
}
#endif /* !__ANDROID__ */
if (isApplication == NO)
{
NSString *maybePath = nil;
if (isNonInstalledTool)
{
/* We're pretty confident about this case. 'path' is
* obtained by {tool location on disk} and walking up
* until we got out of the obj directory. So we're
* now in GNUSTEP_BUILD_DIR. Resources will be in
* Resources/{toolName}.
*/
path = [path stringByAppendingPathComponent: @"Resources"];
maybePath = [path stringByAppendingPathComponent: toolName];
/* PS: We could check here if we found the resources,
* and if not, keep going with the other attempts at
* locating them. But if we know that this is an
* uninstalled tool, really we don't want to use
* installed resources - we prefer resource lookup to
* fail so the developer will fix whatever issue they
* have with their building.
*/
}
else
{
if (maybePath == nil)
{
/* This is for gnustep-make version 2, where tool resources
* are in GNUSTEP_*_LIBRARY/Tools/Resources/{toolName}.
*/
maybePath = _find_main_bundle_for_tool(toolName);
}
/* If that didn't work, maybe the tool was created with
* gnustep-make version 1. So we try {tool location on
* disk after walking up the non-flattened
* dirs}/Resources/{toolName}, which is where
* gnustep-make version 1 would put resources.
*/
if (maybePath == nil)
{
path = [path stringByAppendingPathComponent: @"Resources"];
maybePath = [path stringByAppendingPathComponent: toolName];
}
}
path = maybePath;
}
ASSIGN(_mainBundlePath, path);
}
return _mainBundlePath;
}
static NSArray *
bundle_directory_readable(NSString *path)
{
id found;
[pathCacheLock lock];
found = [[[pathCache objectForKey: path] retain] autorelease];
[pathCacheLock unlock];
if (nil == found)
{
NSFileManager *mgr = manager();
found = [mgr directoryContentsAtPath: path];
if (nil == found)
{
found = [NSNull null];
}
[pathCacheLock lock];
[pathCache setObject: found forKey: path];
[pathCacheLock unlock];
}
if ((id)[NSNull null] == found)
{
found = nil;
}
return (NSArray*)found;
}
/* Get the object file that should be located in the bundle of the same name */
static NSString *
bundle_object_name(NSString *path, NSString* executable)
{
NSFileManager *mgr = manager();
NSString *name, *path0, *path1, *path2;
if (executable)
{
NSString *exepath;
name = [executable lastPathComponent];
exepath = [executable stringByDeletingLastPathComponent];
if ([exepath isEqualToString: @""] == NO)
{
if ([exepath isAbsolutePath] == YES)
path = exepath;
else
path = [path stringByAppendingPathComponent: exepath];
}
}
else
{
name = [[path lastPathComponent] stringByDeletingPathExtension];
path = [path stringByDeletingLastPathComponent];
}
path0 = [path stringByAppendingPathComponent: name];
path = [path stringByAppendingPathComponent: gnustep_target_dir];
path1 = [path stringByAppendingPathComponent: name];
path = [path stringByAppendingPathComponent: library_combo];
path2 = [path stringByAppendingPathComponent: name];
if ([mgr isReadableFileAtPath: path2] == YES)
return path2;
else if ([mgr isReadableFileAtPath: path1] == YES)
return path1;
else if ([mgr isReadableFileAtPath: path0] == YES)
return path0;
#if defined(_WIN32)
/* If we couldn't find the binary, and we are on windows, and the name
* has no path extension, then let's try looking for a dll.
*/
if ([name pathExtension] == nil)
{
if ([mgr isReadableFileAtPath:
[path2 stringByAppendingPathExtension: @"dll"]] == YES)
return [path2 stringByAppendingPathExtension: @"dll"];
else if ([mgr isReadableFileAtPath:
[path1 stringByAppendingPathExtension: @"dll"]] == YES)
return [path1 stringByAppendingPathExtension: @"dll"];
else if ([mgr isReadableFileAtPath:
[path0 stringByAppendingPathExtension: @"dll"]] == YES)
return [path0 stringByAppendingPathExtension: @"dll"];
}
#endif
return path0;
}
/* Construct a path from components */
static void
addBundlePath(NSMutableArray *list, NSArray *contents,
NSString *path, NSString *subdir, NSString *lang)
{
if (nil == contents)
{
return;
}
if (nil != subdir)
{
NSEnumerator *e = [[subdir pathComponents] objectEnumerator];
NSString *subdirComponent;
while ((subdirComponent = [e nextObject]) != nil)
{
if (NO == [contents containsObject: subdirComponent])
{
return;
}
path = [path stringByAppendingPathComponent: subdirComponent];
if (nil == (contents = bundle_directory_readable(path)))
{
return;
}
}
}
if ([lang length] == 0)
{
[list addObject: path];
}
else
{
NSEnumerator *enumerator = [altLang(lang) objectEnumerator];
NSString *alt;
/* Add each language specific subdirectory in order.
*/
while (nil != (alt = [enumerator nextObject]))
{
alt = [alt stringByAppendingPathExtension: @"lproj"];
if (YES == [contents containsObject: alt])
{
alt = [path stringByAppendingPathComponent: alt];
if (nil != (contents = bundle_directory_readable(alt)))
{
[list addObject: alt];
}
}
}
}
}
/* Try to locate name framework in standard places
which are like /Library/Frameworks/(name).framework */
static inline NSString *
_find_framework(NSString *name)
{
NSArray *paths;
NSFileManager *file_mgr = manager();
NSString *file_name;
NSString *file_path;
NSString *path;
NSEnumerator *enumerator;
NSCParameterAssert(name != nil);
file_name = [name stringByAppendingPathExtension: @"framework"];
paths = NSSearchPathForDirectoriesInDomains(GSFrameworksDirectory,
NSAllDomainsMask,YES);
enumerator = [paths objectEnumerator];
while ((path = [enumerator nextObject]))
{
file_path = [path stringByAppendingPathComponent: file_name];
if ([file_mgr fileExistsAtPath: file_path] == YES)
{
return file_path; // Found it!
}
}
return nil;
}
/* This method is the backbone of the resource searching for NSBundle. It
* constructs an array of paths, where each path is a possible location
* for a resource in the bundle. The current algorithm for searching goes:
*
* <rootPath>/Resources/<subPath>
* <rootPath>/Resources/<subPath>/<language.lproj>
* <rootPath>/<subPath>
* <rootPath>/<subPath>/<language.lproj>
*
* NB. If localization is nil we ask NSUserDefaults for the preferred
* languages list (NSLanguages) and add language specific subdirectories
* for each language. It is more efficient to search for a specific
* language by providing a non-nil value, or to provide an empty
* string if no language specific lookup is needed.
*/
static NSArray *
_find_paths(NSString *rootPath, NSString *subPath, NSString *localization)
{
NSString *primary;
NSString *language;
NSArray *languages;
NSArray *contents;
NSMutableArray *array;
NSEnumerator *enumerate;
array = [NSMutableArray arrayWithCapacity: 8];
languages = localization ? nil : [[NSUserDefaults standardUserDefaults]
stringArrayForKey: @"NSLanguages"];
primary = [rootPath stringByAppendingPathComponent: @"Resources"];
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, subPath, nil);
/* If we have been asked for a specific localization, we add it.
*/
if (localization != nil)
{
addBundlePath(array, contents, primary, subPath, localization);
}
else
{
/* This matches OS X behavior, which only searches languages that
* are in the user's preference. Don't use -preferredLocalizations -
* that would cause a recursive loop.
*/
enumerate = [languages objectEnumerator];
while ((language = [enumerate nextObject]))
{
addBundlePath(array, contents, primary, subPath, language);
}
}
#ifdef __ANDROID__
/* Android: check subdir and localization directly, as AAssetDir and thereby
* NSDirectoryEnumerator doesn't list directories
*/
NSString *originalPrimary = primary;
if (subPath)
{
primary = [originalPrimary stringByAppendingPathComponent: subPath];
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, nil, nil);
if (localization)
{
if ([localization length])
{
primary = [primary stringByAppendingPathComponent:
[localization stringByAppendingPathExtension: @"lproj"]];
}
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, nil, nil);
}
else
{
NSString *subPathPrimary = primary;
enumerate = [languages objectEnumerator];
while ((language = [enumerate nextObject]))
{
primary = [subPathPrimary stringByAppendingPathComponent:
[language stringByAppendingPathExtension: @"lproj"]];
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, nil, nil);
}
}
}
if (localization)
{
if ([localization length])
{
primary = [originalPrimary stringByAppendingPathComponent:
[localization stringByAppendingPathExtension: @"lproj"]];
}
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, nil, nil);
}
else
{
enumerate = [languages objectEnumerator];
while ((language = [enumerate nextObject]))
{
primary = [originalPrimary stringByAppendingPathComponent:
[language stringByAppendingPathExtension: @"lproj"]];
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, nil, nil);
}
}
#endif /* __ANDROID__ */
primary = rootPath;
contents = bundle_directory_readable(primary);
addBundlePath(array, contents, primary, subPath, nil);
if (localization != nil)
{
addBundlePath(array, contents, primary, subPath, localization);
}
else
{
enumerate = [languages objectEnumerator];
while ((language = [enumerate nextObject]))
{
addBundlePath(array, contents, primary, subPath, language);
}
}
return array;
}
@implementation NSBundle (Private)
+ (NSString *) _absolutePathOfExecutable: (NSString *)path
{
return AbsolutePathOfExecutable(path, NO);
}
/* Nicola & Mirko:
Frameworks can be used in an application in two different ways:
() the framework is dynamically/manually loaded, as if it were a
bundle. This is the easier case, because we already have the
bundle setup with the correct path (it's the programmer's
responsibility to find the framework bundle on disk); we get all
information from the bundle dictionary, such as the version; we
also create the class list when loading the bundle, as for any
other bundle.
() the framework was linked into the application. This is much
more difficult, because without using tricks, we have no way of
knowing where the framework bundle (needed eg for resources) is on
disk, and we have no way of knowing what the class list is, or the
version. So the trick we use in this case to work around those
problems is that gnustep-make generates a 'NSFramework_xxx' class
and compiles it into each framework. By asking to the class, we
can get the version information and the list of classes which were
compiled into the framework. To get the location of the framework
on disk, we try using advanced dynamic linker features to get the
shared object file on disk from which the NSFramework_xxx class was
loaded. If that doesn't work, because the dynamic linker can't
provide this information on this platform (or maybe because the
framework was statically linked into the application), we have a
fallback trick :-) We look for the framework in the standard
locations and in the main bundle. This might fail if the framework
is not in a standard location or there is more than one installed
framework of the same name (and different versions?).
So at startup, we scan all classes which were compiled into the
application. For each NSFramework_ class, we call the following
function, which records the name of the framework, the version,
the classes belonging to it, and tries to determine the path
on disk to the framework bundle.