Amazon.com Widgets Attempted answer to exercise 16-3-dirname
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 22, 2014, 04:54:00 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)
| | | |-+ Attempted answer to exercise 16-3-dirname
Pages: [1] Go Down
Print
Author Topic: Attempted answer to exercise 16-3-dirname (Read 777 times)
mdeh
Full Member
***
Posts: 166






on: February 18, 2009, 02:28:57 PM

Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>

/* dirname - return the directory portion of a pathname */
/* fails if pathname are all "////" and probably some other circumstances too!!  */

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [ NSFileManager defaultManager];
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
/* NSMutableArray * pComponents;   */
NSString *command_line_path, *dirPath ;
BOOL isDirect, exists;
int pathCount;

/* check usage */

if ( args.count != 2)
{
NSLog(@"Usage:%@ pathname NAME", [proc processName]);
return 1;
}



command_line_path = [args objectAtIndex: 1];

pathCount = [[ command_line_path pathComponents]count];

/* is path a directory */

exists = [fm fileExistsAtPath: command_line_path isDirectory: &isDirect];
if (!isDirect && pathCount > 1)
dirPath = [command_line_path stringByDeletingLastPathComponent];
else if (!isDirect && pathCount == 1)
dirPath = @".";
else
dirPath = command_line_path;
NSLog(@"%@", dirPath);
    [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.