Amazon.com Widgets Chapter 16 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 09:01:57 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 16
| | | |-+  Chapter 16 Exercise 2
Pages: [1]   Go Down
Print
Author Topic: Chapter 16 Exercise 2  (Read 432 times)
clouded
Full Member
***
Posts: 123






« on: June 27, 2012, 02:37:25 PM »

Here's what I did on this one, done in Terminal and proposal.doc locations varies upon your location:

Code: (Objective-C)
//  main.m
//  Chapter 16 Exercise 2
// Write a command-line tool called myfind that takes two arguments. The first
// is a starting directory to begin the search, and the second is a filename to
// locate. So, the command line
//     $ myfind /Users proposal.doc
//     /Users/stevekochan/MyDocuments/proposals/proposal.doc
//     $
// begins searching the file system from /Users to locate the file proposal.doc.
// Print either a full path to the file if it’s found (as shown) or an
// appropriate message if it’s not.

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
    @autoreleasepool {
        NSFileManager           *fm;
        NSString                *path, *filename, *temp;
        NSDirectoryEnumerator   *dirEnum;
        NSProcessInfo           *proc = [NSProcessInfo processInfo];
        NSArray                 *args = [proc arguments];
        
        fm = [NSFileManager defaultManager];
        
        if ([args count] != 3) {
            NSLog(@"Usage: %@ startDirectory, filename", [proc processName]);
            return 1;
        }
        
        path = [args objectAtIndex: 1];
        filename = [args objectAtIndex: 2];
        dirEnum = [fm enumeratorAtPath: path];
        
        int count = 0;
        while ((temp = [dirEnum nextObject]) != nil) {
            if ([temp rangeOfString: filename].location != NSNotFound) {
                NSLog(@"%@/%@",path, temp);
                count++;
            }
        }
        
        if (count == 0)
            NSLog(@"Sorry, %@ not found.", filename);
            
    }
    return 0;
}

Output:

Last login: Wed Jun 27 17:30:34 on ttys000
Cloudeds-MacBook-Pro:~ Clouded$ cd ./Documents
Cloudeds-MacBook-Pro:Documents Clouded$ clang -fobjc-arc -framework Foundation main.m -o myfind
Cloudeds-MacBook-Pro:Documents Clouded$ ./myfind /Users proposal.doc
2012-06-27 17:31:42.350 myfind[509:707] /Users/Clouded/Documents/proposal/proposal.doc
2012-06-27 17:31:42.351 myfind[509:707] /Users/Clouded/Documents/proposal.doc
Cloudeds-MacBook-Pro:Documents Clouded$ ./myfind /Users ranstand
2012-06-27 17:32:43.602 myfind[564:707] Sorry, ranstand not found.
Cloudeds-MacBook-Pro:Documents Clouded$ exit
« Last Edit: June 27, 2012, 02:45:26 PM by clouded » Logged
Wubstep
Newbie
*
Posts: 20






« Reply #1 on: January 10, 2013, 05:21:21 PM »

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


int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        NSFileManager         *fm;
        NSString              *searchPath, *filenameToSearch, *nextStringToEnumerate;
        NSProcessInfo         *proc = [NSProcessInfo processInfo];
        NSArray               *args = [proc arguments];
        NSDirectoryEnumerator *dirEnum;
        BOOL                  isDir, found;
     
     
       
        fm = [NSFileManager defaultManager];
        NSMutableArray *args1 = [NSMutableArray arrayWithArray: args];
       
        [args1 addObject:@"/Users"];      // Just for testing without terminal
        [args1 addObject:@"falala"];       // Just for testing....
       
        // Check that it has exactly arguments (besides the name of the program).
       
        if ([args1 count] != 3)
        {
            NSLog(@"Usage: searchPath filenameToSearch");
            return 1;
        }
       
        // Get the reference to the values.
       
        searchPath       = [args1 objectAtIndex: 1];
        filenameToSearch = [args1 objectAtIndex: 2];
       
        // Check to see that the searchPath exists and is a directory.
       
        if (([fm fileExistsAtPath: searchPath isDirectory: &isDir] == NO) || (!isDir) )
        {
            NSLog(@"Error: the search path \"%@\" doesn't exist is not a directory.", searchPath);
            return 2;
        }
               
        // Set the current working directory path
       
        BOOL changeCurrDirSuccess = [fm changeCurrentDirectoryPath:searchPath];
       
        if (!changeCurrDirSuccess)
        {
            NSLog(@"Error: Cannot change directory to \n%@\n.", searchPath);
            return 3;
        }
   
        // Proceed with checking to see that the file is or isn't in the search path.
       
        dirEnum = [fm enumeratorAtPath: searchPath];
       
        while ((nextStringToEnumerate = [dirEnum nextObject]) != nil)
        {
            // compare strings
           
            if ( [nextStringToEnumerate caseInsensitiveCompare: filenameToSearch] == NSOrderedSame )
            {
                NSLog(@"%@/%@", searchPath, nextStringToEnumerate);
                found = YES;
            }           
       
        }
   
        if (!found)
        NSLog(@"Cannot find \"%@\"", filenameToSearch);
       
    }
    return 0;
}
« Last Edit: January 10, 2013, 05:27:14 PM by Wubstep » 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.