2 Copyright (C) 2000-2006 SKYRIX Software AG
3 Copyright (C) 2006 Helge Hess
5 This file is part of SOPE.
7 SOPE is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with SOPE; see the file COPYING. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGResourceLocator.h"
24 #include "NSNull+misc.h"
27 @implementation NGResourceLocator
29 + (id)resourceLocatorForGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs{
30 return [[[self alloc] initWithGNUstepPath:_path fhsPath:_fhs] autorelease];
33 - (id)initWithGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs {
34 if ((self = [super init])) {
35 self->gsSubPath = [_path copy];
36 self->fhsSubPath = [_fhs copy];
37 self->fileManager = [[NSFileManager defaultManager] retain];
39 self->flags.cacheSearchPathes = 1;
40 self->flags.cachePathMisses = 1;
41 self->flags.cachePathHits = 1;
46 return [self initWithGNUstepPath:@"Library/Resources" fhsPath:@"share"];
50 [self->nameToPathCache release];
51 [self->searchPathes release];
52 [self->fhsSubPath release];
53 [self->gsSubPath release];
54 [self->fileManager release];
60 - (NSArray *)gsRootPathes {
61 static NSArray *pathes = nil;
66 return [pathes isNotNull] ? pathes : (NSArray *)nil;
68 env = [[NSProcessInfo processInfo] environment];
69 if ((apath = [env objectForKey:@"GNUSTEP_PATHPREFIX_LIST"]) == nil)
70 apath = [env objectForKey:@"GNUSTEP_PATHLIST"];
72 if (![apath isNotNull]) return nil;
73 pathes = [[apath componentsSeparatedByString:@":"] copy];
77 - (NSArray *)fhsRootPathes {
78 // TODO: we probably want to make this configurable?! At least with an envvar
79 static NSArray *pathes = nil;
81 pathes = [[NSArray alloc] initWithObjects:
82 #ifdef FHS_INSTALL_ROOT
85 @"/usr/local/", @"/usr/", nil];
90 - (NSArray *)collectSearchPathes {
95 ma = [NSMutableArray arrayWithCapacity:6];
97 e = ([self->gsSubPath length] > 0)
98 ? [[self gsRootPathes] objectEnumerator]
99 : (NSEnumerator *)nil;
100 while ((p = [e nextObject]) != nil) {
101 p = [p stringByAppendingPathComponent:self->gsSubPath];
102 if ([ma containsObject:p])
105 if (![self->fileManager fileExistsAtPath:p])
111 e = ([self->fhsSubPath length] > 0)
112 ? [[self fhsRootPathes] objectEnumerator]
113 : (NSEnumerator *)nil;
114 while ((p = [e nextObject]) != nil) {
115 p = [p stringByAppendingPathComponent:self->fhsSubPath];
116 if ([ma containsObject:p])
119 if (![self->fileManager fileExistsAtPath:p])
128 - (NSArray *)searchPathes {
131 if (self->searchPathes != nil)
132 return self->searchPathes;
134 a = [self collectSearchPathes];
135 if (self->flags.cacheSearchPathes) {
136 ASSIGNCOPY(self->searchPathes, a);
137 return self->searchPathes; /* return copy */
145 - (void)cachePath:(NSString *)_path forName:(NSString *)_name {
146 if (self->nameToPathCache == nil)
147 self->nameToPathCache = [[NSMutableDictionary alloc] initWithCapacity:64];
149 [self->nameToPathCache setObject:(_path ? _path : (NSString *)[NSNull null])
155 - (NSString *)lookupFileWithName:(NSString *)_name {
159 if (![_name isNotNull] || [_name length] == 0)
161 if ((p = [self->nameToPathCache objectForKey:_name]) != nil)
162 return [p isNotNull] ? p : (NSString *)nil;
164 e = [[self searchPathes] objectEnumerator];
165 while ((p = [e nextObject]) != nil) {
166 p = [p stringByAppendingPathComponent:_name];
168 if (![self->fileManager fileExistsAtPath:p])
171 [self cachePath:p forName:_name];
175 if (self->flags.cachePathMisses)
176 [self cachePath:nil forName:_name];
180 - (NSString *)lookupFileWithName:(NSString *)_name extension:(NSString *)_ext {
181 if ([_ext isNotNull] && [_ext length] > 0)
182 _name = [_name stringByAppendingPathExtension:_ext];
183 return [self lookupFileWithName:_name];
186 - (NSArray *)lookupAllFilesWithExtension:(NSString *)_ext
187 doReturnFullPath:(BOOL)_withPath
189 /* only deliver each filename once */
190 NSMutableArray *pathes;
191 NSMutableSet *uniquer;
192 NSArray *lSearchPathes;
195 _ext = ([_ext length] > 0)
196 ? [@"." stringByAppendingString:_ext]
199 uniquer = [NSMutableSet setWithCapacity:128];
200 pathes = _withPath ? [NSMutableArray arrayWithCapacity:64] : nil;
201 lSearchPathes = [self searchPathes];
203 for (i = 0, count = [lSearchPathes count]; i < count; i++) {
207 filenames = [self->fileManager directoryContentsAtPath:
208 [lSearchPathes objectAtIndex:i]];
210 for (j = 0, jcount = [filenames count]; j < jcount; j++) {
213 fn = [filenames objectAtIndex:j];
215 if (![fn hasSuffix:_ext])
219 if ([uniquer containsObject:fn])
222 [uniquer addObject:fn];
224 /* build and cache path */
225 pn = [[lSearchPathes objectAtIndex:i] stringByAppendingPathComponent:fn];
226 [self cachePath:pn forName:fn];
227 if (_withPath) [pathes addObject:pn];
231 return _withPath ? (NSArray *)pathes : [uniquer allObjects];
236 - (NSString *)description {
239 ms = [NSMutableString stringWithCapacity:128];
240 [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
242 [ms appendFormat:@" gs=%@ fhs=%@", self->gsSubPath, self->fhsSubPath];
244 [ms appendString:@" cache"];
245 if (self->flags.cacheSearchPathes)
246 [ms appendString:@":pathes"];
247 if (self->flags.cachePathHits)
248 [ms appendString:@":hits"];
249 if (self->flags.cachePathMisses)
250 [ms appendString:@":misses"];
251 [ms appendFormat:@":#%d", [self->nameToPathCache count]];
253 [ms appendString:@">"];
257 @end /* NGResourceLocator */