2
Copyright (C) 2000-2005 SKYRIX Software AG
4
This file is part of SOPE.
6
SOPE is free software; you can redistribute it and/or modify it under
7
the terms of the GNU Lesser General Public License as published by the
8
Free Software Foundation; either version 2, or (at your option) any
11
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12
WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14
License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with SOPE; see the file COPYING. If not, write to the
18
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22
#include "NGResourceLocator.h"
23
#include "NSNull+misc.h"
26
@implementation NGResourceLocator
28
+ (id)resourceLocatorForGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs{
29
return [[[self alloc] initWithGNUstepPath:_path fhsPath:_fhs] autorelease];
32
- (id)initWithGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs {
33
if ((self = [super init])) {
34
self->gsSubPath = [_path copy];
35
self->fhsSubPath = [_fhs copy];
36
self->fileManager = [[NSFileManager defaultManager] retain];
38
self->flags.cacheSearchPathes = 1;
39
self->flags.cachePathMisses = 1;
40
self->flags.cachePathHits = 1;
45
return [self initWithGNUstepPath:@"Library/Resources" fhsPath:@"share"];
49
[self->nameToPathCache release];
50
[self->searchPathes release];
51
[self->fhsSubPath release];
52
[self->gsSubPath release];
53
[self->fileManager release];
59
- (NSArray *)gsRootPathes {
60
static NSArray *pathes = nil;
65
return [pathes isNotNull] ? pathes : nil;
67
env = [[NSProcessInfo processInfo] environment];
68
if ((apath = [env objectForKey:@"GNUSTEP_PATHPREFIX_LIST"]) == nil)
69
apath = [env objectForKey:@"GNUSTEP_PATHLIST"];
71
if (![apath isNotNull]) return nil;
72
pathes = [[apath componentsSeparatedByString:@":"] copy];
76
- (NSArray *)fhsRootPathes {
77
// TODO: we probably want to make this configurable?! At least with an envvar
78
static NSArray *pathes = nil;
80
pathes = [[NSArray alloc] initWithObjects:@"/usr/local/", @"/usr/", nil];
84
- (NSArray *)collectSearchPathes {
89
ma = [NSMutableArray arrayWithCapacity:6];
91
e = ([self->gsSubPath length] > 0)
92
? [[self gsRootPathes] objectEnumerator]
94
while ((p = [e nextObject])) {
95
p = [p stringByAppendingPathComponent:self->gsSubPath];
96
if ([ma containsObject:p])
99
if (![self->fileManager fileExistsAtPath:p])
105
e = ([self->fhsSubPath length] > 0)
106
? [[self fhsRootPathes] objectEnumerator]
108
while ((p = [e nextObject])) {
109
p = [p stringByAppendingPathComponent:self->fhsSubPath];
110
if ([ma containsObject:p])
113
if (![self->fileManager fileExistsAtPath:p])
122
- (NSArray *)searchPathes {
125
if (self->searchPathes != nil)
126
return self->searchPathes;
128
a = [self collectSearchPathes];
129
if (self->flags.cacheSearchPathes) {
130
ASSIGNCOPY(self->searchPathes, a);
131
return self->searchPathes; /* return copy */
139
- (void)cachePath:(NSString *)_path forName:(NSString *)_name {
140
if (self->nameToPathCache == nil)
141
self->nameToPathCache = [[NSMutableDictionary alloc] initWithCapacity:64];
143
[self->nameToPathCache setObject:_path?_path:(id)[NSNull null] forKey:_name];
148
- (NSString *)lookupFileWithName:(NSString *)_name {
152
if (![_name isNotNull] || [_name length] == 0)
154
if ((p = [self->nameToPathCache objectForKey:_name]))
155
return [p isNotNull] ? p : nil;
157
e = [[self searchPathes] objectEnumerator];
158
while ((p = [e nextObject])) {
159
p = [p stringByAppendingPathComponent:_name];
161
if (![self->fileManager fileExistsAtPath:p])
164
[self cachePath:p forName:_name];
168
if (self->flags.cachePathMisses)
169
[self cachePath:nil forName:_name];
173
- (NSString *)lookupFileWithName:(NSString *)_name extension:(NSString *)_ext {
174
if ([_ext isNotNull] && [_ext length] > 0)
175
_name = [_name stringByAppendingPathExtension:_ext];
176
return [self lookupFileWithName:_name];
179
@end /* NGResourceLocator */