Clean up of sysconfig.m
authorJames Bunton <jamesbunton@fastmail.fm>
Sun, 30 Mar 2008 10:29:23 +0000 (21:29 +1100)
committerJames Bunton <jamesbunton@fastmail.fm>
Sun, 30 Mar 2008 10:29:23 +0000 (21:29 +1100)
 * Use my NSPrint functions because I like them better.
 * Jamesified the style :P
 * Replace the iteration over array with array join function
 * Moved contents of resolveSystemConfiguration into main()

sysconfig.m

index 37360d7..a93be48 100644 (file)
@@ -1,61 +1,72 @@
 #import <Foundation/Foundation.h>
 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
 
-// Begin the nasty hack so that I can easily print out NSStrings to stdout
-void fNSPrint(NSString * outputFilename, NSString * str)
+void
+NSPrintF(NSString* file, NSString* format, ...)
 {
-       NSString * output = [NSString stringWithFormat:@"%@\n", str];
-       [output writeToFile:outputFilename atomically:NO 
-                               encoding:NSUTF8StringEncoding error:nil];
+       va_list args;
+       va_start(args, format);
+       NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
+       va_end(args);
+       [string writeToFile:file atomically:NO encoding:NSUTF8StringEncoding error:nil];
+       [string release];
 }
 
-void NSPrint(NSString *str)
+void
+NSPrint(NSString* format, ...)
 {
-       fNSPrint(@"/dev/stdout", str);
+       va_list args;
+       va_start(args, format);
+       NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
+       va_end(args);
+       [string writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:nil];
+       [string release];
 }
 
-bool printSystemConfiguration(bool quiet, NSString * keyName, NSDictionary * proxies)
+
+bool
+printSystemConfiguration(bool quiet, NSString* keyName, NSDictionary* proxies)
 {
        id value = [proxies objectForKey:keyName];
-       if (nil != value) {
-               NSArray * itemsArray;
-               if ([value isKindOfClass:[NSArray class]]) {
-                       itemsArray = value;
+       if(nil != value) {
+               NSArray* items;
+               if([value isKindOfClass:[NSArray class]]) {
+                       items = value;
                } else {
-                       itemsArray = [NSArray arrayWithObjects:value, nil];
+                       items = [NSArray arrayWithObjects:value, nil];
                }
 
-               NSEnumerator * enumerator = [itemsArray objectEnumerator];
-               id item;
-               while ((item = [enumerator nextObject]))
-                       NSPrint([NSString stringWithFormat:@"%@", item]);
+               NSPrint([items componentsJoinedByString:@","]);
+               NSPrint(@"\n");
 
        } else {
-               if (!quiet)
-                       fNSPrint(@"/dev/stderr", @"Value does not exist");
+               if(!quiet) {
+                       NSPrintF(@"/dev/stderr", @"Value does not exist\n");
+               }
                return FALSE; // Signal a fail condition
        }
        return TRUE;
 }
 
-void printUsage(void)
+void
+printUsage(void)
 {
-       fNSPrint(@"/dev/stderr", @"proxyconf [-q] SystemConfigurationKeyName");
+       NSPrintF(@"/dev/stderr", @"proxyconf [-q] SystemConfigurationKeyName\n");
+       exit(1);
 }
 
-int resolveSystemConfiguration(int argc, const char * argv[])
+int main(int argc, const char* argv[])
 {
-       NSDictionary * proxies = (NSDictionary *)SCDynamicStoreCopyProxies(nil);
-       
+       NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+       NSDictionary* proxies = (NSDictionary*)SCDynamicStoreCopyProxies(nil);
        bool quiet = FALSE;
-       NSString * keyName;
+       NSString* keyName;
 
-       if (2 == argc) {
+       if(2 == argc) {
                keyName = [NSString stringWithCString:argv[1]];
-       } else if (3 == argc) {
-               if (! [[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) {
+       } else if(3 == argc) {
+               if(![[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) {
                        printUsage();
-                       return 1;
                } else {
                        quiet = TRUE;
                }
@@ -63,21 +74,13 @@ int resolveSystemConfiguration(int argc, const char * argv[])
                keyName = [NSString stringWithCString:argv[2]];
        } else {
                printUsage();
-               return 1;
        }
 
-       if (! printSystemConfiguration(quiet, keyName, proxies))
+       if(!printSystemConfiguration(quiet, keyName, proxies)) {
                return 2;
-
+       }
+       
+       [pool release];
        return 0;
 }
 
-int main (int argc, const char * argv[])
-{
-       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
-       int ret = resolveSystemConfiguration(argc, argv);
-       
-    [pool drain];
-    return ret;
-}