4 Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
7 Author: Ovidiu Predescu <ovidiu@net-community.com>
10 This file is part of libFoundation.
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
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.
26 #include <Foundation/Foundation.h>
28 volatile void usage (void)
30 puts ("Tool to manipulate the defaults database of a generic implementation of");
31 puts ("OpenStep Foundation.\n");
33 puts ("Show the defaults for all the domains:");
34 puts ("\tDefaults read");
36 puts ("Show the defaults for a given domain:");
37 puts ("\tDefaults read \"domain's name\"");
39 puts ("Show the defaults for a given key of a given domain:");
40 puts ("\tDefaults read \"domain's name\" \"key\"");
42 puts ("Update the defaults for a given domain:");
43 puts ("\tDefaults write \"domain's name\" \"domain's plist representation\"");
45 puts ("Update the defaults for a given key in a given domain:");
46 puts ("\tDefaults write \"domain's name\" \"key\" \"value\"");
48 puts ("Delete the defaults for a given domain:");
49 puts ("\tDefaults delete \"domain's name\"");
51 puts ("Delete the defaults for a given key in a given domain:");
52 puts ("\tDefaults delete \"domain's name\" \"key\"");
54 puts ("Show all the existing domains:");
55 puts ("\tDefaults domains");
57 puts ("\nCopyright 1995-1997, Ovidiu Predescu and Mircea Oancea. See the libFoundation's");
58 puts ("license for more information.");
62 void read_command (NSArray* arguments)
64 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
65 id result = [NSMutableDictionary dictionary];
66 int argumentsCount = [arguments count];
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];
74 for (i = 0; i < count; i++) {
75 key = [persistentDomainNames objectAtIndex:i];
76 [result setObject:[defaults persistentDomainForName:key]
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];
85 result = [defaults persistentDomainForName:domainName];
87 NSLog(@"Domain '%@' does not exist!", domainName);
91 if (argumentsCount == 4) {
92 NSString* key = [arguments objectAtIndex:3];
93 id value = [result objectForKey:key];
96 NSLog(@"There is no key '%@' under the '%@' domain!",
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];
108 result = [defaults persistentDomainForName:domainName];
110 NSLog (@"Domain '%@' does not exist!", domainName);
116 printf ("%s\n", [[result description] cString]);
119 void write_command (NSArray* arguments)
121 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
122 int argumentsCount = [arguments count];
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];
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);
138 NSLog (format, [arguments objectAtIndex:3]);
142 [defaults removePersistentDomainForName:domainName];
143 [defaults setPersistentDomain:value forName:domainName];
145 else if (argumentsCount == 5) {/* Defaults write "domain name" key value */
146 /* Define here the format to avoid a bug in gcc. */
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;
155 *(&value) = [[arguments objectAtIndex:4] propertyList];
158 NSLog (format, key, [arguments objectAtIndex:4], localException);
163 domainValue = [[[defaults persistentDomainForName:domainName]
166 if (domainValue == nil)
167 domainValue = [NSMutableDictionary dictionary];
169 [defaults removePersistentDomainForName:domainName];
171 [domainValue setObject:value forKey:key];
172 [defaults setPersistentDomain:domainValue forName:domainName];
177 if (![defaults synchronize])
178 NSLog(@"errors during synchronization of defaults !");
181 void delete_command(NSArray *arguments) {
182 /* Defaults delete "domain name" [key] */
183 NSUserDefaults *defaults;
184 NSMutableDictionary *domainValue;
185 NSString *domainName;
188 defaults = [NSUserDefaults standardUserDefaults];
189 argumentsCount = [arguments count];
191 if (argumentsCount < 3 || argumentsCount > 4) {
196 domainName = [arguments objectAtIndex:2];
198 domainValue = [[[defaults persistentDomainForName:domainName]
201 if (domainValue == nil) {
202 NSLog(@"Domain '%@' does not exist!", domainName);
206 if (argumentsCount == 3) {
207 [defaults removePersistentDomainForName:domainName];
209 // TODO: this prints a warning, but it is required
210 [defaults synchronize];
212 else if (argumentsCount == 4) {
213 id key = [arguments objectAtIndex:3];
215 if ([domainValue objectForKey:key] == nil) {
216 NSLog(@"Cannot find the key '%@' under domain name '%@'!",
221 [defaults removePersistentDomainForName:domainName];
222 [domainValue removeObjectForKey:key];
223 [defaults setPersistentDomain:domainValue forName:domainName];
225 if (![defaults synchronize])
226 NSLog(@"errors during synchronization of defaults !");
230 void show_domains(void) {
231 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
232 NSArray* persistentDomainNames = [defaults persistentDomainNames];
233 int i, count = [persistentDomainNames count];
235 for (i = 0; i < count; i++)
236 printf ("%s\n", [[persistentDomainNames objectAtIndex:i] cString]);
239 #include <extensions/GarbageCollector.h>
241 int main(int argc, char **argv, char **env) {
242 NSAutoreleasePool* pool;
247 #if LIB_FOUNDATION_LIBRARY
248 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
251 pool = [NSAutoreleasePool new];
253 arguments = [[NSProcessInfo processInfo] arguments];
254 count = [arguments count];
259 command = [arguments objectAtIndex:1];
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"])