Re: Links with ../ and /
Re: Links with ../ and /
- Subject: Re: Links with ../ and /
- From: Rob Keniger <email@hidden>
- Date: Thu, 20 Nov 2008 16:13:13 +1000
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@hidden)
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:
This email sent to email@hidden