On 20/11/2008, at 3:13 PM, Mr. Gecko wrote:

How do I handle links with ../ for previous directory and / for front of url.


I'm currently doing this:

// Example: [ @"/a/b/c/d" relativePathTo: @"/a/e/f/g/h"] => @"../../e/ f/g/h".
- (NSString*) relativePathTo: (NSString*) endPath
{
        if([self isEqual: endPath])
        {
                return [self lastPathComponent];
        }
        NSArray* startComponents = [self pathComponents];
        NSArray* endComponents = [endPath pathComponents];
        NSMutableArray* resultComponents = nil;
        int prefixCount = 0;
        if( ! [self isEqual: endPath] ){
                int iLen = MIN([startComponents count], [endComponents count]);
for(prefixCount = 0; prefixCount < iLen && [[startComponents objectAtIndex: prefixCount] isEqual: [endComponents objectAtIndex: prefixCount]]; ++prefixCount){}
        }
        if(0 == prefixCount){
                resultComponents = [NSMutableArray arrayWithArray: 
endComponents];
        }else{
resultComponents = [NSMutableArray arrayWithArray: [endComponents subarrayWithRange: NSMakeRange(prefixCount, [endComponents count] - prefixCount)]];
                int lifterCount = [startComponents count] - prefixCount;
                if(1 == lifterCount){
                        //[resultComponents insertObject: @"." atIndex: 0];
                }else{
                        --lifterCount;
                        int i = 0;
                        for(i = 0; i < lifterCount; ++i){
                                [resultComponents insertObject: @".." atIndex: 
0];
                        }
                }
        }
        return [NSString pathWithComponents: resultComponents];
}

--
Rob Keniger



_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to