]> err.no Git - sope/blob - libFoundation/examples/Defaults.m
tweaked layout
[sope] / libFoundation / examples / Defaults.m
1 /*
2    Defaults.m
3
4    Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
5    All rights reserved.
6
7    Author: Ovidiu Predescu <ovidiu@net-community.com>
8    Date: October 1997
9
10    This file is part of libFoundation.
11
12    Permission to use, copy, modify, and distribute this software and its
13    documentation for any purpose and without fee is hereby granted, provided
14    that the above copyright notice appear in all copies and that both that
15    copyright notice and this permission notice appear in supporting
16    documentation.
17
18    We disclaim all warranties with regard to this software, including all
19    implied warranties of merchantability and fitness, in no event shall
20    we be liable for any special, indirect or consequential damages or any
21    damages whatsoever resulting from loss of use, data or profits, whether in
22    an action of contract, negligence or other tortious action, arising out of
23    or in connection with the use or performance of this software.
24 */
25
26 #include <Foundation/Foundation.h>
27
28 volatile void usage (void)
29 {
30     puts ("Tool to manipulate the defaults database of a generic implementation of");
31     puts ("OpenStep Foundation.\n");
32
33     puts ("Show the defaults for all the domains:");
34     puts ("\tDefaults read");
35
36     puts ("Show the defaults for a given domain:");
37     puts ("\tDefaults read \"domain's name\"");
38
39     puts ("Show the defaults for a given key of a given domain:");
40     puts ("\tDefaults read \"domain's name\" \"key\"");
41
42     puts ("Update the defaults for a given domain:");
43     puts ("\tDefaults write \"domain's name\" \"domain's plist representation\"");
44
45     puts ("Update the defaults for a given key in a given domain:");
46     puts ("\tDefaults write \"domain's name\" \"key\" \"value\"");
47
48     puts ("Delete the defaults for a given domain:");
49     puts ("\tDefaults delete \"domain's name\"");
50
51     puts ("Delete the defaults for a given key in a given domain:");
52     puts ("\tDefaults delete \"domain's name\" \"key\"");
53
54     puts ("Show all the existing domains:");
55     puts ("\tDefaults domains");
56
57     puts ("\nCopyright 1995-1997, Ovidiu Predescu and Mircea Oancea. See the libFoundation's");
58     puts ("license for more information.");
59     exit (0);
60 }
61
62 void read_command (NSArray* arguments)
63 {
64     NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
65     id result = [NSMutableDictionary dictionary];
66     int argumentsCount = [arguments count];
67
68     if (argumentsCount == 2) {  /* Defaults read */
69         /* Show the defaults for all the persistent domains */
70         NSArray* persistentDomainNames = [defaults persistentDomainNames];
71         int i, count = [persistentDomainNames count];
72         NSString* key;
73
74         for (i = 0; i < count; i++) {
75             key = [persistentDomainNames objectAtIndex:i];
76             [result setObject:[defaults persistentDomainForName:key]
77                     forKey:key];
78         }
79     }
80     else if (argumentsCount == 3
81              || argumentsCount == 4) {  /* Defaults read "domain name" [key] */
82         /* Show the defaults for a given domain */
83         NSString* domainName = [arguments objectAtIndex:2];
84
85         result = [defaults persistentDomainForName:domainName];
86         if (!result) {
87             NSLog(@"Domain '%@' does not exist!", domainName);
88             exit (1);
89         }
90
91         if (argumentsCount == 4) {
92             NSString* key = [arguments objectAtIndex:3];
93             id value = [result objectForKey:key];
94
95             if (!value) {
96                 NSLog(@"There is no key '%@' under the '%@' domain!",
97                       key, domainName);
98                 exit (1);
99             }
100             else
101                 result = value;
102         }
103     }
104     else if (argumentsCount == 4) {     /* Defaults read "domain name" key */
105         /* Show the defaults for a given key in a domain */
106         NSString* domainName = [arguments objectAtIndex:2];
107
108         result = [defaults persistentDomainForName:domainName];
109         if (!result) {
110             NSLog (@"Domain '%@' does not exist!", domainName);
111             exit (1);
112         }
113     }
114     else
115         usage ();
116     printf ("%s\n", [[result description] cString]);
117 }
118
119 void write_command (NSArray* arguments)
120 {
121     NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
122     int argumentsCount = [arguments count];
123
124     if (argumentsCount == 4) { /* Defaults write "domain name" "plist" */
125         /* Define here the format to avoid a bug in gcc. */
126         id format = @"Cannot parse the representation of the domain name: '%@'";
127         NSString* domainName = [arguments objectAtIndex:2];
128         id value;
129
130         NS_DURING
131             *(&value) = [[arguments objectAtIndex:3] propertyList];
132             if (![value isKindOfClass:[NSDictionary class]]) {
133                 NSLog (@"The domain's value should be a dictionary object! "
134                        @"(got '%@')", value);
135                 exit (1);
136             }
137         NS_HANDLER
138             NSLog (format, [arguments objectAtIndex:3]);
139             exit (1);
140         NS_ENDHANDLER
141
142         [defaults removePersistentDomainForName:domainName];
143         [defaults setPersistentDomain:value forName:domainName];
144     }
145     else if (argumentsCount == 5) {/* Defaults write "domain name" key value */
146         /* Define here the format to avoid a bug in gcc. */
147         id format =
148             @"Cannot parse the value of key '%@' for the domain name '%@': %@";
149         NSString* domainName = [arguments objectAtIndex:2];
150         id key = [arguments objectAtIndex:3];
151         NSMutableDictionary* domainValue;
152         id value;
153
154         NS_DURING {
155             *(&value) = [[arguments objectAtIndex:4] propertyList];
156         }
157         NS_HANDLER {
158             NSLog (format, key, [arguments objectAtIndex:4], localException);
159             exit (1);
160         }
161         NS_ENDHANDLER
162
163         domainValue = [[[defaults persistentDomainForName:domainName]
164                           mutableCopy]
165                           autorelease];
166         if (domainValue == nil)
167             domainValue = [NSMutableDictionary dictionary];
168         else
169             [defaults removePersistentDomainForName:domainName];
170
171         [domainValue setObject:value forKey:key];
172         [defaults setPersistentDomain:domainValue forName:domainName];
173     }
174     else
175         usage ();
176
177     if (![defaults synchronize])
178         NSLog(@"errors during synchronization of defaults !");
179 }
180
181 void delete_command(NSArray *arguments) {
182     /* Defaults delete "domain name" [key] */
183     NSUserDefaults      *defaults;
184     NSMutableDictionary *domainValue;
185     NSString            *domainName;
186     int                 argumentsCount;
187     
188     defaults       = [NSUserDefaults standardUserDefaults];
189     argumentsCount = [arguments count];
190     
191     if (argumentsCount < 3 || argumentsCount > 4) {
192         usage();
193         return;
194     }
195     
196     domainName = [arguments objectAtIndex:2];
197     
198     domainValue = [[[defaults persistentDomainForName:domainName]
199                           mutableCopy]
200                           autorelease];
201     if (domainValue == nil) {
202         NSLog(@"Domain '%@' does not exist!", domainName);
203         exit(1);
204     }
205     
206     if (argumentsCount == 3) {
207         [defaults removePersistentDomainForName:domainName];
208
209         // TODO: this prints a warning, but it is required
210         [defaults synchronize];
211     }
212     else if (argumentsCount == 4) {
213         id key = [arguments objectAtIndex:3];
214         
215         if ([domainValue objectForKey:key] == nil) {
216             NSLog(@"Cannot find the key '%@' under domain name '%@'!",
217                   key, domainName);
218             exit(1);
219         }
220
221         [defaults removePersistentDomainForName:domainName];
222         [domainValue removeObjectForKey:key];
223         [defaults setPersistentDomain:domainValue forName:domainName];
224         
225         if (![defaults synchronize])
226             NSLog(@"errors during synchronization of defaults !");
227     }
228 }
229
230 void show_domains(void) {
231     NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
232     NSArray* persistentDomainNames = [defaults persistentDomainNames];
233     int i, count = [persistentDomainNames count];
234
235     for (i = 0; i < count; i++)
236         printf ("%s\n", [[persistentDomainNames objectAtIndex:i] cString]);
237 }
238
239 #include <extensions/GarbageCollector.h>
240
241 int main(int argc, char **argv, char **env) {
242     NSAutoreleasePool* pool;
243     NSArray* arguments;
244     NSString* command;
245     int count;
246
247 #if LIB_FOUNDATION_LIBRARY
248     [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
249 #endif
250
251     pool = [NSAutoreleasePool new];
252
253     arguments = [[NSProcessInfo processInfo] arguments];
254     count = [arguments count];
255
256     if (count == 1)
257       usage ();
258
259     command = [arguments objectAtIndex:1];
260
261     if ([command isEqual:@"read"])
262         read_command (arguments);
263     else if ([command isEqual:@"write"])
264         write_command (arguments);
265     else if ([command isEqual:@"delete"])
266         delete_command (arguments);
267     else if ([command isEqual:@"domains"])
268         show_domains ();
269     else
270         usage ();
271
272     RELEASE(pool);
273     exit (0);
274     return 0;
275 }
276
277 /*
278   Local Variables:
279   c-basic-offset: 4
280   tab-width: 8
281   End:
282 */