2014-04-24 60 views
0

有沒有人碰到一個解決方案或除了NSURL基於像這樣做使相對文件URL:NSURL:顯示相對URL?

NSURL * from = [NSURL URLWithString:@"/Users/me/scripts/python/test.py"]; 
NSURL * to = [NSURL URLWithString:@"/Users/me/new-scripts/python/test.py"]; 
NSURL * rel = [NSURL URLFrom:from to:to]; 
NSLog(@"%@",rel); --> ../../new-scripts/python/test.py 

而且我也希望能夠採取相對路徑」 ../ ../new-scripts/python/test.py「,並將其與絕對URL合併以獲得新的URL。

[NSURL URLWithString:@"../../new-scripts/python/test.py" relativeToURL:[NSURL fileURLWithPath:@"/Users/me/scripts/python/test.py"]]; 

還沒有碰到過什麼來對谷歌,而我試圖實現自己的東西,但它可以結束了其正確性了很多複雜性。想知道是否已經存在蘋果或其他東西或C/Posix函數?

謝謝!

+0

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString :relativeToURL: – Rakesh

+0

當然,我看過文檔。 NSURL沒有我要找的第一部分。 – gngrwzrd

回答

0

我來到這個https://github.com/karelia/KSFileUtilities,但它不適用於文件URL。所以我寫了這個。 http://pastebin.com/FKYjVwpU

#import <Foundation/Foundation.h> 

NSString * relativize(NSURL * to, NSURL * from, BOOL fromIsDir); 

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

    @autoreleasepool { 
     //NSURL * u = [NSURL URLWithString:@"../../scripts" relativeToURL:[NSURL fileURLWithPath:@"/Users/aaronsmith/Development/aaron/"]]; 
     //NSLog(@"%@",u.absoluteString); 
     //NSLog(@"%@",[NSURL URLWithString:@"../../scripts.py" relativeToURL:[NSURL fileURLWithPath:@"/Users/aaronsmith/scipts/"]]); 

     NSURL * to = NULL; 
     NSURL * from = NULL; 

     to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/upload-issue.py"]; 
     from = [NSURL fileURLWithPath:@"/Users/aaronsmith/scripts.scrpt"]; 
     assert([relativize(to,from,FALSE) isEqualToString:@"./uverse/upload-issue.py"]); 

     to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/test/upload-issue.py"]; 
     from = [NSURL fileURLWithPath:@"/Users/aaronsmith/"]; 
     assert([relativize(to, from, TRUE) isEqualToString:@"./uverse/test/upload-issue.py"]); 
     relativize(to,from,TRUE); 

     to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/test/upload-issue.py"]; 
     from = [NSURL fileURLWithPath:@"/Users/aaronsmith/example/scripts/"]; 
     assert([relativize(to,from,TRUE) isEqualToString:@"../../uverse/test/upload-issue.py"]); 

     to = [NSURL fileURLWithPath:@"/Users/aaronsmith/uverse/test/scripts/stuff/upload-issue.py"]; 
     from = [NSURL fileURLWithPath:@"/Users/johnson/scripts/"]; 
     assert([relativize(to,from,TRUE) isEqualToString:@"../../aaronsmith/uverse/test/scripts/stuff/upload-issue.py"]); 

     to = [NSURL URLWithString:@"/Users/aaronsmith/upload-issue.py"]; 
     from = [NSURL URLWithString:@"/Users/aaronsmith/"]; 
     assert([relativize(to,from,TRUE) isEqualToString:@"./upload-issue.py"]); 

     to = [NSURL URLWithString:@"/Users/aaronsmith/upload-issue.py"]; 
     from = [NSURL URLWithString:@"/Users/aaronsmith/scripts.scrpt"]; 
     assert([relativize(to,from,FALSE) isEqualToString:@"./upload-issue.py"]); 

     to = [NSURL URLWithString: @"/Users/aaronsmith/uverse/test/upload-issue.py"]; 
     from = [NSURL URLWithString:@"/Users/aaronsmith/test/whatever/scripts/script.scrpt"]; 
     assert([relativize(to,from,FALSE) isEqualToString:@"../../../uverse/test/upload-issue.py"]); 

     to = [NSURL URLWithString: @"/Users/aaronsmith/uverse/test/upload-issue.py"]; 
     from = [NSURL URLWithString:@"/Users/aaronsmith/test/whatever/scripts/"]; 
     assert([relativize(to,from,TRUE) isEqualToString:@"../../../uverse/test/upload-issue.py"]); 

     to = [NSURL URLWithString:@"/Users/aaronsmith/uverse/test/upload-issue.py"]; 
     from = [NSURL URLWithString:@"/Users/aaronsmith/uverse/test/scripts.scrpt"]; 
     assert([relativize(to, from, FALSE) isEqualToString:@"./upload-issue.py"]); 

     to = [NSURL URLWithString:@"/Users/aaronsmith/upload-issue.py"]; 
     from = [NSURL URLWithString:@"/Users/aaronsmith/uverse/test/scripts.scrpt"]; 
     assert([relativize(to, from, FALSE) isEqualToString:@"../../upload-issue.py"]); 
    } 

    return 0; 
} 

