Amazon.com Widgets 16-1 Simple way
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 07:38:46 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
| |-+  Answers to Exercises
| | |-+  Chapter 16 (Moderator: sgg_admin)
| | | |-+  16-1 Simple way
Pages: [1]   Go Down
Print
Author Topic: 16-1 Simple way  (Read 915 times)
tadej5553
Full Member
***
Posts: 145


Email




« on: January 16, 2010, 11:43:29 AM »

I added a check to see if the destination exists or is a directory

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

int main (int argc, const char * argv[])
{  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSFileManager *fm = [NSFileManager defaultManager];  
    NSString *dest;  
    NSProcessInfo *proc = [NSProcessInfo processInfo];  
    NSArray *args = [proc arguments];
    NSMutableArray *source = [NSMutableArray array];  
    BOOL isDir;  
    int i;
        
    if (argc < 3)  
    {  
        NSLog(@"Usage: %@ src dest", [proc processName]);  
        return 1;  
    }  
    
    for(i = 1; i < argc-1; ++i)
    {
        [source addObject: [args objectAtIndex: i]];  
    }
        
    for( NSString *temp in source)
    {
        dest = [args lastObject];

        if (![fm isReadableFileAtPath: temp])  
        {  
            NSLog(@"Can't read %@", temp);  
            return 2;  
        }  
        
        [fm fileExistsAtPath:dest isDirectory:&isDir];  
        if (!isDir)  // Added a check to see if the destination is a directory or even exists
        {
            NSLog(@"Destination is not a directory or doesn't exist!");
            return 3;
        }
        
        dest = [dest stringByAppendingPathComponent: [temp lastPathComponent]];
        
        [fm removeItemAtPath: dest error: NULL];  
        
        if (![fm copyItemAtPath: temp toPath: dest error: NULL])  
        {  
            NSLog(@"Copy failed!");  
            return 4;  
        }  
        
        NSLog(@"Copy of %@ to %@ succeeded!", temp, dest);  
    }
    
    [pool drain];  
    return 0;  
}

A question to skochan: Why didn't you use argc in this chapter and used [args count] instead?
I know you try to avoid low-level C stuff, but I think this was unnecesarry.
« Last Edit: January 16, 2010, 12:23:29 PM by tadej5553 » Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: January 16, 2010, 12:36:14 PM »

Dealing withe argv array as an array of character pointers is a little tricky and not object-oriented.  Since I don't require a deep understanding of C in the text,  I decided to use the NSProcessInfo class instead., as I also think it's clearer and easier to understand.

Cheers,

Steve Kochan
Logged
tadej5553
Full Member
***
Posts: 145


Email




« Reply #2 on: January 16, 2010, 12:48:18 PM »

Sorry, but my question was about argc
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.