Amazon.com Widgets Program 16.6
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 30, 2014, 03:30:36 PM
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
| |-+ Program Examples
| | |-+ Chapter 16
| | | |-+ Program 16.6
Pages: [1] Go Down
Print
Author Topic: Program 16.6 (Read 1553 times)
bitshifter
Newbie
*
Posts: 9







on: February 20, 2009, 01:23:22 PM


Code: (Objective-C)
// Implement a basic copy utility

#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSProcessInfo.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm;
NSString *source, *dest;
BOOL isDir;
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];

fm = [NSFileManager defaultManager];

// Check for two arguments on the command line

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

source = [args objectAtIndex: 1];
dest = [args objectAtIndex: 2];

// Make sure the source file can be read

if ([fm isReadableFileAtPath:source] == NO)
{
NSLog(@"Can't read %@", source);
return 2;
}

// See if the destination file is a directory
// if it is, add the source to the end of the destination

[fm fileExistsAtPath:dest isDirectory:&isDir];

if (isDir == YES)
dest = [dest stringByAppendingPathComponent:[source lastPathComponent]];

// Remove the destination file if it already exists

[fm removeItemAtPath: dest error: NULL];

// Okay, time to perform the copy

if ([fm copyItemAtPath:source toPath: dest error: NULL] == NO)
{
NSLog(@"Copy failed!");
return 3;
}

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

    [pool drain];
    return 0;
}

Last Edit: July 28, 2011, 10:37:00 PM by skochan 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.