X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=sope-core%2FNGExtensions%2FNGResourceLocator.m;h=ecfe976bd3615465916bb4f45ccdaaa7ca2f4172;hb=da4fee939ad67e8da731714f06358d6bdd3ee617;hp=c2e2818a918cfd1e77dda240ba7b48ed2fc14c80;hpb=4d91b8d73b64594d7d5c49ba480cfbadd44852c3;p=sope diff --git a/sope-core/NGExtensions/NGResourceLocator.m b/sope-core/NGExtensions/NGResourceLocator.m index c2e2818a..ecfe976b 100644 --- a/sope-core/NGExtensions/NGResourceLocator.m +++ b/sope-core/NGExtensions/NGResourceLocator.m @@ -1,5 +1,6 @@ /* - Copyright (C) 2000-2005 SKYRIX Software AG + Copyright (C) 2000-2006 SKYRIX Software AG + Copyright (C) 2006 Helge Hess This file is part of SOPE. @@ -62,7 +63,7 @@ NSString *apath; if (pathes != nil) - return [pathes isNotNull] ? pathes : nil; + return [pathes isNotNull] ? pathes : (NSArray *)nil; env = [[NSProcessInfo processInfo] environment]; if ((apath = [env objectForKey:@"GNUSTEP_PATHPREFIX_LIST"]) == nil) @@ -76,8 +77,13 @@ - (NSArray *)fhsRootPathes { // TODO: we probably want to make this configurable?! At least with an envvar static NSArray *pathes = nil; - if (pathes == nil) - pathes = [[NSArray alloc] initWithObjects:@"/usr/local/", @"/usr/", nil]; + if (pathes == nil) { + pathes = [[NSArray alloc] initWithObjects: +#ifdef FHS_INSTALL_ROOT + FHS_INSTALL_ROOT, +#endif + @"/usr/local/", @"/usr/", nil]; + } return pathes; } @@ -90,8 +96,8 @@ e = ([self->gsSubPath length] > 0) ? [[self gsRootPathes] objectEnumerator] - : nil; - while ((p = [e nextObject])) { + : (NSEnumerator *)nil; + while ((p = [e nextObject]) != nil) { p = [p stringByAppendingPathComponent:self->gsSubPath]; if ([ma containsObject:p]) continue; @@ -104,8 +110,8 @@ e = ([self->fhsSubPath length] > 0) ? [[self fhsRootPathes] objectEnumerator] - : nil; - while ((p = [e nextObject])) { + : (NSEnumerator *)nil; + while ((p = [e nextObject]) != nil) { p = [p stringByAppendingPathComponent:self->fhsSubPath]; if ([ma containsObject:p]) continue; @@ -140,7 +146,8 @@ if (self->nameToPathCache == nil) self->nameToPathCache = [[NSMutableDictionary alloc] initWithCapacity:64]; - [self->nameToPathCache setObject:_path?_path:(id)[NSNull null] forKey:_name]; + [self->nameToPathCache setObject:(_path ? _path : (NSString *)[NSNull null]) + forKey:_name]; } /* operation */ @@ -151,11 +158,11 @@ if (![_name isNotNull] || [_name length] == 0) return nil; - if ((p = [self->nameToPathCache objectForKey:_name])) - return [p isNotNull] ? p : nil; + if ((p = [self->nameToPathCache objectForKey:_name]) != nil) + return [p isNotNull] ? p : (NSString *)nil; e = [[self searchPathes] objectEnumerator]; - while ((p = [e nextObject])) { + while ((p = [e nextObject]) != nil) { p = [p stringByAppendingPathComponent:_name]; if (![self->fileManager fileExistsAtPath:p]) @@ -176,4 +183,75 @@ return [self lookupFileWithName:_name]; } +- (NSArray *)lookupAllFilesWithExtension:(NSString *)_ext + doReturnFullPath:(BOOL)_withPath +{ + /* only deliver each filename once */ + NSMutableArray *pathes; + NSMutableSet *uniquer; + NSArray *lSearchPathes; + unsigned i, count; + + _ext = ([_ext length] > 0) + ? [@"." stringByAppendingString:_ext] + : (NSString *)nil; + + uniquer = [NSMutableSet setWithCapacity:128]; + pathes = _withPath ? [NSMutableArray arrayWithCapacity:64] : nil; + lSearchPathes = [self searchPathes]; + + for (i = 0, count = [lSearchPathes count]; i < count; i++) { + NSArray *filenames; + unsigned j, jcount; + + filenames = [self->fileManager directoryContentsAtPath: + [lSearchPathes objectAtIndex:i]]; + + for (j = 0, jcount = [filenames count]; j < jcount; j++) { + NSString *fn, *pn; + + fn = [filenames objectAtIndex:j]; + if (_ext != nil) { + if (![fn hasSuffix:_ext]) + continue; + } + + if ([uniquer containsObject:fn]) + continue; + + [uniquer addObject:fn]; + + /* build and cache path */ + pn = [[lSearchPathes objectAtIndex:i] stringByAppendingPathComponent:fn]; + [self cachePath:pn forName:fn]; + if (_withPath) [pathes addObject:pn]; + } + } + + return _withPath ? (NSArray *)pathes : [uniquer allObjects]; +} + +/* description */ + +- (NSString *)description { + NSMutableString *ms; + + ms = [NSMutableString stringWithCapacity:128]; + [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])]; + + [ms appendFormat:@" gs=%@ fhs=%@", self->gsSubPath, self->fhsSubPath]; + + [ms appendString:@" cache"]; + if (self->flags.cacheSearchPathes) + [ms appendString:@":pathes"]; + if (self->flags.cachePathHits) + [ms appendString:@":hits"]; + if (self->flags.cachePathMisses) + [ms appendString:@":misses"]; + [ms appendFormat:@":#%d", [self->nameToPathCache count]]; + + [ms appendString:@">"]; + return ms; +} + @end /* NGResourceLocator */