Amazon.com Widgets exercise 16-1
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 09:11:48 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 16 (Moderator: sgg_admin)
| | | |-+  exercise 16-1
Pages: [1]   Go Down
Print
Author Topic: exercise 16-1  (Read 740 times)
mdziedzic
Newbie
*
Posts: 41



WWW




« on: December 12, 2009, 06:54:19 PM »

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSFileManager *fm;
NSString *dest, *dest2;
NSMutableArray *source = [[NSMutableArray alloc] init];
BOOL isDir, isUnreadable, copyFailed;
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
   
fm = [NSFileManager defaultManager];

// check for at least 3 arguments on the command line

if ([args count] < 3) {
NSLog(@"Usage: %@ src dest", [proc processName]);
return 1;
}

// get source(s) and dest

for (int i = 1; i < [args count] - 1; i++)
[source addObject: [args objectAtIndex: i]];

dest = [args objectAtIndex: [args count] - 1];

for (int i = 0; i < [source count]; i++)
NSLog(@"source: %@", [source objectAtIndex: i]);

NSLog(@"dest: %@", dest);

// make sure the source file(s) can be read

for (int i = 0; i < [source count]; i++) {
if ([fm isReadableFileAtPath: [source objectAtIndex: i]] == NO) {
NSLog(@"Can't read %@", [source objectAtIndex: i]);
isUnreadable = YES;
}
}
if (isUnreadable) return 2;

// make sure destination file is a directory if more than one source

[fm fileExistsAtPath: dest isDirectory: &isDir];

if (!isDir && [source count] > 1) {
NSLog(@"More than one source but dest is not a directory");
return 3;
}

// now perform the copying

for (int i = 0; i < [source count]; i++) {

if (isDir)
dest2 = [dest stringByAppendingPathComponent: [[source objectAtIndex: i] lastPathComponent]];
else
dest2 = dest;

[fm removeItemAtPath: dest2 error: nil];

if ([fm copyItemAtPath: [source objectAtIndex: i] toPath: dest2 error: nil] == NO) {
NSLog(@"Copy failed: %@", [source objectAtIndex: i]);
copyFailed = YES;
}
if (copyFailed) return 3;
}

NSLog(@"Copy of %@ to %@ succeeded!", source, dest);

[source release];

[pool drain];
    return 0;
}
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.