Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
November 23, 2017, 03:24:36 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Program 16.6  (Read 2140 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:  

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 ゥ 2009 classroomM.com. All rights reserved.