NSString * relativize(NSURL * to, NSURL * from, BOOL fromIsDir) { 
    NSString * toString = [[to absoluteString] stringByStandardizingPath]; 
    NSMutableArray * toPieces = [NSMutableArray arrayWithArray:[toString pathComponents]]; 

    NSString * fromString = [[from absoluteString] stringByStandardizingPath]; 
    NSMutableArray * fromPieces = [NSMutableArray arrayWithArray:[fromString pathComponents]]; 

    NSMutableString * relPath = [NSMutableString string]; 

    NSString * toTrimmed = toString; 
    NSString * toPiece = NULL; 
    NSString * fromTrimmed = fromString; 
    NSString * fromPiece = NULL; 

    NSMutableArray * parents = [NSMutableArray array]; 
    NSMutableArray * pieces = [NSMutableArray array]; 

    if(toPieces.count >= fromPieces.count) { 
     NSUInteger toCount = toPieces.count; 
     while(toCount > fromPieces.count) { 
      toPiece = [toTrimmed lastPathComponent]; 
      toTrimmed = [toTrimmed stringByDeletingLastPathComponent]; 
      [pieces insertObject:toPiece atIndex:0]; 
      toCount--; 
     } 

     while(![fromTrimmed isEqualToString:toTrimmed]) { 
      toPiece = [toTrimmed lastPathComponent]; 
      toTrimmed = [toTrimmed stringByDeletingLastPathComponent]; 
      fromPiece = [fromTrimmed lastPathComponent]; 
      fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent]; 
      if(![toPiece isEqualToString:fromPiece]) { 
       if(![fromPiece isEqualToString:[fromPiece lastPathComponent]] || fromIsDir) { 
        [parents addObject:@".."]; 
       } 
       [pieces insertObject:toPiece atIndex:0]; 
      } 
     } 

    } else { 
     NSUInteger fromCount = fromPieces.count; 

     while(fromCount > toPieces.count) { 
      fromPiece = [fromTrimmed lastPathComponent]; 
      fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent]; 
      if(![fromPiece isEqualToString:[fromString lastPathComponent]] || fromIsDir) { 
       [parents addObject:@".."]; 
      } 
      fromCount--; 
     } 

     while(![toTrimmed isEqualToString:fromTrimmed]) { 
      toPiece = [toTrimmed lastPathComponent]; 
      toTrimmed = [toTrimmed stringByDeletingLastPathComponent]; 
      fromPiece = [fromTrimmed lastPathComponent]; 
      fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent]; 
      [parents addObject:@".."]; 
      [pieces insertObject:toPiece atIndex:0]; 
     } 

    } 

    [relPath appendString:[parents componentsJoinedByString:@"/"]]; 
    if(parents.count > 0) [relPath appendString:@"/"]; 
    else [relPath appendString:@"./"]; 
    [relPath appendString:[pieces componentsJoinedByString:@"/"]]; 

    NSLog(@"%@",relPath); 

    return relPath; 
}