Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 11:02:06 AM *
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: Attempted answer to exercise 16-3-dirname  (Read 1268 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:  

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.