]> err.no Git - sope/blob - sope-core/samples/bmlookup.m
Drop apache 1 build-dependency
[sope] / sope-core / samples / bmlookup.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 "common.h"
23 #include <EOControl/EOControl.h>
24 #include <NGExtensions/NSFileManager+Extensions.h>
25 #include <NGExtensions/NGFileFolderInfoDataSource.h>
26 #include <NGExtensions/NGExtensions.h>
27
28 @interface BMLookupTool : NSObject
29 {
30   NGBundleManager *bm;
31 }
32
33 - (int)runWithArguments:(NSArray *)_args;
34
35 @end
36
37 @implementation BMLookupTool
38
39 - (id)init {
40   if ((self = [super init])) {
41     self->bm = [[NGBundleManager defaultBundleManager] retain];
42   }
43   return self;
44 }
45 - (void)dealloc {
46   [self->bm release];
47   [super dealloc];
48 }
49
50 - (void)listResourcesOfType:(NSString *)btype {
51   NSEnumerator *resources;
52   id resource;
53   
54   printf("lookup resources of type: '%s'\n", [btype cString]);
55
56   resources = [[bm providedResourcesOfType:btype] objectEnumerator];
57   while ((resource = [resources nextObject])) {
58     NSString *rname;
59       
60     if ((rname  = [resource objectForKey:@"name"]) != nil) {
61       NSBundle *bundle;
62       
63       bundle = [bm bundleProvidingResource:rname ofType:btype];
64       printf("  resource  '%s'\n",  [rname cString]);
65       printf("    bundle: '%s'\n", [[bundle bundlePath] cString]);
66       printf("    info:   %s\n", [[resource description] cString]);
67     }
68     else
69       printf("  resource info: %s\n", [[resource description] cString]);
70   }
71 }
72
73 - (void)lookupResourceWithName:(NSString *)bname ofType:(NSString *)btype {
74   NSBundle *bundle;
75     
76   printf("lookup resource '%s' of type: '%s'\n", 
77          [bname cString], [btype cString]);
78     
79   bundle = [self->bm bundleProvidingResource:bname ofType:btype];
80   printf("  bundle: '%s'\n", [[bundle bundlePath] cString]);
81   
82   if ([[NSUserDefaults standardUserDefaults] boolForKey:@"load"]) {
83     if (![bundle load])
84       NSLog(@"Could not load bundle: %@", bundle);
85     else
86       printf("  did load bundle: %s\n", [[bundle description] cString]);
87   }
88 }
89
90 - (int)runWithArguments:(NSArray *)_args {
91   if ([_args count] < 2) {
92     NSLog(@"usage: %@ type name", [[_args objectAtIndex:0] lastPathComponent]);
93     return 1;
94   }
95   
96   if ([_args count] == 2) {
97     [self listResourcesOfType:[_args objectAtIndex:1]];
98     return 0;
99   }
100   
101   [self lookupResourceWithName:[_args objectAtIndex:2]
102         ofType:[_args objectAtIndex:1]];
103   return 0;
104 }
105
106 @end /* BMLookupTool */
107
108 int main(int argc, char **argv, char **env) {
109   NSAutoreleasePool *pool;
110   int rc;
111   
112   pool = [[NSAutoreleasePool alloc] init];
113 #if LIB_FOUNDATION_LIBRARY
114   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
115 #endif
116   
117   rc = [[[[BMLookupTool alloc] init] autorelease]
118          runWithArguments:
119            [[NSProcessInfo processInfo] argumentsWithoutDefaults]];
120   
121   [pool release];
122   
123   exit(rc);
124   return rc;
125 }