From: helge Date: Thu, 23 Sep 2004 14:23:25 +0000 (+0000) Subject: added NGResourceLocator X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0034461cf4c49d21032dca02bf4dbb400c825134;p=sope added NGResourceLocator git-svn-id: http://svn.opengroupware.org/SOPE/trunk@163 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- diff --git a/sope-core/NGExtensions/ChangeLog b/sope-core/NGExtensions/ChangeLog index e6d60991..19dd2d5d 100644 --- a/sope-core/NGExtensions/ChangeLog +++ b/sope-core/NGExtensions/ChangeLog @@ -1,5 +1,7 @@ 2004-09-23 Helge Hess + * added NGResourceLocator class (v4.2.116) + * moved NGCString to Recycler (was not compiled since v4.2.93) 2004-09-21 Marcus Mueller diff --git a/sope-core/NGExtensions/GNUmakefile b/sope-core/NGExtensions/GNUmakefile index 0bdc30d7..ace56416 100644 --- a/sope-core/NGExtensions/GNUmakefile +++ b/sope-core/NGExtensions/GNUmakefile @@ -34,6 +34,7 @@ libNGExtensions_HEADER_FILES = \ NGStack.h \ NGObjectMacros.h \ NGCalendarDateRange.h \ + NGResourceLocator.h \ libNGExtensions_OBJC_FILES = \ NGExtensions.m \ @@ -52,6 +53,7 @@ libNGExtensions_OBJC_FILES = \ NGQuotedPrintableCoding.m \ NGStack.m \ NGCalendarDateRange.m \ + NGResourceLocator.m \ ifeq ($(FOUNDATION_LIB), apple) libNGExtensions_OBJC_FILES += FileObjectHolder.m diff --git a/sope-core/NGExtensions/NGBase64Coding.m b/sope-core/NGExtensions/NGBase64Coding.m index 37f0f3d1..6beb918c 100644 --- a/sope-core/NGExtensions/NGBase64Coding.m +++ b/sope-core/NGExtensions/NGBase64Coding.m @@ -18,7 +18,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #include "NGBase64Coding.h" #include "common.h" diff --git a/sope-core/NGExtensions/NGExtensions/EOCacheDataSource.h b/sope-core/NGExtensions/NGExtensions/EOCacheDataSource.h index fd687fb4..fa1e0df2 100644 --- a/sope-core/NGExtensions/NGExtensions/EOCacheDataSource.h +++ b/sope-core/NGExtensions/NGExtensions/EOCacheDataSource.h @@ -1,7 +1,7 @@ /* - Copyright (C) 2000-2003 SKYRIX Software AG + Copyright (C) 2000-2004 SKYRIX Software AG - This file is part of OGo + This file is part of OpenGroupware.org. OGo 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 @@ -18,7 +18,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #ifndef __NGExtensions_EOCacheDataSource_H__ #define __NGExtensions_EOCacheDataSource_H__ diff --git a/sope-core/NGExtensions/NGExtensions/NGResourceLocator.h b/sope-core/NGExtensions/NGExtensions/NGResourceLocator.h new file mode 100644 index 00000000..43895276 --- /dev/null +++ b/sope-core/NGExtensions/NGExtensions/NGResourceLocator.h @@ -0,0 +1,69 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OpenGroupware.org. + + OGo 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, or (at your option) any + later version. + + OGo 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 OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#ifndef __NGExtensions_NGResourceLocator_H__ +#define __NGExtensions_NGResourceLocator_H__ + +#import + +/* + NGResourceLocator + + This class can be used by libraries to lookup resources in either the GNUstep + hierarchy or in FHS locations (/usr/local etc). +*/ + +@class NSString, NSArray, NSFileManager, NSMutableDictionary; + +@interface NGResourceLocator : NSObject +{ + NSString *gsSubPath; + NSString *fhsSubPath; + NSFileManager *fileManager; + + NSArray *searchPathes; + NSMutableDictionary *nameToPathCache; + + struct { + int cacheSearchPathes:1; + int cachePathHits:1; + int cachePathMisses:1; + int reserved:29; + } flags; +} + ++ (id)resourceLocatorForGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs; +- (id)initWithGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs; + +/* resource pathes */ + +- (NSArray *)gsRootPathes; /* GNUSTEP_PATHPREFIX_LIST or MacOSX */ +- (NSArray *)fhsRootPathes; +- (NSArray *)searchPathes; + +/* operations */ + +- (NSString *)lookupFileWithName:(NSString *)_name; +- (NSString *)lookupFileWithName:(NSString *)_name extension:(NSString *)_ext; + +@end + +#endif /* __NGExtensions_NGResourceLocator_H__ */ diff --git a/sope-core/NGExtensions/NGResourceLocator.m b/sope-core/NGExtensions/NGResourceLocator.m new file mode 100644 index 00000000..5e5a29a6 --- /dev/null +++ b/sope-core/NGExtensions/NGResourceLocator.m @@ -0,0 +1,177 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OpenGroupware.org. + + OGo 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, or (at your option) any + later version. + + OGo 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 OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#include "NGResourceLocator.h" +#include "NSNull+misc.h" +#include "common.h" + +@implementation NGResourceLocator + ++ (id)resourceLocatorForGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs{ + return [[[self alloc] initWithGNUstepPath:_path fhsPath:_fhs] autorelease]; +} + +- (id)initWithGNUstepPath:(NSString *)_path fhsPath:(NSString *)_fhs { + if ((self = [super init])) { + self->gsSubPath = [_path copy]; + self->fhsSubPath = [_fhs copy]; + self->fileManager = [[NSFileManager defaultManager] retain]; + + self->flags.cacheSearchPathes = 1; + self->flags.cachePathMisses = 1; + self->flags.cachePathHits = 1; + } + return self; +} +- (id)init { + return [self initWithGNUstepPath:@"Library/Resources" fhsPath:@"share"]; +} + +- (void)dealloc { + [self->nameToPathCache release]; + [self->searchPathes release]; + [self->fhsSubPath release]; + [self->gsSubPath release]; + [self->fileManager release]; + [super dealloc]; +} + +/* search pathes */ + +- (NSArray *)gsRootPathes { + static NSArray *pathes = nil; + NSDictionary *env; + NSString *apath; + + if (pathes != nil) + return [pathes isNotNull] ? pathes : nil; + + env = [[NSProcessInfo processInfo] environment]; + if ((apath = [env objectForKey:@"GNUSTEP_PATHPREFIX_LIST"]) == nil) + apath = [env objectForKey:@"GNUSTEP_PATHLIST"]; + + if (![apath isNotNull]) return nil; + pathes = [[apath componentsSeparatedByString:@":"] copy]; + return pathes; +} + +- (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]; + return pathes; +} + +- (NSArray *)collectSearchPathes { + NSMutableArray *ma; + NSEnumerator *e; + NSString *p; + + ma = [NSMutableArray arrayWithCapacity:6]; + + e = ([self->gsSubPath length] > 0) + ? [[self gsRootPathes] objectEnumerator] + : nil; + while ((p = [e nextObject])) { + p = [p stringByAppendingPathComponent:self->gsSubPath]; + if ([ma containsObject:p]) + continue; + + if (![self->fileManager fileExistsAtPath:p]) + continue; + + [ma addObject:p]; + } + + e = ([self->fhsSubPath length] > 0) + ? [[self fhsRootPathes] objectEnumerator] + : nil; + while ((p = [e nextObject])) { + p = [p stringByAppendingPathComponent:self->fhsSubPath]; + if ([ma containsObject:p]) + continue; + + if (![self->fileManager fileExistsAtPath:p]) + continue; + + [ma addObject:p]; + } + + return ma; +} + +- (NSArray *)searchPathes { + NSArray *a; + + if (self->searchPathes != nil) + return self->searchPathes; + + a = [self collectSearchPathes]; + if (self->flags.cacheSearchPathes) { + ASSIGNCOPY(self->searchPathes, a); + return self->searchPathes; /* return copy */ + } + + return a; +} + +/* cache */ + +- (void)cachePath:(NSString *)_path forName:(NSString *)_name { + if (self->nameToPathCache == nil) + self->nameToPathCache = [[NSMutableDictionary alloc] initWithCapacity:64]; + + [self->nameToPathCache setObject:_path?_path:(id)[NSNull null] forKey:_name]; +} + +/* operation */ + +- (NSString *)lookupFileWithName:(NSString *)_name { + NSEnumerator *e; + NSString *p; + + if (![_name isNotNull] || [_name length] == 0) + return nil; + if ((p = [self->nameToPathCache objectForKey:_name])) + return [p isNotNull] ? p : nil; + + e = [[self searchPathes] objectEnumerator]; + while ((p = [e nextObject])) { + p = [p stringByAppendingPathComponent:_name]; + + if (![self->fileManager fileExistsAtPath:p]) + continue; + + [self cachePath:p forName:_name]; + return p; + } + + if (self->flags.cachePathMisses) + [self cachePath:nil forName:_name]; + return nil; +} + +- (NSString *)lookupFileWithName:(NSString *)_name extension:(NSString *)_ext { + return [self lookupFileWithName:[_name stringByAppendingPathExtension:_ext]]; +} + +@end /* NGResourceLocator */ diff --git a/sope-core/NGExtensions/Version b/sope-core/NGExtensions/Version index 5e078f86..e37e3b3f 100644 --- a/sope-core/NGExtensions/Version +++ b/sope-core/NGExtensions/Version @@ -1,6 +1,6 @@ # version -SUBMINOR_VERSION:=115 +SUBMINOR_VERSION:=116 # v4.3.115 requires libFoundation v1.0.59 # v4.2.72 requires libEOControl v4.2.39 diff --git a/sope-core/samples/ChangeLog b/sope-core/samples/ChangeLog index d15a54cd..16e2d2eb 100644 --- a/sope-core/samples/ChangeLog +++ b/sope-core/samples/ChangeLog @@ -1,3 +1,9 @@ +2004-09-23 Helge Hess + + * addd sope-rsrclookup.m tool for tetsing NGResourceLocator + + * fmdls.m: some code cleanups + 2004-08-29 Helge Hess * moved test_qpdecode to NGMime/samples diff --git a/sope-core/samples/GNUmakefile b/sope-core/samples/GNUmakefile index 03bab9a2..9062c37d 100644 --- a/sope-core/samples/GNUmakefile +++ b/sope-core/samples/GNUmakefile @@ -12,6 +12,7 @@ TOOL_NAME = \ httpu_notify \ parserule \ testurl \ + sope-rsrclookup ifneq ($(OBJC_RUNTIME_LIB),apple) TOOL_NAME += subclassing @@ -27,6 +28,7 @@ testsock_OBJC_FILES = testsock.m parserule_OBJC_FILES = parserule.m httpu_notify_OBJC_FILES = httpu_notify.m testurl_OBJC_FILES = testurl.m +sope-rsrclookup_OBJC_FILES = sope-rsrclookup.m -include GNUmakefile.preamble include $(GNUSTEP_MAKEFILES)/tool.make diff --git a/sope-core/samples/eoqual.m b/sope-core/samples/eoqual.m index 922bea1c..5ad42a55 100644 --- a/sope-core/samples/eoqual.m +++ b/sope-core/samples/eoqual.m @@ -1,7 +1,7 @@ /* - Copyright (C) 2000-2003 SKYRIX Software AG + Copyright (C) 2000-2004 SKYRIX Software AG - This file is part of OGo + This file is part of OpenGroupware.org. OGo 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 @@ -18,7 +18,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #import "EOQualTool.h" #include "common.h" @@ -28,8 +27,7 @@ int main(int argc, char **argv, char **env) { EOQualTool *tool; int res; - pool = [NSAutoreleasePool new]; - + pool = [[NSAutoreleasePool alloc] init]; #if LIB_FOUNDATION_LIBRARY [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; #endif diff --git a/sope-core/samples/fmdls.m b/sope-core/samples/fmdls.m index be3b4ee2..ced04364 100644 --- a/sope-core/samples/fmdls.m +++ b/sope-core/samples/fmdls.m @@ -1,7 +1,7 @@ /* - Copyright (C) 2000-2003 SKYRIX Software AG + Copyright (C) 2000-2004 SKYRIX Software AG - This file is part of OGo + This file is part of OpenGroupware.org. OGo 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 @@ -18,7 +18,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #import #import @@ -26,11 +25,131 @@ #include #include +static void printDirRecord(NSString *path, NSDictionary *record) { + /* id uid gid date name */ + NSString *fileId; + NSString *owner; + int gid; + unsigned size; + NSString *modDate; + NSString *fname; + NSString *ftype; + + fileId = [[record objectForKey:@"NSFileIdentifier"] description]; + owner = [record objectForKey:NSFileOwnerAccountName]; + gid = [[record objectForKey:@"NSFileGroupOwnerAccountNumber"] intValue]; + size = [[record objectForKey:NSFileSize] intValue]; + modDate = [[record objectForKey:NSFileModificationDate] description]; + fname = [record objectForKey:@"NSFileName"]; + ftype = [record objectForKey:NSFileType]; + + if ([ftype isEqualToString:NSFileTypeDirectory]) + fname = [fname stringByAppendingString:@"/"]; + else if ([ftype isEqualToString:NSFileTypeSocket]) + fname = [fname stringByAppendingString:@"="]; + else if ([ftype isEqualToString:NSFileTypeSymbolicLink]) + fname = [fname stringByAppendingString:@"@"]; + + //NSLog(@"record: %@", record); + + printf("%8s %8s %8i %8i %8s %s\n", + [fileId cString], + [owner cString], + gid, + size, + [modDate cString], + [fname cString]); +} + +static void runOnDirPath(NSString *path) { + NSFileManager *fm; + EODataSource *ds; + NSEnumerator *records; + NSDictionary *record; + NSArray *sortOrderings; + EOQualifier *qualifier; + id tmp; + + fm = [NSFileManager defaultManager]; + + if ((ds = [fm dataSourceAtPath:path]) == nil) { + NSLog(@"could not get datasource for path: '%@'", path); + return; + } + + /* build fetch specification */ + + tmp = [[NSUserDefaults standardUserDefaults] stringForKey:@"qualifier"]; + if ([tmp length] > 0) { + qualifier = [EOQualifier qualifierWithQualifierFormat:tmp]; + if (qualifier == nil) + NSLog(@"could not parse qualifier: %@", tmp); + } + else + qualifier = nil; + + tmp = [EOSortOrdering sortOrderingWithKey:@"NSFileName" + selector:EOCompareAscending]; + sortOrderings = [NSArray arrayWithObject:tmp]; + + if ((qualifier != nil) || (sortOrderings != nil)) { + EOFetchSpecification *fs; + + fs = [[EOFetchSpecification alloc] init]; + [fs setQualifier:qualifier]; + [fs setSortOrderings:sortOrderings]; + + [(id)ds setFetchSpecification:fs]; + [fs release]; fs = nil; + } + + /* perform fetch */ + + records = [[ds fetchObjects] objectEnumerator]; + + /* print out */ + + while ((record = [records nextObject])) + printDirRecord(path, record); +} + +static void runOnPath(NSString *path) { + NSFileManager *fm; + BOOL isDir; + + fm = [NSFileManager defaultManager]; + + if (![fm fileExistsAtPath:path isDirectory:&isDir]) { + NSLog(@"file/directory does not exist: %@", path); + return; + } + + if (isDir) + runOnDirPath(path); + else + /* a file */; +} + +static void runit(NSArray *args) { + int i; + + for (i = 1; i < [args count]; i++) { + NSString *path; + + path = [args objectAtIndex:i]; + + if ([path hasPrefix:@"-"]) { // TODO: there is a NSProcessInfo ext for that + i++; + continue; + } + + runOnPath(path); + } +} + int main(int argc, char **argv, char **env) { NSAutoreleasePool *pool; NSArray *args; - NSFileManager *fm; - int i; #if LIB_FOUNDATION_LIBRARY [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; @@ -45,109 +164,7 @@ int main(int argc, char **argv, char **env) { else if ([args count] == 1) args = [args arrayByAddingObject:@"."]; - fm = [NSFileManager defaultManager]; - - for (i = 1; i < [args count]; i++) { - NSString *path; - BOOL isDir; - - path = [args objectAtIndex:i]; - - if ([path hasPrefix:@"-"]) { - i++; - continue; - } - - if (![fm fileExistsAtPath:path isDirectory:&isDir]) { - NSLog(@"file/directory does not exist: %@", path); - continue; - } - - if (isDir) { - EODataSource *ds; - NSEnumerator *records; - NSDictionary *record; - NSArray *sortOrderings; - EOQualifier *qualifier; - id tmp; - - if ((ds = [fm dataSourceAtPath:path]) == nil) { - NSLog(@"could not get datasource for path: '%@'", path); - continue; - } - - /* build fetch specification */ - - tmp = [[NSUserDefaults standardUserDefaults] stringForKey:@"qualifier"]; - if ([tmp length] > 0) { - qualifier = [EOQualifier qualifierWithQualifierFormat:tmp]; - if (qualifier == nil) - NSLog(@"could not parse qualifier: %@", tmp); - } - else - qualifier = nil; - - tmp = [EOSortOrdering sortOrderingWithKey:@"NSFileName" - selector:EOCompareAscending]; - sortOrderings = [NSArray arrayWithObject:tmp]; - - if ((qualifier != nil) || (sortOrderings != nil)) { - EOFetchSpecification *fs; - - fs = [[EOFetchSpecification alloc] init]; - [fs setQualifier:qualifier]; - [fs setSortOrderings:sortOrderings]; - - [(id)ds setFetchSpecification:fs]; - [fs release]; fs = nil; - } - - /* perform fetch */ - - records = [[ds fetchObjects] objectEnumerator]; - - /* print out */ - - while ((record = [records nextObject])) { - /* id uid gid date name */ - NSString *fileId; - NSString *owner; - int gid; - unsigned size; - NSString *modDate; - NSString *fname; - NSString *ftype; - - fileId = [[record objectForKey:@"NSFileIdentifier"] description]; - owner = [record objectForKey:NSFileOwnerAccountName]; - gid = [[record objectForKey:@"NSFileGroupOwnerAccountNumber"] intValue]; - size = [[record objectForKey:NSFileSize] intValue]; - modDate = [[record objectForKey:NSFileModificationDate] description]; - fname = [record objectForKey:@"NSFileName"]; - ftype = [record objectForKey:NSFileType]; - - if ([ftype isEqualToString:NSFileTypeDirectory]) - fname = [fname stringByAppendingString:@"/"]; - else if ([ftype isEqualToString:NSFileTypeSocket]) - fname = [fname stringByAppendingString:@"="]; - else if ([ftype isEqualToString:NSFileTypeSymbolicLink]) - fname = [fname stringByAppendingString:@"@"]; - - //NSLog(@"record: %@", record); - - printf("%8s %8s %8i %8i %8s %s\n", - [fileId cString], - [owner cString], - gid, - size, - [modDate cString], - [fname cString]); - } - } - else { - /* a file */ - } - } + runit(args); [pool release]; exit(0); diff --git a/sope-core/samples/sope-rsrclookup.m b/sope-core/samples/sope-rsrclookup.m new file mode 100644 index 00000000..24e36ef3 --- /dev/null +++ b/sope-core/samples/sope-rsrclookup.m @@ -0,0 +1,80 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OpenGroupware.org. + + OGo 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, or (at your option) any + later version. + + OGo 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 OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#include +#include "common.h" + +static void usage(void) { + fprintf(stderr, + "usage: sope-rsrclookup \n"); +} + +static void testit(NSArray *args) { + NGResourceLocator *loc; + NSString *gs, *fhs, *rsrc; + + if ([args count] < 3) { + usage(); + exit(2); + } + + gs = [args objectAtIndex:1]; + fhs = [args objectAtIndex:2]; + loc = [NGResourceLocator resourceLocatorForGNUstepPath:gs fhsPath:fhs]; + rsrc = [args count] < 4 ? nil : [args objectAtIndex:3]; + + if (rsrc == nil) { + NSArray *a; + + if ((a = [loc gsRootPathes]) != nil) + NSLog(@"GNUstep Lookup Pathes: %@", a); + + if ((a = [loc fhsRootPathes]) != nil) + NSLog(@"FHS Lookup Pathes: %@", a); + + if ((a = [loc searchPathes]) != nil) + NSLog(@"Pathes: %@", a); + } + else { + NSString *p; + + p = [loc lookupFileWithName:rsrc]; + if (p == nil) { + fprintf(stderr, "did not find resource: %s\n", [rsrc cString]); + exit(1); + } + printf("%s\n", [p cString]); + } +} + +int main(int argc, char **argv, char **env) { + NSAutoreleasePool *pool; + + pool = [[NSAutoreleasePool alloc] init]; +#if LIB_FOUNDATION_LIBRARY + [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; +#endif + + testit([[NSProcessInfo processInfo] argumentsWithoutDefaults]); + + [pool release]; + return 0; +} diff --git a/sope-core/samples/subclassing.m b/sope-core/samples/subclassing.m index 4a5ca610..cd9923b5 100644 --- a/sope-core/samples/subclassing.m +++ b/sope-core/samples/subclassing.m @@ -18,7 +18,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #import #include