]> err.no Git - sope/blob - sope-core/NGExtensions/NGResourceLocator.m
fixed gcc 4.0 warnings
[sope] / sope-core / NGExtensions / NGResourceLocator.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGResourceLocator.h"
23 #include "NSNull+misc.h"
24 #include "common.h"
25
26 @implementation NGResourceLocator
27
28 + (id)resourceLocatorForGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs{
29   return [[[self alloc] initWithGNUstepPath:_path fhsPath:_fhs] autorelease];
30 }
31
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];
37     
38     self->flags.cacheSearchPathes = 1;
39     self->flags.cachePathMisses   = 1;
40     self->flags.cachePathHits     = 1;
41   }
42   return self;
43 }
44 - (id)init {
45   return [self initWithGNUstepPath:@"Library/Resources" fhsPath:@"share"];
46 }
47
48 - (void)dealloc {
49   [self->nameToPathCache release];
50   [self->searchPathes release];
51   [self->fhsSubPath   release];
52   [self->gsSubPath    release];
53   [self->fileManager  release];
54   [super dealloc];
55 }
56
57 /* search pathes */
58
59 - (NSArray *)gsRootPathes {
60   static NSArray *pathes = nil;
61   NSDictionary *env;
62   NSString *apath;
63   
64   if (pathes != nil)
65     return [pathes isNotNull] ? pathes : nil;
66   
67   env = [[NSProcessInfo processInfo] environment];
68   if ((apath = [env objectForKey:@"GNUSTEP_PATHPREFIX_LIST"]) == nil)
69     apath = [env objectForKey:@"GNUSTEP_PATHLIST"];
70   
71   if (![apath isNotNull]) return nil;
72   pathes = [[apath componentsSeparatedByString:@":"] copy];
73   return pathes;
74 }
75
76 - (NSArray *)fhsRootPathes {
77   // TODO: we probably want to make this configurable?! At least with an envvar
78   static NSArray *pathes = nil;
79   if (pathes == nil)
80     pathes = [[NSArray alloc] initWithObjects:@"/usr/local/", @"/usr/", nil];
81   return pathes;
82 }
83
84 - (NSArray *)collectSearchPathes {
85   NSMutableArray *ma;
86   NSEnumerator *e;
87   NSString *p;
88   
89   ma = [NSMutableArray arrayWithCapacity:6];
90   
91   e = ([self->gsSubPath length] > 0)
92     ? [[self gsRootPathes] objectEnumerator]
93     : nil;
94   while ((p = [e nextObject])) {
95     p = [p stringByAppendingPathComponent:self->gsSubPath];
96     if ([ma containsObject:p])
97       continue;
98     
99     if (![self->fileManager fileExistsAtPath:p])
100       continue;
101
102     [ma addObject:p];
103   }
104   
105   e = ([self->fhsSubPath length] > 0)
106     ? [[self fhsRootPathes] objectEnumerator]
107     : nil;
108   while ((p = [e nextObject])) {
109     p = [p stringByAppendingPathComponent:self->fhsSubPath];
110     if ([ma containsObject:p])
111       continue;
112     
113     if (![self->fileManager fileExistsAtPath:p])
114       continue;
115     
116     [ma addObject:p];
117   }
118   
119   return ma;
120 }
121
122 - (NSArray *)searchPathes {
123   NSArray *a;
124   
125   if (self->searchPathes != nil)
126     return self->searchPathes;
127   
128   a = [self collectSearchPathes];
129   if (self->flags.cacheSearchPathes) {
130     ASSIGNCOPY(self->searchPathes, a);
131     return self->searchPathes; /* return copy */
132   }
133   
134   return a;
135 }
136
137 /* cache */
138
139 - (void)cachePath:(NSString *)_path forName:(NSString *)_name {
140   if (self->nameToPathCache == nil)
141     self->nameToPathCache = [[NSMutableDictionary alloc] initWithCapacity:64];
142   
143   [self->nameToPathCache setObject:_path?_path:(id)[NSNull null] forKey:_name];
144 }
145
146 /* operation */
147
148 - (NSString *)lookupFileWithName:(NSString *)_name {
149   NSEnumerator *e;
150   NSString *p;
151   
152   if (![_name isNotNull] || [_name length] == 0)
153     return nil;
154   if ((p = [self->nameToPathCache objectForKey:_name]))
155     return [p isNotNull] ? p : nil;
156   
157   e = [[self searchPathes] objectEnumerator];
158   while ((p = [e nextObject])) {
159     p = [p stringByAppendingPathComponent:_name];
160     
161     if (![self->fileManager fileExistsAtPath:p])
162       continue;
163     
164     [self cachePath:p forName:_name];
165     return p;
166   }
167   
168   if (self->flags.cachePathMisses)
169     [self cachePath:nil forName:_name];
170   return nil;
171 }
172
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];
177 }
178
179 @end /* NGResourceLocator */