gngrwzrd

NSURL Relativize

Apr 24th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. NSString * relativize(NSURL * to, NSURL * from, BOOL fromIsDir) {
  2.     NSString * toString = [[to absoluteString] stringByStandardizingPath];
  3.     NSMutableArray * toPieces = [NSMutableArray arrayWithArray:[toString pathComponents]];
  4.    
  5.     NSString * fromString = [[from absoluteString] stringByStandardizingPath];
  6.     NSMutableArray * fromPieces = [NSMutableArray arrayWithArray:[fromString pathComponents]];
  7.    
  8.     NSMutableString * relPath = [NSMutableString string];
  9.    
  10.     NSString * toTrimmed = toString;
  11.     NSString * toPiece = NULL;
  12.     NSString * fromTrimmed = fromString;
  13.     NSString * fromPiece = NULL;
  14.    
  15.     NSMutableArray * parents = [NSMutableArray array];
  16.     NSMutableArray * pieces = [NSMutableArray array];
  17.    
  18.     if(toPieces.count >= fromPieces.count) {
  19.         NSUInteger toCount = toPieces.count;
  20.         while(toCount > fromPieces.count) {
  21.             toPiece = [toTrimmed lastPathComponent];
  22.             toTrimmed = [toTrimmed stringByDeletingLastPathComponent];
  23.             [pieces insertObject:toPiece atIndex:0];
  24.             toCount--;
  25.         }
  26.        
  27.         while(![fromTrimmed isEqualToString:toTrimmed]) {
  28.             toPiece = [toTrimmed lastPathComponent];
  29.             toTrimmed = [toTrimmed stringByDeletingLastPathComponent];
  30.             fromPiece = [fromTrimmed lastPathComponent];
  31.             fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent];
  32.             if(![toPiece isEqualToString:fromPiece]) {
  33.                 if(![fromPiece isEqualToString:[fromPiece lastPathComponent]] || fromIsDir) {
  34.                     [parents addObject:@".."];
  35.                 }
  36.                 [pieces insertObject:toPiece atIndex:0];
  37.             }
  38.         }
  39.        
  40.     } else {
  41.         NSUInteger fromCount = fromPieces.count;
  42.        
  43.         while(fromCount > toPieces.count) {
  44.             fromPiece = [fromTrimmed lastPathComponent];
  45.             fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent];
  46.             if(![fromPiece isEqualToString:[fromString lastPathComponent]] || fromIsDir) {
  47.                 [parents addObject:@".."];
  48.             }
  49.             fromCount--;
  50.         }
  51.        
  52.         while(![toTrimmed isEqualToString:fromTrimmed]) {
  53.             toPiece = [toTrimmed lastPathComponent];
  54.             toTrimmed = [toTrimmed stringByDeletingLastPathComponent];
  55.             fromPiece = [fromTrimmed lastPathComponent];
  56.             fromTrimmed = [fromTrimmed stringByDeletingLastPathComponent];
  57.             [parents addObject:@".."];
  58.             [pieces insertObject:toPiece atIndex:0];
  59.         }
  60.        
  61.     }
  62.    
  63.     [relPath appendString:[parents componentsJoinedByString:@"/"]];
  64.     if(parents.count > 0) [relPath appendString:@"/"];
  65.     else [relPath appendString:@"./"];
  66.     [relPath appendString:[pieces componentsJoinedByString:@"/"]];
  67.    
  68.     NSLog(@"%@",relPath);
  69.    
  70.     return relPath;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment